<template>
	<view>
		<view class="film-des">
			<image class="left-img" :src="filmDes.pic" mode="widthFix"></image>
			<view class="right-box">
				<view class="film-name">
					{{showItem.filmName}}
				</view>
				<view class="film-time">
					{{showItem.showTime}}<br />
					{{showItem.cinemaName}}<br />
					{{showItem.hallName}}
				</view>
				<view class="film-caveat">
					<view class="item">
						<van-icon name="clear" color='#E31818' />
						不支持退票
					</view>
					<view class="item">
						<van-icon name="clear" color='#E31818' />
						不支持改签
					</view>
				</view>
			</view>
		</view>
		<view class="select-des">
			<view class="tit-text">
				选择购票方式
			</view>
			<van-radio-group :value="radioType" @change="radioOnChange">
				<view class="options-box">
					<view class="left-box">
						<image class="img" src="https://t1-1305573081.cos.ap-shanghai.myqcloud.com/wxapp/static/imgs/%E7%89%B9%E6%83%A0%E8%B4%AD%E7%A5%A8.png"
						 mode="widthFix"></image>
						<view class="text-box">
							<text class="top">特惠购票</text>
							<text class="bottom">固定折扣，快速出票</text>
						</view>
					</view>
					<view class="right-box">
						<view class="text-box">
							<text class="top">¥{{aPrice}}/张</text>
							<text class="bottom">优惠 ¥{{aDiscount}}元</text>
						</view>
						<van-radio :name="1" checked-color="#07c160"></van-radio>
					</view>
				</view>
				<view class="options-box">
					<view class="left-box">
						<image class="img" src="https://t1-1305573081.cos.ap-shanghai.myqcloud.com/wxapp/static/imgs/%E5%BF%AB%E9%80%9F%E8%B4%AD%E7%A5%A8.png"
						 mode="widthFix"></image>
						<view class="text-box">
							<text class="top">快速购票</text>
							<text class="bottom">无需等待，即买即兑</text>
						</view>
					</view>
					<view class="right-box">
						<view class="text-box">
							<text class="top">¥{{(showItem.netPrice/100).toFixed(2)}}/张</text>
						</view>
						<van-radio :name="2" checked-color="#07c160"></van-radio>
					</view>
				</view>
				<view class="options-box">
					<view class="left-box">
						<image class="img" src="https://t1-1305573081.cos.ap-shanghai.myqcloud.com/wxapp/static/imgs/%E7%AB%9E%E4%BB%B7.png"
						 mode="widthFix"></image>
						<view class="text-box">
							<text class="top">竞价购买</text>
							<text class="bottom">为您网罗最低的折扣，约耗时几分钟</text>
						</view>
					</view>
					<view class="right-box">
						<view class="text-box">
							<text class="top">等待竞价</text>
						</view>
						<van-radio :name="3" checked-color="#07c160" disabled></van-radio>
					</view>
				</view>
			</van-radio-group>
		</view>
		<view class="seat-select">
			<text class="tit-text">无座时接受系统调座</text>
			<van-switch :checked="acceptChangeSeat==1" @change="onChange" size="24px" />
		</view>
		<!-- <button class="server" open-type="getPhoneNumber" @getphonenumber='getPhoneNumber' :plain="true">
			<van-cell title="手机号(非必填)" :value="userPhone">
				<m-icon slot="icon" type="lxkf" color="#d81e06"/>
			</van-cell>
		</button> -->
		<view class="seat-des">
			<view class="tit-text">
				购票须知
			</view>
			<view class="seat-box">
				<view class="left">
					座位:
				</view>
				<view class="right">
					<view class="item" v-for="(item,index) in seatResult" :key='index'>
						<text class="top">{{item.seatNo}}</text>
						<text class="bottom">￥{{aPrice}}</text>
					</view>
				</view>
			</view>
		</view>
		<view class="buy-des">
			<view class="left">
				<text class="one">总价:</text>
				<text class="two">¥{{allPrice}}</text>
				<text class="three">优惠¥{{allDiscount}}元</text>
			</view>
			<view class="right">
				<text class="text">{{seatResult.length}}张</text>
				<button class="btn" type="default" @click="buy">去支付</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data: () => ({
			showItem: {},
			seatResult: [],
			radioType: 1,
			acceptChangeSeat: 0,
			userPhone:null,
			orderRes: {}
		}),
		onLoad: async function(option) {
			let data = await this.goPageGetData()
			this.showItem = data.showItem
			this.seatResult = data.seatResult.map((val) => {
				return JSON.parse(val)
			})
		},
		computed: {
			filmDes() {
				let des = {}
				this.$store.state.cinema.filmList.forEach((val) => {
					if (val.filmId == this.showItem.filmId) {
						des = val
					}
				})
				return des;
			},
			filmDiscount() {
				return this.$store.state.cinema.filmDiscount;
			},
			discountRule() {
				return this.$store.state.cinema.discountRule;
			},
			aPrice() {
				let discountRule = (this.showItem.netPrice > 3900 ? this.discountRule.upDiscountRate : this.discountRule.downDiscountRate)
				let filmDiscount = this.filmDiscount
				let netPrice = this.showItem.netPrice / 100
				return (netPrice * discountRule * filmDiscount).toFixed(2)
			},
			aDiscount() {
				let discountRule = (this.showItem.netPrice > 3900 ? this.discountRule.upDiscountRate : this.discountRule.downDiscountRate)
				let filmDiscount = this.filmDiscount
				let netPrice = this.showItem.netPrice / 100
				return (netPrice - netPrice * discountRule * filmDiscount).toFixed(2)
			},
			allPrice() {
				let discountRule = (this.showItem.netPrice > 3900 ? this.discountRule.upDiscountRate : this.discountRule.downDiscountRate)
				let filmDiscount = this.filmDiscount
				let netPrice = this.showItem.netPrice / 100
				if (this.radioType == 1) {
					return (this.seatResult.length * netPrice * discountRule * filmDiscount).toFixed(2)
				}
				if (this.radioType == 2) {
					return (this.seatResult.length * netPrice).toFixed(2)
				}
			},
			allDiscount() {
				let discountRule = (this.showItem.netPrice > 3900 ? this.discountRule.upDiscountRate : this.discountRule.downDiscountRate)
				let filmDiscount = this.filmDiscount
				let netPrice = this.showItem.netPrice / 100
				if (this.radioType == 1) {
					return (this.seatResult.length * (netPrice - netPrice * discountRule * filmDiscount)).toFixed(2)
				}
				if (this.radioType == 2) {
					return (this.seatResult.length * (netPrice - netPrice)).toFixed(2)
				}
			},
		},
		methods: {
			getPhoneNumber(e){
				console.log(e)
				this.userPhone=e.phoneNumber
			},
			radioOnChange(e) {
				this.orderRes = {}
				this.radioType = e.detail
			},
			onChange({
				detail
			}) {
				// 需要手动对 checked 状态进行更新
				if (detail) {
					this.acceptChangeSeat = 1
				} else {
					this.acceptChangeSeat = 0
				}
			},
			async buy() {
				//已有订单则直接请求历史数据
				if (this.orderRes.code == 200) {
					let payRes = await wx.requestPayment(this.orderRes.data)
					console.log(payRes)
					return false
				}

				let seat = [],
					seatId = [],
					seatNo = []
				this.seatResult.forEach(val => {
					seat.push(val.seatNo)
					seatNo.push(val.seatNo)
					seatId.push(val.seatId)
				})
				this.$utils.wxUtils.subscribeToNews().then(toNew=>{
					console.log(toNew)
					this.$api.cinema.prepareOrder({
						acceptChangeSeat: this.acceptChangeSeat,
						seat: seat.join(','),
						seatId: seatId.join('|'),
						seatNo: seatNo.join('|'),
						showId: this.showItem.showId,
						cinemaId: this.showItem.cinemaId,
						goodsPictureUrl: this.filmDes.pic,
						goodsType: '1',
						ticketType: this.radioType
					}).then(async res => {
						this.orderRes = res
						let payRes = await wx.requestPayment(this.orderRes.data)
						if (payRes.errMsg == 'requestPayment:ok') {
							this.$utils.goPage(`/pages/cinema/orderdes?outTradeNo=${this.orderRes.data.outTradeNo}`, 'reLaunch')
						}
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.film-des {
		padding: 40rpx 30rpx;
		margin-top: 20rpx;
		background-color: #FFFFFF;
		display: flex;

		.left-img {
			width: 170rpx;
			height: 236rpx;
			margin-right: 20rpx;
		}

		.right-box {
			display: flex;
			flex-direction: column;
			justify-content: space-between;

			.film-name {
				font-size: 30rpx;
				font-weight: 400;
				color: #0F0404;
			}

			.film-time {
				font-size: 26rpx;
				font-weight: 400;
				color: #666666;
				line-height: 39rpx;
			}

			.film-caveat {
				display: flex;

				.item {
					display: flex;
					align-items: center;
					margin-right: 33rpx;
					font-size: 26rpx;
					font-weight: 400;
					color: #0F0404;
				}
			}
		}
	}
	
	.server {
		border: none;
		background-color: none;
		margin: 0;
		padding: 0;
		text-align: left;
		display: flexbox;
	}

	.select-des {
		background-color: #FFFFFF;
		margin-top: 20rpx;
		padding-bottom: 1rpx;

		.tit-text {
			padding: 30rpx;
			font-size: 26rpx;
			font-weight: 400;
			color: #0F0404;
			border-bottom: 1rpx solid #E8E8E8;
		}

		.options-box {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin: 30rpx;

			.left-box {
				display: flex;

				.img {
					width: 66rpx;
					height: 66rpx;
					margin-right: 20rpx;
				}

				.text-box {
					display: flex;
					flex-direction: column;

					.top {
						font-size: 26rpx;
						font-weight: 400;
						color: #0F0404;
					}

					.bottom {
						font-size: 22rpx;
						font-weight: 400;
						color: #666666;
					}
				}
			}

			.right-box {
				display: flex;
				align-items: center;

				.text-box {
					display: flex;
					flex-direction: column;
					justify-content: space-around;
					align-items: flex-end;
					margin-right: 23rpx;

					.top {
						font-size: 26rpx;
						font-weight: 400;
						color: #E31818;
					}

					.bottom {
						font-size: 20rpx;
						font-weight: 400;
						color: #605A5A;
					}
				}
			}
		}
	}

	.seat-select {
		background-color: #FFFFFF;
		margin-top: 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 20rpx 30rpx;

		.tit-text {
			font-size: 26rpx;
			font-weight: 400;
			color: #E31818;
		}
	}

	.seat-des {
		background-color: #FFFFFF;
		margin-top: 20rpx;

		.tit-text {
			font-size: 26rpx;
			font-weight: 400;
			color: #0F0404;
			padding: 30rpx;
			border-bottom: 1rpx solid #E8E8E8;
		}

		.seat-box {
			display: flex;
			justify-content: flex-start;
			padding: 10rpx 30rpx;

			.left {
				white-space: nowrap;
				height: 96rpx;
				margin-top: 10rpx;
			}

			.right {
				display: flex;
				flex-wrap: wrap;
				justify-content: flex-start;
				align-items: center;

				.item {
					width: 134rpx;
					height: 96rpx;
					margin: 10rpx;
					background: #EBEBEB;
					border-radius: 10rpx;
					display: flex;
					flex-direction: column;
					justify-content: center;
					align-items: center;

					.top {
						font-size: 26rpx;
						font-weight: 400;
						color: #666666;
					}

					.bottom {
						font-size: 22rpx;
						font-weight: 400;
						color: #E31923;
					}
				}
			}
		}
	}

	.buy-des {
		display: flex;
		justify-content: space-between;
		align-items: center;
		background-color: #FFFFFF;
		width: 100vw;
		box-sizing: border-box;
		position: fixed;
		bottom: 0;
		padding: 15rpx 30rpx;
		padding-bottom: calc(10px + env(safe-area-inset-bottom)/2);

		.left {
			display: flex;
			justify-content: space-between;
			align-items: center;
			width: 50%;

			.one {
				font-size: 26rpx;
				color: #0F0404;
			}

			.two {
				font-size: 36rpx;
				color: #E31919;
			}

			.three {
				font-size: 22rpx;
				color: #E86125;
			}
		}

		.right {
			width: 40%;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.text {
				font-size: 26rpx;
				color: #0F0404;
			}

			.btn {
				margin: 0;
				width: 200rpx;
				height: 80rpx;
				background: linear-gradient(90deg, #E31818, #ED3E24, #ED4F24);
				border-radius: 40rpx;
				font-size: 30rpx;
				color: #FFFFFF;
				display: flex;
				justify-content: center;
				align-items: center;
				white-space: nowrap;
			}
		}
	}
</style>
