<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="role" label="角色">
        <Select v-model="modalForm.role" placeholder="请选择角色">
          <Option
            v-for="(val, key) in ROLE_TYPE"
            :key="key"
            :value="key"
            :label="val"
          ></Option>
        </Select>
      </FormItem>
      <FormItem
        prop="markRight"
        label="权限"
        v-if="modalForm.role === 'MARKER'"
      >
        <Select v-model="modalForm.markRight" placeholder="请选择权限">
          <Option
            v-for="(val, key) in MARKER_RIGHT_TYPE"
            :key="key"
            :value="key"
            :label="val"
          ></Option>
        </Select>
      </FormItem>
      <FormItem
        prop="weight"
        label="权重"
        v-if="
          modalForm.role === 'MARKER' &&
            modalForm.markRight &&
            modalForm.markRight !== 'ALLOW_SCORING'
        "
      >
        <InputNumber
          v-model.trim="modalForm.weight"
          :min="1"
          :max="100"
          :precision="2"
          placeholder="请输入权重"
          style="width: 120px"
        ></InputNumber>
      </FormItem>
      <FormItem v-if="modalForm.role === 'MARK_LEADER'">
        <Checkbox v-model="modalForm.oneClickLevel">是否一键定档</Checkbox
        ><br />
        <Checkbox v-model="modalForm.standardVolume">是否设立标准卷</Checkbox
        ><br />
        <Checkbox v-model="modalForm.levelCallback"
          >是否建议档位打回档</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 { updateGradingUser } from "@/api";
import { commonCode, password, numberValidator } from "@/plugins/formRules";
import { ROLE_TYPE, MARKER_RIGHT_TYPE } from "@/constants/enumerate";

const initModalForm = {
  id: "",
  workId: "",
  subject: "",
  loginName: "",
  password: "123456",
  name: "",
  role: null,
  markRight: null,
  weight: null,
  oneClickLevel: false,
  standardVolume: false,
  levelCallback: 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,
      ROLE_TYPE,
      MARKER_RIGHT_TYPE,
      modalForm: { ...initModalForm },
      rules: {
        loginName: commonCode({ prop: "账号" }),
        password,
        name: [
          {
            required: true,
            min: 2,
            max: 20,
            message: "请输入姓名，长度2-20个字符",
            trigger: "change"
          }
        ],
        role: [
          {
            required: true,
            message: "请选择角色",
            trigger: "change"
          }
        ],
        markRight: [
          {
            required: true,
            message: "请选择权限",
            trigger: "change"
          }
        ],
        weight: numberValidator("请输入权重")
      }
    };
  },
  methods: {
    initData(val) {
      this.modalForm = this.$objAssign(initModalForm, val);
    },
    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;
      let result = true;
      await updateGradingUser(this.modalForm).catch(() => {
        result = false;
      });
      this.isSubmit = false;

      if (!result) return;

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