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

<script lang="ts" setup>
    import {ref, computed, unref} from 'vue';
    import {BasicModal, useModalInner} from '/@/components/Modal';
    import {BasicForm, useForm} from '/@/components/Form/index';
    import {formSchema} from '../AuditProjectReportConfig.data';
    import {saveOrUpdate,queryByProjectId} from '../AuditProjectReportConfig.api';
    // Emits声明
    const emit = defineEmits(['register','success']);
    const isUpdate = ref(true);
    //表单配置
    const [registerForm, {setProps,resetFields, setFieldsValue, validate}] = useForm({
        //labelWidth: 150,
        schemas: formSchema,
        showActionButtonGroup: false,
        baseColProps: {span: 24}
    });
    //表单赋值
    const projectId = ref();
    const [registerModal, {setModalProps, closeModal}] = useModalInner(async (data) => {
        projectId.value = data.projectId
        const record = await queryByProjectId({projectId:projectId.value})
        isUpdate.value = false;
        await resetFields();
        if(record) {
            isUpdate.value = true;
            await setFieldsValue({
                ...record,
            });
        }
       setModalProps({confirmLoading: false,showCancelBtn:!!data?.showFooter,showOkBtn:!!data?.showFooter});
       setProps({ disabled: !data?.showFooter })
    });
    //设置标题
    const title = "报告配置"
    //表单提交事件
    async function handleSubmit(v) {
        try {
            let values = await validate();
            values["projectId"] = projectId.value
            
            setModalProps({confirmLoading: true});
            //提交表单
            await saveOrUpdate(values, isUpdate.value);
            //关闭弹窗
            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>