import {
	gotoOrderAppPayApi,
	gotoOrderH5PayApi,
	gotoOrderPayApi,
	payOrderSuccessApi
} from '../api/user'

// #ifdef H5
const jweixin = require('jweixin-module')

/**
 * 普通H5处理
 * @param payInfo 结算返回的支付信息
 */

async function payH5InEquipment(payInfo) {
	try {
		const res = await gotoOrderH5PayApi(payInfo)
		location.replace(res.data.mwebUrl)
	} catch (e) {
		uni.showToast({ title: '支付失败', icon: 'none' })
		if ([1, 2].includes(payInfo.purchaseMode)) {
			uni.redirectTo({ url: '/pages_category_page1/orderModule/index?type=1' })
		} else if ([3, 4, 5].includes(payInfo.purchaseMode)) {
			uni.redirectTo({ url: '/pages_user/serve/payment-completed/index?state=fail' })
		}
	} finally {
		uni.hideLoading()
	}
}

/**
 * 微信内H5处理
 * @param payInfo 结算返回的支付信息
 * @param orderId 订单ID
 */

async function payH5InWechat(payInfo) {
	const res = await gotoOrderPayApi(payInfo)
	jweixin.config({
		debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
		appId: res.data.appId, // 必填,公众号的唯一标识
		timestamp: res.data.timeStamp, // 必填,生成签名的时间戳
		nonceStr: res.data.nonceStr, // 必填,生成签名的随机串
		signature: res.data.paySign, // 必填,签名,见附录1
		jsApiList: [ 'chooseWXPay' ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
	})
	if ([1, 2].includes(payInfo.purchaseMode)) {
		jweixin.ready(function () {
			jweixin.checkJsApi({
				jsApiList: [ 'chooseWXPay' ], // 需要检测的JS接口列表,所有JS接口列表见附录2,
				success(res) { },
				fail(res) { }
			})
			jweixin.chooseWXPay({
				timestamp: res.data.timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
				nonceStr: res.data.nonceStr, // 支付签名随机串,不长于 32 位
				package: res.data.package, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***)
				signType: res.data.signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
				paySign: res.data.paySign, // 支付签名
				success(res) { // 支付成功后的回调函数
					uni.showToast({ icon: 'none', title: '支付成功' })
					uni.redirectTo({ url: '/pages_category_page1/orderModule/paySuccessful?orderId=' + payInfo.orderId })
				},
				cancel(r) {
					uni.showToast({ icon: 'none', title: '用户取消支付' })
					uni.redirectTo({ url: '/pages_category_page1/orderModule/index?type=1' })
				},
				fail(res) {
					uni.showToast({ icon: 'none', title: '微信内支付错误' })
					uni.redirectTo({ url: '/pages_category_page1/orderModule/index?type=1' })
				}
			})
		})
		jweixin.error(function (res) {
			uni.showToast({ icon: 'none', title: '微信内支付加载失败', duration: 3000 })
			uni.redirectTo({ url: '/pages_category_page1/orderModule/index?type=1' })
		})
	} else if ([3, 4, 5].includes(payInfo.purchaseMode)) {
		jweixin.ready(function () {
			jweixin.checkJsApi({
				jsApiList: [ 'chooseWXPay' ], // 需要检测的JS接口列表,所有JS接口列表见附录2,
				success(res) { },
				fail(res) { }
			})
			jweixin.chooseWXPay({
				timestamp: res.data.timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
				nonceStr: res.data.nonceStr, // 支付签名随机串,不长于 32 位
				package: res.data.package, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***)
				signType: res.data.signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
				paySign: res.data.paySign, // 支付签名
				success(res) { // 支付成功后的回调函数
					uni.showToast({ icon: 'none', title: '支付成功' })
					uni.redirectTo({ url: '/pages_user/serve/payment-completed/index' })
				},
				cancel(r) {
					uni.showToast({ icon: 'none', title: '用户取消支付' })
					uni.redirectTo({ url: '/pages_user/serve/payment-completed/index?state=fail' })
				},
				fail(res) {
					uni.showToast({ icon: 'none', title: '微信内支付错误' })
					uni.redirectTo({ url: '/pages_user/serve/payment-completed/index?state=fail' })
				}
			})
		})
		jweixin.error(function (res) {
			uni.showToast({ icon: 'none', title: '微信内支付加载失败', duration: 3000 })
			uni.redirectTo({ url: '/pages_user/serve/payment-completed/index?state=fail' })
		})
	}
}

