<template>
	<view class="centenr">
		<view class="Candywrapper">
			<view class="bai-bs">
				<view class="">
					<image :src="couponInfo.backgroundImage" v-if="couponInfo.backgroundImage"
						style="width: 60px; height: 60px; "></image>
					<image src="../../static/logo.png" v-else style="width: 60px; height: 60px; "></image>
				</view>
				<view class="rr-box">
					<view class="title_">{{couponInfo.name}}</view>
					<view style="font-size: 12px;color: #666666;">{{couponInfo.useCondition}}</view>
					<view style="width: 100%;display: flex;justify-content: space-between; ">
						<view class="red-size" v-if="couponInfo.type==1">代金券</view>
						<view class="red-size" v-if="couponInfo.type==2">兑换券</view>
						<view class="red-size" v-if="couponInfo.type==3">折扣券</view>
						<view class="red-size" v-if="couponInfo.type==4">油品立减券</view>
						<view class="red-size" v-if="couponInfo.type==5">单品代金券</view>
						<view class="red-size">
							剩余{{ couponInfo.tfGetNum ? (couponInfo.tfTotal - couponInfo.tfGetNum) : couponInfo.tfTotal}}张
						</view>
					</view>
				</view>
			</view>
			<view class="bai-box">
				<view class="title_">使用须知</view>
				<view class="si_">有效期：领取后{{couponInfo.validityDay || 0}}天内有效</view>
				<view class="si_">领取说明：</view>
				<view class="si_">1、每人限领{{couponInfo.getNumLimit || 0}}张</view>
				<view class="si_">2、每日限领{{couponInfo.dayGetLimit || 0}}张</view>
				<view class="si_">核销说明：</view>
				<view class="si_" style="display: flex;" v-for="(item,index) in couponInfo.hxList" :key="index">
					<view
						style="width: 6px;height: 6px;border-radius: 50%;margin-right: 5px;background-color: #FF9655;margin-top: 6px;">
					</view>
					<view>{{item}}</view>
				</view>
			</view>
			<view class="bai-box">
				<view style="width: 100%; display: flex;align-items: center;justify-content: space-between; ">
					<view class="title_">适用门店</view>
					<view class="d-s" style="font-size: 14px;color: #FF2828;" @click="goGoGo()">
						<image src="../../static/icon/reddh.png" style="width: 18px; height: 18px; "></image>
						导航
					</view>
				</view>
				<view class="si">{{store.name}}</view>
				<view class="si">{{store.address}} | {{distance}}km</view>

			</view>

		</view>
		<view class="bottom-box">
			<view class="or-num">免费</view>
			<view class="anniu" @click="getCardCoupon()">立即领取</view>
		</view>
	</view>
</template>

<script>
	import request from '../../utils/request';
	export default {
		data() {
			return {
				value: 0,
				certificateId: '',
				couponInfo: {},
				AppToken: uni.getStorageSync("App-Token"),
				// 当前经度信息
				longitude: "",
				// 当前纬度信息
				latitude: "",
				// 店铺经度信息
				lon: "",
				// 店铺纬度信息
				lat: "",
				// 店铺信息
				store: {
					name: "测试油站",
					description: "济南分店",
					address: "济南槐荫区"
				},
				distance: 0,
			}
		},
		onLoad(e) {
			this.certificateId = e.certificateId
			this.getInfo()
			this.getAddress(uni.getStorageSync("storeId"))
		},
		methods: {
			goGoGo() {
				let lat = Number(this.lat)
				let lon = Number(this.lon)
				uni.openLocation({
					latitude: lat,
					longitude: lon,
					name: this.store.name,
					address: this.store.address,

					success: function() {
						console.log('success');
					},
					complete: function(res) {
						console.log(res);
					}
				});
			},
			// 获取当前位置
			getAddress(storeId) {
				let _this = this;
				uni.getLocation({
					// 谷歌使用wgs84  其他使用gcj02
					type: 'gcj02', // 使用国测局坐标系
					success: function(res) {
						if (_this.longitude == "" && _this.latitude == "") {

							_this.longitude = res.longitude;
							_this.latitude = res.latitude
						}
						console.log('经度: ' + _this.longitude);
						console.log('纬度: ' + _this.latitude);
						request({
							url: 'business/storeInformation/store/recentlyStore',
							method: 'post',
							data: {
								"lon": _this.longitude,
								"lat": _this.latitude,
								"storeId": storeId,
								"isLogin": _this.AppToken ? "0" : "1", // 0为登录

							},
						}).then((response) => {
							if (response.data.store) {
								_this.distance = (Math.ceil(response.data.distance))
									.toFixed(1)
								_this.store = response.data.store

								_this.lon = _this.store.longitude
								_this.lat = _this.store.latitude
								console.log(_this.store, 129);
							} else {
								uni.showToast({
									title: "当前店铺已关闭！！！",
									icon: "none"
								})
							}

						}).catch(err => {})
					},
					fail: function(err) {
						console.log('获取位置信息失败: ' + err.errMsg);
					}
				});
			},
			//领取优惠券
			getCardCoupon() {
				request({
					url: '/cardCoupon/userGetCoupon',
					method: 'post',
					data: this.couponInfo.id
				}).then(res => {
					if (res.code == 200) {
						if (res.data == 2) {
							uni.showToast({
								title: "优惠券已下线",
								icon: "none"
							})
						} else if (res.data == 0) {
							uni.showToast({
								title: "优惠券剩余不足",
								icon: "none"
							})
						} else if (res.data == 3) {
							uni.showToast({
								title: "你已领取上限",
								icon: "none"
							})
						} else {
							uni.showToast({
								title: "领取成功",
								icon: "none"
							})
						}
					}
				})
			},
			getInfo() {
				request({
					url: '/cardCoupon/' + this.certificateId,
					method: 'get',
				}).then(res => {
					console.log(res, 74);
					if (res.code == 200) {
						this.couponInfo = res.data
					}
				})
			},
			valChange(e) {
				console.log('当前值为: ' + e.value)
			}
		}
	}
</script>
<style scoped lang="scss">
	.centenr {
		width: 100%;
		height: 100vh;
		background: #F9F9F9;
	}

	.Candywrapper {
		background: #F9F9F9;
		box-sizing: border-box;
		padding: 10px;
	}

	.bottom-box {
		width: 100%;
		background: #fff;
		box-sizing: border-box;
		padding: 15px 10px;
		display: flex;
		align-items: center;
		justify-content: space-between;
		position: fixed;
		left: 0px;
		bottom: 0px;
	}

	.or-num {
		font-weight: 600;
		font-size: 20px;
		color: #FA6400;
	}

	.bai-box {
		background-color: #fff;
		box-sizing: border-box;
		padding: 10px;
		width: 100%;
		margin: 10px auto;
	}

	.bai-bs {
		background-color: #fff;
		box-sizing: border-box;
		padding: 10px;
		width: 100%;
		margin: 10px auto;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.title_ {
		font-weight: 600;
		font-size: 16px;
		margin-bottom: 5px;
	}

	.anniu {
		width: 90px;
		height: 30px;
		background: #FA6400;
		border-radius: 50px;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #fff;
	}

	.rr-box {
		width: 80%;

	}

	.red-size {
		font-size: 12px;
		color: #E02020;
	}

	.si_ {
		font-size: 14px;
		color: #333333;
	}

	.d-s {
		display: flex;
		align-items: center;
	}
</style>