<template>
	<view>
		<!-- #ifdef MP-ALIPAY -->
		<u-navbar :placeholder="true" bgColor="#fff" :autoBack="true" :border="true" title="优惠券详情" leftIconSize="0">
		</u-navbar>
		<!-- #endif -->
		<!-- #ifndef MP-ALIPAY -->
		<u-navbar :placeholder="true" bgColor="#fff" :autoBack="true" :border="true" title="优惠券详情"></u-navbar>
		<!-- #endif -->

		<view class="detail">
			<!-- 优惠券信息 -->
			<view class="flex detail-item">
				<!-- <image src="../../static/icon/coupon.png" mode=""></image>
				<view class="flex info">
					<view class="flex desc">
						<view class="title">{{ info.title }}</view>
						<view class="txt">使用期限：{{ $parseTime(info.validStart, '{y}.{m}.{d}') }}-{{ $parseTime(info.validEnd, '{y}.{m}.{d}') }}</view>
						<view class="txt">使用范围：{{ info.useAreaDesc || '-' }}</view>
					</view>
					<view class="flex btn">
						<view class="amt"><text>￥</text>{{ info.discount / 100 }}</view>
					</view>
				</view> -->
				<view class="content flex">
					<view class="content-left flex">
						<image src="../static/prize_coupon.png" mode="scaleToFill"></image>
						<view class="price flex">
							<text>{{ info.discount / 100 }}</text>
							<text>元</text>
						</view>
					</view>
					<view class="content-right flex">
						<view class="title ells-one">{{ info.title }}</view>
						<view class="desc"
							v-if="info.type && JSON.parse(info.type).value == 2 && info.useArea && JSON.parse(info.useArea).value == 1">
							适用范围：点击查看</view>
						<view class="desc"
							v-else-if="info.type && JSON.parse(info.type).value == 2 && info.useArea && JSON.parse(info.useArea).value == 0">
							适用范围：点击查看</view>
						<view class="desc" v-else>适用范围：{{ info.useAreaDesc || '-' }}</view>
						<view class="desc">使用期限：{{ $parseTime(info.validStart, '{y}.{m}.{d}') }} ~
							{{ $parseTime(info.validEnd, '{y}.{m}.{d}') }}</view>
					</view>
				</view>
			</view>

			<!-- 间隔 -->
			<view class="detail-interval flex">
				<view class="detail-interval__left"></view>
				<view class="detail-interval__line"></view>
				<view class="detail-interval__right"></view>
			</view>

			<!-- 二维码 -->
			<view class="flex detail-code" v-if="info && info.useArea && JSON.parse(info.useArea).value  != 4">
				<canvas style="width: 220px;height: 220px;" canvas-id="couponQrcode" id="couponQrcode"></canvas>
			</view>
			<!-- 范围 -->
			<view class="detail-explain" v-if="info && info.useArea && JSON.parse(info.useArea).value  == 1">
				<view class="txt">使用范围：</view>
				<view class="txt detail">共{{info.channelTotal}}家门店可使用 <view class="detail-address" @click="toAddress">立即查看</view>
				</view>
			</view>
			<view class="detail-explain" v-if="info && info.useArea && JSON.parse(info.useArea).value  == 0">
				<view class="txt">使用范围：</view>
				<view class="txt detail">共{{info.channelTotal}}家门店可使用 <view class="detail-address" @click="toAddress">立即查看</view>
				</view>
			</view>
			<!-- 说明 -->
			<view class="detail-explain">
				<view class="txt">使用说明：</view>
				<view class="txt detail" v-html="info.description" style="white-space: pre-wrap;"></view>
			</view>
		</view>
	</view>
</template>

<script>
	import $http from '@/utils/request.js'
	import parse from 'mini-html-parser2';
	const qrCode = require('@/common/weapp-qrcode.js')
	export default {
		data() {
			return {
				info: {},
				id: null,
			};
		},
		onLoad(options) {
			this.id = options.id
			this.getDetail()
		},
		methods: {
			getDetail() {
				uni.showLoading({
					title: '加载中'
				});
				$http.post('/api/v1/mp/user/mine/coupon/detail', {
					id: this.id
				}).then(res => {
					uni.hideLoading();
					if (res.code == 0) {
						// #ifndef MP-ALIPAY
						res.data.description = res.data.description.replaceAll('\n','<br/>')
						// #endif
						// #ifdef MP-ALIPAY
						parse(res.data.description,(err,nodes) => {
							if(!err){
								res.data.description = nodes
							}
						})
						// #endif
						this.info = res.data
						setTimeout(() => {
							uni.hideLoading();
							this.couponQrCode()
						}, 50)
					}
				}).catch(() => {
					uni.hideLoading();
				})
			},
			//地址
			toAddress() {
				uni.navigateTo({
					url:`/packagePrize/address/index?couponId=${this.info.couponId}`,
				})
			},
			// 二维码生成工具
			couponQrCode() {
				new qrCode('couponQrcode', {
					text: this.info.verifyCode,
					width: 213,
					height: 213,
					colorDark: "#333333",
					colorLight: "#FFFFFF",
					correctLevel: qrCode.CorrectLevel.H
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	
	.detail {
		margin: 20rpx;
		background-color: #FFFFFF;
		border-radius: 10rpx;
		overflow: hidden;

		// 优惠券信息
		&-item {
			padding: 30rpx 0 30rpx;
			background-color: #fff;

			.content {
				width: 100%;
				height: 184rpx;
				justify-content: flex-start;

				&-left {
					width: 210rpx;
					height: 100%;
					margin-right: 20rpx;

					image {
						width: 100%;
						height: 100%;
					}

					.price {
						position: absolute;
						color: #fff;

						text:first-child {
							font-size: 45rpx;
						}

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

				&-right {
					flex: 1;
					align-items: flex-start;
					flex-direction: column;
					justify-content: space-between;
					height: 100%;
					padding: 24rpx 0;

					.title {
						font-size: 32rpx;
						line-height: 32rpx;
						height: 32rpx;
						overflow: hidden;
						font-weight: bold;
					}

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

		// 间隔
		&-interval {
			position: relative;
			width: 100%;
			height: 30rpx;
			margin-bottom: 10rpx;

			&__left {
				position: absolute;
				left: -15rpx;
				width: 30rpx;
				height: 30rpx;
				background: #F9F7F5;
				border-radius: 50%;
			}

			&__line {
				height: 1px;
				background-color: #F9F7F5;
				width: 90%;
			}

			&__right {
				position: absolute;
				right: -15rpx;
				width: 30rpx;
				height: 30rpx;
				background: #F9F7F5;
				border-radius: 50%;
			}
		}

		// 二维码
		&-code {
			width: 100%;
			height: 460rpx;
			margin-bottom: 20rpx;
		}

		// 说明
		&-explain {
			font-size: 26rpx;
			padding: 0 34rpx 34rpx;

			.detail {
				margin-left: 20rpx;

				&-address {
					margin-left: 20rpx;
					display: inline-block;
					width: 150rpx;
					height: 40rpx;
					color: #FE9E48;
					border: 2rpx solid #FE9E48;
					text-align: center;
					line-height: 36rpx;
					font-size: 24rpx;
					border-radius: 8rpx;
				}
			}

			.txt {
				line-height: 40rpx;
			}
		}
	}
</style>
