<template>
	<view class="content">
		<view class="search-box flex">
			<input type="text" v-model="phone" placeholder="输入手机号码" />
			<view class="search-btn" @click="goCz()">
				充值
			</view>
		</view>
		<view class="item-wrap" v-for="item in info">
			<view class="tit">
				{{item.cname}}
			</view>
			<view class="item-list flex">
				<view class="item " v-for="itemt in item.goods" :class="{'action': itemt.goods_id == choose.goods_id}"
					@click="chooseGood(itemt)">
					<view class="num">
						{{itemt.num}}元
					</view>
					<view class="base-price">
						<!-- 官方￥{{itemt.num}} -->
						赠送：{{(itemt.num*1*pUser.recharge_resumption*0.01).toFixed(0)}}复投积分
					</view>
					<!-- <view class="price">
						实付{{(itemt.num * 1 * pUser.recharge_discount*1*0.01).toFixed(2)}}
					</view> -->
				</view>
			</view>
		</view>
		<view class="item-wrap" v-if="content">
			<view class="tit">
				注意事项
			</view>
			<view class="tip" v-html="content">

			</view>
		</view>
		<uni-popup ref="popup" type="bottom">
			<view class="cz-model">
				<view class="price">
					{{choose.num}}
				</view>
				<view class="">
					{{choose.goods_name}}
				</view>
				<view class="">
					到账时间：1-96小时 （高峰期96小时内）
				</view>
				<view class="">
					充值号码：<text class="tip">{{this.phone}}</text>
				</view>
				<view class="tip tips">
					请仔细核对号码。输错号码后果自负
				</view>
				<view class="pay-type-list ">
					<!-- #ifdef APP-PLUS -->
					<view class="type-item b-b flex" @click="changePayType(2)">
						<text class="icon iconfont iconzhifubao"></text>
						<view class="con"><text class="tit">支付宝支付</text></view>
						<label class="radio"><radio value="" color="#5dbc7c" :checked="payType == 2"></radio></label>
					</view>
					<!-- #endif -->
					<!-- <view class="type-item" @click="changePayType(3)">
						<text class="icon iconfont iconyue"></text>
						<view class="con">
							<text class="tit">消费积分支付</text>
							<text>可用消费积分 ¥{{ userInfo.now_money }}</text>
						</view>
						<label class="radio"><radio value="" color="#5dbc7c" :checked="payType == 3"></radio></label>
					</view> -->
				</view>
				<view class="btn" @click="goNew">
					立即充值
				</view>
			</view>
		</uni-popup>
	</view>

</template>

<script>
	import { mapState } from 'vuex';
	import {
		getCzList,
		createCz
	} from '@/api/three'
	import { passUser } from '@/api/zero.js'
	export default {
		data() {
			return {
				pUser: {},
				phone: '',
				info: [],
				content: '',
				choose: {
					goods_id: 0
				},
				payType: 3,
				payName: 'ali'
			}
		},
		computed: {
			// #ifdef H5
			...mapState(['weichatObj']),
			// #endif
			...mapState('user',['userInfo'])
		},
		onLoad() {},
		onShow() {
			this.passUser()
		},
		onReachBottom() {

		},
		onReady() {

		},
		onNavigationBarButtonTap(event) {
			console.log('nav button pressed', event)
			uni.navigateTo({
				url: '/pages/index/lifeOrder'
			})
		},
		methods: {
			//选择支付方式
			changePayType(type) {
				this.payType = type;
				if (this.payType == 1) {
					this.payName = 'weixin';
				}
				if (this.payType == 2) {
					this.payName = 'ali';
				}
				if (this.payType == 3) {
					this.payName = 'yue';
				}
			},
			passUser() {
				passUser().then(res => {
					this.pUser = res.data
					this.getCzList()
				})
			},
			goCz() {
				if (!this.phone) {
					return this.$api.msg('请输入手机号')
				}
				var pattern = /^1[0-9]{10}$/;
				if (!pattern.test(this.phone)) {
					return this.$api.msg('请输入正确的手机号')
				}
				if (!this.choose.goods_id) {
					return this.$api.msg('请选择需要充值的套餐')
				}
				// 校验手机
				let phoneType = this.getMobileOperator(this.phone)
				if(this.choose.goods_name.indexOf(phoneType) == -1) {
					this.choose = {}
					return uni.showModal({
						title: '温馨提醒',
						content: `您当前的手机号为${phoneType}号码，与充值套餐的运营商不一致，请重新选择套餐！`,
						showCancel:false
					})
					
				}
				
				this.$refs.popup.open()
			},
			getMobileOperator(phoneNumber) {
			  // 移动号段
			  var cmccPrefixes = ['134', '135', '136', '137', '138', '139', '150', '151', '152', '157', '158', '159', '182', '183', '184', '187', '188', '178', '147', '172', '198'];
			  // 联通号段
			  var cuccPrefixes = ['130', '131', '132', '155', '156', '185', '186', '145', '176', '175', '166'];
			  // 电信号段
			  var ctcPrefixes = ['133', '153', '180', '181', '189', '177', '173', '199'];
			  // 虚拟运营商号段
			  var virtualPrefixes = ['170', '171'];
			  var prefix = phoneNumber.substring(0, 3);
			  
			  if (cmccPrefixes.includes(prefix)) {
			    return '移动';
			  } else if (cuccPrefixes.includes(prefix)) {
			    return '联通';
			  } else if (ctcPrefixes.includes(prefix)) {
			    return '电信';
			  } else if (virtualPrefixes.includes(prefix)) {
			    return '虚拟运营商';
			  } else {
			    return '未知运营商';
			  }
			},
			// // 示例用法
			// var phoneNumber = '13812345678';
			// var operator = getMobileOperator(phoneNumber);
			// console.log(operator); // 输出: 中国移动

			goNew() {
				let that = this
				if(this.payName == 'yue' && this.userInfo.now_money*1 < this.choose.num) {
					return this.$api.msg('当前消费积分不足，请选择其他方式支付！')
				}
				createCz({
					rechargeno: this.phone,
					pay_type: this.payName,
					goods_id: this.choose.goods_id,
					pay_price: this.choose.num,
					price: this.choose.num
				}).then(res => {
					if(res.data.status == 'PAY_DEFICIENCY' ||res.data.status ==  'PAY_ERROR') {
						this.$api.msg(res.msg)
					}else {
						if(this.payName == 'ali') {
							let da = res.data.result.jsConfig;
							uni.requestPayment({
							    provider: 'alipay',
							    orderInfo: da, //微信、支付宝订单数据 【注意微信的订单信息，键值应该全部是小写，不能采用驼峰命名】
							    success: function (res) {
							        console.log('success:' + JSON.stringify(res));
									uni.showToast({
										icon: 'success',
										title: '充值成功'
									})
									// obj.paySuccessTo();
							    },
							    fail: function (err) {
							        console.log('fail:' + JSON.stringify(err));
									// uni.navigateTo({
									// 	url: '/pages/order/order?state=0'
									// });
							    }
							});
						}else {
							uni.showToast({
								icon: 'success',
								title: '充值成功'
							})
						}
						
					}
					this.$refs.popup.close()
					
				})
			},
			chooseGood(item) {
				this.choose = item
			},
			getCzList() {
				let that = this
				getCzList().then(res => {
					this.info = res.data.data
					this.info.forEach(item => {
						if (item.goods.length > 0) {
							item.goods.forEach(itemt => {
								if (itemt.desc && itemt.desc.split('<p>').length > 1) {
									that.content = itemt.desc
								}
							})
						}
					})
				})
			},

		}
	}
