<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="序号" width="70" prop="examId">

      </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="marks">
        <template slot-scope="{row}">  
          <el-input v-if="row.edit" type="textarea" size="small" v-model="row.groupName" />
          <span v-else>{{row.groupName}}</span>
        </template>
      </el-table-column>
	  
	  <el-table-column label="开始时间"  prop="startAt">
	    <template slot-scope="{row}">  
		  <el-date-picker v-if="row.edit"
			v-model="row.startAt"
			value-format="yyyy-MM-dd HH:mm"
			type="datetime"
			align="left"
		  ></el-date-picker>
	      <span v-else>{{row.startAt}}</span>
	    </template>
	  </el-table-column>
	  
	  <el-table-column label="结束时间"  prop="endAt">
	    <template slot-scope="{row}">  
	      <el-date-picker v-if="row.edit"
			v-model="row.endAt"
			value-format="yyyy-MM-dd HH:mm"
			type="datetime"
			align="left"
	      ></el-date-picker>
	      <span v-else>{{row.endAt}}</span>
	    </template>
	  </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="name" >
          <el-input  v-model="form.name" type="text" class="user-form-item" clearable></el-input>
        </el-form-item>
        <el-form-item  label="考题组" prop="name">
          <drop-search :info="form.groupName" type="group" class="user-form-item" @select="okGroup"/>
        </el-form-item>
		
		<el-form-item  label="开始时间" prop="startTime">
		  <el-date-picker 
		                placeholder="开始时间"
		                v-model="form.startTime"
						value-format="yyyy-MM-dd HH:mm"
						format="yyyy-MM-dd HH:mm"
						type="datetime"
		                clearable
		              ></el-date-picker>
		</el-form-item>
		
		<el-form-item  label="时长[分钟]" prop="name" >
		  <el-input  v-model="form.duration" type="number" class="user-form-item" clearable></el-input>
		</el-form-item>
		
		
      </el-form>
      <div class="dialog-footer" slot="footer">
        <el-button @click="dialogFormVisible=false">取 消</el-button>
        <el-button @click="addExam" type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

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

export default {
  name: "Api",
  data() {
    return {
	  listApi: findExam,
      dialogFormVisible: false,
      dialogTitle: "新增广告",
      boolVal:[0,1],
      form: {
        name:'',
		startTime:'',
		groupId:0,
		groupName:'',
		duration: 30,
      },
      searchInfo:{},
      page: 1,
      total: 10,
      pageSize: 10,
      tableData: [],
      rules: {
        name: [{ required: true, message: "请输入标题", trigger: "blur" }],
        groupName: [ { required: true, message: "请输入考题组", trigger: "blur" }],
		startTime: [ { required: true, message: "请输入开始时间", trigger: "blur" }],
		duration: [ { required: true, message: "请输入时长", trigger: "blur" }],
      }
    };
  },
  components:{DropSearch},
  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;
    },
    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.startTime = str2time( param.startAt )
	  param.counter = +param.counter
	  param.endTime = str2time( param.endAt )
	  delete param["temp"]
	  addExam(param).then((res) => {
	    if( res.code == 200){
			row.edit = false;
			delete( row["temp"] )
			this.$message.successMsg("编辑成功", 1);
	    }
	  });
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getTableData()
    },
    handleCurrentChange(val) {
      this.page = val
      this.getTableData()
    },
	okGroup( item ){
		console.log("groupName", item)
		this.form.groupId = item.id
		this.form.groupName = item.name
		
	},
	addExam() {
	  this.$refs["elForm"].validate((valid) => {
		if (!valid) return;
		let param = Object.assign({}, this.form);
		param.startTime = str2time( param.startTime )
		param.counter = +param.counter
		param.endTime = param.startTime + (60*param.duration)
		addExam(param).then((res) => {
		  if( res.code == 200){
			this.$message.successMsg("新增成功", 1);
		  }
		});
	  })
	}
  },
  filters: {
    toDatetime
  },
  created() {
    this.getTableData();
  }
};
</script>
<style scoped lang="scss">
.el-tag--mini {
  margin-left: 5px;
}
.warning {
  color: #dc143c;
}
</style>
