<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>
                <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>
                <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>
                <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>
            </div>
        </template>

        <component :is="orderLog" ref="orderLogRef" />
        <component :is="orderDetail" ref="orderDetailRef" @refresh="pageRef.value?.reload()" />
        <userBindTag ref="userTagRef" />
    </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';

defineOptions({ name: 'abnormalSign' });

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

const useStatus = ref('1');
function handleStatusChange() {
    pageRef.value.reload({ useStatus: useStatus.value });
}

const pageConfig = reactive({
    pageUrl: '/order/orderInfo/exceptionOrderList',
    exportUrl: '/order/orderInfo/exceptionOrderExport',
    fileName: '路由异常签收',
    cacheKey: 'abnormalSignTable',
    params: { status: '1' }
});

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

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

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

const { 
    handleCancelOrder, 
    applyForInterception, 
    handleLogisticsSigning, 
    fallbackOrder,
    toOrderDetail,
    openOrderLog,
    orderLogRef,
    orderDetailRef,
    orderLog,
    orderDetail
} = useOrderOperation(pageRef);
</script>
