import { apiPayPrepay, apiQueryOrder, apiH5PayPrepay } from '@/api/app'
import { wxOaPay } from '@/hooks/wechat'
import { wxpay } from '@/utils/pay'
import { getClient } from '@/utils/client'
import { PayEnum } from '../enums/appEnums'
import { ref } from 'vue'

/**
 * @description 支付函数钩子
 * @param { typeValue } 支付的枚举
 * @param { emit } 是否刷新页面
 * @return { Function } 暴露钩子
 */
export function usePay(typeValue: any, emit: any = () => {}) {
    // 支付枚举来源
    const form = ref<any>(PayEnum[typeValue])
    // 订单ID
    const order_id = ref(0)

    /**
     * @description 1. 初始化获取支付方式
     * @param { params } 支付订单数据
     */
    const initPayWay = async (orderId, orderSn): Promise<void> => {
        try {
            console.log('orderId:', orderId, orderSn)
            order_id.value = orderId
            handlePayPrepay(orderId)
        } catch (err) {
            console.log('获取支付方式', err)
            uni.$u.toast('支付方式' + err)
        }
    }

    /**
     * @description 2. 预支付
     * @param { params } 支付订单数据
     */
    const handlePayPrepay = async (orderId): Promise<void> => {
        const scene = getClient()
        let payChannel = ''
        switch (scene) {
            case 1:
                payChannel = 'mp_channel'
                break
            case 2:
                payChannel = 'oa_channel'
                break
            case 3:
                payChannel = 'h5_channel'
                break
        }
        try {
            if (payChannel === 'mp_channel' || payChannel === 'oa_channel') {
                const res = await apiPayPrepay({
                    orderId,
                    payChannel
                })
                console.log('预支付', res)
                // 支付
                handlePay(res)
            } else if (payChannel === 'h5_channel') {
                const res = await apiH5PayPrepay({
                    orderId,
                    payChannel
                })
                console.log('h5预支付', res)
                // 支付
                handlePay(res)
            }
        } catch (err) {
            console.log('获取预支付', err)
            uni.$u.toast('预支付' + err)
        }
    }

    /**
     * @description 3. 吊起支付并处理支付结构
     * @param { params } 支付订单数据
     */
    const handlePay = async (params): Promise<void> => {
        try {
            // #ifndef H5
            await wxpay(params)
            // #endif

            // #ifdef H5
            console.log('支付前')
            await wxOaPay(params)
            console.log('支付返回')
            // #endif

            // 只在结算订单界面跳转
            // 查询订单状态
            await apiQueryOrder({ id: order_id.value })
            if (form.value === 'orderBuy') uni.reLaunch({ url: `/pages/order/index` })
            else emit('refresh') // 订单列表与详情的情况下刷新
        } catch (err) {
            if (form.value === 'orderBuy') {
                uni.reLaunch({ url: `/pages/order/index` })
            }
            if (Number(mount) == 0) {
                return uni.$u.toast('下单成功')
            }
            uni.$u.toast('支付取消')
            console.log(err, '支付取消')
        }
    }

    return {
        initPayWay,
        handlePayPrepay,
        handlePay
    }
}
