<template>
	<view class="mg-lr-24">
		<view class="common-card mg-bot-20">
			<view class="u-f u-f-jcfs u-f-aifs">
				<view class="fw400 light-gray fs28 mg-right-14 vertical-text" style="flex: 28rpx;">适用门店</view>
				<view class="right u-f u-f-jcfs">
					<view class="mg-right-12">
						<image style="width: 134rpx;height: 134rpx;" class="border-radius-9" :lazy-load="true"
							:mode="'scaleToFill'" :src="goodsInfo.storeLogo"></image>
					</view>
					<view style="width: calc(100% - 133rpx)">
						<view class="u-f u-f-jcsb u-f-aic mg-bot-10">
							<view style="flex: 3" class="fs28 fw400 light-black one-line-ellipsis">
								{{goodsInfo.storeName}}</view>
							<!-- <view style="flex: 1" class="u-f u-f-jcfs u-f-aic" @click="goToShopDetail">
								<text
									class="fs24 fw400 light-gray mg-right-18">{{goodsInfo.storeNum}}家门店</text>
								<image src="../.././static/common/big_gray_right_arrow.png"
									:mode="'widthFix'" style="width: 13rpx;height: 26rpx"></image>
							</view> -->
						</view>
						<view class="mg-bot-10">
							<text class="fs28 light-gray fw400">{{goodsInfo.distance}}km</text>
						</view>
						<view class="u-f u-f-jcsb u-f-aic" @click="skipCityMap(goodsInfo)">
							<text class="fs28 fw400 light-gray one-line-ellipsis">{{goodsInfo.detailedAddress}}</text>
							<view class="u-f u-f-jcfe u-f-aic">
								<image class="send-icon" :mode="'widthFix'" src="../.././static/common/send.png">
								</image>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>

		<view class="white-bg-wrap">
			<view class="u-f u-f-jcsb u-f-aic mg-bot-20">
				<image :src="goodsInfo.goodsImage" style="width: 128rpx;height: 128rpx;flex: 1" :mode="'scaleToFill'"
					class="mg-right-10 border-radius-9"></image>
				<view class="u-f u-f-fdc" style="width: calc(100% - 138rpx)">
					<view class="fs28 light-black fw400 mg-bot-10">{{goodsInfo.goodsName}}</view>
					<view class="mg-bot-10 u-f u-f-aic u-f-jcsb">
						<text class="light-gray fs24 mg-right-8">{{goodsInfo.specificationsName}}</text>
					</view>
					<view class="u-f u-f-aic u-f-jcsb">
						<view class="u-f u-f-jcsb u-f-aic" style="margin-right: 96rpx;">
							<text class="fs32 main-color fw600">{{goodsInfo.price}}</text>
						</view>
						<view class="fs24 light-gray">x{{goodsInfo.goodsNum}}</view>
					</view>
				</view>
			</view>
		</view>

		<view class="fix-bot u-f u-f-jcsa u-f-aic">
			<view class="u-f u-f-jcfs u-f-aic">
				<text class="fs24 light-gray mg-right-22">所需积分</text>
				<text class="fs32 main-color fw600">{{goodsInfo.price}}</text>
			</view>
			<view class="line-gradient-btn" style="padding: 24rpx 0;width: 50%" @click="handleExchange">
				立即兑换
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				key: 0,
				goodsInfo: {}
			}
		},
		onLoad(option) {
			this.key = option.key
			this.getOrderDetails()
		},
		methods: {
			async getOrderDetails() {
				let data = {
					key: this.key
				}

				let res = await this.$http.get('/api/client/pointsRecord/orderDetails', data, {
					token: true,
					checkToekn: true
				})
				let resData = res.data.data
				this.goodsInfo = resData
				console.log("resData", resData)
			},
			async jifenExchange() {
				let data = {
					pointsGoodsId: this.goodsInfo.pointsGoodsId,
					storeId: this.goodsInfo.storeId,
					specificationsId: this.goodsInfo.specificationsId,
					goodsNum: this.goodsInfo.goodsNum
				}

				let res = await this.$http.post('/api/client/pointsRecord/redeem', data, {
					token: true,
					checkToekn: true
				})
				let resData = res.data.data

				if (res.data.code === 200) {
					uni.redirectTo({
						url: '/pagesE/exchange-success/exchange-success'
					})
				}
			},
			async placeOrder() {
				// 下单
				let data = {
					"storeId": this.goodsInfo.storeId,
					"type": 4,
					"userCarId": this.goodsInfo.userCarId,
					"goodsId": this.goodsInfo.goodsId,
					"specificationsId": this.goodsInfo.specificationsId,
					"goodsNum": this.goodsInfo.goodsNum,
					"pointsId":this.goodsInfo.pointsGoodsId,
				}
				let res = await this.$http.post('/api/client/order/placeOrder', data)
				// console.log("下单", res)
				if (res.data.code === 200) {
					console.log(res.data)
					this.orderId = res.data.data
					// 支付
					this.payMoney()
				} else {
					uni.showToast({
						title: '下单失败',
						icon: 'none'
					})
				}
			},
			async payMoney() {
				let data = {
					"orderId": this.orderId,
				}

				let res = await this.$http.get('/api/client/lkl/pay', data)
				console.log("支付", res.data)
				let resData = res.data.data

				this.wakeWxPay(resData)
			},
			wakeWxPay(resData) {
				var app_id = resData.resp_data.acc_resp_fields.app_id
				var nonce_str = resData.resp_data.acc_resp_fields.nonce_str
				var package_word = resData.resp_data.acc_resp_fields['package']
				var partner_id = resData.resp_data.acc_resp_fields.partner_id
				var prepay_id = resData.resp_data.acc_resp_fields.prepay_id
				var time_stamp = resData.resp_data.acc_resp_fields.time_stamp
				var pay_sign = resData.resp_data.acc_resp_fields.pay_sign
				var sign_type = resData.resp_data.acc_resp_fields.sign_type

				var orderInfo = {
					appid: app_id, // 微信开放平台 - 应用 - AppId，注意和微信小程序、公众号 AppId 可能不一致
					nonceStr: nonce_str, // 随机字符串
					package: package_word, // 固定值
					partnerid: partner_id, // 微信支付商户号
					prepayid: prepay_id, // 统一下单订单号
					timeStamp: time_stamp, // 时间戳（单位：秒）
					signType: sign_type,
					paySign: pay_sign // 签名，这里用的 MD5/RSA 签名
				}

				uni.requestPayment({
					provider: "wxpay",
					appid: app_id, // 微信开放平台 - 应用 - AppId，注意和微信小程序、公众号 AppId 可能不一致
					nonceStr: nonce_str, // 随机字符串
					package: package_word, // 固定值
					partnerid: partner_id, // 微信支付商户号
					prepayid: prepay_id, // 统一下单订单号
					timeStamp: time_stamp, // 时间戳（单位：秒）
					signType: sign_type,
					paySign: pay_sign, // 签名，这里用的 MD5/RSA 签名
					success(res) {
						console.log("success", res)
						uni.redirectTo({
							url: "/pagesE/pay-success/pay-success"
						})
					},
					fail(e) {
						console.log("fail", e)
					}
				})
			},
			handleExchange() {
				console.log('this.goodsInfo.isPay', this.goodsInfo.isPay)
				// 1 金额支付 2 积分兑换
				if (this.goodsInfo.isPay == 1) {
					this.placeOrder()
				} else {
					this.jifenExchange()
				}
			},
			skipCityMap(data) {
				uni.openLocation({
					// 
					latitude: Number(data.lat),
					longitude: Number(data.lon),
					name: data.storeName,
					address: data.detailedAddress,
					success: function(res) {
						console.log('打开系统位置地图成功')
					},
					fail: function(error) {
						console.log(error)
					}
				})
			},
			goToShopDetail() {
				this.User.navigate({
					url: "/pagesC/choice-shop/choice-shop?goodsId=" + this.goodsInfo.goodsId + '&storeType2Id=' + 0
				}, "navigateTo", true)
			}
		}
	}
</script>

<style>
	page {
		background-color: #FAFBFF;
	}
</style>

<style lang="scss" scoped>
	.send-icon {
		width: 56rpx;
		height: 56rpx;
		// background: url(../.././static/maintenance/send.png) no-repeat center center;
		background-size: 100%;
	}

	.fix-bot {
		width: 100%;
		position: fixed;
		bottom: 0;
		left: 0;
		padding: 10rpx 0;
		background-color: #ffffff;
	}

	.common-card {
		padding: 20rpx;
		border-radius: 18rpx;
		background-color: #ffffff;
	}

	.white-bg-wrap {
		padding: 20rpx;
	}
</style>