<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="courseId">

      </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="judge"> </el-table-column>
	  
	  <el-table-column label="单选"  prop="single"> </el-table-column>
	  
	  <el-table-column label="多选"  prop="multi"> </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="deleteGroup(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="content">
          <el-input v-model="form.content" 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 { getGroupList, addGroup, deleteGroup} from "@/api/category";
import {toDatetime} from "@/utils/date"
import infoList from "@/mixins/infoList";

export default {
  name: "Api",
  data() {
    return {
	  listApi: getGroupList,
      dialogFormVisible: false,
      form: {
        id:0,
        name:'',
        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) 
        addGroup(param).then(data => {
          this.$message.successMsg("新增成功", 1)
          this.dialogFormVisible = false
          this.getTableData()
        });
      })
    },
	deleteGroup( row, index){
		this.$confirm("此操作将永久删除考题组, 是否继续?", "提示", 
			{
			confirmButtonText: "确定",
			cancelButtonText: "取消",
			type: "warning"
		}).then(() => {
			deleteGroup( {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 )
      addGroup(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>