<template>
    <order-page ref="pageRef" :pageConfig="pageConfig">
        <template #toolbar>
            <el-radio-group @change="handleStatusChange" v-model="useStatus">
                <el-radio-button label="待处理订单" value="1" />
                <el-radio-button label="历史异常签收" value="2" />
            </el-radio-group>
        </template>

        <template #action="{ row }">
            <div>
                <template v-if="useStatus === '1'">
                    <el-button
                        type="primary"
                        link
                        v-permission="'recycleLogistics:abnormalSign:agree'"
                        @click="handleAgree(row)"
                    >
                        [同意]
                    </el-button>
                    <el-button
                        type="danger"
                        link
                        v-permission="'recycleLogistics:abnormalSign:reject'"
                        @click="handleReject(row)"
                    >
                        [驳回]
                    </el-button>
                </template>
                <el-button
                    type="success"
                    link
                    v-permission="'recycleLogistics:abnormalSign:detail'"
                    @click="toOrderDetail(row)"
                >
                    [订单详情]
                </el-button>
                <el-button
                    type="warning"
                    link
                    v-permission="'recycleLogistics:abnormalSign:log'"
                    @click="openOrderLog(row)"
                >
                    [订单日志]
                </el-button>
                <template v-if="useStatus === '1'">
                    <el-button
                        type="danger"
                        link
                        v-permission="'recycleLogistics:abnormalSign:cancel'"
                        @click="handleCancelOrder(row)"
                    >
                        [取消订单]
                    </el-button>
                    <el-button
                        type="primary"
                        link
                        v-permission="'recycleLogistics:abnormalSign:fallback'"
                        @click="fallbackOrder(row)"
                    >
                        [回退状态]
                    </el-button>
                    <el-button
                        type="success"
                        link
                        v-permission="'recycleLogistics:abnormalSign:receive'"
                        @click="handleLogisticsSigning(row)"
                    >
                        [快递签收]
                    </el-button>
                    <el-button
                        type="warning"
                        link
                        v-permission="'recycleLogistics:abnormalSign:userTag'"
                        @click="openEditUserTag(row)"
                    >
                        [用户标签]
                    </el-button>
                    <el-button
                        type="danger"
                        link
                        v-permission="
                            'recycleLogistics:abnormalSign:interception'
                        "
                        @click="applyForInterception(row)"
                    >
                        [申请拦截退回]
                    </el-button>
                </template>
            </div>
        </template>

        <component :is="orderLog" ref="orderLogRef" />
        <component
            :is="orderDetail"
            ref="orderDetailRef"
            @refresh="handleStatusChange"
        />
        <userBindTag ref="userTagRef" />
        <reject-modal ref="rejectModalRef" @refresh="handleStatusChange" />
    </order-page>
</template>

<script setup>
    import { ref, reactive, getCurrentInstance } from 'vue';
    import { ElMessageBox } from 'element-plus/es';
    import { EleMessage } from 'ele-admin-plus/es';
    import OrderPage from '@/views/recycleLogistics/components/order-page.vue';
    import { useRouter } from 'vue-router';
    import { useOrderOperation } from '@/utils/use-order-operation';
    //用户标签
    import userBindTag from '@/views/recycleOrder/components/user-bind-tag.vue';
    import RejectModal from '@/views/recycleLogistics/components/reject-modal.vue';

    defineOptions({ name: 'AbnormalSign' });

    let router = useRouter();
    const { proxy } = getCurrentInstance();
    /** 页面组件实例 */
    const pageRef = ref(null);
    //用户标签
    const userTagRef = ref(null);
    //驳回弹窗
    const rejectModalRef = ref(null);

    const useStatus = ref('1');

    const pageConfig = reactive({
        get pageUrl() {
            return useStatus.value === '1'
                ? '/order/orderInfo/exceptionOrderList'
                : '/order/orderInfo/exceptionHistoryList';
        },
        get exportUrl() {
            return useStatus.value === '1'
                ? '/order/orderInfo/exceptionOrderExport'
                : '/order/orderInfo/exceptionHistoryExport';
        },
        fileName: '路由异常签收',
        cacheKey: 'abnormalSignTable',
        params: { status: '1' }
    });

    function handleStatusChange() {
        pageRef.value?.reload();
    }

    //用户绑定标签
    function openEditUserTag(row) {
        userTagRef.value?.handleOpen(row);
    }

    // 同意异常签收
    function handleAgree(row) {
        ElMessageBox.confirm('确认同意异常签收？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '关闭',
            type: 'warning'
        }).then(() => {
            proxy.$http
                .post('/order/orderInfo/exceptcheck', {
                    id: row.exceptOrderCheckInfo.id,
                    checkStatus: 2
                })
                .then((res) => {
                    if (res.data.code === 200) {
                        EleMessage.success('操作成功');
                        pageRef.value?.reload();
                    } else {
                        EleMessage.error(res.data.msg);
                    }
                });
        });
    }

    // 驳回异常签收
    function handleReject(row) {
        let id = row.exceptOrderCheckInfo.id;
        rejectModalRef.value?.handleOpen(id);
    }

    const {
        handleCancelOrder,
        applyForInterception,
        fallbackOrder,
        toOrderDetail,
        openOrderLog,
        orderLogRef,
        orderDetailRef,
        orderLog,
        orderDetail
    } = useOrderOperation(pageRef);

    // Add logistics signing function
    function handleLogisticsSigning(row) {
        ElMessageBox.confirm('确认签收？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '关闭',
            type: 'warning'
        }).then(() => {
            proxy.$http
                .post('/order/orderInfo/adminDeliverySigned', {
                    orderId: row.orderId
                })
                .then((res) => {
                    if (res.data.code === 200) {
                        EleMessage.success('操作成功');
                        pageRef.value?.reload();
                    } else {
                        EleMessage.error(res.data.msg);
                    }
                });
        });
    }
</script>
