<template>
	<view>
		<!-- #ifdef MP-ALIPAY -->
		<u-navbar :placeholder="true" bgColor="#fff" :autoBack="true" :border="true" title="选择优惠券" leftIconSize="0">
		</u-navbar>
		<!-- #endif -->
		<!-- #ifdef MP-WEIXIN -->
		<u-navbar :placeholder="true" bgColor="#fff" :autoBack="true" :border="true" title="选择优惠券"></u-navbar>
		<!-- #endif -->
		<!-- #ifndef MP-WEIXIN || MP-ALIPAY -->
		<view v-if="pagesNum > 1">
			<u-navbar :placeholder="true" bgColor="#fff" :autoBack="true" :border="true" title="选择优惠券" />
		</view>
		<view v-else>
			<u-navbar leftIconSize="0" :placeholder="true" bgColor="#fff" :border="true" title="选择优惠券">
				<view class="nav-left flex" slot="left" @click="$toIndex()">
					<u-icon name="arrow-left" size="20" color="#333"></u-icon>
				</view>
			</u-navbar>
		</view>
		<!-- #endif -->

		<view class="coupon-title">可用优惠券：{{ total }}</view>
		<view class="coupon-list">
			<!-- <u-radio-group v-model="radiovalue1" placement="column" @change="changeChechk($event)"> -->
				<view class="flex coupon-list-item" v-for="(item,index) in list" :key="index"
					:style="{backgroundImage:`url(${ index == isActive ? backOn : backOff })`}" @click="changeChechk(item.id)">
					<view class="flex coupon-list-item-info" @click="exclusive(index)">
						<view class="money">
							<!-- <view class="">￥<text>{{ item.discount / 100 }}</text></view> -->
							<view class="price flex">
								<text>{{ item.discount / 100 }}</text>
								<text>元</text>
							</view>
						</view>
						<view class="flex content">
							<view class=" title ells-one">{{ item.title }}</view>
							<view class="txt ells-one top">适用范围：{{ item.useAreaDesc || '-' }}</view>
							<view class="txt">
								使用期限：{{ $parseTime(item.validStart, '{y}.{m}.{d}')}}-{{$parseTime(item.validEnd, '{y}.{m}.{d}')}}
							</view>
						</view>
					</view>
					<view class="uradio" @click="exclusive(index)">
						<view class="" style="background-color: #F9822C;width: 24px;height: 24px;border-radius: 50%;" v-if="index == isActive">
							<div style="display: inline-block;margin: 4px;">
								<u-icon name="checkbox-mark" color="#fff" size="16"></u-icon>
							</div>
						</view>
						<view class="" style="background-color: rgb(200, 201, 204);width: 24px;height: 24px;border-radius: 50%;" v-else>
							<div style="display: inline-block;margin: 4px;">
								<u-icon name="checkbox-mark" color="#fff" size="16"></u-icon>
							</div>
						</view>
						<!-- <u-radio :customStyle="{ marginBottom: '8px' }" :name="item.id" activeColor="#F9822C" size="24"
							@change="exclusive(index)">
						</u-radio> -->
					</view>
				</view>
				<u-loadmore :line="true" v-if="list.length>5" :status="status" :loading-text="'努力加载中'"
					:nomore-text="'已经到底了'" />
			<!-- </u-radio-group> -->
		</view>

		<view class="flex empty" v-if="!list.length">
			<view class="center">
				<image class="center-img"
					src="https://mp-public-1310078123.cos.ap-shanghai.myqcloud.com/v2/nodata_1.png" mode="scaleToFill">
				</image>
				<view class="center-font">暂无优惠券</view>
			</view>
			<!-- <u-empty text="数据为空" mode="order" /> -->
		</view>

		<view class="footer-fixed">
			<view class="flex btn">
				<button type="default" @click="exchange">确认</button>
			</view>
		</view>
	</view>
</template>

