<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"
            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() {
      return {
        loginName: [{ required: true, message: "登录名必填" }],
        name: [{ required: true, message: "姓名必填" }],
        password: [{ required: !this.isEdit, message: "密码必填" }],
        roleCode: [{ required: true, message: "角色必填" }],
      };
    },
  },
  data() {
    return {
      visible: false,
      form: {},
      loading: false,
    };
  },
  watch: {
    user(val) {
      let tmp = { ...val };
      if (!tmp.id) {
        tmp = {
          orgId: this.$store.state.user.orgId,
          name: "",
          loginName: "",
          password: "",
          mobileNumber: "",
          roleCode: [],
          enable: 1,
        };
      }
      this.form = tmp;
    },
  },
  methods: {
    openDialog() {
      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>
