<template>
	<loading v-if="loading"></loading>
	<view v-else>
		<view class="top">
			<image class="left-icon" :src="icon" mode="scaleToFill"></image>
			<text class="tit">{{goodsInfoList[current].product_name.split('-')[1]}}</text>
		</view>
		<view class="user-data">
			<van-cell-group>
				<van-field v-for="(item,index) in goodsTemplate.ElementInfo.Inputs" :key='item.SortId'
					:value="charge_account" @change='fieldChange' required clearable :label="item.Name"
					icon="question-o" :placeholder="'请输入'+item.Name" @click-icon="onClickIcon('请输入'+item.Name)" />
				<van-cell :title="goodsTemplate.ElementInfo.ChargeNum.Name">
					<van-stepper slot="right-icon" :value="buy_num" integer @change="stepperChange" :min="1" />
				</van-cell>
			</van-cell-group>
		</view>

		<view class="prompt">
			<text class="top-tit">购买须知</text>
			<view class="dex-box">
				<text v-for="(item,index) in remark" :key='index'>{{index+1}}、{{item}}</text>
			</view>
		</view>

		<view class="commodity-selection">
			<text class="top-tit">充值类型</text>
			<view class="selection-box">
				<view class="commodity" :class="{selected:current==index}" @click="current=index"
					v-for="(item,index) in goodsInfoList" :key='item.product_id'>
					<text>{{item.product_name.split('-')[1]}}</text>
					<text>{{item.product_name.split('-')[2]}}</text>
					<text>¥{{item.purchase_price*fulu_vip_discount}}</text>
					<text>¥{{item.face_value}}</text>
					<m-icon v-if="current==index" class="xuanzhong-icon" type="xuanzhong" color="#47CB79"></m-icon>
				</view>
			</view>
		</view>

		<view class="buy-btn-box"></view>
		<view class="buy-btn" @click="buy">
			立即充值
		</view>
	</view>
</template>

<script>
	export default {
		data: () => ({
			current: 0,
			goodsInfoList: [],
			goodsTemplate: {},
			product_ids: [],
			icon: null,
			remark: [],
			loading: true,
			buy_num: 1,
			charge_account: null,
			fulu_vip_discount: 1,
		}),
		onLoad: async function(option) {
			let data = await this.goPageGetData()
			this.product_ids = data.product_ids
			this.icon = data.icon
			this.remark = data.remark
		},
		async mounted() {
			let keywordRes = await this.$api.fulu.getPriceKeyword()
			if(keywordRes.code == 200){
				this.fulu_vip_discount = keywordRes.data
			}
			this.loading = true
			let arr = []
			this.product_ids.forEach(id => {
				arr.push(this.$api.fulu.goods.infoGet({
					product_id: id
				}))
			})
			await Promise.all(arr).then(res => {
				res.forEach(resObj => {
					if (resObj.code == 200)
						resObj.data.code == 0 && this.goodsInfoList.push(JSON.parse(resObj.data
							.result))
				})
			})
			let tempRes = await this.$api.fulu.goods.templateGet({
				template_id: this.goodsInfoList[this.current].template_id
			})
			this.goodsTemplate = JSON.parse(tempRes.data.result)
			this.$nextTick(() => {
				this.loading = false
			})
		},
		methods: {
			async buy() {
				this.$utils.wxUtils.subscribeToNews().then(async res => {
					let orderRes = await this.$api.fulu.prepareOrder({
						buy_num: this.buy_num,
						charge_account: this.charge_account,
						goodsPictureUrl: this.icon,
						product_id: this.goodsInfoList[this.current].product_id
					})
					let payRes = await wx.requestPayment(orderRes.data)
					if (payRes.errMsg == 'requestPayment:ok') {
						this.$utils.goPage(
							`/pages/fulu/orderdes?outTradeNo=${orderRes.data.outTradeNo}`,
							'reLaunch')
					}
				})
			},
			stepperChange(e) {
				this.buy_num = e.detail
			},
			fieldChange(e) {
				this.charge_account = e.detail
			},
			onClickIcon(msg) {
				wx.showToast({
					title: msg,
					icon: 'none'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.top {
		margin-top: 20rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		background-color: #FFFFFF;
		padding: 23rpx;

		.left-icon {
			width: 87rpx;
			height: 79rpx;
			margin-right: 22rpx;
		}

		.tit {
			white-space: nowrap;
			font-size: 30rpx;
			font-weight: 400;
			color: #0F0404;
		}
	}

	.user-data {
		background-color: #FFFFFF;
		display: flex;
		flex-direction: column;
	}

	.commodity-selection {
		margin-top: 20rpx;
		background-color: #FFFFFF;
		display: flex;
		flex-direction: column;

		.top-tit {
			font-size: 26rpx;
			font-weight: 400;
			color: #666666;
			line-height: 39rpx;
			border-bottom: 1rpx solid #e8e8e8;
			padding: 28rpx;
		}

		.selection-box {
			margin-top: 30rpx;
			display: flex;
			flex-wrap: wrap;
			justify-content: flex-start;
			padding-left: 15rpx;

			.selected {
				border: 1rpx solid #ECBF7A !important;
				background-color: #FDF3D7;
			}

			.commodity {
				margin-bottom: 15rpx;
				margin-right: 15rpx;
				width: 230rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				border: 1rpx solid #E8E8E8;
				box-sizing: border-box;
				padding: 30rpx 20rpx;
				border-radius: 6rpx;
				position: relative;

				.xuanzhong-icon {
					position: absolute;
					right: 0;
					bottom: 0;
				}

				text {
					&:nth-child(1) {
						color: #999999;
						font-size: 26rpx;
					}

					&:nth-child(2) {
						color: #999999;
						font-size: 26rpx;
					}

					&:nth-child(3) {
						color: #E31818;
						font-size: 40rpx;
					}

					&:nth-child(4) {
						color: #999999;
						font-size: 26rpx;
						text-decoration: line-through;
					}
				}
			}
		}
	}

	.prompt {
		margin-top: 20rpx;
		background-color: #FFFFFF;
		display: flex;
		flex-direction: column;

		.top-tit {
			font-size: 26rpx;
			font-weight: 400;
			color: #666666;
			line-height: 39rpx;
			border-bottom: 1rpx solid #e8e8e8;
			padding: 28rpx;
		}

		.dex-box {
			padding: 28rpx;
			display: flex;
			flex-direction: column;
			font-size: 26rpx;
			font-weight: 400;
			color: #666666;
			line-height: 39rpx;
		}
	}

	.buy-btn-box {
		margin-top: 30rpx;
		width: 600rpx;
		height: 80rpx;
		opacity: 0;
	}

	.buy-btn {
		position: fixed;
		bottom: 30rpx;
		left: 50%;
		transform: translateX(-50%);
		width: 600rpx;
		height: 80rpx;
		background: linear-gradient(90deg, #E31818, #ED3E24, #ED4F24);
		border-radius: 40rpx;
		font-size: 30rpx;
		font-weight: 400;
		color: #FFFFFF;
		display: flex;
		justify-content: center;
		align-items: center;
	}
</style>