</script>

<style lang="scss" scoped>
	.content {
		padding-top: 20rpx;
		padding-bottom: 20rpx;
	}

	.phone {
		width: 690rpx;
		height: 80rpx;
		background-color: #fff;
		border-radius: 220rpx;
		margin: 0 auto 20rpx;
	}

	.item-wrap {

		width: 690rpx;
		background-color: #fff;
		margin: 0 auto 20rpx;
		padding: 20rpx 34rpx;
		border-radius: 20rpx;

		.tip {
			width: 620rpx;
			margin: auto;
			background-color: #e9f1fe;
			padding: 20rpx;
			border-radius: 20rpx;
			margin-top: 40rpx;

		}

		.tit {
			font-size: 30rpx;
			font-weight: bold;
		}

		.item-list {
			justify-content: flex-start;
			padding-top: 40rpx;

			.item {
				// width: 192rpx;
				border-radius: 20rpx;
				margin-right: 20rpx;
				background-color: #e9f1fe;
				text-align: center;
				padding: 40rpx;

				.num {
					color: #409EFF;
					font-weight: bold;
					font-size: 28rpx;
				}

				.base-price {
					padding-top: 10rpx;
					font-size: 22rpx;
				}

				.price {
					font-size: 28rpx;
					color: #f3253a;
					padding-top: 10rpx;
				}
			}

			.action {
				border: 1px solid #f3253a;
			}
		}
	}



	.top-search {
		height: 80rpx;
		padding: 0 20rpx;
		width: 100%;
		height: 116rpx;
		z-index: 99;

		.search {
			width: 24rpx;
			margin-right: 20rpx;
		}

	}

	.search-box {
		justify-content: flex-start;
		width: 690rpx;
		height: 72rpx;
		background: rgb(255, 255, 255);
		border-radius: 30rpx;
		padding: 0 10rpx 0 20rpx;
		margin: 0 auto 20rpx;

		input {
			flex-grow: 1;
		}

		.search-btn {
			flex-shrink: 0;
			width: 110rpx;
			height: 52rpx;
			text-align: center;
			line-height: 50rpx;
			color: #fff;
			background-color: $base-color;
			border-radius: 26rpx;

		}
	}

	.cz-model {
		width: 750rpx;
		background-color: #fff;
		padding: 60rpx 0 30rpx;
		text-align: center;
		border-radius: 25rpx 25rpx 0 0;

		view {
			padding: 10rpx 0;
		}

		.price {
			color: $base-color;
			font-size: 48rpx;
			font-weight: bold;
		}

		.tip {
			color: #409eff;
		}

		.tips {
			padding-bottom: 20rpx;
		}

		.btn {
			width: 690rpx;
			text-align: center;
			line-height: 60rpx;
			color: #fff;
			background-color: $base-color;
			border-radius: 40rpx;
			margin: auto;
		}
	}
	.pay-type-list {
		margin-top: 20upx;
		background-color: #fff;
		padding-left: 60upx;
		.type-item {
			height: 120upx;
			padding: 20upx 0;
			display: flex;
			justify-content: flex-start;
			align-items: center;
			padding-right: 60upx;
			font-size: 30upx;
			position: relative;
		}
	
		.icon {
			width: 100upx;
			font-size: 52upx;
		}
		.iconyue {
			color: #fe8e2e;
		}
		.iconweixin {
			color: #36cb59;
		}
		.iconzhifubao {
			color: #01aaef;
		}
		.tit {
			font-size: $font-lg;
			color: $font-color-dark;
			margin-bottom: 4upx;
		}
		.con {
			flex: 1;
			display: flex;
			flex-direction: column;
			font-size: $font-sm;
			color: $font-color-light;
		}
	}
</style>