/**
 * H5拉起支付
 * @param payInfo 结算返回的支付信息
 */

async function h5Pay(payInfo) {
	const ua = navigator.userAgent.toLowerCase()
	if (ua.match(/MicroMessenger/i) == 'micromessenger') {
		await payH5InWechat(payInfo)
	} else {
		await payH5InEquipment(payInfo)
	}
}
// #endif

// #ifdef MP-ALIPAY
/**
 * 支付宝小程序拉起支付
 * @param payInfo 结算返回的支付信息
 * @return {Promise<void>}
 */

async function mpAliPay(payInfo) {
	if ([1, 2].includes(payInfo.purchaseMode)) {
		try {
			const res = await gotoOrderPayApi(payInfo)
			uni.requestPayment({
				provider: 'alipay',
				orderInfo: res.data.tradeNo,
				success(payRes) {
					if (payRes.resultCode == '6001') {
						uni.showToast({ icon: 'none', title: '取消支付' })
						uni.redirectTo({ url: '/pages_category_page1/orderModule/index?type=1' })
					} else if (payRes.resultCode == '9000') {
						uni.showToast({ icon: 'none', title: '支付成功' })
						uni.redirectTo({ url: '/pages_category_page1/orderModule/paySuccessful?orderId=' + payInfo.orderId })
					}
				},
				fail(err) {
					uni.showToast({ icon: 'none', title: '支付取消' })
					uni.redirectTo({ url: '/pages_category_page1/orderModule/index?type=1' })
				}
			})
		} catch (e) {
			uni.showToast({ title: '支付宝支付异常', icon: 'none' })
			uni.redirectTo({ url: '/pages_category_page1/orderModule/index?type=1' })
		} finally {
			uni.hideLoading()
		}
	} else if ([3, 4, 5].includes(payInfo.purchaseMode)) {
		try {
			const res = await gotoOrderPayApi(payInfo)
			uni.requestPayment({
				provider: 'alipay',
				orderInfo: res.data.tradeNo,
				success(payRes) {
					if (payRes.resultCode == '6001') {
						uni.showToast({ icon: 'none', title: '取消支付' })
						uni.redirectTo({ url: '/pages_user/serve/payment-completed/index?state=fail' })
					} else if (payRes.resultCode == '9000') {
						uni.showToast({ icon: 'none', title: '支付成功' })
						uni.redirectTo({ url: '/pages_user/serve/payment-completed/index' })
					}
				},
				fail(err) {
					uni.showToast({ icon: 'none', title: '支付取消' })
					uni.redirectTo({ url: '/pages_user/serve/payment-completed/index?state=fail' })
				}
			})
		} catch (e) {
			uni.showToast({ title: '支付宝支付异常', icon: 'none' })
			uni.redirectTo({ url: '/pages_user/serve/payment-completed/index?state=fail' })
		} finally {
			uni.hideLoading()
		}
	}
}
// #endif

// #ifdef MP-WEIXIN
/**
 * 微信小程序拉起支付
 * @param payInfo
 * @return {Promise<void>}
 */

