<template>
  <div>
    <n-modal
      v-model:show="showModal"
      :mask-closable="false"
      :show-icon="false"
      preset="dialog"
      transform-origin="center"
      :title="formValue.id > 0 ? '编辑前台用户表 #' + formValue.id : '添加前台用户表'"
      :style="{
        width: dialogWidth,
      }"
    >
      <n-scrollbar style="max-height: 87vh" class="pr-5">
        <n-spin :show="loading" description="请稍候...">
          <n-form
            ref="formRef"
            :model="formValue"
            :rules="rules"
            :label-placement="settingStore.isMobile ? 'top' : 'left'"
            :label-width="100"
            class="py-4"
          >
            <n-grid cols="1 s:1 m:1 l:1 xl:1 2xl:1" responsive="screen">
              <n-gi span="1">
                <n-form-item label="手机号" path="phone">
                  <n-input placeholder="请输入手机号" v-model:value="formValue.phone" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="昵称" path="nickname">
                  <n-input placeholder="请输入昵称" v-model:value="formValue.nickname" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="邮箱" path="email">
                  <n-input placeholder="请输入邮箱" v-model:value="formValue.email" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="头像URL" path="avatar">
                  <UploadImage :maxNumber="1" v-model:value="formValue.avatar" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="个人简介" path="introduce">
                  <Editor style="height: 450px" id="introduce" v-model:value="formValue.introduce" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="真实姓名" path="realname">
                  <n-input placeholder="请输入真实姓名" v-model:value="formValue.realname" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="性别" path="gender">
                  <n-input-number placeholder="请输入性别" v-model:value="formValue.gender" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="生日" path="birthday">
                  <n-input placeholder="请输入生日" v-model:value="formValue.birthday" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="学校" path="school">
                  <n-input placeholder="请输入学校" v-model:value="formValue.school" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="学历" path="grade">
                  <n-input placeholder="请输入学历" v-model:value="formValue.grade" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="专业" path="major">
                  <n-input placeholder="请输入专业" v-model:value="formValue.major" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="用户状态" path="status">
                  <n-select v-model:value="formValue.status" :options="dict.getOptionUnRef('sys_normal_disable')" />
                </n-form-item>
              </n-gi>
            </n-grid>
          </n-form>
        </n-spin>
      </n-scrollbar>
      <template #action>
        <n-space>
          <n-button @click="closeForm">
            取消
          </n-button>
          <n-button type="info" :loading="formBtnLoading" @click="confirmForm">
            确定
          </n-button>
        </n-space>
      </template>
    </n-modal>
  </div>
</template>

<script lang="ts" setup>
  import { ref, computed } from 'vue';
  import { useDictStore } from '@/store/modules/dict';
  import { Edit, View } from '@/api/users';
  import { State, newState, rules } from './model';
  import UploadImage from '@/components/Upload/uploadImage.vue';
  import Editor from '@/components/Editor/editor.vue';
  import { useProjectSettingStore } from '@/store/modules/projectSetting';
  import { useMessage } from 'naive-ui';
  import { adaModalWidth } from '@/utils/hotgo';

  const emit = defineEmits(['reloadTable']);
  const message = useMessage();
  const settingStore = useProjectSettingStore();
  const dict = useDictStore();
  const loading = ref(false);
  const showModal = ref(false);
  const formValue = ref<State>(newState(null));
  const formRef = ref<any>({});
  const formBtnLoading = ref(false);
  const dialogWidth = computed(() => {
    return adaModalWidth(840);
  });

  // 提交表单
  function confirmForm(e) {
    e.preventDefault();
    formRef.value.validate((errors) => {
      if (!errors) {
        formBtnLoading.value = true;
        Edit(formValue.value)
          .then((_res) => {
            message.success('操作成功');
            closeForm();
            emit('reloadTable');
          })
          .finally(() => {
            formBtnLoading.value = false;
          });
      } else {
        message.error('请填写完整信息');
      }
    });
  }

  // 关闭表单
  function closeForm() {
    showModal.value = false;
    loading.value = false;
  }

  // 打开模态框
  function openModal(state: State) {
    showModal.value = true;

    // 新增
    if (!state || state.id < 1) {
      formValue.value = newState(state);

      return;
    }

    // 编辑
    loading.value = true;
    View({ id: state.id })
      .then((res) => {
        formValue.value = res;
      })
      .finally(() => {
        loading.value = false;
      });
  }

  defineExpose({
    openModal,
  });
</script>

<style lang="less"></style>