<template>
	<view class="mg-lr-24" style="padding-bottom: 110rpx;">
		<view class="white-bg-wrap">
			<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" style="width: calc(100% - 28rpx);">
					<view class="mg-right-12" style="width: 133rpx;">
						<image class="border-radius-9" style="width: 134rpx;height: 134rpx;" :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 class="fs28 fw400 light-black">{{goodsInfo.storeName}}</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 style="width: 56rpx;height:56rpx;" class="send-icon" :mode="'widthFix'"
									src="../.././static/common/send.png"></image>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>

		<div class="goodsInfoBox">
			<image class="goodsInfoBoxImage" :src="goodsInfo.goodsImage" mode=""></image>
			<div class="goodsInfo">
				<div class="name">
					{{goodsInfo.goodsName}}
				</div>
				<div class="spce">
					{{goodsInfo.goodsSpecification}}
				</div>

				<div class="priceBox">
					<div class="price">
						￥{{goodsInfo.unitPrice}}
					</div>
					<div class="num">
						×{{goodsInfo.goodsNum}}
					</div>
				</div>
			</div>
		</div>

		<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>￥{{goodsInfo.orderAmount}}</text>
			</view>

			<view class="u-f u-f-jcsb light-black fs28 fw400 mg-bot-42">
				<view>优惠券</view>
				<view v-if="goodsInfo.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" class="small-gray-right-arrow"
						:mode="'widthFix'" />
				</view>
				<view v-else class="u-f u-f-jcfs u-f-aic">
					<text class="mg-right-6">暂无可用优惠</text>
				</view>
			</view>
			<view class="u-f u-f-aic u-f-jcsb light-black fs28 fw400 mg-bot-40"
				v-if="goodsInfo.memberDiscountAmount!=0">
				<text></text>
				<text class="main-color" style="font-size: 24rpx;">
					<text style="color: #000;">会员优惠</text>￥{{goodsInfo.memberDiscountAmount}}</text>
			</view>
			<view class="u-f u-f-aic u-f-jcsb light-black fs28 fw400 mg-bot-40">
				<text></text>
				<text class="main-color" style="font-size: 24rpx;">
					<text style="color: #000;">总优惠</text>￥{{youhuiInfo.discountAmount}}</text>
			</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>￥{{youhuiInfo.couponDiscountAmount}}</text>
				</view>
				<view class="light-black">合计</view>
				<view class="main-color mg-right-6">
					<text class="fs24">￥</text>
					<text class="fw600 fs32">{{youhuiInfo.discountedAmount}}</text>
				</view>
			</view> -->
		</view>

		<view class="white-bg-wrap mg-top-20 u-f u-f-jcsb u-f-aic" @click="enterMyGarage">
			<view class="u-f u-f-fdc mg-right-64" style="flex: 3" v-if="carInfo.brandLogo!=null">
				<view class="u-f u-f-jcsb u-f-aic">
					<view class="u-f u-f-jcfs u-f-aic">
						<image v-if="carInfo.brandLogo!=null" :src="carInfo.brandLogo"
							style="width: 56rpx;height: 56rpx;" :mode="'widthFix'" class="mg-right-10"></image>
						<text class="fw400 fs30 light-black">{{carInfo.vehicleBrand}}{{carInfo.vehicleModel}}</text>
					</view>
					<view class="u-f u-f-aifs u-f-jcfs fs24">
						<text
							:class="[carInfo.carType == 1 ? 'blue-province' : 'green-province']">{{carInfo.plateNo?carInfo.plateNo[0]:''}}</text>
						<text
							:class="[carInfo.carType == 1 ? 'blue-car-num' : 'green-car-num']">{{carInfo.plateNo?carInfo.plateNo.slice(1):''}}</text>
					</view>
				</view>
				<view class="fs24 light-gray mg-top-14">{{carInfo.vehicleConfig}}</view>
			</view>
			<text class="u-f-1 fs28" v-if="carInfo.brandLogo==null">添加爱车</text>
			<view class="u-f u-f-jcfs u-f-aic">
				<image src="../.././static/common/sg_right_arrow.png" class="small-gray-right-arrow"
					:mode="'widthFix'" />
			</view>
		</view>

		<view v-if="goodsInfo.isReservation!=2" class="mg-top-30">
			<view class="mg-bot-20">
				<common-title :msg="'预约'"></common-title>
			</view>

			<view class="mg-bot-20 white-bg-wrap u-f u-f-jcsb u-f-aic" @click="showSelectTimeModal">
				<view class="fs28 light-black">到店预约</view>
				<view class="u-f u-f-aic u-f-jcfs">
					<text class="fs26 light-gray mg-right-10">{{reservationTime}}</text>
					<image src="../.././static/common/sg_right_arrow.png" class="small-gray-right-arrow"
						:mode="'widthFix'" />
				</view>
			</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;" v-model="remark" 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: 20rpx;margin-bottom: 20rpx;">
				<text class="wx-apply mg-right-6"></text>
				<text>微信支付</text>
			</view>
		</view> -->

		<view class="fix-bot">
			<view class="content u-f u-f-jcsb u-f-aic">
				<view class="fs22 light-gray fw400 u-f" v-if="false">
					<text class="mg-right-6">总优惠</text>
					<view class="main-color">
						<text>￥</text>
						<text>{{youhuiInfo.discountAmount ? youhuiInfo.discountAmount : 0}}</text>
					</view>
				</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.discountedAmount}}</text>
							</view>
						</view>
					</view>
				</view>
				<view class="line-gradient-btn" @click="$debounce(handlePay)">
					立即支付
				</view>
			</view>
		</view>

		<uni-popup :position="'bottom'" :show="show" @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 goodsInfo.couponList"
							:key="item.id">
							<view class="mg-right-10" style="flex: 1">
								<image style="width: 156rpx;height:156rpx" :src="item.couponIcon"></image>
							</view>
							<view style="flex: 3">
								<view class="u-f u-f-jcsb u-f-aic">
									<view class="u-f u-f-fdc" @click="goDetail('/pagesF/coupon-detail/coupon-detail')">
										<text class="fs28 light-black mg-bot-24">{{item.couponName}}</text>
										<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>

		<uni-popup :position="'bottom'" class="updatePopup" :show="showSelectTime" @hidePopup="hideSelectTimeModal">
			<view class="tire-modal">
				<image class="gray-close" src="../.././static/common/gray_close.png" :mode="'widthFix'"
					@click="hideSelectTimeModal"></image>
				<view class="u-f u-f-aic u-f-jcc mg-bot-24">
					<text class="fs32 fw600 light-blac">到店预约</text>
				</view>
				<view>
					<view class="mg-bot-20 u-f u-f-jcfs">
						<common-title :msg="'2024年8月'"></common-title>
					</view>
					<view class="cards mg-bot-36 u-f u-f-jcfs u-f-aic u-f-fwnw">
						<view @click="selectDate(item, index)" :class="['list-card', active == index ? 'active' : '']"
							v-for="(item, index) in selectTimeInfo" :key="index">
							<text class="mg-bot-10">{{item.week}}</text>
							<text>{{item.date}}</text>
						</view>
					</view>
					<view class="mg-bot-20 u-f u-f-jcfs">
						<common-title :msg="'到店时间'"></common-title>
					</view>
					<view class="cards mg-bot-36 u-f u-f-jcfs u-f-aic u-f-fww">
						<view v-for="(innerItem, idx) in selectTimeInfo[active].appointmentPeriodList" :key="idx"
							:class="['list-card2 u-f u-f-fdc', active2 === idx ? 'selected-card' : '', innerItem.status === 3 ? 'disabled-card' : '']"
							@click="selectTimeSlot(innerItem, idx)">
							<text class="mg-bot-10">{{innerItem.timeSlot}}</text>
							<text>{{innerItem.status === 2 ? '可预约' : '已过期'}}</text>
						</view>
					</view>

					<view class="u-f u-f-jcc u-f-aic" style="width: 100%;">
						<text style="width: calc(100% - 48rpx);text-align: center;" class="line-gradient-btn"
							@click="confirmSelectTime">确认</text>
					</view>
				</view>
			</view>
		</uni-popup>

	</view>
