<template>
	<view class="mg-lr-24">
		<view class="white-bg-wrap">
			<common-title :msg="'已选商品'"></common-title>
			<view class="mg-top-22" v-for="item in shopSureBillInfo.orderInfoList">
				<view class="title u-f u-f-jcfs u-f-aic mg-bot-20">
					<text class="fs24 fw400 light-black mg-right-10">{{item.storeName}} </text>
					<image src="../.././static/common/big_gray_right_arrow.png" :mode="'widthFix'"
						style="width: 13rpx;height: 26rpx"></image>
				</view>
				<view class="u-f u-f-jcfs u-f-aic mg-bot-20 white-bg-wrap">
					<image :src="item.goodsImage" style="width: 128rpx;height: 128rpx;" :mode="'scaleToFill'"
						class="mg-right-10 border-radius-9"></image>
					<view class="u-f u-f-fdc" style="flex: calc(100% - 138rpx)">
						<view class="fs28 light-black fw400 mg-bot-10">{{item.goodsName}}</view>
						<view class="mg-bot-10 u-f u-f-aic u-f-jcfs">
							<text class="light-gray fs24 mg-right-8">{{item.goodsSpecification}}</text>
							<text class="gray-down-arrow"></text>
						</view>
						<view class="u-f u-f-aic u-f-jcsb">
							<view class="u-f u-f-jcsb u-f-aic main-color" style="margin-right: 96rpx;">
								<text class="fs24">￥</text>
								<text class="fs32 fw600">{{item.unitPrice}}</text>
							</view>
							<view>
								<!-- <uni-number :min="1" :disabled="true" :value="item.goodsNum" @change="handleChangeNum(item)"></uni-number> -->
								<uni-number :min="1" :disabled="true" :value="item.goodsNum"></uni-number>
							</view>
						</view>
					</view>
				</view>

				<!-- <view class="u-f u-f-jcfs u-f-aic mg-bot-20">
					<view class="fw400 light-gray fs28 mg-right-14 vertical-text">适用门店</view>
					<view class="right u-f u-f-jcfs" style="flex: 5">
						<view class="mg-right-12">
							<image style="width: 134rpx;height: 134rpx;" :mode="'widthFix'"
								:src="item.storeLogo"></image>
						</view>
						<view style="flex: 4">
							<view class="u-f u-f-jcsb u-f-aic mg-bot-10" @click="jumpToShopDetail">
								<view class="fs28 fw400 light-black">{{item.storeName}}</view>
								<view class="u-f u-f-jcfs u-f-aic">
									<text class="fs24 fw400 light-gray mg-right-18">{{item.storeNum}}家门店</text>
									<image class="big-gray-right-arrow" src="../.././static/common/big_gray_right_arrow.png" style="width: 13rpx;height: 26rpx;" :mode="'widthFix'"></image>
								</view>
							</view>
							<view class="mg-bot-10">
								<text class="fs28 light-gray fw400">{{item.distance}}km</text>
							</view>
							<view class="u-f u-f-jcsb u-f-aic" @click="openCity(item)">
								<text class="fs28 fw400 light-gray">{{item.detailedAddress}}</text>
								<text class="send-icon"></text>
							</view>
						</view>
					</view>
				</view> -->
			</view>
		</view>

		<view class="white-bg-wrap mg-top-20" style="padding-top: 16rpx;">
			<view class="u-f u-f-aic u-f-jcsb light-black fs28 fw400 mg-bot-40">
				<text>现价</text>
				<text>￥{{shopSureBillInfo.orderAmount}}</text>
			</view>
			<view class="u-f u-f-jcsb light-black fs28 fw400 mg-bot-42">
				<view>优惠券</view>
				<view v-if="shopSureBillInfo.couponList.length>0" class="u-f u-f-jcfs u-f-aic"
					@click="handleShowGoodQuan">
					<text class="mg-right-6">{{youhuiquan.couponName}}</text>
					<text
						class="main-color mg-right-10">{{ youhuiquan.discountMethod == 1 ? '￥' : '' }}{{youhuiquan.discountResult}}</text>
					<image src="../.././static/common/sg_right_arrow.png" :mode="'widthFix'"
						class="small-gray-right-arrow" />
				</view>
				<view v-else class="u-f u-f-jcfs u-f-aic">
					<text class="mg-right-6">暂无可用优惠</text>
					<image src="../.././static/common/sg_right_arrow.png" :mode="'widthFix'"
						class="small-gray-right-arrow" />
				</view>
			</view>
			<view class="u-f u-f-jcfe u-f-aic fs24">
				<view class="light-black mg-right-10">已优惠</view>
				<view class="main-color mg-right-20">
					<text>￥{{shopSureBillInfo.couponList.length>0 ? youhuiquan.discountResult : 0}}</text>
				</view>
				<view class="light-black">合计</view>
				<view class="mg-right-6">
					<text class="fs24">￥</text>
					<text
						class="fw600 fs32">{{youhuiInfo.couponDiscountAmount ? youhuiInfo.couponDiscountAmount : youhuiInfo.orderPrice}}</text>
				</view>
			</view>
		</view>

		<view class="white-bg-wrap u-f u-f-jcsb u-f-aic mg-top-20" @click="enterMyGarage">
			<view class="u-f u-f-fdc mg-right-64" style="flex: 3">
				<view class="u-f u-f-jcsb">
					<view class="u-f u-f-jcfs u-f-aic">
						<image v-if="shopSureBillInfo.brandLogo" :src="shopSureBillInfo.brandLogo"
							style="width: 56rpx;height: 56rpx;" :mode="'widthFix'" class="mg-right-10"></image>
						<text class="fw400 fs30 light-black">{{shopSureBillInfo.vehicleBrand}}</text>
					</view>
					<view class="u-f u-f-aifs u-f-jcfs" v-if="shopSureBillInfo.plateNo">
						<text class="province">{{shopSureBillInfo.plateNo.slice(0,1)}}</text>
						<text class="car-num" v-if="shopSureBillInfo.plateNo">{{shopSureBillInfo.plateNo}}</text>
					</view>
				</view>
				<view class="fs24 light-gray mg-top-14">{{shopSureBillInfo.vehicleConfig}}</view>
			</view>
			<view class="u-f u-f-jcfs u-f-aic">
				<text class="gray-right-arrow"></text>
			</view>
		</view>

		<!-- <view class="mg-top-30">
			<common-title :msg="预约"></common-title>
		</view>

		<view class="mg-bot-20 white-bg-wrap">
			<view class="fs28 light-black">到店预约</view>
			<view>
				<text class="fs26 light-gray">预约服务时间</text>
				<text class="gray-right-arrow"></text>
			</view>
		</view>


		<view class="mg-top-30">
			<common-title :msg="备注"></common-title>
		</view>

		<view class="mg-top-20 white-bg-wrap">
			<textarea placeholder-style="color:#999999;font-size: 26rpx;" placeholder="请输入..." auto-height />
		</view> -->

		<view class="mg-top-20 white-bg-wrap u-f u-f-jcsb u-f-aic fs28 light-black">
			<view>支付方式</view>
			<view class="u-f u-f-jcfs" style="margin-top: 40rpx;margin-bottom: 40rpx;">
				<text class="wx-apply mg-right-6"></text>
				<text>{{shopSureBillInfo.trading ? '微信支付' : '-'}}</text>
			</view>
		</view>

		<view class="fix-bot">
			<view class="content u-f u-f-jcsb u-f-aic">
				<view class="fs24 light-gray fw400">
					<text>共 {{goodsAccount}} 件</text>
				</view>
				<view>
					<view class="u-f u-f-aic u-f-fdc">
						<view class="u-f u-f-aic u-f-jcfs">
							<text class="fs24 light-black mg-right-6">合计</text>
							<view class="main-color">
								<text class="fs24">￥</text>
								<text class="fs32 fw600">{{youhuiInfo.orderPrice ? youhuiInfo.orderPrice : 0}}</text>
							</view>
						</view>
					</view>
				</view>
				<view class="line-gradient-btn" @click="handlePay">
					立即购买
				</view>
			</view>
		</view>

		<uni-popup :position="'bottom'" class="updatePopup" :show="showModal" @click="handleHideGoodQuan">
			<view class="share-modal">
				<image class="gray-close" src="../.././static/common/gray_close.png" :mode="'widthFix'"
					@click="handleHideGoodQuan"></image>
				<view class="u-f u-f-aic u-f-jcc one-line-ellipsis mg-bot-32">
					<slot name="share-modal-title">商品券</slot>
				</view>

				<view class="mg-bot-32">
					<scroll-view scroll-y="true" style="height: 400rpx;">
						<view class="u-f u-f-jcfs u-f-aifs mg-bot-20" v-for="item in shopSureBillInfo.couponList"
							:key="item.id">
							<view class="mg-right-10">
								<image style="width: 156rpx;height:156rpx" :src="item.couponIcon"></image>
							</view>
							<view style="flex: calc(100% - 156rpx)">
								<view class="u-f u-f-jcsb u-f-aic">
									<view class="u-f u-f-fdc aifs"
										@click="goDetail('/pagesF/coupon-detail/coupon-detail')">
										<view class="u-f"><text
												class="fs28 light-black mg-bot-24">{{item.couponName}}</text></view>
										<text class="fs24 light-gray mg-bot-30">仅用于商城指定产品</text>
										<!-- <view class="u-f u-f-jcfs u-f-aifs">
											<text class="fs24 light-gray mg-right-12">所需等级</text>
											<text class="fs26 deep-gray">{{item.userType}}</text>
										</view> -->
									</view>
									<view class="u-f u-f-aic" @click="selectYouHuiQuan(item)">
										<text class="small-gradient-btn">去使用</text>
									</view>
								</view>
								<view class="light-gray fs24 mg-top-10" style="text-align: right;">
									{{item.deadline}}(含)前使用</view>
							</view>
						</view>
					</scroll-view>
				</view>
			</view>
		</uni-popup>

	</view>
