<template>
    <BasicModal :maskClosable="false" :width="440" @register="registerModal" v-bind="$attrs" :title="getTitle" centered
        @ok="handleSubmit">
        <BasicForm @register="registerForm" />
    </BasicModal>
</template>
<script>
import { defineComponent, reactive, ref, onMounted, watch, computed } from 'vue';
import { BasicModal, useModalInner } from '/@/components/Modal';
import { BasicForm, useForm } from '/@/components/Form/index';
import { message } from 'ant-design-vue';
import { saveVersion } from '/@/api/sys/version';
import { session } from '/@/utils/Memory';
import moment from 'moment';
import { formSchema } from './version.data';

export default defineComponent({
    name: 'VersionDrawer',
    components: { BasicModal, BasicForm },
    setup(_, { emit }) {
        const modalStatus = ref('add');
        const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
            console.log(data)
            resetForm();
            if (data.status === 'add') {
                updateSchema({ field: 'addType', ifShow: true })
            } else if (data.status === 'edit') {
                updateSchema([
                    { field: 'addType', ifShow: false }
                ])
                setFieldsValue({ ...data.form })
            }
            modalStatus.value = data.status;
        });
        const [registerForm, { setFieldsValue, updateSchema, resetFields, validate, getFieldsValue }] = useForm({
            // labelWidth: 100,
            schemas: formSchema,
            labelCol: { span: 6 },
            wrapperCol: { span: 16 },
            showActionButtonGroup: false
        });
        const getTitle = computed(() => {
            if (modalStatus.value === 'add') {
                return '新增版本';
            }
            if (modalStatus.value === 'edit') {
                return '编辑版本';
            }
        });
        const resetForm = () => {
            resetFields();
            updateSchema([
                { field: 'addType', ifShow: true },
                { field: 'VERSION_NAME', ifShow: true, componentProps: { disabled: false } },
                { field: 'VERSION_NAME_1', ifShow: false, componentProps: { disabled: false } },
                { field: 'VERSION_DISP', componentProps: { disabled: false } }
            ])
        }
        // 提交信息
        const handleSubmit = () => {
            if (modalStatus.value === 'add') {
                validate().then(async (res) => {
                    if (res) {
                        const { VERSION_NAME, VERSION_DISP } = JSON.parse(JSON.stringify(res));
                        await saveVersion({ version: { VERSION_NAME, VERSION_DISP } }).then((saveRes) => {
                            console.log(saveRes)
                        })
                        console.log('res', res);
                        // 关闭弹窗
                        closeModal();
                        // 触发父组件事件
                        emit('success', res);
                    }
                });
            } else if (modalStatus.value === 'edit') {
                validate().then(async (res) => {
                    if (res) {
                        const { FID, VERSION_NAME, VERSION_DISP } = JSON.parse(JSON.stringify(res));
                        await saveVersion({ version: { FID, VERSION_NAME, VERSION_DISP } }).then((saveRes) => {
                            console.log(saveRes)
                        })
                        console.log('res', res);
                        // 关闭弹窗
                        closeModal();
                        // 触发父组件事件
                        emit('success', res);
                    }
                })
            }
        };
        return {
            registerModal,
            registerForm,
            getTitle,
            handleSubmit,
        };
    },
});
</script>