var core = getApp().globalData.requirejs("core"),
	that
import cwxKeyboard from '@/components/cwx-keyboard/cwx-keyboard'
import rechargeDeductFn from './utils/rechargeDeductFn'
import creditDeductFn from './utils/creditDeductFn'
import orderSubmitFn from './utils/orderSubmitFn'
import moneyChangeFn from './utils/moneyChangeFn'
import getMerchFn from './utils/getMerchFn'
import intFn from './utils/intFn'

export default {
	data() {
		return {
			list: {
				realprice: 0.00,
				credit: 0.00,
				money_sub: 0.00,
				deductcredit: 0.00,
				deductprice: 0.00,
				needpay: 0.00,
				deduct_recharge: 0.00,
				deductprice_zs:0.00,
				deductcredit_zs:0.00
				
			},
			merch: {},
			mockInputFocusText: ' ',
			showKeyBoard: true,
			showRemarkInput: false,
			merchid: 0,
			use_deductprice: false,
			hideRechargeDeduct: false,
			hideCreditDeduct: false,
			// 是否已经获取公众号openid
			getOpenIDAlready: '',
			credit1: "",
			// 储值卡
			use_deduct_recharge: false,
			recharge: 0,
			submit: "", //提交状态
			min_deduct_credit_price: 0.02,
			on_paying: false,
			option: {},
			//活动赠送金豆
			credit1_hot_set:0,
			credit1_hot_rang:0,
			use_deductprice_hot: false,
			//活动金额范围 大于20才行
			credit1_hot_set_jine:0,
			//授权登录字段
			openid:'',
			ismobile:false,
			//限额
			quota:2000,
			//可抵扣最大金额
			use_max_credit_money:0
		}
	},
	components: {
		cwxKeyboard
	},
	props: {},
	filters: {
	    limitDecimal(val) {
	      return parseFloat(val).toFixed(2);
	    },
	},
	onLoad: function (t) {
		that = this
		this.option = t
		// uni.showLoading({
		// 	title: '加载中'
		// });
		mockInputFocus()
		intFn.init(that)
		//加载用户openid
		getApp().globalData.util.loginByCode(null, false).then(userInfo => {
			
			if(userInfo.mobile==null||userInfo.mobile==''){
				that.setData({
					ismobile:true
				})
			}
			that.setData({
				openid:userInfo.openid
			})
		})
	},
	onShow: function () {

	},
	onHide: function () {
		that.submit = false;
		that.list = {};
		that.use_deductprice = false;
		uni.removeStorageSync('qr_scan_data');
		this.$refs.clearMoney._handleClearKey();
	},
	methods: {
		/**
		 * 检测是否需要跳转其他页面
		 * **/
		isNeedGo2OtherPage: function (queryParam) {
			let page = false
			if (!queryParam) {
				queryParam = that.option
			} else {
				return
			}
			let pageParam = '?id=' + queryParam.merchid
			if (queryParam.needGo2MerCredit) {
				page = 'merch/index_credit'
			} else if (queryParam.needGo2MerGoods || queryParam.table_id) {
				page = 'merch/index'
			} else if (queryParam.needGo2MerDetails) {
				page = 'merch/intro'
			} else if (typeof queryParam.cashierid != "undefined" && queryParam.cashierid != "" && queryParam
				.total >= 0.01) {
				that.onMoneyInput(queryParam.total)
				return
			}
			//如果是扫码点餐
			if (queryParam.table_id) {
				pageParam += '&table_id=' + queryParam.table_id
			}
			if (page) {
				uni.navigateTo({
					url: '/pages/' + page + pageParam
				})
			}
		},

		// 创建线下支付订单
		getList: function (merchid) {
			if (!merchid) {
				uni.hideLoading()
				uni.showModal({
					title: '提示',
					content: '网络开小差啦,重新扫码试试吧!1623124886'
				})
				return
			}
			getApp().globalData.request({
				s: 'Merch_Index.GetMerchData4OfflinePay',
				id: merchid
			}).then(function (merchData) {
				that.hasMerchData2Next(merchData)
			});
		},
		// 获取到商家数据后 进行下一步操作
		hasMerchData2Next: function (merchData) {
			uni.hideLoading()
			let merch = (merchData.data.data)
			//默认金豆最大抵扣 
			that.use_max_credit_money=parseFloat(that.credit1*merch.credit_pay_rate).toFixed(2);
			//金豆兑换比例

			if (!merch) {
				uni.showModal({
					title: '提示',
					content: '网络开小差啦,重新扫码试试吧!1623930867'
				})
				return;
			}
			if (0 == merch.err_code) {
				// 获得当前商家设置信息
				uni.setStorage({
					key: 'cur_offline_merch_data',
					data: merch
				});
				that.quota=merch.quota;
				uni.setNavigationBarTitle({
					title: '向' + merch.merchname + '付款'
				})
				that.min_deduct_credit_price = parseFloat(merch.min_offline_price) + 0.01
				that.min_deduct_credit_price = getApp().globalData.util.numberFloor(that.min_deduct_credit_price)
				// 生成随机数
				const str = merch.credit_give_hot_rang;
				const [min, max] = str.split(',');
				const randomNum = Math.floor(Math.random() * (max - min + 1)) + parseInt(min);
				
				let pt_title=["余乐兑平台"];
				if(merch.activity_platform){
					pt_title = JSON.parse(merch.activity_platform);
				}
				
				const index = Math.floor(Math.random() * pt_title.length);
				
				that.setData({
					merch: merch,
					merchid: merch.id,
					credit1_hot_set: merch.credit_give_hot_set,
					credit1_hot_rang: randomNum,
					activity_platform:pt_title[index]
				})

				if (merch.pay_need_mobile) {
					getApp().globalData.checkBindMobile()
				}
				// 获取储值卡余额
				that.getUserRecharge()
				// 获取二步动作, 跳转等
				that.isNeedGo2OtherPage()
			} else if (merch && typeof merch.err_code_desc != undefined && merch.err_code_desc) {
				let tipsText = ''
				if (merch && merch.err_code_desc) {
					tipsText += merch.err_code_desc
				}
				uni.hideLoading();
				uni.showModal({
					title: '提示',
					content: tipsText,
					showCancel: false
				})
			} else {
				let tipsText = '网络开小差啦,重新扫码试试吧!1623124887';
				if (merch && merch.err_code_desc) {
					tipsText += merch.err_code_desc;
				}
				uni.hideLoading()
				uni.showModal({
					title: '提示',
					content: tipsText
				})
			}
		},

		// 获取储值卡和金豆余额
		getUserCredit: function () {
			getApp().globalData.util.loginByCode(null, false).then(userInfo => {
				that.setData({
					credit1: userInfo.credit1
				})
			})
		},

		// 获取用户储值卡余额
		getUserRecharge: function () {
			getApp().globalData.request({
				s: 'Member_Recharge.getUserRechargeBan',
				userid: uni.getStorageSync('userInfo').id,
				merchid: that.merchid
			}).then(userRecharge => {
				that.recharge = userRecharge.data.data.balance
			})
		},
		//点击付款
		submitFun: function () {
			if(!this.use_deductprice_hot){
				that.setData({
					activity_platform:''
				})
			}
			console.log("---------------------")
			orderSubmitFn.orderSubmit(that)
		},
		/** 检查是否使用了金豆/储值卡抵扣,如果使用了,必须绑定手机号 */
		checkCreditReBindMobile(list) {
			if (list.deductcredit > 0 || list.deduct_recharge > 0) {
				return true
			}
			return false
		},
		// 是否使用金豆抵扣
		useCreditCheckBox: function (e) {
			that.getUserCredit()
			let noUseDeduct = false; // 不使用金豆抵扣
			if (e.detail.value.length) {
				noUseDeduct = true
			}
			this.use_deductprice = noUseDeduct;
			creditDeductFn.modifyCreditDeduct(that)
		},
		// 是否使用赠送金豆活动抵扣
		useCreditCheckBox_hot: function (e) {
			let noUseDeduct = false; // 不使用金豆抵扣
			if (e.detail.value[0]=='1') {
				noUseDeduct = true
			}
			this.use_deductprice_hot = noUseDeduct;
			creditDeductFn.modifyCreditDeduct(that)
		},

		// 是否使用储值卡
		useRechargeCheckBox: function (e) {
			that.getUserRecharge();
			let noUseDeduct = false; // 不使用储值卡抵扣
			if (e.detail.value.length) {
				noUseDeduct = true
			}
			this.use_deduct_recharge = noUseDeduct
			rechargeDeductFn.modifyRechargeDeduct(that, true)
		},

		onMoneyInput(e) {
			//用于判断活动大于20元  回头改下
			this.credit1_hot_set_jine=e
			moneyChangeFn.changeEv(e, that)
		},
		toggleKeyboard() {
			
			this.showKeyBoard = !this.showKeyBoard
		},

		showRemarkClick() {
			this.showRemarkInput = true
		},

		// 显示推广人备注信息
		showSpreadRemark() {
			getApp().globalData.request({
				s: 'Merch_Index.GetMerchData4OfflinePay',
				id: that.merch.id
			}).then(res => {
				uni.showModal({
					title: '提示',
					showCancel: false,
					content: '推广人备注:' + res.data.data.remark_spread
				})
			})
		},

		// 显示最小现金支付金额提示
		showMiniCreditdeductTips() {
			getApp().globalData.request({
				s: 'Merch_Index.CreditCannotFullDeductTips',
				id: that.merch.id
			}).then(res => {
				uni.showModal({
					title: "提示",
					showCancel: false,
					content: res.data.data
				})
			})
		},
		// 使用数字人民币支付
		useEcnyPay() {
			uni.showModal({
				title: "提示",
				content: that.merch.e_cny_qrurl_psbc_tips,
				showCancel: false
			})
			getApp().globalData.request({
				s: 'Merch_Ecny.ShowDeviceEcnyQrCode',
				merchid: that.merch.id,
				e_cny_qrurl_psbc: that.merch.e_cny_qrurl_psbc
			})
		},
		// 跳转到商家店铺页面
		go2MerchIndex(merchid) {
			// #ifdef H5
			let h5Pla = getApp().globalData.util.getH5Platform()
			if (h5Pla == "H5-ALIPAY") {
				window.location.href = "alipays://platformapi/startapp?appId=2021001187681202&page=/pages/merch/index/index?id=" + merchid
			}
			// #endif
			// #ifdef MP-ALIPAY||MP-WEIXIN
			uni.navigateTo({
				url: '/pages/merch/index/index?id=' + merchid
			})
			// #endif
		},
		//微信获取手机号
		getPhoneNumber(e){
			getApp().globalData.request({
				s: 'Member_User.getPhone',
				code: e.detail.code,
				openid: that.openid,
				platform: 'MP-WEIXIN'
			}).then(function (res) {
				if(res.data.data.result_code=="SUCCESS"){
					that.ismobile=false;  //手机按钮隐藏
					that.use_deductprice=true; //复选框勾选
					creditDeductFn.modifyCreditDeduct(that)
				}
				else{
					uni.showToast({
						title:'取消金豆会员抵扣',
						icon:'none'
					})
				}
				return false;

			});
			
			return false;
		},
		//微信获取手机号-平台赠送活动选项
		getPhoneNumber_hot(e){
			getApp().globalData.request({
				s: 'Member_User.getPhone',
				code: e.detail.code,
				openid: that.openid,
				platform: 'MP-WEIXIN'
			}).then(function (res) {
				if(res.data.data.result_code=="SUCCESS"){
					that.ismobile=false;  //手机按钮隐藏
					that.use_deductprice_hot=true; //复选框勾选
					creditDeductFn.modifyCreditDeduct(that)
				}
				else{
					uni.showToast({
						title:'取消活动赠送金豆',
						icon:'none'
					})
				}
				return false;

			});
			
			return false;
		},
	} // / methods
};

// 模拟光标
function mockInputFocus() {
	
	setInterval(function () {
		if (that.mockInputFocusText == '0.1') {
			that.mockInputFocusText = '0.5'
		} else {
			that.mockInputFocusText = '0.1'
		}
	}, 1000)
}
