<template>
  <el-dialog
    class="modify-print-plan"
    :visible.sync="modalIsShow"
    :title="title"
    top="10px"
    width="700px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @open="visibleChange"
  >
    <el-form
      ref="modalFormComp"
      label-width="110px"
      :rules="rules"
      :model="modalForm"
    >
      <div class="part-box part-box-pad part-box-border">
        <h4 class="part-box-tips">基本信息：</h4>
        <el-form-item prop="name" label="印刷计划名称：">
          <el-input
            v-model.trim="modalForm.name"
            clearable
            placeholder="请输入"
            :disabled="!editable"
          ></el-input>
        </el-form-item>
        <el-form-item prop="examStartTime" label="考试时间：">
          <el-date-picker
            v-model="createTime"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="timestamp"
            align="right"
            unlink-panels
            :disabled="!editable"
            @change="dateChange"
          >
          </el-date-picker>
        </el-form-item>
      </div>

      <div class="part-box part-box-pad part-box-border">
        <h4 class="part-box-tips">试卷&题卡印品：</h4>
        <el-form-item prop="printContent" label="试卷、题卡：">
          <el-checkbox-group
            v-model="modalForm.printContent"
            :disabled="!editable"
          >
            <el-checkbox
              v-for="(val, key) in PRINT_CONTENT_TYPE"
              :key="key"
              :label="key"
              >{{ val }}</el-checkbox
            >
          </el-checkbox-group>
        </el-form-item>
        <el-form-item
          prop="backupMethod"
          label="试卷备份方式："
          :required="!!modalForm.printContent.length"
        >
          <el-select
            v-model="modalForm.backupMethod"
            class="mr-2"
            size="small"
            placeholder="请选择"
            :disabled="!editable"
          >
            <el-option
              v-for="(val, key) in PAPER_BACKUP_TYPE"
              :key="key"
              :value="key"
              :label="val"
            ></el-option>
          </el-select>
          <el-input-number
            class="mr-1"
            v-model="modalForm.backupCount"
            size="small"
            :min="1"
            :max="200"
            :step="1"
            step-strictly
            :controls="false"
            :disabled="!editable"
          ></el-input-number>
          <span>份</span>
        </el-form-item>
        <el-form-item
          prop="drawRule"
          label="抽卷规则："
          :required="!!modalForm.printContent.length"
        >
          <el-radio-group v-model="modalForm.drawRule" :disabled="!editable">
            <el-radio
              v-for="(val, key) in DRAW_RULE_TYPE"
              :label="key"
              :key="key"
              >{{ val }}</el-radio
            >
          </el-radio-group>
          <p class="tips-info">
            1.只抽取一次：不同印刷计划下，同一试卷编号下的卷型只能被抽取一次；
          </p>
          <p class="tips-info">
            2.可反复抽取：不同印刷计划下，同一试卷编号下的卷型可重复抽取，系统默认优先抽取未曝光卷型。
          </p>
        </el-form-item>
      </div>

      <div class="part-box part-box-pad part-box-border">
        <h4 class="part-box-tips">变量印品：</h4>
        <el-form-item
          v-for="(item, index) in modalForm.variableContent"
          :key="item.type"
          :label="`${TEMPLATE_CLASSIFY[item.type]}：`"
          :prop="`variableContent.${index}.templateId`"
          :rules="{
            required: false,
            validator: templateValidator,
            trigger: 'change'
          }"
          :required="!!item.templateId.length"
        >
          <el-checkbox-group
            v-model="item.templateId"
            :disabled="!editable"
            @change="vals => tempChange(vals, `variableContent.${index}`)"
          >
            <el-checkbox
              v-for="temp in templateSources[item.type]"
              :label="temp.id"
              :key="temp.id"
              >{{ temp.name }}</el-checkbox
            >
          </el-checkbox-group>
          <el-select
            v-model="item.backupMethod"
            class="mr-2"
            size="small"
            placeholder="请选择"
            :disabled="!editable"
          >
            <el-option
              v-for="(val, key) in PRINT_BACKUP_TYPE"
              :key="key"
              :value="key"
              :label="val"
            ></el-option>
          </el-select>
          <el-input-number
            v-model="item.backupCount"
            class="mr-1"
            size="small"
            :min="1"
            :max="200"
            :step="1"
            step-strictly
            :controls="false"
            :disabled="!editable"
          ></el-input-number>
          <span>份</span>
        </el-form-item>
      </div>

      <div class="part-box part-box-pad part-box-border">
        <h4 class="part-box-tips">普通印品：</h4>
        <el-form-item
          v-for="(item, index) in modalForm.ordinaryContent"
          :key="item.type"
          :label="`${TEMPLATE_CLASSIFY[item.type]}：`"
          label-width="130px"
          :prop="`ordinaryContent.${index}.templateId`"
          :rules="{
            required: false,
            validator: templateValidator,
            trigger: 'change'
          }"
          :required="!!item.templateId.length"
        >
          <el-checkbox-group
            v-model="item.templateId"
            :disabled="!editable"
            @change="vals => tempChange(vals, `ordinaryContent.${index}`)"
          >
            <el-checkbox
              v-for="temp in templateSources[item.type]"
              :label="temp.id"
              :key="temp.id"
              >{{ temp.name }}</el-checkbox
            >
          </el-checkbox-group>
          <el-select
            v-model="item.backupMethod"
            class="mr-2"
            size="small"
            placeholder="请选择"
            :disabled="!editable"
          >
            <el-option
              v-for="(val, key) in PRINT_BACKUP_TYPE"
              :key="key"
              :value="key"
              :label="val"
            ></el-option>
          </el-select>
          <el-input-number
            v-model="item.backupCount"
            class="mr-1"
            size="small"
            :min="1"
            :max="200"
            :step="1"
            step-strictly
            :controls="false"
            :disabled="!editable"
          ></el-input-number>
          <span>份</span>
        </el-form-item>
      </div>

      <el-form-item prop="selectedPrint"></el-form-item>
    </el-form>

    <div slot="footer">
      <el-button
        v-if="editable"
        type="primary"
        :disabled="isSubmit"
        @click="submit"
        >确认</el-button
      >
      <el-button type="danger" @click="cancel" plain>取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  PRINT_CONTENT_TYPE,
  DRAW_RULE_TYPE,
  PRINT_BACKUP_TYPE,
  PAPER_BACKUP_TYPE,
  TEMPLATE_CLASSIFY
} from "@/constants/enumerate";
// import { deepCopy } from "@/plugins/utils";
import { updatePrintPlan, printPlanTemplateList } from "../api";

