<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 />
        </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">保 存</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;
    },
  },
  data() {
    return {
      visible: false,
      form: {},
      rules: {},
    };
  },
  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() {
      let data = this.form;
      if (this.isEdit) {
        data = { ...data, id: this.user.id };
      }
      await saveUser(data);
      this.$emit("reload");
      this.closeDialog();
    },
  },
};
</script>

<style></style>
