<template>
  <Modal
    class="modify-grading-user"
    v-model="modalIsShow"
    :title="title"
    :mask-closable="false"
    @on-visible-change="visibleChange"
  >
    <Form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      :key="modalForm.id"
      :label-width="80"
    >
      <FormItem prop="loginName" label="账号">
        <Input
          v-model.trim="modalForm.loginName"
          placeholder="请输入账号"
          clearable
        ></Input>
      </FormItem>
      <FormItem prop="password" label="密码">
        <Input
          v-model.trim="modalForm.password"
          placeholder="请输入密码"
          readonly
        ></Input>
      </FormItem>
      <FormItem prop="name" label="姓名">
        <Input
          v-model.trim="modalForm.name"
          placeholder="请输入姓名"
          clearable
        ></Input>
      </FormItem>
      <FormItem prop="roleCode" label="角色">
        <Select v-model="modalForm.roleCode" placeholder="请选择角色">
          <Option value=""></Option>
        </Select>
      </FormItem>
      <FormItem prop="rightCode" label="权限">
        <Select v-model="modalForm.rightCode" placeholder="请选择权限">
          <Option value=""></Option>
        </Select>
      </FormItem>
      <FormItem prop="weight" label="权重">
        <InputNumber
          v-model.trim="modalForm.weight"
          :min="1"
          :max="100"
          placeholder="请输入权重"
          style="width: 120px"
        ></InputNumber>
      </FormItem>
      <FormItem>
        <Checkbox v-model="modalForm.canConfirmGrade">是否一键定档</Checkbox
        ><br />
        <Checkbox v-model="modalForm.canSetStandard">是否设立标准卷</Checkbox
        ><br />
        <Checkbox v-model="modalForm.canRufuseGrade"
          >是否建议档位打回档</Checkbox
        >
      </FormItem>
    </Form>
    <div slot="footer">
      <Button type="text" @click="cancel">取消</Button>
      <Button type="primary" :disabled="isSubmit" @click="submit">确认</Button>
    </div>
  </Modal>
</template>

<script>
import { uploadGradingUser } from "@/api";
import { password } from "@/plugins/formRules";

const initModalForm = {
  id: "",
  workId: "",
  subjectId: "",
  loginName: "",
  password: "123456",
  name: "",
  roleCode: null,
  rightCode: null,
  weight: null,
  canConfirmGrade: false,
  canSetStandard: false,
  canRufuseGrade: false
};

export default {
  name: "modify-grading-user",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  computed: {
    isEdit() {
      return !!this.instance.id;
    },
    title() {
      return (this.isEdit ? "编辑" : "新增") + "评卷账号";
    }
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      modalForm: { ...initModalForm },
      rules: {
        loginName: [
          {
            required: true,
            pattern: /^[a-zA-Z0-9_-]{2,19}$/,
            message: "账号只能包含字母、数字、下划线以及短横线，长度3-20位",
            trigger: "change"
          }
        ],
        password
      }
    };
  },
  methods: {
    initData(val) {
      if (val.id) {
        this.modalForm = this.$objAssign(initModalForm, val);
      } else {
        this.modalForm = { ...initModalForm };
      }
    },
    visibleChange(visible) {
      if (visible) {
        this.initData(this.instance);
      }
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate();
      if (!valid) return;

      if (this.isSubmit) return;
      this.isSubmit = true;
      const data = await uploadGradingUser(this.modalForm).catch(() => {
        this.isSubmit = false;
      });

      if (!data) return;

      this.isSubmit = false;
      this.$Message.success(this.title + "成功！");
      this.$emit("modified");
      this.cancel();
    }
  }
};
</script>