</template>

<script>
	import commonTitle from "../.././components/common-title/common-title.vue"
	import uniPopup from "../../components/uni-popup/uni-popup.vue"
	import uniNumber from "../.././components/uni-number-box/uni-number-box.vue"
	export default {
		components: {
			commonTitle,
			uniPopup,
			uniNumber
		},
		data() {
			return {
				id: 0,
				show: false,
				billId: 0,
				shopSureBillInfo: {},
				youhuiquan: {},
				youhuiInfo: {},
				showModal: false,
				storeId: 0,
				orderId: 0,
				discountAmount: 0,
				orderPrice: 0
			}
		},
		onLoad(option) {
			this.billId = option.id

			if (!option.isShopCarSettle) {
				this.orderId = option.orderId
				this.storeId = option.storeId
			}
			this.shoppCartOrderDetails()
		},
		computed: {
			goodsAccount() {
				return this.shopSureBillInfo.orderInfoList.reduce((num, item) => num += item.goodsNum == null ? 0 : item
					.goodsNum, 0)
			}
		},
		methods: {
			// async editShopCarNum(value) {
			// 	let data = {
			// 	  "shopCarId": value.shopCarId,
			// 	  "goodsId": value.goodsId,
			// 	  "goodsNum": value.goodsNum
			// 	}

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

			// 	this.shoppCartOrderDetails()
			// },
			async shoppCartOrderDetails() {
				let res = await this.$http.get('/api/client/shopping/shoppCartOrderDetails?key=' + this.billId)
				let resData = res.data.data
				console.log("resData", resData)
				this.shopSureBillInfo = resData || {}
				this.youhuiquan = JSON.stringify(this.shopSureBillInfo) !== '{}' && this.shopSureBillInfo.couponLis ?
					this.shopSureBillInfo.couponList[0] : {}
				this.handleDiscountAmount()
			},
			async handleDiscountAmount() {
				let selectList = this.shopSureBillInfo.orderInfoList
				let shopIdList = selectList.map(item => item.shopCarId)
				let data = {
					shopCarIdList: shopIdList,
				}

				if (this.youhuiquan && this.youhuiquan.id) {
					data = Object.assign(data, {
						couponId: this.youhuiquan.id
					})
				}

				let res = await this.$http.post('/api/client/shopping/calculateDiscountAmount', data, {
					token: true,
					checkToekn: true
				})
				let resData = res.data.data
				this.youhuiInfo = resData
				console.log('this.youhuiInfo', this.youhuiInfo)
				this.discountAmount = resData.discountAmount || 0
				this.orderPrice = resData.orderPrice || 0
			},
			async placeOrder() {
				let shopCarIdList = this.shopSureBillInfo.orderInfoList.map(item => item.shopCarId)
				// 下单
				let data = {
					shopCarIdList: shopCarIdList,
				}

				if (this.shopSureBillInfo.couponList.length > 0) {
					data = Object.assign(data, {
						couponId: this.youhuiquan.id
					})
				}

				let res = await this.$http.post('/api/client/shopping/shopCartOrder', 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)
					}
				})
			},
			handlePay() {
				this.placeOrder()
			},
			enterMyGarage() {
				uni.navigateTo({
					url: "/pagesA/my-garage/my-garage"
				})
			},
			handleShowGoodQuan() {
				this.showModal = true;
			},
			handleHideGoodQuan() {
				this.showModal = false;
			},
			jumpToShopDetail() {
				this.User.navigate({
					url: '/pagesC/shop-detail/shop-detail?id=' + this.storeId
				})
			},
			openCity(data) {
				uni.openLocation({
					// 
					latitude: Number(data.lat),
					longitude: Number(data.lon),
					name: '地点',
					address: data.address,
					success: function(res) {
						console.log('打开系统位置地图成功')
					},
					fail: function(error) {
						console.log(error)
					}
				})
			},
			selectYouHuiQuan(item) {
				this.handleHideGoodQuan()
				this.youhuiquan = item
				this.handleDiscountAmount()
			},
			// handleChangeNum(value) {
			// 	console.log("value", value)
			// 	let { shopCarId, goodsId, goodsNum } = value;
			// 	this.editShopCarNum(value)
			// }
		}
	}