</template>

<script>
	var app = getApp()
	import commonTitle from "../.././components/common-title/common-title.vue"
	import uniPopup from "../../components/uni-popup/uni-popup.vue"
	export default {
		components: {
			commonTitle,
			uniPopup
		},
		data() {
			return {
				goodsInfo: {},
				show: false,
				reservationTime: '预约服务时间',
				remark: '',
				couponId: 0,
				pointsId: 0,
				youhuiquan: {},
				youhuiInfo: {},
				orderId: 0,
				carInfo: '',
				showSelectTime: false,
				selectTimeInfo: '',
				active: 0,
				active2: 0,
				date: '',
				timeSlot: '',
			}
		},

		onLoad(options) {
			this.goodsInfo = JSON.parse(decodeURIComponent(options.data))
			this.youhuiquan = this.goodsInfo.couponList.length > 0 ? this.goodsInfo.couponList[0] : ''
			this.discountedAmount()
			this.getCarInfo()
			this.getPeriod()
		},

		onShow() {
			console.log(app);
			this.getCarInfo()
		},

		methods: {
			async getPeriod() {
				let res = await this.$http.get('/api/client/order/appointmentPeriod')

				let selectTimeInfo = res.data.data
				this.selectTimeInfo = selectTimeInfo || {}
				this.date = selectTimeInfo[this.active] || ''
				this.timeSlot = selectTimeInfo[this.active].appointmentPeriodList[this.acitve2] || ''
			},
			async getCarInfo() {
				let res = await this.$http.get('/api/client/index/carInfo')
				let carInfo = res.data.data
				if (carInfo && carInfo.id) {
					uni.setStorageSync('cartId', this.carInfo.id)
				}
				this.carInfo = carInfo || {}
			},
			async placeOrder() {
				// 下单
				let data = {
					storeId: this.goodsInfo.storeId,
					type: 1,
					userCarId: this.carInfo.id,
					goodsId: this.goodsInfo.goodsId,
					specificationsId: this.goodsInfo.goodsSpecificationId,
					goodsNum: this.goodsInfo.goodsNum
				}

				if (this.remark) {
					data = Object.assign(data, {
						remark: this.remark
					})
				}

				if (this.reservationTime !== '预约服务时间') {
					data = Object.assign(data, {
						reservationTime: this.reservationTime
					})
				}

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

				uni.showLoading({
					title: '正在下单...',
					mask: true
				})

				let res = await this.$http.post('/api/client/order/placeOrder', data)

				uni.hideLoading()

				if (res.data.code === 200) {
					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
				if (res.data.code == 2001) {
					uni.redirectTo({
						url: "/pagesE/pay-success/pay-success"
					})
				} else {
					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)
					}
				})
			},
			async payNotice() {
				let data = {
					"storeId": this.goodsInfo.storeId,
				}

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

				if (res.data.code === 200) {
					uni.redirectTo({
						url: "/pagesE/pay-success/pay-success"
					})
				}
			},

			async discountedAmount() {
				let data = {
					goodsNum: this.goodsInfo.goodsNum,
					goodsId: this.goodsInfo.goodsId,
					specificationsId: this.goodsInfo.goodsSpecificationId
				}

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

				let res = await this.$http.post('/api/client/order/discountedAmount', data)
				let resData = res.data.data
				this.youhuiInfo = resData || {}
			},
			handlePay() {
				this.placeOrder()
			},
			enterMyGarage() {
				uni.navigateTo({
					url: "/pagesA/my-garage/my-garage"
				})
			},
			handleShowGoodQuan() {
				console.log("handleShowGoodQuan")
				this.show = true;
			},
			handleHideGoodQuan() {
				this.show = false;
			},
			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)
					}
				})
			},
			selectYouHuiQuan(item) {
				this.handleHideGoodQuan()
				this.youhuiquan = item
				this.discountedAmount()
			},
			hideSelectTimeModal() {
				this.showSelectTime = false
			},
			showSelectTimeModal() {
				this.showSelectTime = true
			},
			selectDate(item, index) {
				this.active = index
				this.date = item
				this.timeSlot = item.appointmentPeriodList[0]
				this.active2 = 0
			},
			selectTimeSlot(innerItem, idx) {
				this.timeSlot = innerItem
				this.active2 = idx
				console.log('this.active2', this.active2)
				console.log('idx', idx)
			},
			confirmSelectTime() {
				if (this.timeSlot.status === 3) {
					uni.showToast({
						title: `您选择的时间段${this.date.date} ${this.timeSlot.timeSlot}已过期，请重新选择`,
						icon: 'none'
					})
				} else {
					this.hideSelectTimeModal()
					this.reservationTime = this.date.date + ' ' + this.timeSlot.timeSlot
				}
			}
		}
	}
