<template>
  <el-dialog
    class="modify-user"
    :visible.sync="modalIsShow"
    :title="title"
    top="10vh"
    width="448px"
    :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="100px"
    >
      <el-form-item prop="loginName" label="用户名/工号：">
        <el-input
          v-model.trim="modalForm.loginName"
          placeholder="请输入用户名"
          :disabled="isEdit"
        ></el-input>
      </el-form-item>
      <el-form-item prop="realName" label="姓名：">
        <el-input
          v-model.trim="modalForm.realName"
          placeholder="请输入姓名"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="mobileNumber" label="手机号：">
        <el-input
          v-model.trim="modalForm.mobileNumber"
          placeholder="请输入手机号"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="roleIds" label="角色：">
        <el-select
          style="width: 100%"
          v-model="modalForm.roleIds"
          placeholder="请选择角色"
          multiple
          filterable
        >
          <el-option
            v-for="item in roles"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          >
          </el-option>
        </el-select>
      </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 { updateUser } from "../api";
import { phone } from "@/plugins/formRules";

const initModalForm = {
  id: "",
  loginName: "",
  realName: "",
  // code: "",
  mobileNumber: "",
  roleIds: [],
};

export default {
  name: "modify-user",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      },
    },
    roles: {
      type: Array,
      default() {
        return [];
      },
    },
  },
  computed: {
    isEdit() {
      return !!this.instance.id;
    },
    title() {
      return (this.isEdit ? "编辑" : "新增") + "用户";
    },
  },
  data() {
    const roleIdsValidator = (rule, value, callback) => {
      if (!value || !value.length) {
        callback(new Error("请选择角色"));
      } else {
        callback();
      }
    };
    return {
      modalIsShow: false,
      isSubmit: false,
      modalForm: {},
      rules: {
        mobileNumber: phone,
        loginName: [
          {
            required: true,
            message: "请输入用户名/工号",
            trigger: "change",
          },
          {
            max: 50,
            message: "用户名/工号不能超过50",
            trigger: "change",
          },
        ],
        realName: [
          {
            required: true,
            message: "请输入姓名",
            trigger: "change",
          },
          {
            max: 50,
            message: "姓名不能超过50",
            trigger: "change",
          },
        ],
        roleIds: [
          {
            required: true,
            validator: roleIdsValidator,
            trigger: "change",
          },
        ],
      },
      user: {},
      courses: [],
      roleTypes: [],
    };
  },
  methods: {
    initData(val) {
      if (val.id) {
        this.modalForm = this.$objAssign(initModalForm, val);
        this.modalForm.roleIds = val.roles.map((item) => item.id);
      } 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 updateUser(datas).catch(() => {});
      this.isSubmit = false;
      if (!data) return;

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