<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}}
					</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.price}}
				</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="btn" @click="goNew">
					立即充值
				</view>
			</view>
		</uni-popup>
	</view>

</template>

<script>
	import {
		getCzList,
		createCz
	} from '@/api/three'
	import { passUser } from '@/api/zero.js'
	export default {
		data() {
			return {
				pUser: {},
				phone: '',
				info: [],
				content: '',
				choose: {
					goods_id: 0
				}
			}
		},
		onLoad() {},
		onShow() {
			this.passUser()
		},
		onReachBottom() {

		},
		onReady() {

		},
		onNavigationBarButtonTap(event) {
			console.log('nav button pressed', event)
			uni.navigateTo({
				url: '/pages/index/lifeOrder'
			})
		},
		methods: {
			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('请选择需要充值的套餐')
				}
				this.$refs.popup.open()
			},
			goNew() {
				createCz({
					rechargeno: this.phone,
					pay_type: 'yue',
					goods_id: this.choose.goods_id,
					pay_price: (this.choose.num * 1 * this.pUser.recharge_discount*1*0.01).toFixed(2)
				}).then(res => {
					if(res.data.status == 'PAY_DEFICIENCY') {
						this.$api.msg(res.msg)
					}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 0;

				.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;
		}
	}
</style>