<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    width="720px"
    :visible="visible"
    :append-to-body="true"
    :close-on-click-modal="true"
    custom-class="ele-dialog-form"
    :title="isUpdate ? '修改用户' : '添加用户'"
    @update:visible="updateVisible"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="用户账号:" prop="loginName">
            <el-input
              clearable
              :maxlength="20"
              :disabled="isUpdate"
              v-model="form.loginName"
              placeholder="请输入用户账号"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="用户角色:" prop="roleId">
            <role-select v-model="form.roleId" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="!isUpdate" label="登录密码:" prop="loginPwd">
            <el-input
              show-password
              :maxlength="20"
              v-model="form.loginPwd"
              placeholder="请输入登录密码"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="启用状态:" prop="enable">
            <el-switch
              v-model="form.enable"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
            >
            </el-switch>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template v-slot:footer>
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script>
  // import { emailReg, phoneReg } from 'ele-admin';
  import RoleSelect from './role-select.vue';
  import { addUsers, putUsers } from '@/api/system/user';

  export default {
    components: { RoleSelect },
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object
    },
    data() {
      const defaultForm = {
        id: null,
        loginName: '',
        roleId: [],
        loginPwd: '',
        enable: 1
      };
      return {
        defaultForm,
        // 表单数据
        form: { ...defaultForm },
        // 表单验证规则
        rules: {
          loginName: [
            { required: true, message: '请输入用户账号', trigger: 'blur' }
          ],
          roleId: [
            { required: true, message: '请选择角色', trigger: 'change' }
          ],
          loginPwd: [
            {
              required: true,
              pattern: /^[\S]{5,18}$/,
              message: '密码必须为5-18位非空白字符',
              trigger: 'blur'
            }
          ]
        },
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false
      };
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    methods: {
      /* 保存编辑 */
      save() {
        this.$refs.form.validate((valid) => {
          if (!valid) {
            return false;
          }
          this.loading = true;
          if (!this.isUpdate) {
            delete this.form.id;
          }
          const data = {
            ...this.form
            // roleId: this.form.roleIds.map((d) => {
            //   return { roleId: d };
            // })
          };
          const saveOrUpdate = this.isUpdate ? putUsers : addUsers;
          saveOrUpdate(data)
            .then((msg) => {
              this.loading = false;
              this.$message.success(msg);
              this.updateVisible(false);
              this.$emit('done');
            })
            .catch((e) => {
              this.loading = false;
              // this.$message.error(e.message);
            });
        });
      },
      /* 更新visible */
      updateVisible(value) {
        this.$emit('update:visible', value);
      }
    },
    watch: {
      visible(visible) {
        if (visible) {
          if (this.data) {
            this.$util.assignObject(this.form, {
              ...this.data,
              roleId: this.data.roleList.map((d) => d.id),
              loginPwd: ''
            });
            this.isUpdate = true;
          } else {
            this.isUpdate = false;
          }
        } else {
          this.$refs.form.clearValidate();
          this.form = { ...this.defaultForm };
        }
      }
    }
  };
</script>