<script>
	import env from '../../config/env.js'
	import appId from '@/config/appId.js'
	import $http from '@/utils/request.js'
	export default {
		data() {
			return {
				checked: false,
				actionIndex: 0,
				pageNum: 1,
				total: 0,
				list: [],
				listNum: '', //可用优惠券数量
				radiovalue1: '', //选中项的下标
				couponId: '', //选中项的id
				boxId: '',
				ticketId: '',
				status: 'nomore', //上拉刷新状态

				isActive: undefined,
				backOn: "https://mp-public-1310078123.cos.ap-shanghai.myqcloud.com/v2/coupon_bkg2.png",
				backOff: "https://mp-public-1310078123.cos.ap-shanghai.myqcloud.com/v2/coupon_bkg1.png",
				channelId: '',
				pagesNum: '',
				siteId: '',
			};
		},
		onShow() {
			this.pagesNum = getCurrentPages().length
			this.pageList()
			this.exclusive()
		},
		onLoad(opthios) {
			this.boxId = opthios.boxId

			if (opthios.ticketId) {
				this.ticketId = opthios.ticketId
			}
			if (opthios.couponId) {
				this.channelId = opthios.channelId
				this.couponId = opthios.couponId
				this.radiovalue1 = opthios.couponId
			}
			if(opthios.siteId){
				this.siteId = opthios.siteId
			}
		},
		methods: {
			pageList() {
				this.list = []
				this.getList()
			},

			getList() {
				let _this = this
				$http.post(`/api/v1/mp/user/ticket/order/coupon/listPage?pageNum=${_this.pageNum}&pageSize=20`, {}).then(
					res => {
						if (res.code == 0) {
							this.listNum = res.rows.length
							res.rows.forEach(item => {
								let useAreaDesc = JSON.parse(item.useArea)
								item.useAreaDesc = useAreaDesc.desc
							})
							_this.list = [..._this.list, ...res.rows]
							_this.total = res.total
							for (var i = 0; i < _this.list.length; i++) {
								if (_this.list[i].id == _this.couponId) {
									_this.isActive = i
								}
							}
						}

					}).catch(() => {
				})
			},

			exclusive(index) {
				this.isActive = index
			},

			// 点击切换事件
			changeChechk(e) {
				if (this.couponId == e) {
					this.radiovalue1 = -1
					this.isActive = -1
					this.couponId = []
					return
				} else {
					this.couponId = [e]
					return
				}
			},

			// 点击确认事件
			exchange() {
				let data = {
					userCouponIds: this.couponId,
					autoCoupon: 0,
					boxId: this.boxId,
					ticketId: this.ticketId,
					orderNum: 1,
					appSource: appId
				}
				if( this.siteId ){
					data.siteId = this.siteId
				}
				$http.post('/api/v1/mp/user/ticket/order/settle', data).then(res => {
					if (res.code == 0) {
						let pages = getCurrentPages()
						let page = pages[pages.length - 2]
						let payInfo = {
							...res.data,
							channelId: this.channelId,
							picUrl: env.filePublic + res.data.picUrl.split(',')[0],
							couponDiscount: res.data && res.data.couponList && res.data.couponList.length &&
								res
								.data.couponList[0].discount,
							couponId: res.data && res.data.couponList && res.data.couponList.length && res.data
								.couponList[0].id,
							listNum: this.listNum
						}
						if(this.siteId){
							payInfo.siteId = this.siteId
						}
						page.$vm.payInfo = payInfo
						uni.navigateBack({
							delta: 1
						})
					}
				})
			}
		},
		onReachBottom() {
			// 判断是否有数据
			if (this.total < this.pageNum * 20) return;
			this.status = 'loading';
			++this.pageNum
			if (this.total < this.pageNum * 20) this.status = 'nomore';
			else this.status = 'loading';
			this.getList()
		},
	}
</script>

<style lang="scss" scoped>
	.coupon {

		&-title {
			position: fixed;
			z-index: 10;
			width: 100%;
			padding: 24rpx 32rpx;
			background-color: #FFFFFF;
		}

		&-list {
			margin-top: 100rpx;
			padding: 0 20rpx 200rpx;

			&-item {
				justify-content: space-between;
				padding: 20rpx;
				// background-color: #FFFFFF;
				margin-bottom: 20rpx;
				background-size: 100%;

				&-info {
					flex: 1;
					justify-content: flex-start;
					height: 175rpx;

					image {
						width: 84rpx;
						height: 132rpx;
						margin-right: 20rpx;
					}

					.content {
						width: 445rpx;
						height: 150rpx;
						flex-direction: column;
						align-items: flex-start;
						justify-content: space-between;

						.txt {
							font-size: 24rpx;
							line-height: 24rpx;
							color: #999999;
						}
					}

					.content .title {
						font-size: 36rpx;
						color: #333;
						line-height: 40rpx;
						// font-weight: bold;
					}

					.money {
						width: 220rpx;
						height: 100%;
						margin: 0 10rpx 0;

						.price {
							position: absolute;
							color: #fff;
							z-index: 0;

							text:first-child {
								line-height: 150rpx;
								font-size: 70rpx;
								padding-right: 20rpx;
							}

							text:last-child {
								font-size: 40rpx;
							}
						}
					}
				}

				.uradio {
					position: absolute;
					right: 40rpx;
				}

				.circle {
					width: 40rpx;
					height: 40rpx;
					border-radius: 50%;
					border: 1px solid;
				}

				.action {
					width: 30rpx;
					height: 30rpx;
					border-radius: 50%;
					background-color: $uni-bg-color;
				}
			}
		}
	}

	.empty {
		height: 60vh;

		.center {
			text-align: center;

			&-img {
				width: 228rpx;
				height: 320rpx;
			}

			&-font {
				font-size: 30rpx;
				font-weight: 400;
				color: #999999;
				margin-bottom: 250rpx;
			}
		}
	}

	.footer-fixed {
		position: fixed;
		bottom: var(--window-bottom);
		left: 0;
		right: 0;
		z-index: 11;
		box-shadow: 0 -4rpx 40rpx 0 rgba(151, 151, 151, 0.24);
		background: #fff;
		// 设置ios刘海屏底部横线安全区域
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);

		.btn {
			padding: 20rpx 0;

			::v-deep button {
				width: 441rpx;
				height: 88rpx;
				line-height: 88rpx;
				font-size: 36rpx;
				color: #fff;
				background-color: #F9822C;
				border: none;
				border-radius: 44rpx;
			}
		}
	}
</style>
