<template>
  <vxe-modal
    resize
    v-model="showModal"
    :title="modalTitle"
    @hide="onHide"
    width="400px"
    show-footer
    esc-closable
  >
    <el-form ref="formRef" inline :model="form" :rules="rules" label-width="80px">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item class="w-full" label="手机号" prop="phone">
            <el-input v-model="form.phone" placeholder="请输入手机号" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item class="w-full" label="用户名称" prop="username">
            <el-input v-model="form.username" placeholder="请输入用户名称" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item class="w-full" label="状态" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio value="enable">启用</el-radio>
              <el-radio value="disable">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <el-button type="default" @click="showModal = false">取消</el-button>
      <el-button type="primary" @click="submitForm">提交</el-button>
    </template>
  </vxe-modal>
</template>

<script setup>
  import { computed } from 'vue';
  import { ref } from 'vue';
  import { saveLeaderGroup,saveUser } from '@/api/user';
  import { getDefualtRoles } from '@/api/role';
  import { ElMessage } from 'element-plus';

  const emits = defineEmits(['success']);
  const formRef = ref();
  const showModal = ref(false);
  const loading = ref(false);
  const form = ref({
    username: '',
    phone: '',
    status: 'enable',
  });
  const options = ref([]);
  const validatePhone = (rule, value, callback) => {
    const reg = /^1[3-9]\d{9}$/;
    if (!value) {
      callback(new Error('请输入手机号'));
    } else if (!reg.test(value)) {
      callback(new Error('请输入正确的手机号'));
    } else {
      callback();
    }
  };
  const rules = ref({
    username: { required: true, message: '请输入用户名称', trigger: 'blur' },
    phone: [
      { required: true, message: '请输入手机号', trigger: 'blur' },
      { validator: validatePhone, trigger: 'blur' },
    ],
  });

  const currentAccount = ref(null);
  const isEdit = computed(() => !!currentAccount.value);
  const modalTitle = computed(() => (isEdit.value ? '编辑账号' : '新增账号'));

  const submitForm = async () => {
    try {
      await formRef.value.validate();
      loading.value = true;
      if (isEdit.value) {
        await saveUser(form.value)
      } else {
        await saveLeaderGroup({...form.value, role: 'risk_control_commissioner'});
      }
      ElMessage({
        type: 'success',
        message: isEdit.value ? '修改成功！' : '添加成功！',
        plain: true,
      });
      emits('success');
      showModal.value = false;
    } catch {}
    loading.value = false;
  };

  const onHide = () => {
    form.value = {
      username: '',
      roles: [],
      phone: '',
      status: 'enable',
    };
    formRef.value.clearValidate();
    currentAccount.value = null;
    showModal.value = false;
  };
  const openModal = (account) => {
    account && (form.value = account.staff);
    getDefualtRoles().then((res) => {
      options.value = res.result;
    });
    currentAccount.value = account;
    showModal.value = true;
  };

  defineExpose({
    openModal,
  });
</script>
<style lang="scss" scoped></style>
