<template>
  <BasicModal v-bind="$attrs" @register="registerModal" destroyOnClose :title="title" :width="800" @ok="handleSubmit" okText="确定">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>

<script lang="ts" setup>
  import { ref, computed, unref } from 'vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { formSchemaApprove } from '../BaosongTaskReview.data';
  import { saveOrUpdate } from '../BaosongTaskReview.api';
  const { createConfirm } = useMessage();
  // Emits声明
  const emit = defineEmits(['register', 'success']);
  const isUpdate = ref(true);
  //表单配置
  const [registerForm, { setProps, resetFields, setFieldsValue, validate }] = useForm({
    //labelWidth: 150,
    schemas: formSchemaApprove,
    showActionButtonGroup: false,
    baseColProps: { span: 24 },
  });

  ///const recordTp  = ref()
  //表单赋值
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    //重置表单
    await resetFields();

    isUpdate.value = !!data?.isUpdate;
    if (isUpdate.value) {
      data.record.tp = '' + data.record.tp;
    }
    setModalProps({ confirmLoading: false, showCancelBtn: !!data?.showFooter, showOkBtn: !!data?.showFooter });
    //recordTp.value = data.record.tp

    // 隐藏底部时禁用整个表单
    setProps({ disabled: !data?.showFooter });
    if (unref(isUpdate)) {
      await setFieldsValue({
        ...data.record,
      });
    }
  });

  const title = '审批';

  async function handleSubmit() {
    try {
      let values = await validate();
      createConfirm({
        iconType: 'warning',
        title: '确认',
        content: '是否完成审批并确认无误？',
        okText: '确认',
        cancelText: '取消',
        onOk: async () => {
          console.log('values', values);
          setModalProps({ confirmLoading: true });
          await saveOrUpdate(values, true);
          await resetFields();
          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>
