<template>
	<view class="content">
		<view class="title">
			<text class="iconfont icon-qian"></text>
			<text>江西南昌交通学院</text>
			<text>（墨轩湖校区）</text>
		</view>
		<view class="input_amount">
			<view class="amount_tip">充值金额（元）</view>
			<view class="amount_inp">
				<text></text>
				<input class="inp" type="number" maxlength="4" v-model="amount" @input="onInput" @blur="onBlur"
					placeholder="请输入大于10元，小于100元" placeholder-class="ph_class" />
			</view>
			<text>最多可输入金额100元</text>
		</view>
		<view class="amount_select">
			<view class="amount_btn">
				<text @tap="sel_amount(10)" :class="{selStyle:amount == 10}">充10元</text>
				<text @tap="sel_amount(20)" :class="{selStyle:amount == 20}">充20元</text>
				<text @tap="sel_amount(50)" :class="{selStyle:amount == 50}">充50元</text>
				<text @tap="sel_amount(100)" :class="{selStyle:amount == 100}">充100元</text>
			</view>
			<view class="reminder">温馨提示：最少充值金额为10元</view>
			<view class="payment">支付金额：<text>{{amount}}</text>元</view>
			<button class="btn_submit" type="primary" @tap="tip(true)">确认提交</button>
			<button class="btn_tel" type="default" @tap="callPhone">客服热线：{{phone_number}}</button>
			<view class="tips">
				尊敬的用户，你好！因项目提现业务配置问题导致您无法提现对此，我们深表歉意！您可致电运营商处理提现问题，运营商电话：13645689854。感谢您对我们工作的支持和理解。
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				amount: '', // 金额
				phone_number: '13645689854', // 客户热线电话
				IP: '',
				code: '',
				ceshi: 'code',
				test: this.$store.state.test,
				isXiaoqu: false
			};
		},
		onLoad() {
			// 系统正在维护...
			if (this.test == 'weihuzhong') {
				uni.redirectTo({
					url: '../index/index'
				})
				
				return;
			}
			// 测试环境
			if (this.test) {
				this.amount = 0.01
			}
		},
		onReady() {
			this.has_user_info();
		},
		methods: {
			/**
			 * 获得code
			 */
			getCode(param_ip) {
				uni.login({
					success: (res) => {
						// console.log('recharge', res);
						if (res.code) {
							// 组合地址，发起支付
							this.jsapi(param_ip, res.code)
						} else {
							uni.showToast({
								title: res.errMsg,
								icon: 'none'
							});
						}
					}
				})
			},
			/**
			 * 获取IP
			 */
			async getIP() {
				const res = await this.$myRequest({
					host: 'ip',
					url: '?ie=utf-8',
					method: 'POST'
				})

				if (res) {
					// console.log(res);
					const reg = /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/;
					let ip = reg.exec(res.data);

					// 获取code
					this.getCode(ip[0])
				}
			},
			/**
			 * 请求服务器，获取支付参数，并支付
			 */
			async jsapi(param_ip, param_code) {
				if (param_code == '' || this.amount == '' || param_ip == '') {
					uni.showToast({
						icon: 'none',
						title: 'code、充值金额或IP为空'
					});
					return
				}

				const res = await this.$myRequest({
					host: this.ceshi,
					url: '/HotWaters/wpPay.action',
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						code: param_code,
						num: this.amount,
						ip: param_ip
					}
				})

				// console.log(res);
				if (res.data.pay == 'error') {
					uni.showToast({
						title: '未获得支付参数',
						icon: 'success',
						duration: 3000
					});
				} else {
					wx.requestPayment({
						timeStamp: res.data.pay.timeStamp,
						nonceStr: res.data.pay.nonceStr,
						package: 'prepay_id=' + res.data.pay.prepay_id,
						signType: res.data.pay.signType,
						paySign: res.data.pay.paySign,
						success: (res) => {
							// console.log(res);
							if (res.errMsg == 'requestPayment:ok') {
								this.$store.state.payInfo.from = 'reshui_pay'
								this.$store.state.payInfo.resultMsg = '支付成功'
								this.$store.state.reshui_amount = this.amount
								
								uni.navigateBack({
									delta: 1
								});
							}
						},
						fail: (res) => {
							if (res.errMsg == 'requestPayment:fail cancel') {
								uni.showToast({
									title: '支付已取消',
									icon: 'success',
									duration: 2000
								});
							}
						},
						complete: (res) => {
							// console.log(res);
						}
					});
				}
			},
			/**
			 * 输入充值金额
			 */
			onInput(e) {
				if (this.test) { // 测试环境
					return
				}

				const v = e.detail.value

				const zero = /^(0{1,})|[^0-9]/g
				let final = 0
				if (!v) {
					final = 0
				} else {
					final = v.toString().replace(zero, (v) => {
						return 0
					})

					if (final.split('')[0] * 1 === 0) {
						final = final.slice(1) - 0 || 0
					}

					if (final > 100) {
						final = 100
					}
				}
				this.$nextTick(() => {
					if (final.toString() == '0') {
						this.amount = ''
					} else {
						this.amount = final.toString() || '0'
					}
				})
			},
			/**
			 * 输入框，失去焦点时
			 */
			onBlur() {
				if (this.test) { // 测试环境
					return
				}
				if (this.amount < 10) {
					this.amount = 10
				}
			},
			/**
			 * 选择充值金额
			 */
			sel_amount(m) {
				this.amount = m
			},
			/**
			 * 是否有用户信息
			 */
			has_user_info() {
				try {
					const value = uni.getStorageSync('userinfo_storage_key');
					// console.log(value.campus);
					if (value.campus != '墨轩湖校区') {
						this.isXiaoqu = false;
					} else {
						this.isXiaoqu = true;
					}
				} catch (e) {
					// console.log(e)
					uni.showToast({
						title: '异常：' + e,
						duration: 3000
					})
				}

				this.tip(false);
			},
			/**
			 * 提示
			 */
			tip(param) {
				if (!this.isXiaoqu) {
					if (param == false) {
						uni.showModal({
							title: '提示',
							content: '目前只对【墨轩湖校区】进行充值！请知晓！',
							showCancel: false,
							confirmText: '我已知晓',
							confirmColor: '#F00',
							success: function(res) {
								if (res.confirm) {
									// console.log('用户点击确定');
								}
							}
						});
					} else {
						let _this = this;
						uni.showModal({
							title: '提示',
							content: '目前只对【墨轩湖校区】的【热水钱包】进行充值！',
							confirmText: '对，没错',
							confirmColor: '#F00',
							cancelText: '哦，错了',
							success: function(res) {
								if (res.confirm) {
									// console.log('用户点击确定');
									_this.chongzhi()
								} else if (res.cancel) {
									// console.log('用户点击取消');
								}
							}
						});
					}
				} else {
					if (param == true) {
						this.chongzhi()
					}
				}
			},
			/**
			 * 调用接口实现充值功能
			 */
			chongzhi() {
				if (isNaN(this.amount)) {
					uni.showToast({
						title: '请输入正确金额',
						duration: 2000
					})
					return
				}
				// 测试环境下执行
				if (this.test) { // 测试环境
					uni.showModal({
						title: '提示',
						content: '您选择了充值：￥' + this.amount + ' 元',
						cancelText: '算了',
						confirmText: '充值',
						success: (res) => {
							if (res.confirm) {
								// 获取IP
								this.getIP()
							} else if (res.cancel) {
								return
							}
						}
					})
					return
				}
				// 生产环境
				if (this.amount < 10 || this.amount > 100) {
					uni.showToast({
						icon: 'none',
						title: '最少充值10元，最多充值100元！',
						mask: true,
						duration: 3000
					})
					return
				}
				// 确认
				uni.showModal({
					title: '提示',
					content: '您选择了充值：￥' + this.amount + ' 元',
					cancelText: '算了',
					confirmText: '充值',
					success: (res) => {
						if (res.confirm) {
							// 获取IP
							this.getIP()
						} else if (res.cancel) {
							return
						}
					}
				})
			},
			/**
			 * 拨打电话
			 */
			callPhone() {
				uni.makePhoneCall({
					phoneNumber: this.phone_number
				});
			}
		}
	}
