<!-- 编辑弹窗 -->
<template>
    <ele-modal
        form
        :width="560"
        v-model="visible"
        title="批量审核完成"
        @open="handleOpen"
    >
        <SimpleForm
            :items="items"
            labelWidth="200px"
            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 } 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({
        orderIds: [],
        checkType: '1'
    });
    /** 弹窗打开事件 */
    const handleOpen = (orderIds = []) => {
        if (orderIds.length) {
            visible.value = true;
            form.value.orderIds = orderIds;
            formRef.value?.setData(form.value);
        }
    };

    const items = reactive([
        {
            label: '所选订单未审核图书审核为',
            prop: 'checkType',
            type: 'dictRadio',
            props: { code: 'batch_complete' },
            required: true
        }
    ]);

    const formRef = ref();

    const emit = defineEmits(['refresh']);
    const handleSumbit = () => {
        formRef.value?.submitForm().then((data) => {
            data.orderIds = form.value.orderIds;
            request
                .post('/order/orderInfo/adminCheckBatch', data)
                .then((res) => {
                    if (res.data.code === 200) {
                        visible.value = false;
                        ElMessage.success('操作成功');
                        emit('refresh');
                    } else {
                        ElMessage.error(res.data.msg);
                    }
                });
        });
    };

    defineExpose({
        handleOpen
    });
</script>
