import { store } from "@/store"
import toast from "@/utils/toast"
import order from "@/api/https/order"

/*************************************************
 * 异常订单处理
*/
function abonmrmalFn(props: { item: AnyObject }) {
    // 是否拒绝用户退款
    function fnCancelOrder(num: number) {
        let data = {
            vendorOrderID: props.item.vendorOrderID,
            vendorID: props.item.vendorID,
            acceptIt: Boolean(num),
            reason: '理由暂无'
        }
        // let data = {
        //     afsOrderID: props.item.afsOrderID,
        //     vendorID: props.item.vendorID,
        //     approveType: num,
        //     reason: '理由暂无'
        // }
        // 拒绝退款
        if (num == 0) {
            uni.jxConfirm({
                title: '提示',
                content: '是否拒绝用户取消该订单',
                success: async () => {
                    let res = await order.agree_or_refuse_cancel(data)
                    // let res = await order.agree_orRefuse_refund(data)
                    if (res.code == 0) {
                        toast('拒绝成功', 1)
                        store.commit('serveInfo/setUpdateOrder', Date.now())
                    } else {
                        toast('决绝失败', 2)
                    }
                }
            })
        }

        // 同意退款
        if (num == 1) {
            uni.jxConfirm({
                title: '提示',
                content: '是否同意用户取消该订单',
                success: async () => {
                    let res = await order.agree_or_refuse_cancel(data)
                    // let res = await order.agree_orRefuse_refund(data)
                    if (res.code == 0) {
                        toast('同意成功', 1)
                        store.commit('serveInfo/setUpdateOrder', Date.now())
                    } else {
                        toast('同意失败', 2)
                    }
                }
            })
        }
    }


    /*************************************************
     * 骑手申请取消
    */
    function fnCancelDeliver(num: number) {
        let data = {
            vendorOrderID: props.item.vendorOrderID,
            vendorID: props.item.vendorID,
            acceptIt: Boolean(num),
            reason: '理由暂无'
        }
        // 
        if (num == 0) {
            uni.jxConfirm({
                title: '提示',
                content: '拒绝后只能由该配送员完成本订单',
                success: async () => {
                    let res = await order.accept_or_refuse_failed_get_order(data)
                    if (res.code == 0) {
                        store.commit('serveInfo/setUpdateOrder', Date.now())
                        fnCallNew()
                    } else {
                        toast('操作失败', 2)
                    }
                }
            })
        }

        if (num == 1) {
            uni.jxConfirm({
                title: '提示',
                content: '是否同意取消该订单配送',
                success: async () => {
                    let res = await order.accept_or_refuse_failed_get_order(data)
                    if (res.code == 0) {
                        store.commit('serveInfo/setUpdateOrder', Date.now())
                        fnCallNew()
                    } else {
                        toast('操作失败', 2)
                    }
                }
            })
        }
    }
    // 重新召唤骑手
    async function fnCallNew() {
        let data = {
            vendorOrderID: props.item.vendorOrderID,
            vendorID: props.item.vendorID
        }
        uni.jxConfirm({
            title: '提示',
            content: '是否重新召唤骑手进行配送',
            success: async () => {
                let res = await order.callP_m_courier(data)
                if (res.code == 0) {
                    toast('操作成功')
                    store.commit('serveInfo/setUpdateOrder', Date.now())
                } else {
                    toast('操作失败')
                }
            }
        })
    }


    /*************************************************
     * 是否收到退货
    */
    function fnGoodsReturn() {
        let data = {
            vendorOrderID: props.item.vendorOrderID,
            vendorID: props.item.vendorID,
        }
        uni.jxConfirm({
            title: '提示',
            content: '确认已经收到退回的货物',
            success: async () => {
                let res = await order.confirm_receive_goods(data)
                if (res.code == 0) {
                    toast('操作成功')
                    store.commit('serveInfo/setUpdateOrder', Date.now())
                } else {
                    toast('操作失败')
                }
            }
        })
    }


    return {
        fnCancelOrder,              // 是否统一用户取消订单
        fnCancelDeliver,            // 骑手申请取消
        fnCallNew,                  // 重新召唤骑手
        fnGoodsReturn,              // 是否退回商品
    }
}

export default abonmrmalFn