<template>
  <BasicModal v-bind="$attrs" @register="registerModal" destroyOnClose :title="title" :width="800" @ok="handleSubmit">
    <BasicForm @register="registerForm" />
    <template #insertFooter>
      <a-button v-if="props.showfotterflag" type="link" danger @click="toItemPage">创建后请在条目中维护数据，在附件中上传文件</a-button>
    </template>
  </BasicModal>
</template>

<script lang="ts" setup>
  import { Modal } from 'ant-design-vue';
  import { computed, ref, unref } from 'vue';
  import { saveOrUpdate } from '../StComplianeInfo.api';
  import { formSchema } from '../StComplianeInfo.data';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  // Emits声明
  const emit = defineEmits(['register', 'success', 'goItem']);
  const isUpdate = ref(true);
  const isDetail = ref(false);
  const props = defineProps({
    showfotterflag: { type: Boolean, default: true },
  });
  //表单配置
  const [registerForm, { setProps, resetFields, setFieldsValue, validate, updateSchema }] = useForm({
    //labelWidth: 150,
    schemas: formSchema,
    showActionButtonGroup: false,
    baseColProps: { span: 24 },
  });
  //表单赋值
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    //重置表单
    await resetFields();
    setModalProps({ confirmLoading: false, showCancelBtn: !!data?.showFooter, showOkBtn: !!data?.showFooter });
    isUpdate.value = !!data?.isUpdate;
    isDetail.value = !data?.showFooter;
    if (unref(isUpdate)) {
      //表单赋值
      await setFieldsValue({
        ...data.record,
      });
    } else {
      console.log(data?.complianeClass);
      await setFieldsValue({ complianeClass: data?.complianeClass });
    }
    // 隐藏底部时禁用整个表单
    setProps({ disabled: !data?.showFooter });
    if (!data?.showFooter) {
      console.log('更新');
      updateSchema({ field: 'fileUploadPath', componentProps: { buttonVisible: false } });
    } else {
      updateSchema({ field: 'fileUploadPath', componentProps: { buttonVisible: true } });
    }
  });
  //设置标题
  const title = computed(() => (!unref(isUpdate) ? '新建文件' : unref(isDetail) ? '查看文件' : '修改文件'));
  async function toItemPage() {
    if (unref(isUpdate)) {
      let values = await validate();
      //关闭弹窗
      closeModal();
      emit('goItem', values);
    }
  }
  function gotoItem(complianeNo, values) {
    Modal.confirm({
      title: '提示',
      content: '是否需要跳转到条目界面',
      okText: '确认',
      cancelText: '取消',
      onOk: () => {
        values.complianeNo = complianeNo;
        emit('goItem', values);
      },
    });
  }
  //表单提交事件
  async function handleSubmit(v) {
    try {
      let values = await validate();
      setModalProps({ confirmLoading: true });
      //提交表单
      let complianeNo = await saveOrUpdate(values, isUpdate.value);
      gotoItem(complianeNo, values);
      //关闭弹窗
      closeModal();
      //刷新列表
      emit('success');
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>

<style lang="less" scoped>
  /** 时间和数字输入框样式 */
  :deep(.ant-input-number) {
    width: 100%;
  }

  :deep(.ant-calendar-picker) {
    width: 100%;
  }
</style>
