<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['hezhi:exam:edit']"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table :data="examBatchList" >
      <el-table-column label="考试批次" align="center" prop="examBatch" />
<!--      <el-table-column label="报名开始时间" align="center" prop="signStartTime" >-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.signStartTime) }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="报名结束时间" align="center" prop="signEndTime" >-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.signEndTime) }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="考试开始时间" align="center" prop="examStartTime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.examStartTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="考试结束时间" align="center" prop="examEndTime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.examEndTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row, scope.$index)"
            v-hasPermi="['hezhi:exam:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row, scope.$index)"
            v-hasPermi="['hezhi:exam:edit']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改考试批次对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="考试批次" prop="examBatch">
          <el-input v-model="form.examBatch" placeholder="请输入考试批次" style="width: 100%" />
        </el-form-item>
<!--        <el-form-item label="报名开始时间" prop="signStartTime">-->
<!--          <el-date-picker-->
<!--            v-model="form.signStartTime"-->
<!--            value-format="yyyy-MM-dd HH:mm:ss"-->
<!--            type="datetime"-->
<!--            placeholder="选择报名开始时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="报名结束时间" prop="signEndTime">-->
<!--          <el-date-picker-->
<!--            v-model="form.signEndTime"-->
<!--            value-format="yyyy-MM-dd HH:mm:ss"-->
<!--            type="datetime"-->
<!--            placeholder="选择报名结束时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
        <el-form-item label="考试开始时间" prop="examStartTime">
          <el-date-picker
            v-model="form.examStartTime"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
            placeholder="选择考试开始时间"
            style="width: 100%"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="考试结束时间" prop="examEndTime">
          <el-date-picker
            v-model="form.examEndTime"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
            placeholder="选择考试结束时间"
            style="width: 100%">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

export default {
  name: "ExamBatch",
  props: {
    // 考试批次表格数据
    examBatchList: {
      type: Array,
      default: []
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        examBatch: [
          { required: true, message: "考试批次不能为空", trigger: "blur" }
        ],
        examStartTime: [
          { required: true, message: "考试开始时间不能为空", trigger: "blur" }
        ],
        examEndTime: [
          { required: true, message: "考试结束时间不能为空", trigger: "blur" }
        ],
        signStartTime: [
          { required: true, message: "报名开始时间不能为空", trigger: "blur" }
        ],
        signEndTime: [
          { required: true, message: "报名结束时间不能为空", trigger: "blur" }
        ]
      },
      // 操作的数据下标
      operationIndex: 0,
      // 操作标识（0-新增，1-编辑）
      operationFlag: 0
    };
  },
  methods: {
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        examBatchId: 0,
        examBatch: null,
        examStartTime: null,
        examEndTime: null,
        signStartTime: null,
        signEndTime: null
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.operationFlag = 0;
      this.title = "添加考试批次";
    },
    /** 修改按钮操作 */
    handleUpdate(row, index) {
      this.reset();
      this.operationIndex = index;
      this.form = {...this.examBatchList[index]};
      this.open = true;
      this.operationFlag = 1;
      this.title = "编辑考试批次";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          var data = {...this.form}
          if (this.operationFlag === 1) {
            // 判断是否重复
            if(this.examBatchList.length > 0){
              let someResult = this.examBatchList.some((item, index) => {
                return item.examBatch === data.examBatch && index !== this.operationIndex;
              });
              if (someResult){
                this.$modal.msgWarning('"'+data.examBatch+'"已存在');
                return;
              }
            }
            // 修改数据
            this.examBatchList.splice(this.operationIndex, 1, data);
            this.$modal.msgSuccess("编辑成功");
          } else {
            // 判断是否重复
            if(this.examBatchList.length > 0){
              let someResult = this.examBatchList.some(item => item.examBatch === data.examBatch);
              if (someResult){
                this.$modal.msgWarning('"'+data.examBatch+'"已存在');
                return;
              }
            }
            // 新增
            this.examBatchList.push(data);
            this.$modal.msgSuccess("新增成功");
          }
          this.open = false;
          this.reset();
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row, index) {
      let data = this.examBatchList[index]
      this.$modal.confirm('是否确认删除"'+data.examBatch+'"？')
        .then(_ => {
          // 移除
          this.examBatchList.splice(index, 1);
          this.$modal.msgSuccess("删除成功");
        }).catch(() =>{});
    },
  }
};
</script>
<style scoped>
.el-input.is-disabled /deep/ .el-input__inner {
  color: #606266;
}
.el-input-number.is-disabled /deep/ .el-input__inner {
  color: #606266;
}
</style>
