<template>
  <div>
    <div class="button-box clearflex">
      <el-button @click="dialogFormVisible=true" type="primary">新增分类</el-button>
    </div>

    <div class="search-term">
      <el-form :inline="true" :model="searchInfo" class="demo-form-inline">
        
      </el-form>
    </div>

    <el-table :data="tableData"  border stripe>
      <el-table-column label="序号" min-width="60" prop="id">

      </el-table-column>
      <el-table-column label="标题"  prop="name">
        <template slot-scope="{row}">  
          <el-input v-if="row.edit" class="edit-input" size="small" v-model="row.name" />
          <span v-else>{{row.name}}</span>
        </template>
      </el-table-column>
	  
	  <el-table-column label="排序"  prop="orderId">
	    <template slot-scope="{row}">  
	      <el-input v-if="row.edit" class="edit-input" size="small" v-model="row.orderId" />
	      <span v-else>{{row.orderId}}</span>
	    </template>
	  </el-table-column>
	  
	  <el-table-column label="图片"  prop="icon">
	    <template slot-scope="{row}">  
	      <el-input v-if="row.edit" class="edit-input" size="small" v-model="row.icon" />
	      <span v-else>{{row.icon}}</span>
	    </template>
	  </el-table-column>
	  <el-table-column label="是否开放"  prop="icon">
	    <template slot-scope="{row}">  
			<el-select v-if="row.edit" v-model="row.isOpen" class="edit-input" size="small" >
			  <el-option  label="是" :value="1"> </el-option>
			  <el-option  label="否" :value="0"> </el-option>
			</el-select>
			  
	      <span v-else>{{row.isOpen?'是':'否'}}</span>
	    </template>
	  </el-table-column>
	  
	  <el-table-column label="宽度"  prop="span">
	    <template slot-scope="{row}">  
	      <el-input v-if="row.edit" class="edit-input" size="small" v-model="row.span" />
	      <span v-else>{{row.span}}</span>
	    </template>
	  </el-table-column>

      <el-table-column label="描述"  prop="content">
        <template slot-scope="{row}">  
          <el-input v-if="row.edit" type="textarea" size="small" v-model="row.content" />
          <span v-else>{{row.content}}</span>
        </template>
      </el-table-column>
      
      <el-table-column
        align="center"
        min-width="80px"
        label="操作"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="{row, $index}">
          <el-button
            v-if="row.edit"
            type="success"
            size="small"
            icon="el-icon-circle-check"
            @click="confirmEdit(row, $index)"
          >提交</el-button>
          <el-button
            v-if="row.edit"
            class="cancel-btn"
            size="small"
            icon="el-icon-refresh"
            type="warning"
            @click="cancelEdit(row, $index)"
          >取消</el-button>
          <el-button
            v-if="!row.edit"
            type="primary"
            size="small"
            icon="el-icon-edit"
            @click="initEdit(row, $index)"
          >编辑</el-button>
		  <el-button
		    v-if="!row.edit"
		    type="danger"
		    size="small"
		    icon="el-icon-delete"
		    @click="deleteType(row, $index)"
		  >删除</el-button>
		  
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="page"
      :page-size="size"
      :page-sizes="[10, 30, 50, 100]"
      :style="{float:'right',padding:'20px'}"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>

    <el-dialog title="新增分类" center :visible.sync="dialogFormVisible"  >
      <el-form :model="form" :rules="rules" label-width="120px" ref="elAdd">
        <el-form-item  label="标题" prop="name" >
          <el-input  v-model="form.name" type="text" ></el-input>
        </el-form-item>
		<el-form-item  label="描述" prop="orderId">
		  <el-input v-model="form.orderId" type="text"></el-input>
		</el-form-item>
        <el-form-item  label="描述" prop="content">
          <el-input v-model="form.content" type="text"></el-input>
        </el-form-item>
		<el-form-item  label="图片" prop="content">
		  <el-input v-model="form.icon" type="text"></el-input>
		</el-form-item>
		<el-form-item  label="宽度" prop="content">
		  <el-input v-model="form.span" type="text"></el-input>
		</el-form-item>
      </el-form>
      <div class="dialog-footer" slot="footer">
        <el-button @click="dialogFormVisible=false">取 消</el-button>
        <el-button @click="doAddCategory" type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getTypeList, addType, deleteType} from "@/api/category";
import {toDatetime} from "@/utils/date"
import infoList from "@/mixins/infoList";

export default {
  name: "Api",
  data() {
    return {
	  listApi: getTypeList,
      dialogFormVisible: false,
      mixins: [infoList],
      form: {
        id:0,
        name:'',
		span:6,
		icon: "i1",
		orderId:1,
        content:'',
      },
      rules: {
        name: [{ required: true, message: "请输入名字", trigger: "blur" }],
        content: [ { required: true, message: "请输入描述", trigger: "blur" }]
      }
    };
  },
  mixins: [infoList],
  methods: {
    //条件搜索前端看此方法
    onSubmit() {
      this.page = 1;
      this.pageSize = 10;
      this.getTableData();
    },
    cancelEdit(row, index) {
      row = Object.assign(row, row.temp);
      row.edit = false;
      delete( row["temp"] );
    },
    initEdit(row, index) {
      let form = Object.assign({}, row);
      row.edit = true;
      row.temp = form;
    },
    doAddCategory(){
      this.$refs.elAdd.validate(async (valid) => {
        if (!valid) return;
        let param = Object.assign( {}, this.form )
        param.end_time = toDatetime( param.end_time) 
		param.orderId = +param.orderId;
        addType(param).then(data => {
          this.$message.successMsg("新增成功", 1)
          this.dialogFormVisible = false
          this.getTableData()
        });
      })
    },
	deleteType( row, index){
		this.$confirm("此操作将永久删除分类, 是否继续?", "提示", 
			{
			confirmButtonText: "确定",
			cancelButtonText: "取消",
			type: "warning"
		}).then(() => {
			deleteType( {id: row.id}).then(res=>{
				if( res.code == 200){
					this.$message.successMsg("删除成功",1)
					this.tableData.splice( index, 1)
				}
			})
		})
	},
    confirmEdit( row ) {
      let param = Object.assign( {}, row )
      addType(param).then(data => {
        row.edit = false;
        delete( row["temp"] )
        this.$message.successMsg( '数据更新成功',1 );
      });
    }
  },
  created() {
    this.getTableData();
  }
};
</script>
<style scoped lang="scss">
.button-box {
  padding: 10px 20px;
  .el-button {
    float: right;
  }
}
.el-tag--mini {
  margin-left: 5px;
}
.warning {
  color: #dc143c;
}
</style>