async function mpWechatPay(payInfo) {
	if ([1, 2].includes(payInfo.purchaseMode)) {
		try {
			const res = await gotoOrderPayApi(payInfo)
			uni.requestPayment({
				provider: 'wxpay',
				timeStamp: res.data.timeStamp,
				nonceStr: res.data.nonceStr,
				package: res.data.package,
				signType: res.data.signType,
				paySign: res.data.paySign,
				success: async (payRes) => {
					// 拼团微信支付成功回调
					if (payInfo.collageId) await payOrderSuccessApi({ orderId: payInfo.orderId, collageId: payInfo.collageId })
					uni.showToast({ icon: 'none', title: '支付成功' })
					uni.redirectTo({ url: '/pages_category_page1/orderModule/paySuccessful?orderId=' + payInfo.orderId })
				},
				fail(err) {
					uni.showToast({ icon: 'none', title: '用户取消支付' })
					uni.redirectTo({ url: '/pages_category_page1/orderModule/index?type=1' })
				}
			})
		} catch (e) {
			uni.showToast({ title: '微信支付拉起失败', icon: 'none' })
			uni.redirectTo({ url: '/pages_category_page1/orderModule/index?type=1' })
		}
	} else if ([3, 4, 5].includes(payInfo.purchaseMode)) {
		try {
			const res = await gotoOrderPayApi(payInfo)
			uni.requestPayment({
				provider: 'wxpay',
				timeStamp: res.data.timeStamp,
				nonceStr: res.data.nonceStr,
				package: res.data.package,
				signType: res.data.signType,
				paySign: res.data.paySign,
				success: async (payRes) => {
					// 拼团微信支付成功回调
					if (payInfo.collageId) await payOrderSuccessApi({ orderId: payInfo.orderId, collageId: payInfo.collageId })
					uni.showToast({ icon: 'none', title: '支付成功' })
					uni.redirectTo({ url: '/pages_user/serve/payment-completed/index' })
				},
				fail(err) {
					uni.showToast({ icon: 'none', title: '用户取消支付' })
					uni.redirectTo({ url: '/pages_user/serve/payment-completed/index?state=fail' })
				}
			})
		} catch (e) {
			uni.showToast({ title: '微信支付拉起失败', icon: 'none' })
			uni.redirectTo({ url: '/pages_user/serve/payment-completed/index?state=fail' })
		}
	}
}
// #endif

// #ifdef APP
/**
 * App拉起微信支付
 * @param payInfo
 * @return {Promise<void>}
 */

async function appWechatPay(payInfo) {
	if ([1, 2].includes(payInfo.purchaseMode)) {
		try {
			const res = await gotoOrderAppPayApi(payInfo)
			const obj = {
				appid: res.data.appId,
				noncestr: res.data.nonceStr,
				package: 'Sign=WXPay',
				prepayid: res.data.prepayId,
				timestamp: res.data.timeStamp,
				sign: res.data.paySign,
				partnerid: res.data.partnerId
			}
			uni.requestPayment({
				provider: 'wxpay',
				orderInfo: obj,
				success(payRes) {
					uni.showToast({ icon: 'none', title: '支付成功' })
					uni.redirectTo({ url: '/pages_category_page1/orderModule/paySuccessful?orderId=' + payInfo.orderId })
				},
				fail(err) {
					uni.showToast({ icon: 'none', title: '用户取消支付' })
					uni.redirectTo({ url: '/pages_category_page1/orderModule/index?type=1' })
				}
			})
		} catch (e) {
			uni.showToast({ title: 'APP拉起微信支付失败', icon: 'none' })
			uni.redirectTo({ url: '/pages_category_page1/orderModule/index?type=1' })
		} finally {
			uni.hideLoading()
		}
	} else if ([3, 4, 5].includes(payInfo.purchaseMode)) {
		try {
			const res = await gotoOrderAppPayApi(payInfo)
			const obj = {
				appid: res.data.appId,
				noncestr: res.data.nonceStr,
				package: 'Sign=WXPay',
				prepayid: res.data.prepayId,
				timestamp: res.data.timeStamp,
				sign: res.data.paySign,
				partnerid: res.data.partnerId
			}
			uni.requestPayment({
				provider: 'wxpay',
				orderInfo: obj,
				success(payRes) {
					uni.showToast({ icon: 'none', title: '支付成功' })
					uni.redirectTo({ url: '/pages_user/serve/payment-completed/index' })
				},
				fail(err) {
					uni.showToast({ icon: 'none', title: '用户取消支付' })
					uni.redirectTo({ url: '/pages_user/serve/payment-completed/index?state=fail' })
				}
			})
		} catch (e) {
			uni.showToast({ title: 'APP拉起微信支付失败', icon: 'none' })
			uni.redirectTo({ url: '/pages_user/serve/payment-completed/index?state=fail' })
		} finally {
			uni.hideLoading()
		}
	}
}
// #endif