</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%;
	}

	.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: 13rpx;
		height: 26rpx;
	}

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

	.goodsInfoBox {
		margin-top: 20rpx;
		padding: 20rpx;
		width: 702rpx;
		height: 178rpx;
		background: #FFFFFF;
		border-radius: 18rpx 18rpx 18rpx 18rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;

		.goodsInfoBoxImage {
			width: 128rpx;
			height: 128rpx;
			background: #FFFFFF;
			border-radius: 9rpx 9rpx 9rpx 9rpx;
			margin-right: 10rpx;
		}

		.goodsInfo {
			flex: 1;

			.name {
				font-size: 28rpx;
				color: #333333;
				margin-bottom: 10rpx;
			}

			.spce {
				font-size: 24rpx;
				color: #999999;
				margin-bottom: 10rpx;
			}

			.priceBox {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.price {
					font-weight: bold;
					font-size: 32rpx;
					color: #FF4502;
				}

				.num {
					font-size: 24rpx;
					color: #999999;
				}
			}
		}
	}

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

	.tire-modal {
		position: relative;
		padding: 34rpx 24rpx 24rpx 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%;
		}
	}

	.cards {
		.list-card {
			padding-top: 30rpx;
			padding-bottom: 30rpx;
			width: calc(100% / 7 - 10rpx);
			background-color: #ffffff;
			border-radius: 8rpx;
			border: 1rpx solid #999999;
			margin-right: 10rpx;
			font-size: 24rpx;

			&:nth-child(7n) {
				margin-right: 0;
			}

			&.active {
				border: 1rpx solid #FF4502;
			}
		}

		.list-card2 {
			width: calc(100% / 3 - 15rpx);
			padding-top: 17rpx;
			padding-bottom: 17rpx;
			background: #ffffff;
			border-radius: 9rpx;
			border: 1rpx solid #999999;
			margin-right: 10rpx;
			margin-bottom: 17rpx;
			font-size: 24rpx;
			color: #333333;

			&:nth-child(3n) {
				margin-right: 15;
			}

			&.selected-card {
				border-color: #FF4502;
			}

			&.disabled-card {
				cursor: not-allowed;
				color: #999999;
			}
		}
	}
</style>