<template>
  <div class="app-container" style="width:80% ;">
    <el-card>
      <el-row :gutter="10" class="mb8">
        <el-col :span="24">
          <el-button v-if="this.status == '0'" style="float:right ;" type="primary" @click="publish">发布
          </el-button>
        </el-col>
      </el-row>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="考试名称" prop="examName">
          <el-input v-model="form.examName" placeholder="请输入考试名称" :readOnly="isHandle"/>
        </el-form-item>
        <el-row :gutter="25">
          <el-col :span="12">
            <el-form-item label="年度" prop="examYear">
              <el-date-picker style="width:100%;" v-model="form.examYear" type="year" value-format="yyyy"
                              placeholder="选择年度" :readonly="isHandle">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报名截止时间" prop="deadline">
              <el-date-picker clearable style="width:100%;" v-model="form.deadline" type="datetime"
                              value-format="yyyy-MM-dd HH:mm"
                              format="yyyy-MM-dd HH:mm" placeholder="请选择报名截止时间" :readonly="isHandle">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="考试开始时间" prop="startTime">
              <el-date-picker
                v-model="form.startTime"
                type="date"
                style="width: 100%"
                :readonly="isHandle"
                placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考试结束时间" prop="endTime">
              <el-date-picker
                v-model="form.endTime"
                type="date"
                :picker-options="pickerOptionsEnd"
                style="width: 100%"
                :readonly="isHandle"
                placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>


        <!-- <el-form-item label="考试结束时间" prop="endTime">
        <el-date-picker clearable v-model="form.endTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择考试结束时间">
        </el-date-picker>
      </el-form-item> -->
        <el-form-item label="测试项目" prop="projectName" v-if="!isHandle">
          <!-- <el-select v-model="form.projectIdList" multiple placeholder="请选择测试项目" style="width: 100%"
              :disabled="isHandle">
              <el-option v-for="item in projectList" :key="item.id" :label="item.project"
                  :value="item.id"></el-option>
          </el-select> -->
          <el-input v-model="form.projectName" placeholder="请选择测试项目" @click.native="selectProjectList"/>

        </el-form-item>
        <el-form-item label="测试项目" prop="projectName" v-if="isHandle">
          <!-- <el-select v-model="form.projectIdList" multiple placeholder="请选择测试项目" style="width: 100%"
          :disabled="isHandle">
          <el-option v-for="item in projectList" :key="item.id" :label="item.project"
              :value="item.id"></el-option>
      </el-select> -->
          <el-input v-model="form.projectName" placeholder="请选择测试项目" readOnly/>

        </el-form-item>
        <!-- <el-form-item label="考试安排" prop="examArrange">
            <el-input v-model="form.examArrange" type="textarea" :autosize="{ minRows: 3, maxRows: 5 }"
                placeholder="请输入内容" />
        </el-form-item> -->
        <div v-if="!isHandle">
          <el-form-item label="考试安排" prop="examArrange">
            <Editor v-model="form.examArrange" :min-height="150"></Editor>
          </el-form-item>
          <el-form-item label="考试注意事项" prop="notice">
            <Editor v-model="form.notice" :min-height="150"></Editor>
          </el-form-item>
        </div>
        <div v-if="isHandle">
          <el-form ref="form" :model="form" :rules="rules" label-width="110px">
            <el-form-item label="考试安排" prop="examArrange">
              <Editor v-model="form.examArrange" :min-height="150" readOnly></Editor>
            </el-form-item>
            <el-form-item label="考试注意事项" prop="notice">
              <Editor v-model="form.notice" :min-height="150" readOnly></Editor>
            </el-form-item>
          </el-form>
        </div>
      </el-form>

      <el-dialog title="测试项目" :visible.sync="openSelect" width="800px" append-to-body @close="DialogClose">
        <el-table ref="examInfo-table-project" key="b" @row-click="rowClick" size="small" height="400px"
                  :data="projectList" @selection-change="handleSelectionChange" :row-key="getRowId">
          <el-table-column type="selection" width="55" reserve-selection align="center"/>
          <el-table-column label="序号" min-width="100" type="index" align="center"/>
          <el-table-column label="项目名称" min-width="100" align="center" prop="project"/>
          <el-table-column label="项目类型" min-width="100" align="center" prop="type">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.professional_category" :value="scope.row.type"></dict-tag>
            </template>
          </el-table-column>
          <el-table-column label="项目创建时间" min-width="100" align="center" prop="createTime"/>
        </el-table>
        <div slot="footer">
          <el-button type="primary" @click="selectConfirm">确 定</el-button>
          <el-button @click="selectClose">取 消</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import {getExam, editPub, listProject} from "@/api/smartSchool/examinationManage";