</script>

<style scoped lang="scss">
	.content {
		display: flex;
		flex-direction: column;
		background-color: #F5F5F5;
		width: 750rpx;

		.title {
			display: flex;
			align-items: center;
			padding: 40rpx;
			font-size: 36rpx;
			font-family: Microsoft YaHei-3970(82674968);
			color: #333333;
			font-weight: bold;

			text:nth-child(1) {
				color: $my-color-primary;
				font-size: 50rpx;
			}

			text:nth-child(2) {
				margin-left: 15rpx;
			}

			text:nth-child(3) {
				color: #F00;
			}
		}

		.input_amount {
			background-color: #FFFFFF;
			width: 670rpx;
			height: 210rpx;
			padding: 40rpx 40rpx 0rpx 40rpx;
			border-radius: 40rpx 40rpx 0rpx 0rpx;

			.amount_tip {
				font-size: 32rpx;
				font-family: Microsoft YaHei-3970(82674968);
			}

			.amount_inp {
				display: flex;
				flex-direction: row;
				border-bottom: 1px solid #cccccc;
				margin-top: 20rpx;
				margin-bottom: 10rpx;

				text {
					width: 50rpx;
					height: 74rpx;
					font-size: 50rpx;
					font-weight: bold;
					padding-top: 6rpx;
					font-family: Microsoft YaHei-3970(82674968);
				}

				text::before {
					content: '￥';
				}

				.inp {
					width: 620rpx;
					height: 80rpx;
					font-size: 50rpx;
					font-weight: bold;
					font-family: Microsoft YaHei-3970(82674968);
				}

				/deep/.ph_class {
					font-size: 32rpx;
					font-weight: normal;
					color: #B2B2B2;
				}
			}

			text {
				font-size: 26rpx;
				font-family: Microsoft YaHei-3970(82674968);
				font-weight: 500;
			}
		}

		.amount_select {
			padding: 0rpx 40rpx 40rpx 40rpx;

			.amount_btn {
				display: flex;
				justify-content: space-between;
				margin: 30rpx 0 20rpx;

				text {
					width: 150rpx;
					height: 120rpx;
					line-height: 120rpx;
					font-size: 32rpx;
					font-family: Microsoft YaHei-3970(82674968);
					color: #333333;
					text-align: center;
					background: #FFFFFF;
					border: 3rpx solid #AAAAAA;
				}

				.selStyle {
					border: 0rpx;
					width: 156rpx;
					height: 126rpx;
					background: url(../../static/images/jinebeijing2x.png) 0rpx 0rpx no-repeat;
					background-size: 100%;
				}
			}

			.reminder {
				height: 26rpx;
				font-size: 26rpx;
				font-family: Microsoft YaHei-3970(82674968);
				color: $my-color-primary;
			}


			.payment {
				margin-top: 65rpx;
				font-size: 32rpx;
				font-family: Microsoft YaHei-3970(82674968);
				color: #333333;

				text {
					color: $my-color-primary;
				}
			}

			button {
				margin-top: 38rpx;
			}

			.btn_submit {
				border-radius: 10rpx;
				background-color: $my-color-primary;
				font-family: Microsoft YaHei-3970(82674968);
				color: #FFFFFF;
			}

			.btn_tel {
				border-radius: 10rpx;
				background: #CCCCCC;
				font-family: Microsoft YaHei-3970(82674968);
				color: #FFFFFF;
			}

			.tips {
				margin-top: 20rpx;
				font-size: 24rpx;
				font-family: Microsoft YaHei-3970(82674968);
				color: $my-color-primary;
				line-height: 36rpx;
			}
		}
	}
</style>
