import order from "@/api/https/order"
import toast from "@/utils/toast"
import { timeFormatHMS } from "@/utils/tools"
import { onLoad } from "@dcloudio/uni-app"
import { computed, ref, Ref } from "vue"
import configData from "@/utils/configCms"

/*************************************************
 * 售后详情
 *@author zhang-shu-wei <2966211270@qq.com>
 *@date 2023-02-06 09:10:52
 *@param {}-
 *@return {*}
*/
function afterSalesOrderDetailFn() {
    /*************************************************
     * 获取售后商品数据
    */
    let afsOrderID = ''     // 售后单号
    onLoad(async (options) => {
        afsOrderID = options!.afsOrderID
        // 查询售后单
        await GetAfsOrders()
        await GetAfsOrderSkuInfo()
        await GetOrders()
    })

    /*************************************************
     * 查询售后单
    */
    const afsOrder: Ref<AnyObject> = ref({ vendorOrderID: 99 })
    async function GetAfsOrders() {
        let data = {
            afsOrderID: afsOrderID
        }
        let res = await order.get_afs_orders(data)
        if (res.code == 0 && res.data.totalCount >= 1) {
            afsOrder.value = res.data.data[0]
        } else {
            uni.jxAlert({
                title: '提示',
                content: '售后单数据异常',
                success: () => {
                    uni.navigateBack()
                }
            })
        }
    }

    /*************************************************
     * 查询售后单商品
    */
    const afsOrderSku = ref<Array<AnyObject>>([])   // 售后商品
    async function GetAfsOrderSkuInfo() {
        let data = {
            afsOrderID: afsOrderID,
            vendorID: afsOrder.value.vendorID
        }
        let res = await order.get_afs_order_sku_info(data)
        if (res.code == 0) {
            afsOrderSku.value = res.data
        } else {
            uni.jxAlert({
                title: '提示',
                content: '售后单数据异常',
                success: () => {
                    uni.navigateBack()
                }
            })
        }
    }

    /*************************************************
     * 查询订单
    */
    const orderData = ref<AnyObject>({})
    async function GetOrders() {
        let data = {
            vendorOrderID: afsOrder.value.vendorOrderID
        }
        let res = await order.get_orders(data)
        if (res.code == 0 && res.data.totalCount >= 1) {
            orderData.value = res.data.data[0]
        } else {
            uni.jxAlert({
                title: '提示',
                content: '售后单数据异常',
                success: () => {
                    uni.navigateBack()
                }
            })
        }
    }

    /*************************************************
     * 售后方式
    */
    const configCms:AnyObject = configData.serveInfo
    const afsAppealTypeName = computed(() => {
        if (afsOrder.value.appealType !== undefined) {
            return configCms.afsAppealTypeName[afsOrder.value.appealType]
        } else {
            return '未知'
        }
    })

    /*************************************************
     * 订单创建时间
    */
    const createTime = computed(() => {
        if (afsOrder.value.afsCreatedAt !== undefined) {
            return timeFormatHMS(afsOrder.value.afsCreatedAt)
        } else {
            return ''
        }
    })

    /*************************************************
     * 售后单状态
    */
    const afsStatus = computed(() => {
        if (afsOrder.value.status !== undefined) {
            return configCms.orderStatus[afsOrder.value.status]
        } else {
            return '未知'
        }
    })

    /*************************************************
     * 配送方式
    */
    const waybillVendorName = computed(() => {
        if (orderData.value.waybillVendorID !== undefined) {
            return configCms.vendorName[orderData.value.waybillVendorID]
        } else {
            return '门店自送'
        }
    })

    /*************************************************
     * 售后原因
    */
    const afsReason = computed(() => {
        if (afsOrder.value.reasonType !== undefined) {
            return configCms.afsReasonTypeName[afsOrder.value.reasonType]
        } else {
            return '未知'
        }
    })

    /*************************************************
     * 图片列表
    */
    const imgList = computed(() => {
        if (afsOrder.value.reasonImgList !== undefined) {
            return afsOrder.value.reasonImgList.split(',')
        } else {
            return []
        }
    })

    /*************************************************
     * 同意退款
    */
    function handleAgree() {
        uni.jxConfirm({
            title: '提示',
            content: '是否同意处理该售后单',
            success: () => {
                agreeOrRefuse(1)
            },
        })
    }

    /*************************************************
     * 同意退款
    */
    async function agreeOrRefuse(type: number) {
        // if (afsOrder.value.vendorID == 3) {
        //     if (type == 1) {
        //         let data = {
        //             vendorOrderID: afsOrder.value.vendorOrderID,
        //             vendorID: afsOrder.value.vendorID,
        //             acceptIt: Boolean(1),  // 拒绝为0，同意为1
        //             reason: type === 1 ? '同意退款' : reason.value,
        //         }
        //         let res = await order.agree_or_refuse_cancel(data)
        //         if (res.code == 0) {
        //             toast('操作成功', 1)
        //             afsOrder.value.flag = 1
        //         } else {
        //             toast(res.desc || res.data)
        //         }
        //     } else {
        //         let data = {
        //             vendorOrderID: afsOrder.value.vendorOrderID,
        //             vendorID: afsOrder.value.vendorID,
        //             acceptIt: Boolean(0),  // 拒绝为0，同意为1
        //             reason: type === 1 ? '同意退款' : reason.value,
        //         }
        //         let res = await order.agree_or_refuse_cancel(data)
        //         if (res.code == 0) {
        //             toast('操作成功', 1)
        //             afsOrder.value.flag = 3
        //         } else {
        //             toast(res.desc || res.data)
        //         }
        //     }
        // } else {
        let data = {
            afsOrderID: afsOrder.value.afsOrderID,
            vendorID: afsOrder.value.vendorID,
            approveType: type,
            reason: type === 1 ? '同意退款' : reason.value
        }
        let res = await order.agree_orRefuse_refund(data)
        if (res.code == 0) {
            toast('操作成功', 1)
        } else {
            toast(res.desc || res.data)
        }
        // }
    }

    /*************************************************
     * 退货待确认
    */
    function confirmGoods() {
        uni.jxConfirm({
            title: '提示',
            content: '是否已收到货',
            success: async () => {
                let data = {
                    afsOrderID: afsOrder.value.afsOrderID,
                    vendorID: afsOrder.value.vendorID
                }
                let res = await order.confirm_received_return_goods(data)
                if (res.code == 0) {
                    toast('操作成功', 1)
                    afsOrder.value.flag = 4
                } else {
                    toast(res.desc || res.data)
                }
            },
        })
    }

    /*************************************************
     * 取消驳回
    */
    const popup = ref<any>(null)
    const reason = ref<string>()        // 输入内容
    function cancelFn() {
        reason.value = ''
        popup.value.close()
    }

    /*************************************************
     * 同意驳回
    */
    function diaConfirm() {
        if (reason.value) {
            agreeOrRefuse(3)
            reason.value = ''
            popup.value.close()
        } else {
            toast('请输入驳回原因')
        }
    }

    return {
        afsOrder,           // 售后订单
        afsOrderSku,        // 售后商品数据
        orderData,          // 订单数据
        afsAppealTypeName,  // 售后方式
        createTime,         // 订单创建时间
        afsStatus,             // 售后单状态
        waybillVendorName,  // 配送方式
        afsReason,          // 售后原因
        imgList,            // 图片列表
        popup,              // 售后dialog 实例
        handleAgree,        // 同意退款
        confirmGoods,       // 退货待确认
        reason,             // 驳回原因
        cancelFn,           // 取下驳回
        diaConfirm,         // 同意驳回
    }
}


export default afterSalesOrderDetailFn

