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

    <el-table :data="tableData"  border stripe>
      <el-table-column label="题组Id"  prop="examId"></el-table-column>
      <el-table-column label="题组名字"  prop="name"></el-table-column>

      <el-table-column
        align="center"
        width="160px"
        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>
		 
		  
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="page"
      :page-size="pageSize"
      :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="elForm">
        <el-form-item  label="训练题库" prop="title" >
          <el-input  v-model="form.title" type="text" class="user-form-item" clearable></el-input>
        </el-form-item>
		
        <el-form-item  label="考题数" prop="counter">
          <el-input  v-model="form.counter" type="number" class="user-form-item" clearable></el-input>
        </el-form-item>
		
		<el-form-item  label="时长[秒]" prop="duration">
		  <el-input  v-model="form.duration" type="number" class="user-form-item" clearable></el-input>
		</el-form-item>
		
		<el-form-item  label="开始时间" prop="fromDate">
		  <el-date-picker 
		  			v-model="form.fromDate"
		  			value-format="yyyy-MM-dd"
		  			type="date"
		  			align="left"
		  ></el-date-picker>

		</el-form-item>
		
		<el-form-item  label="结束时间" prop="toDate">
		  <el-date-picker 
		  	v-model="form.toDate"
		  	value-format="yyyy-MM-dd"
		  	type="date"
		  	align="left"
		  ></el-date-picker>
		
		</el-form-item>
		
		
      </el-form>
      <div class="dialog-footer" slot="footer">
        <el-button @click="dialogFormVisible=false">取 消</el-button>
        <el-button @click="addGroup" type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getWxGroupList, postData} from "@/api/exam.js";
import {toDatetime} from "@/utils/date"
import { time2str, str2time} from "@/utils/index.js";
import DropSearch from '@/components/select/DropSearch.vue';

export default {
  name: "Api",
  data() {
    return {
      dialogFormVisible: false,
      dialogTitle: "新增广告",
      boolVal:[0,1],
      form: {
		id: 0,
        title:'',
		fromDate:'',
		counter:10,
		toDate:'',
		duration: 300,
      },
      searchInfo:{},
      page: 1,
      total: 10,
      pageSize: 10,
      tableData: [],
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
		counter: [{ required: true, message: "请输入题数", trigger: "blur" }],
        fromDate: [ { required: true, message: "请输入开始时间", trigger: "blur" }],
		toDate: [ { required: true, message: "请输入结束时间", trigger: "blur" }],
		duration: [ { required: true, message: "请输入时长", trigger: "blur" }],
      }
    };
  },
  components:{DropSearch},
  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;
    },
    doCreateCategory(){
      this.$refs.addAd.validate(async (valid) => {
        if (!valid) return;
        let param = Object.assign( {}, this.form )
        param.starime = toDatetime( param.end_time) 
		param.counter = +param.counter
		param.end_time = param.startTime + (60*param.duration)
        createCategory(param).then(data => {
          this.$message.successMsg("新增成功", 1)
          this.dialogFormVisible = false
          this.getTableData()
        });
      })
    },
    confirmEdit( row ) {
      let param = Object.assign( {}, row )
	  param.counter = +param.counter
	  postData("addWxGroup", param).then((res) => {
	    if( res.code == 200){
			row.edit = false;
			delete( row["temp"] )
			this.$message.successMsg("编辑成功", 1);
	    }
	  });
    },
    handleSizeChange(val) {
      this.pageSize = val
    },
    handleCurrentChange(val) {
      this.page = val
    },
	getTableData(){
		postData("GetAllExamList", {}).then( res=>{
			if( res.code == 200){
				this.tableData = res.data||[]
			}
		})
	},
	okGroup( item ){
		console.log("groupName", item)
		this.form.groupId = item.id
		this.form.groupName = item.name
	},
	gotoPaper( groupId ){
		this.$router.push( {name:'exam_paper', query:{groupId}})
	},
	addGroup() {
	  this.$refs["elForm"].validate((valid) => {
		if (!valid) return;
		let param = Object.assign({}, this.form);
		param.duration = +param.duration
		param.counter = +param.counter
		postData('addWxGroup', param).then((res) => {
		  if( res.code == 200){
			this.dialogFormVisible = false
			this.$message.successMsg("新增成功", 1);
			this.getTableData()
		  }
		});
	  })
	}
  },
  filters: {
    toDatetime
  },
  created() {
    this.getTableData();
  }
};
</script>
<style scoped lang="scss">
.el-tag--mini {
  margin-left: 5px;
}
.warning {
  color: #dc143c;
}
</style>
