<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    :width="700"
    :visible="visible"
    :maskClosable="false"
    :maxable="maxable"
    :title="isUpdate ? '审核证件' : '审核证件'"
    :body-style="{ paddingBottom: '28px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-form
      ref="formRef"
      :model="form"
      :rules="rules"
      :label-col="{ md: { span: 5 }, sm: { span: 20 } }"
      :wrapper-col="{ md: { span: 20 }, sm: { span: 20 } }"
    >
      <a-form-item label="编号" name="certificateId">
        <span>{{ form.certificateId }}</span>
      </a-form-item>
      <a-form-item label="会员信息" name="nickname">
        <a-tooltip :title="`ID：${form.userId}`">
          <a-avatar
            :size="30"
            :src="`${form.avatar}`"
            style="margin-right: 4px"
          >
            <template #icon>
              <UserOutlined />
            </template>
          </a-avatar>
          {{ form.nickname }}
        </a-tooltip>
      </a-form-item>
      <a-form-item label="审核状态" name="status">
        <a-radio-group v-model:value="form.status">
          <a-radio-button :value="10">待审核</a-radio-button>
          <a-radio-button :value="20">不通过</a-radio-button>
          <a-radio-button :value="30">审核通过</a-radio-button>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="理由" name="comments" v-if="form.status === 20">
        <a-textarea
          :rows="4"
          :maxlength="200"
          placeholder="请输入理由"
          v-model:value="form.comments"
        />
      </a-form-item>
    </a-form>
  </ele-modal>
</template>

<script lang="ts" setup>
  import {ref, reactive, watch, computed} from 'vue';
  import { Form, message } from 'ant-design-vue';
  import { assignObject } from "ele-admin-pro";
  import { addCertificate, updateCertificate } from '@/api/love/certficate';
  import type { Certificate } from '@/api/love/certficate/model';
  import { useUserStore } from '@/store/modules/user';
  import { FormInstance } from "ant-design-vue/es/form";

  const userStore = useUserStore();
  // 当前用户信息
  const loginUser = computed(() => userStore.info ?? {});
  // 是否是修改
  const isUpdate = ref(false);
  const useForm = Form.useForm;
  const props = defineProps<{
    // 弹窗是否打开
    visible: boolean;
    // 修改回显的数据
    form?: Certificate | null;
  }>();

  const emit = defineEmits<{
    (e: 'done'): void;
    (e: 'update:visible', visible: boolean): void;
  }>();

  // 是否显示最大化切换按钮
  const maxable = ref(true);

  // 用户信息
  const form = reactive<Certificate>({
    certificateId: undefined,
    certificateType: 'idCard',
    avatar: '',
    userId: undefined,
    nickname: '',
    status: 10,
    comments: '',
  });

  // 已上传数据, 可赋初始值用于回显
  const images = ref(<any>[]);

  /* 更新visible */
  const updateVisible = (value: boolean) => {
    emit('update:visible', value);
  };

  // 表格选中数据
  const formRef = ref<FormInstance | null>(null);

  // 表单验证规则
  const rules = reactive({
    comments: [
      {
        required: true,
        type: 'string',
        message: '请填写理由',
        trigger: 'blur'
      }
    ]
  });

  // const handleTabs = (e) => {
  //   const index = e.target.value
  //   form.certificateType = index;
  // }

  const { resetFields, validate, validateInfos } = useForm(form, rules);

  /* 保存编辑 */
  const save = () => {
    if (!formRef.value) {
      return;
    }
    formRef.value
      .validate()
      .then(() => {
        const saveOrUpdate = isUpdate.value ? updateCertificate : addCertificate;
        saveOrUpdate(form)
          .then((msg) => {
            message.success(msg);
            updateVisible(false);
            emit('done');
          })
          .catch((e) => {
            message.error(e.message);
          });
      })
      .catch(() => {});
  };

  watch(
    () => props.visible,
    (visible) => {
      if (visible) {
        if (props.form) {
          assignObject(form, props.form);
          isUpdate.value = true;
        } else {
          isUpdate.value = false;
        }
      } else {
        resetFields();
      }
    }
  );
</script>
<style lang="less"></style>
