<!-- 驳回弹窗 -->
<template>
    <ele-modal
        form
        :width="540"
        v-model="visible"
        title="驳回提示"
        @open="handleOpen"
    >
        <SimpleForm
            :items="formItems"
            labelWidth="80"
            ref="formRef"
            :initKeys="form"
        />
        <template #footer>
            <el-button @click="handleCancel">取消</el-button>
            <el-button type="primary" @click="handleSumbit">确定</el-button>
        </template>
    </ele-modal>
</template>

<script setup>
    import { ref, reactive, nextTick, computed } from 'vue';
    import SimpleForm from '@/components/CommonPage/SimpleForm.vue';
    import request from '@/utils/request';

    /** 弹窗是否打开 */
    const visible = defineModel({ type: Boolean });

    /** 关闭弹窗 */
    const handleCancel = () => {
        visible.value = false;
        nextTick(() => {
            formRef.value?.resetForm();
        });
    };

    const form = ref({
        id: '',
        checkStatus: 3,
        refusalReson: ''
    });

    /** 弹窗打开事件 */
    const handleOpen = (id) => {
        if (!id) return;
        visible.value = true;
        nextTick(() => {
            form.value.id = id;
            formRef.value?.setData(form.value);
        });
    };

    const formItems = computed(() => {
        return [
            {
                type: 'dictSelect',
                label: '驳回原因',
                prop: 'refusalReson',
                required: true,
                props: {
                    code: 'audit_reject_reason',
                    class: 'custom-select'
                }
            }
        ];
    });

    const formRef = ref();

    const emit = defineEmits(['refresh']);
    const handleSumbit = () => {
        formRef.value?.submitForm().then((data) => {
            const formattedData = {
                id: form.value.id,
                checkStatus: form.value.checkStatus,
                refusalReson: data.refusalReson,
                refusalResonCode: data.refusalReson
            };
            request
                .post('/order/orderInfo/exceptcheck', formattedData)
                .then(() => {
                    visible.value = false;
                    ElMessage.success('驳回成功');
                    emit('refresh', formattedData);
                });
        });
    };

    defineExpose({
        handleOpen
    });
</script>