</script>

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

<style lang="scss" scoped>
	.gray-down-arrow {
		width: 16rpx;
		height: 16rpx;
		// background: url(../.././static/shop/bot-arrow.png) no-repeat center center;
		background-size: 100%;
	}

	.gray-right-arrow {
		width: 16rpx;
		height: 16rpx;
		// background: url(../.././static/right-arrow.png) no-repeat center center;
		background-size: 100%;
	}

	.check {
		width: 36rpx;
		height: 36rpx;
		background-color: red;
		// background: url(../.././static/common/check.png) no-repeat center center;
		background-size: 80%;
	}

	.province {
		padding: 12rpx 20rpx;
		color: #ffffff;
		background-color: #20CF55;
		border-radius: 8rpx 0rpx 0rpx 8rpx;
		border: 1rpx solid #0087EF;
	}

	.car-num {
		padding: 12rpx 20rpx;
		color: #20CF55;
		background: #E5EEF5;
		border-radius: 0 8rpx 8rpx 0;
		border: 1rpx solid #20CF55;
	}

	.fix-bot {
		width: 100%;
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 333;

		.content {
			background-color: #ffffff;
			padding: 10rpx 24rpx;
		}
	}

	.line-gradient-btn {
		background: linear-gradient(90deg, #FF4502 0%, #FF8903 100%);
		border-radius: 41rpx 41rpx 41rpx 41rpx;
		text-align: center;
		padding: 22rpx 120rpx;
		color: #ffffff;
	}

	.wx-apply {
		width: 44rpx;
		height: 44rpx;
		background: url(../.././static/common/wx_pay.png) no-repeat center center;
		background-size: 100%;
	}

	.small-gray-right-arrow {
		width: 16rpx;
		height: 16rpx;
		// background: url(../.././static/common/gray_right_arrow.png) no-repeat center center;
		background-size: 100%;
	}

	.share-modal {
		position: relative;
		padding: 24rpx;

		.gray-close {
			position: absolute;
			right: 28rpx;
			top: 28rpx;
			width: 42rpx;
			height: 42rpx;
			// background: url(../.././static/common/gray-close.png) no-repeat center center;
			background-size: 100%;
		}

		.content-wrap {
			width: 100%;

			.inner {
				padding: 12rpx;
				border-radius: 10rpx;
				background-color: #ffffff;
				border: 10rpx solid #FF4502;
			}
		}
	}

	.send-icon {
		width: 56rpx;
		height: 56rpx;
		background: url(../.././static/common/send.png) no-repeat center center;
		background-size: 100%;
	}

	.updatePopup :deep(.uni-popup-bottom) {
		border-radius: 40rpx 40rpx 0 0;
	}
</style>