<template>
  <el-dialog
    ref="dialog"
    :title="(isEdit ? '编辑' : '新增') + '用户'"
    width="450px"
    :visible.sync="visible"
    @close="closeDialog"
  >
    <el-form
      :model="form"
      ref="form"
      :rules="rules"
      label-position="right"
      label-width="120px"
    >
      <el-row>
        <el-form-item v-if="$store.state.user.orgId === null" label="机构">
          <OrgSelect v-model="form.orgId"></OrgSelect>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="登录名" prop="loginName">
          <el-input
            class="pull_length"
            v-model="form.loginName"
            placeholder="登录名"
          />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="姓名" prop="name">
          <el-input
            class="pull_length"
            v-model="form.name"
            placeholder="姓名"
          />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="密码" prop="password">
          <el-input
            class="pull_length"
            v-model="form.password"
            type="password"
            autocomplete="new-password"
            placeholder="密码"
          />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="再次输入密码" prop="passwordConfirm">
          <el-input
            class="pull_length"
            v-model="form.passwordConfirm"
            type="password"
            autocomplete="new-password"
            placeholder="再次输入密码"
          />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="角色" prop="roleCode">
          <RoleSelect v-model="form.roleCode" :multiple="true" />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="手机号" prop="mobileNumber">
          <el-input
            class="pull_length"
            v-model="form.mobileNumber"
            placeholder="联系方式"
          />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="状态" prop="enable">
          <el-radio-group class="pull_right_sm" v-model="form.enable">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-row>
      <el-row class="d-flex justify-content-center">
        <el-button type="primary" @click="submitForm" :loading="loading"
          >保 存</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import RoleSelect from "@/components/RoleSelect";
import { saveUser } from "@/api/system-user";
import OrgSelect from "@/components/OrgSelect";
export default {
  name: "UserManagementDialog",
  components: { RoleSelect, OrgSelect },
  props: {
    user: Object,
  },
  computed: {
    isEdit() {
      return this.user.id;
    },
    rules() {
      const that = this;
      return {
        loginName: [
          {
            required: true,
            message: "登录名必填",
            whitespace: true,
          },
          { min: 1, max: 20, message: "最长为20个字符" },
        ],
        name: [
          { required: true, message: "姓名必填", whitespace: true },
          { min: 1, max: 20, message: "最长为20个字符" },
        ],
        password: [
          { required: !this.isEdit, message: "密码必填" },
          {
            required: !this.isEdit,
            min: 1,
            max: 16,
            message: "最长为16个字符",
          },
          {
            required: !this.isEdit,
            type: "string",
            pattern: /^[a-z0-9_]+$/i,
            message: "仅支持大小写英文字母、数字和下划线",
          },
        ],
        passwordConfirm: [
          { required: !this.isEdit, message: "密码必填" },
          {
            required: !this.isEdit,
            type: "string",
            pattern: /^[a-z0-9_]+$/i,
            message: "仅支持大小写英文字母、数字和下划线",
          },
          {
            type: "string",
            trigger: "blur",
            validator: function (rule, value, callback) {
              console.log(that.isEdit, value, that.form.password);
              if (
                that.isEdit &&
                (that.form.password === undefined ||
                  that.form.password.trim().length === 0) &&
                (value === undefined || value.trim().length === 0)
              ) {
                console.log("object");
                callback();
                return;
              }
              console.log("pos");
              if (value !== that.form.password) {
                callback(new Error("与密码不一致"));
              } else {
                callback();
              }
            },
            message: "与密码不一致",
          },
        ],
        roleCode: [{ required: true, message: "角色必填" }],
        mobileNumber: [
          { required: false, pattern: /^\d*$/i, message: "手机号必须是数字" },
        ],
      };
    },
  },
  data() {
    return {
      visible: false,
      form: {},
      loading: false,
    };
  },
  watch: {
    user(val) {
      this.resetForm(val);
    },
  },
  methods: {
    resetForm(val) {
      let tmp = { ...val };
      // console.log({ user: this.user, tmp, val });
      if (!tmp.id) {
        tmp = {
          orgId: this.$store.state.user.orgId,
          name: "",
          loginName: "",
          password: "",
          mobileNumber: "",
          roleCode: [],
          enable: 1,
        };
      }
      this.form = tmp;
    },
    openDialog() {
      this.resetForm(this.user);
      this.visible = true;
    },
    closeDialog() {
      this.visible = false;
    },
    async submitForm() {
      try {
        const valid = await this.$refs.form.validate();
        if (!valid) return;
      } catch (error) {
        console.log("校验失败", error);
        return;
      }

      let data = this.form;
      if (this.isEdit) {
        data = { ...data, id: this.user.id };
      }
      try {
        this.loading = true;
        await saveUser(data);
        this.$emit("reload");
        this.$notify({ title: "保存成功", type: "success" });
        this.closeDialog();
      } finally {
        this.loading = false;
      }
    },
  },
};
</script>

<style></style>