export default {
  name: "Exam",
  dicts: ['professional_category', 'registration_status'],
  props: ['examId'],
  created() {
    // this.getList();
    // this.getTestList()
  },
  data() {
    return {
      isFirst: true,
      pickerOptionsEnd: {
        disabledDate: (time) => {
          return new Date(time) < new Date(this.form.startTime)
        },
      },
      tableSelection: [],
      projectIdList: null,
      projectName: null,
      status: null,
      // 遮罩层
      loading: true,
      //
      openSelect: false,
      // 是否修改
      isHandle: false,
      // 测试项目
      projectList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // delFlag: [
        //   { required: true, message: "$comment不能为空", trigger: "blur" }
        // ],
        examName: [
          {required: true, message: "考试名称不能为空", trigger: "blur"}
        ],
        examYear: [
          {required: true, message: "考试年度不能为空", trigger: "blur"}
        ],
        deadline: [
          {required: true, message: "报名截止时间不能为空", trigger: "blur"}
        ],
        startTime: [
          {required: true, message: "开始时间不能为空", trigger: "blur"}
        ],
        endTime: [
          {required: true, message: "结束时间不能为空", trigger: "blur"}
        ],
        type: [
          {required: true, message: "测试项目不能为空", trigger: "blur"}
        ],
        examArrange: [
          {required: true, message: "考试安排不能为空", trigger: "blur"}
        ],
        notice: [
          {required: true, message: "考试注意事项不能为空", trigger: "blur"}
        ],
        projectName: [
          {required: true, message: "测试项目不能为空", trigger: "blur"}
        ],
      }
    };
  },

  methods: {
    /** 查询列表 */
    getList() {
      // this.loading = false;
      this.loading = true;
      getExam(this.examId).then(response => {
        this.form = response.data;
        this.form.examYear = this.form.examYear + ''
        this.loading = false;
      });
    },
    /** 发布按钮 */
    publish() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          editPub(this.form).then(response => {
            this.$modal.msgSuccess("发布成功");
            this.$emit("sendStatus")
          });
        }
      });
    },

    // 获取当前考试信息
    getStatus(statu) {
      this.getList();
      listProject().then(response => {
        this.projectList = response.rows;
      });
      this.status = statu
      if (this.status != '0') {
        this.isHandle = true
      }
    },
    /** 查询所有的测试项目列表 */
    getTestList() {
      this.loading = true;
      listProject().then(response => {
        this.projectList = response.rows;
        this.loading = false;
      });
    },


    /**    ************************************ 开 始   ***********************************       */
    /**测试项目列表弹框--确定按钮*/
    selectConfirm() {
      this.form.projectIdList = this.tableSelection.map(item => item.id);
      let projectName = this.tableSelection.map(item => item.project);
      // this.$set(this.form, 'projectName', '')
      this.$set(this.form, 'projectName', projectName.join(','))
      this.$nextTick(() => {
        this.$refs['examInfo-table-project'].clearSelection()
      })
      // this.$nextTick(() => {
      //   this.$refs.form.clearValidate();
      // })

      this.openSelect = false;
    },

    rowClick(row) {
      this.$refs['examInfo-table-project'].toggleRowSelection(row)
    },


    /** 多选框选中数据发生改变*/
    handleSelectionChange(selection) {
      this.tableSelection = selection
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /** 点击测试项目触发*/
    selectProjectList() {
      this.openSelect = true
      this.getTestList() //获取所有项目列表
      this.form.projectIdList.map(item => {
        this.projectList.map(itemData => {
          if (itemData.id == item) {
            this.$nextTick(() => {
              this.$refs["examInfo-table-project"].toggleRowSelection(itemData, true)
            })
          }
        });
      });
    },

    DialogClose() {
      this.$refs["examInfo-table-project"].clearSelection()
    },


    /**    ************************************  结束   ***********************************       */

    selectClose() {
      this.openSelect = false
    },
    getRowId(row) {
      return row.id;  // row-key绑定的必须为唯一值
    },
  }
};
</script>
