<!-- 搜索表单 -->
<template>
    <simple-form-modal
        title="初审提示"
        :items="formItems"
        ref="editRef"
        :baseUrl="baseUrl"
        :formatData="formatData"
        @success="(data) => emit('success', data)"
    />
</template>

<script setup>
    import { reactive, ref, defineEmits, getCurrentInstance } from 'vue';
    import SimpleFormModal from '@/components/CommonPage/SimpleFormModal.vue';
    const { proxy } = getCurrentInstance();

    const emit = defineEmits(['success']);

    const formItems = computed(() => {
        return [
            {
                type: 'select',
                label: '快递',
                prop: 'finalExpress',
                options: expressList.value.map((v) => {
                    return { label: v.dictLabel, value: v.dictValue };
                })
            },
            {
                type: 'select',
                label: '收货仓库',
                prop: 'godownId',
                options: godownList.value.map((d) => {
                    return { label: d.godownName, value: d.id };
                })
            },
            {
                type: 'dictRadio',
                label: '审核结果',
                prop: 'auditStatus',
                props: { code: 'recycle_audit_result' }
            },
            {
                type: 'dictSelect',
                label: '驳回原因',
                prop: 'auditReason',
                props: { code: 'audit_reject_reason', class: 'custom-select' },
                vIf: (data) => data.auditStatus == 2
            }
        ];
    });

    const godownList = ref([]);
    async function getGodownList() {
        return proxy.$http.post('/baseinfo/godown/searchGodown?name=');
    }

    //获取快递列表
    const expressList = ref([]);
    async function getExpressList() {
        return proxy.$http.get('/system/dict/data/type/final_express');
    }

    //默认值
    const baseUrl = reactive({
        add: '/order/orderInfo/auditFirst',
        update: '/order/orderInfo/auditFirst'
    });
    const formData = ref({
        orderIds: '',
        godownId: 1,
        finalExpress: '3',
        auditStatus: '1'
    });

    const formatData = (data) => {
        return {
            ...data,
            orderIds: formData.value.orderIds
        };
    };

    const editRef = ref(null);

    async function handleOpen(orderIds) {
        formData.value.orderIds = orderIds;

        await getGodownList().then((res) => {
            if (res.data.code == 200) {
                godownList.value = res.data.data.filter(
                    (v) => v.useStatus == 1
                );
                formData.value.godownId =
                    godownList.value.length > 0 ? godownList.value[0].id : '';
            }
        });
        await getExpressList().then((res) => {
            if (res.data.code == 200) {
                expressList.value = res.data.data;
                formData.value.finalExpress =
                    expressList.value.length > 0
                        ? expressList.value[0].dictValue
                        : '';
            }
        });
        editRef.value?.handleOpen(formData.value);
    }

    defineExpose({ handleOpen });
</script>
