<template>
  <BasicModal
    v-bind="$attrs"
    width="720px"
    :showCancelBtn="false"
    okText="通过"
    @register="registerModal"
    @ok="handleSubmit"
  >
    <template #centerFooter>
      <a-button danger type="primary" @click="handleReject">驳回</a-button>
    </template>
    <BasicForm @register="registerForm"/>
  </BasicModal>
</template>
<script lang="ts" setup>
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { formSchema } from './enterpriseAudit.data';
  import { getPushEnterprise, editPushEnterprise, delPushEnterprise } from '/@/api/platform/common/controller/pushEnterprise';
  import { BasicModal, ModalProps, useModalInner } from '/@/components/Modal';
  import { ref } from 'vue';
  import { useMessage } from '/@/hooks/web/useMessage';

  const { createMessage } = useMessage();
  const id = ref<string>('');
  const emit = defineEmits(['success', 'register']);
  const [registerForm, { resetFields, setFieldsValue, validate, clearValidate, updateSchema, setProps }] = useForm({
    labelWidth: 100,
    schemas: formSchema,
    showActionButtonGroup: false,
    baseColProps: { span: 24 }
  });
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data: WindowInnerData = { _tag: '' }) => {
    // 处理清除脏数据
    await resetFields();
    await clearValidate();
    // 处理设置数据
    id.value = data.record?.id;
    const props: Partial<ModalProps> = { confirmLoading: false };
    // 采用tag标签区分操作
    props.title = '企业审核';
    let enterprise = await getPushEnterprise(id.value);
    enterprise.license = String(enterprise.license).split(',');
    await setFieldsValue(enterprise);
    await setProps({ disabled: true });
    // 尾部:设置处理后的最终配置数据
    setModalProps(props);
  });

  async function handleReject() {
    await delPushEnterprise(id.value);
    closeModal();
    createMessage.info('审核驳回!');
    emit('success');
  }

  async function handleSubmit() {
    try {
      const formData = await validate();
      formData.license = Array(formData.license).join(',');
      formData.status = '1';
      setModalProps({ confirmLoading: true });
      await editPushEnterprise(formData);
      closeModal();
      createMessage.success('审核通过!');
      emit('success');
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>
