<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    width="680px"
    :visible="visible"
    :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="82px">
      <el-row :gutter="15">
        <el-col v-bind="styleResponsive ? { sm: 12 } : { span: 12 }">
          <el-form-item label="所属机构:" prop="organizationId">
            <ele-tree-select
              :data="organizationList"
              label-key="organizationName"
              value-key="organizationId"
              v-model="form.organizationId"
              :multiple="false"
              :clearable="true"
              placeholder="请选择所属机构"
              :disabled="false"
              :default-expand-all="true"
              size="small"
              :check-strictly="false"
            />
          </el-form-item>
          <el-form-item label="手机号:" prop="mobile">
            <el-input
              clearable
              :maxlength="11"
              :disabled="isUpdate"
              v-model="form.mobile"
              placeholder="请输入手机号"
            />
          </el-form-item>
        </el-col>
        <el-col v-bind="styleResponsive ? { sm: 12 } : { span: 12 }">
          <el-form-item label="姓名:" prop="uname">
            <el-input
              clearable
              :maxlength="20"
              v-model="form.uname"
              placeholder="请输入姓名"
            />
          </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 { phoneReg } from 'ele-admin';
  import {
    addMembers,
    updateMembers,
    checkExistence
  } from '@/api/sylive/members';

  export default {
    components: {},
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object,
      // 全部机构
      organizationList: Array
    },
    data() {
      const defaultForm = {
        userId: null,
        organizationId: null,
        mobile: '',
        uname: ''
      };
      return {
        defaultForm,
        // 表单数据
        form: { ...defaultForm },
        // 表单验证规则
        rules: {
          mobile: [
            {
              required: true,
              trigger: 'blur',
              validator: (_rule, value, callback) => {
                if (this.data) {
                  return callback();
                }
                if (!value) {
                  return callback(new Error('请输入手机号'));
                }
                const st = new RegExp(phoneReg);
                if (!st.test(value)) {
                  return callback(new Error('手机号格式不正确'));
                }
                checkExistence('mobile', value, this.form.organizationId)
                  .then(() => {
                    callback(new Error('手机号已经存在'));
                  })
                  .catch(() => {
                    callback();
                  });
              }
            }
          ],
          organizationId: [
            {
              required: true,
              message: '请选择所属机构',
              trigger: 'blur'
            }
          ],
          uname: [
            {
              required: true,
              message: '请输入姓名',
              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;
          const data = {
            ...this.form
          };
          const saveUpdate = this.isUpdate ? updateMembers : addMembers;
          saveUpdate(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
            });
            this.isUpdate = true;
          } else {
            this.form.organizationId = null;
            this.isUpdate = false;
          }
        } else {
          this.$refs.form.clearValidate();
          this.form = { ...this.defaultForm };
        }
      }
    }
  };
</script>