/**
 * 银行卡支付
 * @param payInfo
 * @return {Promise<void>}
 */

async function bankCardPay(payInfo) {
	try {
		let res
		if ([1, 2].includes(payInfo.purchaseMode)) {
		} else if (payInfo.purchaseMode === 3) {
			// res = await payGotoH5SettlePayApi(payInfo)
			// uni.redirectTo({ url: '/pages_user/serve/payment-completed/index' })
		} else if (payInfo.purchaseMode === 4) {
			// res = await payGotoH5VoucherApi(payInfo)
			// uni.redirectTo({ url: '/pages_user/serve/payment-completed/index' })
		}
	} catch (e) {
		if ([1, 2].includes(payInfo.purchaseMode)) {
		} else if ([3, 4, 5].includes(payInfo.purchaseMode)) {
			uni.showToast({ title: '银行卡支付失败', icon: 'none' })
			uni.redirectTo({ url: '/pages_user/serve/payment-completed/index?state=fail' })
		}
	} finally {
		uni.hideLoading()
	}
}

/**
 * 通联H5处理
 * @param payInfo 结算返回的支付信息
 */

async function h5TonglianPay(payInfo) {
	try {
		const res = await gotoOrderH5PayApi(payInfo)
		console.log(res)
		if (res.data === '支付成功') { // 零元支付情况
			uni.redirectTo({ url: '/pages_user/serve/payment-completed/index' })
		} else {
			const payData = JSON.parse(res.data.package)
			const form = document.createElement('form')
			form.setAttribute('action', res.data.mwebUrl)
			form.setAttribute('method', 'POST')
			let input
			for (const key in payData) {
				input = document.createElement('input')
				input.name = key
				input.value = payData[key]
				form.appendChild(input)
			}
			document.body.appendChild(form)
			form.submit()
			document.body.removeChild(form)
		}
	} catch (e) {
		console.log(e)
		uni.showToast({ title: '支付失败', icon: 'none' })
		if ([1, 2].includes(payInfo.purchaseMode)) {
			uni.redirectTo({ url: '/pages_category_page1/orderModule/index?type=1' })
		} else if ([3, 4, 5].includes(payInfo.purchaseMode)) {
			uni.redirectTo({ url: '/pages_user/serve/payment-completed/index?state=fail' })
		}
	} finally {
		uni.hideLoading()
	}
}

/**
 * 处理支付
 * @param submitResult 结算结果
 */

export async function handleDoPay(submitResult, purchaseMode) {
	uni.showLoading({
		mask: true,
		title: '支付中...'
	})
	if (purchaseMode) {
		const submitInfo = { ...submitResult, purchaseMode }
		if (submitInfo.paymentMode === 999) {
			await bankCardPay(submitInfo)
		} else if (submitInfo.paymentMode === 1) {
			// 微信支付
			// #ifdef APP
			await appWechatPay(submitInfo)
			// #endif
			// #ifdef MP-WEIXIN
			await mpWechatPay(submitInfo)
			// #endif
			// #ifdef H5
			await h5Pay(submitInfo)
			// #endif
		} else if (submitInfo.paymentMode === 4) {
			// #ifdef H5
			await h5TonglianPay(submitInfo)
			// #endif
		} else if ([2, 3].includes(submitInfo.paymentMode)) {
			// 支付宝
			// #ifdef MP-ALIPAY
			await mpAliPay(submitInfo)
			// await appWechatPay(submitResult,this.orderId)
			throw new Error('支付宝相关支付暂时只支持支付宝小程序')
			// #endif
		}
		// #ifndef H5
		// setTimeout(()=>{
		//     uni.navigateTo({
		//         url: '/pages_category_page1/orderModule/index?type=2'
		//     })
		// },1000)
		// #endif
		/*  uni.navigateTo({
							url: '/pages_category_page1/orderModule/index?type=2'
					})*/
		// uni.hideLoading()
	}
}