const initModalForm = {
  id: null,
  name: "",
  examStartTime: "",
  examEndTime: "",
  printContent: [],
  backupMethod: "PLACE",
  backupCount: 1,
  drawRule: "ONE",
  variableContent: [
    {
      type: "SIGN",
      templateId: [],
      oldTemplateId: [],
      backupMethod: "ROOM",
      backupCount: 1
    },
    {
      type: "PACKAGE",
      templateId: [],
      oldTemplateId: [],
      backupMethod: "STUDENT",
      backupCount: 1
    }
  ],
  ordinaryContent: [
    {
      type: "CHECK_IN",
      templateId: [],
      oldTemplateId: [],
      backupMethod: "ROOM",
      backupCount: 1
    }
  ]
};

export default {
  name: "modify-print-plan",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      }
    },
    editType: {
      type: String,
      default: "ADD",
      validator: val => ["ADD", "PREVIEW", "EDIT"].includes(val)
    }
  },
  computed: {
    isEdit() {
      return !!this.instance.id;
    },
    title() {
      const names = {
        ADD: "新增印刷计划",
        PREVIEW: "印刷计划详情",
        EDIT: "编辑印刷计划"
      };
      return names[this.editType];
    },
    editable() {
      return this.editType !== "PREVIEW";
    }
  },
  data() {
    const printContentValidator = (rule, value, callback) => {
      if (value.includes("PAPER") && !value.includes("CARD")) {
        callback(new Error("选择了试卷，同时必须选择题卡"));
      } else {
        callback();
      }
    };
    const backupMethodValidator = (rule, value, callback) => {
      if (!this.modalForm.printContent.length) {
        return callback();
      }
      if (!value) {
        return callback(new Error("请选择备份方式"));
      }
      if (!this.modalForm.backupCount) {
        return callback(new Error("请输入备份数量"));
      }

      callback();
    };
    const selectedPrintValidator = (rule, value, callback) => {
      const printInfo = [
        ...this.modalForm.variableContent,
        ...this.modalForm.ordinaryContent
      ];
      const hasPrintInfo = printInfo.some(item => item.templateId.length);

      if (hasPrintInfo || this.modalForm.printContent.length) {
        callback();
      } else {
        callback(new Error("必须选择一项印品"));
      }
    };
    return {
      modalIsShow: false,
      isSubmit: false,
      modalForm: { ...initModalForm },
      createTime: [],
      PRINT_CONTENT_TYPE,
      DRAW_RULE_TYPE,
      PRINT_BACKUP_TYPE,
      PAPER_BACKUP_TYPE,
      TEMPLATE_CLASSIFY,
      variableContent: [],
      ordinaryContent: [],
      templateSources: {},
      rules: {
        name: [
          {
            required: true,
            message: "请输入印刷计划名称，长度不要超过100个字符",
            max: 100,
            trigger: "change"
          }
        ],
        examStartTime: [
          {
            required: true,
            message: "请设置考试时间",
            trigger: "change"
          }
        ],
        printContent: [
          {
            required: false,
            validator: printContentValidator,
            trigger: "change"
          }
        ],
        backupMethod: [
          {
            required: false,
            validator: backupMethodValidator,
            trigger: "change"
          }
        ],
        drawRule: [
          {
            required: false,
            message: "请选择抽卷规则",
            trigger: "change"
          }
        ],
        selectedPrint: [
          {
            required: false,
            validator: selectedPrintValidator,
            trigger: "change"
          }
        ]
      }
    };
  },
  mounted() {
    this.getTemplates();
  },
  methods: {
    async getTemplates() {
      const data = await printPlanTemplateList();
      const templateSources = {};
      const templates = [...data.variable, ...data.ordinary];
      templates.forEach(item => {
        templateSources[item.type] = item.template;
      });
      this.templateSources = templateSources;
    },
    templateValidator(rule, value, callback) {
      const [field, index] = rule.field.split(".");
      const val = this.modalForm[field][index];
      if (val.templateId.length) {
        if (!val.backupMethod) {
          return callback(new Error("请选择备份方式"));
        }
        if (!val.backupCount) {
          return callback(new Error("请输入备份数量"));
        }
        callback();
      } else {
        callback();
      }
    },
    tempChange(vals, name) {
      const [field, index] = name.split(".");
      const info = this.modalForm[field][index];
      const newVals = vals.filter(item => !info.oldTemplateId.includes(item));
      info.templateId = newVals;
      info.oldTemplateId = newVals;

      this.$refs.modalFormComp.validateField("selectedPrint");
    },
    initData(val) {
      if (val.id) {
        this.createTime = [val.examStartTime, val.examEndTime];
        this.modalForm = this.$objAssign(initModalForm, val);
        const transformInfo = item => {
          return {
            type: item.type,
            templateId: [item.templateId],
            oldTemplateId: [item.templateId],
            backupMethod: item.backupMethod,
            backupCount: item.backupCount
          };
        };
        this.modalForm.variableContent = val.variableContent.map(transformInfo);
        this.modalForm.ordinaryContent = val.ordinaryContent.map(transformInfo);
        this.modalForm.printContent = val.printContent
          ? val.printContent.split(",")
          : [];
      } else {
        this.modalForm = { ...initModalForm };
        this.createTime = [];
      }
    },
    visibleChange() {
      this.initData(this.instance);

      this.$nextTick(() => {
        this.$refs.modalFormComp.clearValidate();
      });
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    dateChange() {
      if (this.createTime) {
        this.modalForm.examStartTime = this.createTime[0];
        this.modalForm.examEndTime = this.createTime[1];
      } else {
        this.modalForm.examStartTime = "";
        this.modalForm.examEndTime = "";
      }
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      if (this.isSubmit) return;
      this.isSubmit = true;
      const datas = { ...this.modalForm };
      const transformInfo = item => {
        return {
          type: item.type,
          templateId: item.templateId.join(),
          backupMethod: item.backupMethod,
          backupCount: item.backupCount
        };
      };
      datas.variableContent = JSON.stringify(
        this.modalForm.variableContent.map(transformInfo)
      );
      datas.ordinaryContent = JSON.stringify(
        this.modalForm.ordinaryContent.map(transformInfo)
      );
      const data = await updatePrintPlan(datas).catch(() => {});
      this.isSubmit = false;
      if (!data) return;

      this.$message.success("保存成功！");
      this.$emit("modified");
      this.cancel();
    }
  }
};
</script>
