<template>
  <div class="exam_plan_paper_uncoiling pg-main">
    <div class="pg-title">
      <span>开卷管理</span>
    </div>
    <div class="pg-select-infos">
      <Form ref="kw_progress_queryform" :label-width="80" inline>
        <Row :gutter="20">
          <Col span="10">
            <FormItem label="考试任务" style="width: 100%">
              <Select v-model="query.examCode">
                <Option value="">全部</Option>
                <Option
                  v-for="item in examList"
                  :value="item.examCode"
                  :key="item.examCode"
                  >{{ item.name }}</Option
                >
              </Select>
            </FormItem>
          </Col>
          <Col span="8">
            <Button @click="doQuery" type="primary" icon="md-search"
              >查询</Button
            >
          </Col>
        </Row>
      </Form>
    </div>
    <div class="pg-body">
      <Table
        ref="exam_paper_uncolining"
        :columns="columns"
        :data="list"
        :stripe="true"
        :disabled-hover="true"
      ></Table>
    </div>
    <!-- 设置开卷密码-->
    <Modal v-model="showmodal" title="设置开卷密码">
      <Form
        ref="setpassword_form"
        :model="pageItem"
        :rules="ruleValidateForUncoiling"
        :label-width="80"
      >
        <FormItem label="考试任务：" prop="examName">
          <span>{{ pageItem.examName }}</span>
        </FormItem>
        <FormItem label="批次：" prop="batchName">
          <span>{{ pageItem.batchName }}</span>
        </FormItem>
        <FormItem label="开卷密码" prop="password">
          <Input
            type="text"
            v-model="pageItem.password"
            placeholder="请输入开卷密码"
          ></Input>
        </FormItem>
      </Form>
      <div slot="footer" id="my_footer">
        <Button type="text" @click="cancel">取消</Button>
        <Button type="primary" @click="save">确定</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import { axios } from "../../libs/http";
import util from "../../libs/util";

export default {
  name: "exam_plan_paper_uncoiling",
  data() {
    return {
      showmodal: false,
      pageItem: {
        examCode: "",
        batchId: "",
        examName: "",
        batchName: "",
        password: "",
      },
      ruleValidateForUncoiling: {
        password: [
          { required: true, message: "请输入开卷密码", trigger: "blur" },
        ],
      },
      query: {
        examCode: "",
        pageNo: 1,
        pageSize: 10,
        totalElements: 0,
        totalPages: 0,
        batchId: "",
      },
      list: [],
      examList: [],
      batchList: [],
      columns: [
        {
          type: "index_seq",
          title: "序号",
          align: "center",
          width: 80,
          render: (h, params) => {
            return h(
              "span",
              (this.query.pageNo - 1) * this.query.pageSize + params.index + 1
            );
          },
        },
        {
          title: "考试名称",
          key: "examName",
          minWidth: 200,
        },
        {
          title: "批次",
          key: "batchName",
          align: "center",
        },
        {
          title: "是否已开卷",
          align: "center",
          render: (h, params) => {
            if (params.row.time) {
              return h("i", {
                props: {
                  "aria-hidden": true,
                },
                class: {
                  "fa fa-check": true,
                },
                style: {
                  fontSize: "16px",
                },
              });
            }
          },
        },
        {
          title: "试卷密码导入时间",
          key: "time",
          align: "center",
          width: 190,
          render: (h, params) => {
            if (params.row.time) {
              return h("span", util.dateFormat(params.row.time));
            } else {
              return h("span", "");
            }
          },
        },
        {
          title: "操作",
          align: "center",
          render: (h, params) => {
            return h("div", [
              h(
                "Tooltip",
                {
                  props: {
                    content: "设置开卷密码",
                    placement: "top",
                  },
                },
                [
                  h(
                    "Button",
                    {
                      props: {
                        disabled:
                          params.row.time != undefined &&
                          params.row.time != null,
                        type: "text",
                      },
                      style: {
                        padding: "0 10px",
                      },
                      on: {
                        click: () => {
                          this.doEdit(params.row);
                        },
                      },
                    },
                    [
                      h(
                        "Icon",
                        {
                          props: {
                            size: "22",
                            type: "ios-create",
                          },
                        },
                        ""
                      ),
                    ]
                  ),
                ]
              ),
              h(
                "Tooltip",
                {
                  props: {
                    content: "清除开卷密码",
                    placement: "top",
                  },
                },
                [
                  h(
                    "Button",
                    {
                      props: {
                        disabled:
                          params.row.time == undefined ||
                          params.row.time == null,
                        type: "text",
                      },
                      style: {
                        padding: "0 10px",
                      },
                      on: {
                        click: () => {
                          this.remove(params.row);
                        },
                      },
                    },
                    [
                      h(
                        "Icon",
                        {
                          props: {
                            size: "22",
                            type: "ios-trash",
                          },
                        },
                        ""
                      ),
                    ]
                  ),
                ]
              ),
            ]);
          },
        },
      ],
    };
  },
  methods: {
    save() {
      this.$refs["setpassword_form"].validate((valid) => {
        if (valid) {
          let params = {
            examCode: this.pageItem.examCode,
            batchId: this.pageItem.batchId,
            password: this.pageItem.password,
          };
          axios.post("/system/paperUncoiling/save", params).then((response) => {
            let data = response.data.data;
            this.showmodal = false;
            if (data) {
              this.doQuery();
            }
          });
        } else {
          this.showmodal = true;
        }
      });
    },
    cancel() {
      this.showmodal = false;
    },
    remove(row) {
      if (!row.id) {
        this.$Modal.error({
          title: "系统提示",
          content: "该批次尚未设置开卷密码",
        });
        return;
      }
      let _$ = this;
      this.$Modal.confirm({
        title: "系统提示",
        content: "是否清除选定批次的开卷密码？",
        onOk() {
          axios
            .post("/system/paperUncoiling/clean", { id: row.id })
            .then((response) => {
              let data = response.data;
              if (data.code == 0) {
                _$.doQuery();
                this.$Notice.success({
                  title: "系统提示",
                  desc: "<p>操作成功</p>",
                });
              }
            });
        },
      });
    },
    doEdit(row) {
      this.showmodal = true;
      this.pageItem.examCode = row.examCode;
      this.pageItem.batchId = row.batchId;
      this.pageItem.examName = row.examName;
      this.pageItem.batchName = row.batchName;
      this.pageItem.password = row.password;
    },
    load() {
      if (!this.query.examCode) {
        this.$Notice.error({
          title: "系统提示",
          desc: "<p>请选择一个考试任务</p>",
        });
        return;
      }
      axios
        .post("/system/paperUncoiling/query", this.query)
        .then((response) => {
          let data = response.data.data;
          if (data) {
            this.list = data;
          }
        });
    },
    doQuery() {
      this.query.pageNo = 1;
      this.load();
    },
  },
  created() {
    let myexamCode = this.query.examCode;
    axios.post("/system/kj/hz/getPlans", {}).then((response) => {
      let data = response.data.data;
      if (data) {
        this.examList = [];
        data.forEach((item, index) => {
          this.examList.push(item);
          this.query.examCode = myexamCode;
        });
      }
    });
  },
};
</script>
