import {
	defineStore
} from 'pinia';

import {
	ref
} from 'vue';

import {
	APIpayltPayOrder,
	APIpayltPayVip,
	APIrefundVip
} from '@/common/api/pay.js'

import {
	APIcreateOrder
} from '@/common/api/order/index.js'

export const Memberpay = defineStore('memberpay', {
	actions: {
		//会员充值
		actionspayltPayVip(data) {
			return new Promise(async (resolve, reject) => {
				try {
					let res = await APIpayltPayVip({
						...data,
						// #ifdef MP-WEIXIN
						payType: 'WECHAT',
						openId: uni.cache.get('userInfo').wechatOpenId,
						// #endif
						// #ifdef MP-ALIPAY
						payType: 'aliPay',
						openId: uni.cache.get('userInfo').wechatOpenId,
						// #endif
					})
					if (res) {
						uni.showLoading({
							title: '加载中',
							mask: true
						})
						uni.requestPayment({
							// #ifdef MP-WEIXIN
							provider: 'wxpay', //支付类型-固定值
							partnerid: res.appId, // 微信支付商户号
							timeStamp: res.timeStamp, // 时间戳（单位：秒）
							nonceStr: res.nonceStr, // 随机字符串
							package: res.package, // 固定值
							signType: res.signType, //固定值
							paySign: res.paySign, //签名
							// #endif
							// #ifdef MP-ALIPAY
							provider: 'alipay', //支付类型-固定值
							orderInfo: res.tradeNo, // 微信支付商户号
							// #endif
							success: (res) => {
								uni.hideLoading()
								// #ifdef MP-WEIXIN
								uni.showToast({
									title: "支付成功"
								})
								console.log('支付成功')
								// setTimeout(() => {
								// 	uni.navigateBack()
								// }, 1000)
								resolve(true)
								// #endif
								// #ifdef MP-ALIPAY
								if (res.resultCode == '9000') {
									uni.showToast({
										title: "支付成功"
									})
									// setTimeout(() => {
									// 	uni.navigateBack()
									// }, 1000)
									resolve(true)
								} else {
									uni.showToast({
										title: "支付失败"
									})
								}
								// #endif
							},
							fail: (res) => {
								setTimeout(() => {
									uni.hideLoading()
								}, 1000)
								reject(false)
							}
						});
					}
				} catch (e) {
					uni.showToast({
						title: "支付失败"
					})
					setTimeout(() => {
						uni.hideLoading()
					}, 1000)
					reject(false)
				}
			})
		},

		//下单支付 
		actionsltPayOrder(data) {
			return new Promise(async (resolve, reject) => {
				try {
					let res = await APIpayltPayOrder({
						shopId: uni.cache.get('shopId'),
						checkOrderPay: {
							...data.checkOrderPay,
							...{
								userId: uni.cache.get('userInfo').id
							}
						},
						// #ifdef MP-WEIXIN
						payType: 'WECHAT',
						openId: uni.cache.get('userInfo').wechatOpenId,
						// #endif
						// #ifdef MP-ALIPAY
						payType: 'aliPay',
						openId: uni.cache.get('alipayOpenId').alipayOpenId,
						// #endif
						returnUrl: data.returnUrl,
						buyerRemark: data.buyerRemark,
						userId: uni.cache.get('userInfo').id || ''
					})
					console.log('actionsltPayOrder:res',res);
					if(typeof res ==='string'){
						resolve(res)
						return 
					}
					if(!res){
						console.log('支付失败');
						reject(false)
					}
					if (res) {
						uni.showLoading({
							title: '加载中...',
							mask: true
						})
						uni.requestPayment({
							// #ifdef MP-WEIXIN
							provider: 'wxpay', //支付类型-固定值
							partnerid: res.appId, // 微信支付商户号
							timeStamp: res.timeStamp, // 时间戳（单位：秒）
							nonceStr: res.nonceStr, // 随机字符串
							package: res.package, // 固定值
							signType: res.signType, //固定值
							paySign: res.paySign, //签名
							// #endif
							// #ifdef MP-ALIPAY
							provider: 'alipay', //支付类型-固定值
							orderInfo: res.tradeNo, // 微信支付商户号
							// #endif
							success: (res) => {
								uni.hideLoading()
								// #ifdef MP-WEIXIN
								uni.showToast({
									title: "支付成功"
								})
								console.log('支付成功')
								resolve(true)
								// #endif
								// #ifdef MP-ALIPAY
								if (res.resultCode == '9000') {
									uni.showToast({
										title: "支付成功"
									})
									resolve(true)
								} else {
									uni.showToast({
										title: "支付失败"
									})
								}
								// #endif
							},
							fail: (res) => {
								setTimeout(() => {
									uni.hideLoading()
								}, 1000)
								reject(false)
							}
						});
					}
				} catch (e) {
					uni.showToast({
						title: "支付失败"
					})
					setTimeout(() => {
						uni.hideLoading()
					}, 1000)
					reject(false)
				}
			})
		},

		//会员支付
		balancePayOrder(data) {
			return new Promise(async (resolve, reject) => {
				try {
					let res = await APIrefundVip({
						shopId: uni.cache.get('shopId'),
						checkOrderPay: {
							...data.checkOrderPay,
							...{
								userId: uni.cache.get('userInfo').id
							}
						},
						pwd: data.pwd,
						payType: data.payType,
						returnUrl: data.returnUrl,
						buyerRemark: data.buyerRemark,
						shopUserId: data.shopUserId,
						userId: uni.cache.get('userInfo').id || '' //
					})
					resolve(true)
				} catch (e) {
					uni.showToast({
						title: "支付失败"
					})
					setTimeout(() => {
						uni.hideLoading()
					}, 1000)
					reject(false)
				}
			})
		},

		// 生成订单
		actionscreateOrder(data) {
			console.log('actionscreateOrder:生成订单',data);	
			return new Promise(async (resolve, reject) => {
				let res = await APIcreateOrder({
					...data,
					orderId: data.orderId, //多次下单时使用
					shopId: uni.cache.get('shopId'), //店铺Id
					userId: uni.cache.get('userInfo').id || '', //
					seatNum: data.seatNum, //用餐人数
					packFee: data.packFee, //打包费
					originAmount: data.originAmount, //订单原金额（包含打包费） 不含折扣价格 不含餐位费
					tableCode:data.tableCode|| uni.cache.get('tableCode'), //台桌编码
					dineMode: 'dine-in', //堂食 dine-in 外带 take-out 外卖 take-away
					remark: data.remark, //备注
					placeNum: data.placeNum, //当前订单下单次数
					waitCall: data.waitCall //是否等叫 0 否 1 等叫
				})
				console.log(res);
				if (res) {
					resolve(res)
				} else {
					reject(false)
				}
			})
		}

	}
});