const {
	type
} = require("os");

const core = getApp().globalData.requirejs("core")
module.exports = {
	orderSubmit: function(that) {
		if (that.submit) {
			return 0;
		}
		if (that.checkCreditReBindMobile(that.list)) {
			// #ifdef MP-WEIXIN
			let checkMobileRes = getApp().globalData.checkBindMobile(true, "为了您的账号安全,需要绑定手机号后,才能使用抵扣功能")
			if (!checkMobileRes) {
				return
			}
			// #endif
		}
		let userInfo = uni.getStorageSync("userInfo")
		that.list.realprice = parseFloat(that.list.realprice)
		//未输入
		if (!that.list.realprice || that.list.realprice == 0) {
			uni.showToast({
				title: "请输入正确的数值",
				icon: "none",
				duration: 1000,
			});
			return false
		}
		// 支付金额不能小于多少元
		if (parseFloat(that.list.realprice) < parseFloat(that.min_deduct_credit_price) || that.list.realprice ==
			0) {
			if (userInfo.openid != "ovJcw5ao__ysZvNn5tnzKpFzGjoM" && userInfo.openid !=
				"2088402217442862" && userInfo.openid != "2088442092209671") {
				uni.showModal({
					title: '提示',
					content: '最小支付金额为' + that.min_deduct_credit_price + '元!',
					showCancel: false
				})
				return
			}
		}
		//支付金额不能超过2000元
		if (parseFloat(that.list.realprice) > that.quota) {
			uni.showModal({
				title: '提示',
				content: '最大支付金额为' + that.quota + '元!',
				showCancel: false
			})
			return;
		}
		// 获取储值卡和金豆余额
		that.getUserCredit()
		let isalipay = 'no'
		//#ifdef MP-ALIPAY
		isalipay = 'yes'
		//#endif
		let h5Pla = getApp().globalData.util.getH5Platform()
		if (h5Pla == "H5-ALIPAY") {
			isalipay = 'yes'
		}
		// //金豆赠送功能
		// if(that.list.deductcredit_zs>0){
		// 	if(that.list.deductcredit>0){
		// 		that.list.deductcredit=that.list.deductcredit+that.list.deductcredit_zs;
		// 		that.list.deductprice=that.list.deductprice+that.list.deductprice_zs;
		// 	}
		// 	else{
		// 		that.list.deductcredit=that.list.deductcredit_zs;
		// 		that.list.deductprice=that.list.deductprice_zs;
		// 	}
		// }
		let submitData = {
			// // 订单金额
			// realprice: that.list.realprice,
			// remark: that.list.remark ? that.list.remark : '',
			// // 需要支付金额
			// needpay: that.list.needpay,

			// // 抵扣金豆数量
			// deductcredit: that.list.deductcredit,
			// // 抵扣金额
			// deductprice: that.list.deductprice,
			// // 抵扣赠送金豆数量
			// deductcredit_zs: that.list.deductcredit_zs,
			// // 抵扣赠送金豆金额
			// deductprice_zs: that.list.deductprice_zs,
			// // 支付环境
			// isalipay: isalipay,
			// // 是否使用金豆
			// is_use_credit_deduct: that.list.is_use_credit_deduct,
			// // 是否使用储值卡
			// is_use_recharge_deduct: that.list.is_use_recharge_deduct,
			// deduct_recharge: that.list.deduct_recharge,
			// // 随机立减
			// money_sub: that.list.money_sub,
			// merchid: that.merchid,
			// submit: true,
			// cashierid: 0,
			// remarkplatform:that.activity_platform?that.activity_platform:'',
			// s: 'App.Order_Offline.submit',
			shopId: shopId, //商户ID
			price: that.list.realprice, //商品原价
			amount: that.list.remark ? that.list.remark : '', //支付价格
			bean: that.list.needpay, //抵扣金豆
			openId: openId, //微信OpenId
			mobile: mobile //支付用户手机号
		}
		if (that.option && typeof that.option.cashierid != "undefined" && that.option.cashierid != "" && that
			.option.total > 0) {
			submitData.cashierid = that.option.cashierid
		}
		that.submit = true
		uni.showLoading({
			title: '支付中...',
			mask: true
		})
		getApp().globalData.request(submitData).then(orderSubRes => {
			uni.hideLoading()
			let subRes = orderSubRes.data.data
			if ("SUCCESS" == subRes.result_code) {
				// 判断是否全部金豆抵扣
				let isAllUserCreditPay = false
				if (subRes.data.price == subRes.data.deductprice) {
					isAllUserCreditPay = true
				}
				// 是否包含有效充值区间
				let rechargeMinIndex = -1
				/** 删掉赠送金额为空或0 的区间 */
				if (typeof(that.merch.rechargeArr) != 'undefined' && that.merch
					.rechargeArr.length) {
					for (let i = 0; i < that.merch.rechargeArr.length; i++) {
						if (that.merch.rechargeArr[i].give_money <= 0) {
							continue
						}
						let judge = (parseFloat(that.merch.rechargeArr[i].recharge_money) >
							parseFloat(that.list.realprice))
						if (judge) {
							rechargeMinIndex = i
							break
						}
					}
				}
				// 如果商家开启了储值系统
				if (that.merch['open_recharge'] && !isAllUserCreditPay &&
					!that.use_deduct_recharge && rechargeMinIndex > -1) {
					// 去储值页面
					uni.navigateTo({
						url: "/pages/order_sub/recharge_card/index?nothing=1",
						success: function(res) {
							// 通过eventChannel向被打开页面传送数据
							res.eventChannel.emit('acceptDataFromOfflinePay', {
								order: subRes.data
							})
						}
					})
				} else {
					uni.navigateTo({
						url: "/pages/order_sub/pay/index?nothing=1",
						success: function(res) {
							// 通过eventChannel向被打开页面传送数据
							res.eventChannel.emit('acceptDataFromOfflinePay', {
								order: subRes.data
							})
						}
					})
				}
			} else {
				core.alert(subRes.err_code_des)
				that.submit = false
			}
		})
	}
}