<template>
	<view>
		<u-navbar :placeholder="true" bgColor="#fff" :autoBack="true" :border="true" title="选择优惠券"></u-navbar>
		<view class="coupon-title">可用优惠券：{{listNum}}</view>
		<view class="coupon-list">
			<u-radio-group v-model="radiovalue1" placement="column" @change="changeChechk()">
				<view class="flex coupon-list-item" v-for="(item,index) in list" :key="index">
					<view class="flex coupon-list-item-info">
						<view class="money">
							<view class="">￥<text>{{ item.discount / 100 }}</text></view>
						</view>
						<view class="flex content">
							<view class="txt title">{{ item.title }}</view>
							<view class="txt">
								使用期限：{{ $parseTime(item.validStart, '{y}.{m}.{d}')}}-{{$parseTime(item.validEnd, '{y}.{m}.{d}')}}
							</view>
							<view class="txt">使用范围：{{ item.useAreaDesc || '-' }}</view>
						</view>
					</view>
					<u-radio :customStyle="{ marginBottom: '8px' }" :name="item.id" activeColor="#E96737" size="24">
					</u-radio>
				</view>
			</u-radio-group>
		</view>

		<view class="flex empty" v-if="!list.length">
			<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 $http from '@/utils/request.js'
	export default {
		data() {
			return {
				checked: false,
				actionIndex: 0,
				pageNum: 1,
				total: 0,
				list: [],
				listNum: '', //可用优惠券数量
				radiovalue1: '', //选中项的下标
				couponId: '', //选中项的id
				boxId: '',
				ticketId: '',

			};
		},
		onShow() {
			this.pageList()
		},
		onLoad(opthios) {
			this.boxId = opthios.boxId

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

			getList() {
				uni.showLoading({
					title: '加载中'
				});
				$http.post('/api/v1/mp/user/ticket/order/coupon/list', {}).then(
					res => {
						uni.hideLoading();
						if (res.code == 0) {
							this.listNum = res.data.length
							res.data.forEach(item => {
								let useAreaDesc = JSON.parse(item.useArea)
								item.useAreaDesc = useAreaDesc.desc
							})
							this.list = res.data
							console.log(this.list)
						}

					}).catch(() => {
					uni.hideLoading();
				})
			},

			// 点击切换事件
			changeChechk(e) {
				if (this.couponId == e) {
					this.radiovalue1 = -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
				}
				$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,
							picUrl: env.filePublic + res.data.picUrl,
							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
						}
						page.$vm.payInfo = payInfo
						uni.navigateBack({
							delta: 1
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.coupon {
		&-title {
			position: fixed;
			width: 100%;
			padding: 24rpx 32rpx;
			background-color: #FFFFFF;
		}

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

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

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

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

					.content {
						height: 132rpx;
						flex-direction: column;
						align-items: flex-start;
						justify-content: space-between;
					}

					.content .title {
						font-size: 32rpx;
						line-height: 50rpx;
					}

					.money {
						padding-right: 30rpx;
						font-weight: bold;

						text {
							font-size: 50rpx;
						}
					}
				}

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

	.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;

			/deep/ button {
				width: 640rpx;
				height: 90rpx;
				line-height: 90rpx;
				font-size: 36rpx;
				color: #fff;
				background-color: $uni-bg-color;
				border: none;
				border-radius: 20rpx;
			}
		}
	}
</style>
