<template>
  <el-dialog
    class="modify-archives"
    :visible.sync="modalIsShow"
    :title="title"
    top="10vh"
    width="500px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @open="visibleChange"
  >
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      label-width="110px"
    >
      <el-form-item prop="archivesName" label="档案名称：">
        <el-input
          v-model.trim="modalForm.archivesName"
          placeholder="请输入档案名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="archivesTypeId" label="档案类型：">
        <archives-type-select
          class="width-full"
          v-model="modalForm.archivesTypeId"
          placeholder="档案类型"
        >
        </archives-type-select>
      </el-form-item>
      <el-form-item prop="semesterId" label="所属学期：">
        <semester-select
          class="width-full"
          v-model="modalForm.semesterId"
        ></semester-select>
      </el-form-item>
      <el-form-item prop="managerId" label="档案管理员：">
        <archives-manage-user-select
          class="width-full"
          v-model="modalForm.managerId"
          placeholder="档案管理员"
        >
        </archives-manage-user-select>
      </el-form-item>
      <el-form-item prop="managerOrgId" label="档案管理部门：">
        <org-select
          class="width-full"
          v-model="modalForm.managerOrgId"
          :semesterId="modalForm.semesterId"
        ></org-select>
      </el-form-item>
      <el-form-item prop="warningTime" label="到期预警时间：">
        <el-date-picker
          v-model="modalForm.warningTime"
          type="datetime"
          value-format="timestamp"
          placeholder="请选择到期预警时间"
          style="width: 100%"
        >
        </el-date-picker>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="primary" :disabled="isSubmit" @click="submit"
        >确认</el-button
      >
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateArchives } from "../api";

const initModalForm = {
  id: "",
  archivesName: "",
  archivesTypeId: "",
  semesterId: "",
  managerId: "",
  managerOrgId: "",
  warningTime: undefined
};

export default {
  name: "modify-archives",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  computed: {
    isEdit() {
      return !!this.instance.id;
    },
    title() {
      return (this.isEdit ? "编辑" : "新增") + "档案";
    }
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      modalForm: {},
      rules: {
        name: [
          {
            required: true,
            message: "请输入档案名称",
            trigger: "change"
          },
          {
            max: 100,
            message: "档案名称不能超过100字符",
            trigger: "change"
          }
        ],
        archivesTypeId: [
          {
            required: true,
            message: "请选择档案类型",
            trigger: "change"
          }
        ],
        semesterId: [
          {
            required: true,
            message: "请选择学期",
            trigger: "change"
          }
        ],
        managerId: [
          {
            required: true,
            message: "请选择档案管理员",
            trigger: "change"
          }
        ],
        managerOrgId: [
          {
            required: true,
            message: "请选择档案管理部门",
            trigger: "change"
          }
        ]
      },
      orgs: [],
      archivesTypes: [],
      adminUsers: []
    };
  },
  methods: {
    initData(val) {
      if (val.id) {
        this.modalForm = this.$objAssign(initModalForm, val);
      } else {
        this.modalForm = { ...initModalForm };
        this.$nextTick(() => {
          this.$refs.modalFormComp.clearValidate();
        });
      }
    },
    visibleChange() {
      this.initData(this.instance);
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    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 data = await updateArchives(datas).catch(() => {});
      this.isSubmit = false;
      if (!data) return;

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