import { getCurrentInstance, ref } from 'vue';
import { ElMessageBox } from 'element-plus/es';
import { EleMessage } from 'ele-admin-plus/es';
import { useRouter } from 'vue-router';
//订单日志
import orderLog from '@/views/recycleOrder/components/order-log.vue';
//订单详情
import orderDetail from '@/views/recycleOrder/components/order-detail.vue';

export function useOrderOperation(pageRef) {
    const { proxy } = getCurrentInstance();
    const router = useRouter();

    // 组件引用
    const orderLogRef = ref(null);
    const orderDetailRef = ref(null);

    function messageBoxConfirm({ message, fetch }) {
        ElMessageBox.confirm(message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '关闭',
            type: 'warning'
        }).then(() => {
            fetch().then((res) => {
                if (res.data.code === 200) {
                    EleMessage.success('操作成功');
                    pageRef.value?.reload();
                } else {
                    EleMessage.error(res.data.msg);
                }
            });
        });
    }

    return {
        // 组件引用
        orderLogRef,
        orderDetailRef,
        orderLog,
        orderDetail,

        //订单详情
        toOrderDetail(row) {
            orderDetailRef.value?.handleOpen(row);
        },

        //订单日志
        openOrderLog(row) {
            orderLogRef.value?.handleOpen(row.orderId);
        },

        //取消订单
        handleCancelOrder(row) {
            const rows = row == null ? [] : [row];
            if (!rows.length) {
                EleMessage.error('请至少选择一条数据');
                return;
            }
            messageBoxConfirm({
                message: '是否确认取消订单？',
                fetch: () =>
                    proxy.$http.post('/order/orderInfo/adminCancel', {
                        orderIds: rows.map((item) => item.orderId)
                    })
            });
        },
        //申请拦截退出
        applyForInterception(row) {
            messageBoxConfirm({
                message: '确认拦截?',
                fetch: () =>
                    proxy.$http.post(
                        `/order/orderInfo/intercept/${row.orderId}`
                    )
            });
        },
        //物流签收
        handleLogisticsSigning(row) {
            messageBoxConfirm({
                message: '确认签收？',
                fetch: () =>
                    proxy.$http.post('/order/orderInfo/adminSigned', {
                        orderIds: [row.orderId]
                    })
            });
        },
        //回退状态
        fallbackOrder(row) {
            messageBoxConfirm({
                message: '确认回退？',
                fetch: () =>
                    proxy.$http.post(
                        `/order/orderInfo/statusBack/${row.orderId}`
                    )
            });
        },
        //确认收货
        handleConfirmReceipt(row) {
            const rows = row == null ? [] : [row];
            if (!rows.length) {
                EleMessage.error('请至少选择一条数据');
                return;
            }
            messageBoxConfirm({
                message: '确认收货？',
                fetch: () =>
                    proxy.$http.post('/order/orderInfo/adminConfirm', {
                        orderIds: rows.map((item) => item.orderId)
                    })
            });
        },
        //物流揽件
        materialPickup(row) {
            messageBoxConfirm({
                message: '确认物流揽件？',
                fetch: () =>
                    proxy.$http.post('/order/orderInfo/adminPickup', {
                        orderIds: [row.orderId]
                    })
            });
        },
        //取消拦截退出
        cancelInterception(row) {
            messageBoxConfirm({
                message: '确认取消拦截?',
                fetch: () =>
                    proxy.$http.post(
                        `/order/orderInfo/interceptBack/${row.orderId}`
                    )
            });
        }
    };
}
