<template>
	<view class="virtual">
		
		<view class="box" v-for="(item,index) in goodsData" :key="index">
			<view class="goods-item" v-for="i in item.goodsInfos" :key="i.goodsId" @click="handleItem(i,item.catalogName)">
				<image class="icon" :src="i.mainImg" mode=""></image>
				<view class="goods-info">
					<view class="goods-title">
						{{i.name}}
					</view>
					<view class="goods-desc">
						{{item.catalogName}}
					</view>
				</view>
				<view class="price">
					￥{{i.price}}
				</view>
			</view>
		</view>
		
		<u-popup :show="show" round="32rpx" mode="bottom" @close="close" @open="open">
			<view class="content">
				<view class="popup-title">会员充值</view>
				<view class="goods-box">
					<image class="icon" :src="chooseInfo.mainImg" mode=""></image>
					<view class="goods-info">
						<view class="goods-title">
							{{chooseInfo.name}}
						</view>
						<view class="goods-desc">
							{{chooseInfo.catalogName}}
						</view>
					</view>
					<view class="price">
						<u-loading-icon v-if="loadingPrice" text="获取价格中"></u-loading-icon>
						<template v-else>
							￥{{chooseInfo.price}}
						</template>
					</view>
				</view>
				
					<u--form labelPosition="top" :model="form" ref="uForm" borderBottom labelWidth="180rpx" :labelStyle="{'color': '#222222',
'font-weight': 'bold','line-height':'60rpx','font-size': '28rpx'}">
						<u-form-item label="充值账号" prop="account" borderBottom ref="item1">
							<u--input v-model.trim="form.account" placeholder="请输入充值账号" border="none"></u--input>
						</u-form-item>
					</u--form>
					<view class="goods-describe">
						
					<rich-text :nodes="chooseInfo.goodsDescribe"></rich-text>
					</view>
				<button class="btn" type="default" :loading="loading" @click="handleBuy">确认</button>
			</view>
		</u-popup>

	</view>
</template>

<script>
	import {getVirtuallist,getVirtualInfo} from '@/api/goods.js'
	import {SHOP_ID} from '@/utils/config.js'
	export default {
		data() {
			return {
				show: false,
				goodsData:[],
				chooseInfo:null,
				loading:false,
				loadingPrice:false,
				form:{
					account:''
				},
			}
		},
		methods: {
			handleItem(item,catalogName) {
				this.chooseInfo = JSON.parse(JSON.stringify(item) ) 
				this.chooseInfo.catalogName = catalogName
				this.show = true
				this.getVirtualInfo(item.mainId)
			},
			 open() {
			},
			close() {
				this.show = false
			},
			getVirtuallist(){
				getVirtuallist().then(res=>{
					if(res.state == 'Success'){
						this.goodsData = res.content
					}
				})
			},
			getVirtualInfo(commodityId){
				this.loadingPrice = true
				getVirtualInfo({commodityId}).then(res=>{
					this.loadingPrice = false
					if(res.state == 'Success'){
						this.chooseInfo.price = res.content.realPrice
					}
				})
			},
			// 创建订单
			handleBuy() {
				if (uni.getStorageSync('token')) {
					if(!this.form.account) return uni.showToast({
						title:'请输入充值账号',
						icon:'none'
					})
					let info = {
						goodsId:this.chooseInfo.goodsId,
						goodsName:this.chooseInfo.name,
						buyPrice:this.chooseInfo.price,
						goodsImg:this.chooseInfo.mainImg,
						shopId:SHOP_ID,
						extend:JSON.stringify({account:this.form.account})
					}
					uni.navigateTo({
						url:`/pay/pay`,
						  success: function(res) {
						    // 通过eventChannel向被打开页面传送数据
						    res.eventChannel.emit('pay', info)
						  }
					})
				} else {
					uni.showModal({
						title:'请登录',
						confirmText:'去登录',
						success(res){
							console.log(res);
							if(res.confirm){
								uni.navigateTo({
									url:'/login/login/login?redirect=/detail/virtualGoods/index'
								})
							}
						}
					})
				}
				
			
			}
		},
		created() {
			this.getVirtuallist()
		},
	}
</script>

<style lang="scss" >
	
	.virtual {
		padding: 0 24rpx 80rpx;
		background: #fff;

		.title-box {
			display: flex;
			align-items: center;
			padding: 24rpx 0;

			.icon {
				width: 40rpx;
				height: 40rpx;
				border-radius: 50%;
				background: red;
			}

			.title {
				font-size: 32rpx;
				color: #222222;
				font-weight: bold;
				margin-left: 12rpx;
			}
		}
		.u-grid{
			
			.item{
				width: 218rpx;
				height: 218rpx;
				background: rgba(249,249,249,0.39);
				border: 2rpx solid #F0F0F0;
				border-radius: 16rpx;
				padding: 26rpx;
				box-sizing: border-box;
				margin-bottom: 24rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				.item-title{
					font-size: 24rpx;	
					color: #000000;
					font-weight: bold;
					margin-top: 14rpx;
					text-align: center;
				}
				.price{
					font-size: 40rpx;
					font-weight: bold;
					color: #A97D3C;
					margin-top: 16rpx;
					text-align: center;
					justify-self: flex-end;
				}
			}
		}
		.goods-item{
			display: flex;
			border-bottom: 2rpx solid #F0F0F0;
			padding: 28rpx 0;
			margin-top: 28rpx;
			.icon{
				width: 160rpx;
				height: 160rpx;
				border-radius: 16rpx;
				background: #F0F0F0;
			}
			.goods-info{
				display: flex;
				flex-direction: column;
				justify-content: center;
				flex: 1;
				margin-left: 20rpx;
				.goods-title{
					font-weight: 600;
					font-size: 32rpx;
					color: #222222;
				}
				.goods-desc{
					font-weight: 300;
					font-size: 24rpx;
					color: #AAAAAA;
					margin-top: 20rpx;
				}
			}
			.price{
				font-weight: 600;
				font-size: 36rpx;
				color: #FF4D3A;
				align-self: center;
			}
		}
		.content{
			padding: 24rpx;
			.popup-title{
				font-size: 32rpx;
				font-weight: bold;
				color: #222222;
			}
			.goods-box{
				display: flex;
				margin-top: 28rpx;
				.icon{
					width: 160rpx;
					height: 160rpx;
					border-radius: 16rpx;
					background: #F0F0F0;
				}
				.goods-info{
					display: flex;
					flex-direction: column;
					justify-content: space-around;
					flex: 1;
					margin-left: 20rpx;
					.goods-title{
						color: #222222;
						font-weight: bold;
						font-size: 36rpx;
					}
					.goods-desc{
						font-size: 28rpx;
						color: #AAAAAA;
					}
				}
				.price{
					color: #FF4D3A;
					font-size: 36rpx;
					font-weight: bold;
					align-self: center;
				}
			}
			.goods-describe{
				color: #222222;
				font-size: 24rpx;
				max-height: 480rpx;
				padding-top: 28rpx;
				overflow: auto;
			}
			.btn{
				width: 688rpx;
				height: 80rpx;
				line-height: 80rpx;
				background: linear-gradient(283deg, #F8D48C 0%, #FBDDA6 100%);
				border-radius: 46rpx;
				margin-top: 54rpx;
				font-size: 28rpx;
			}
		}
	}
</style>