<template>
	<view class="login-main">
		<view style="width: 100%">
			<view style="width: 100%; height: 270rpx">
				<image class="img" src="../../static/images/payBack.png" style="width: 100%; height: 100%"></image>
			</view>
		</view>
		<view class="main">
			<view class="login-main-content">
				<image class="img" :src="shopInfo.shopLogoUrl
					? shopInfo.shopLogoUrl
					: '/static/images/error.jpeg'
					" mode=""></image>
				<h4 class="shopName">
					{{ shopInfo ? shopInfo.shopName : "" }}
				</h4>
			</view>

			<view class="login-contont">
				<view class="uInput">
					<u-input placeholder="请输入金额" border="bottom" :fontSize="40" type="digit"
						@change="validateInput($event, 1)" v-model="price" :customStyle="inpustyle">
						<span slot="prefix" style="font-size: 30px">￥</span>
					</u-input>
				</view>
				<view class="login-contont-button">
					<button class="btn" @click="Pay">立即支付</button>
					<view style="padding: 0px 30rpx">
						<view style="margin-top: 66rpx; background: #fff7f6">
							<u-cell-group :border="false">
								<u-cell title="代金券折扣" :value="couponList.length + '个券可使用'" :border="false" isLink
									@click="voucher"></u-cell>
							</u-cell-group>
						</view>

						<view style="margin-top: 20rpx; font-size: 26rpx">
							支付成功后，次日将有随机奖励红包以及店铺代金券，可抵现金花，AI天枢云小程序领取
						</view>
					</view>
				</view>
			</view>
		</view>


		<u-popup :show="showPopup" mode="bottom" :round="20">
			<view class="coupon-popup">
				<view class="popup-header">
					<text class="title">店铺代金券</text>
					<u-icon name="close" size="22" @click="showPopup = false" />
				</view>

				<view class="coupon-list">
					<view class="coupon-item" :class="item.status === 0 ? 'disabled' : 'active'"
						v-for="(item, index) in couponList" :key="index" @click="selectCoupon(index)">
						<view class="left">
							<view class="tag" :class="item.type === 1 ? 'independent' : 'chain'">
								{{ item.type === 1 ? '独立券' : '连锁券' }}
							</view>
							<view class="price">
								<text class="yen">¥</text>
								<text class="num">{{ item.price }}</text>
								<text class="tip">{{ item.desc }}</text>
							</view>
							<view class="info">
								<text>{{ item.time }}</text>
								<text>{{ item.scope }}</text>
							</view>
						</view>
						<view class="right">
							<u-radio :checked="selectIndex === index" disabled :custom-style="radioStyle" />
						</view>
					</view>
				</view>
			</view>
		</u-popup>

	</view>
</template>

<script>
import api from "@/api/index.js";
import { requestConfig } from "@/app.config.js";
export default {
	data() {
		return {
			showPopup: false,
			requestConfig: requestConfig,
			code: "",
			appid: "",
			value: "",
			userInfo: {},
			inpustyle: {
				fontWeight: "bold",
				fontSize: "40px",
			},
			price: null,
			shopId: "",
			shopInfo: {},
			localUserInfo: {},
			couponList: []
		};
	},
	onLoad(options) {
		this.shopId = options.shopId;
		localStorage.setItem("shopId", this.shopId);
		let info = localStorage.getItem("weChatUserInfo");
		this.localUserInfo = JSON.parse(info);
		if (this.localUserInfo) {
			this.getShopInfo();
		} else {
			this.getInfo();
			this.getVoucher();
		}
	},
	methods: {
		getVoucher() {
			api
				.byStore({
					storeId: '2018881434293329922',
				})
				.then((res) => {
					if (res.code == 200) {
						this.couponList = res.data;
					} else {
						uni.$u.toast(res.msg);
					}
				});
		},
		voucher() {
			this.showPopup = true;
		},
		getInfo() {
			let code = this.GetQueryString("code");
			localStorage.removeItem("token");
			localStorage.removeItem("weChatUserInfo");
			api
				.weChatH5Login({
					code: code,
				})
				.then((res) => {
					console.log(res);
					if (res.code == 200) {
						this.userInfo = res.data;
						localStorage.setItem("token", this.userInfo.token);
						localStorage.setItem(
							"weChatUserInfo",
							JSON.stringify(this.userInfo),
						);
						if (this.userInfo.bindPhoneStatus == 0) {
							this.showPhone = true;
						} else {
							this.showPhone = false;
						}
						this.getShopInfo();
					} else {
						uni.$u.toast("获取微信登录信息失败,请重新登录！");
					}
				});
		},
		async getShopInfo() {
			api
				.getStoreInfoByScan({
					shopId: this.shopId,
				})
				.then((res) => {
					if (res.code == 200) {
						this.shopInfo = res.data;
					} else {
						uni.$u.toast(res.msg);
					}
				});
		},
		async Pay() {
			uni.showLoading({
				title: "支付中",
			});
			if (!this.price) {
				uni.$u.toast("请输入支付金额！");
			}
			api
				.payOrder({
					shopType: this.shopInfo.shopType,
					shopUserId: this.shopInfo.shopUserId,
					shopId: this.shopInfo.shopId,
					payAmount: Number(this.price),
					payWay: 6,
					userId: this.localUserInfo.userId
				})
				.then((res) => {
					if (res.code == 200) {
						WeixinJSBridge.invoke(
							"getBrandWCPayRequest",
							{
								// 公众号名称，由商户传入
								appId: res.data.appId,
								// 时间戳，自1970年以来的秒数
								timeStamp: res.data.timeStamp,
								// 随机串
								nonceStr: res.data.nonceStr,
								package: res.data.package,
								// 微信签名方式：
								signType: res.data.signType,
								// 微信签名
								paySign: res.data.paySign,
							},
							function (res) {
								if (res.err_msg == "get_brand_wcpay_request:ok") {
									// 使用以上方式判断前端返回,
									// 微信团队郑重提示：
									uni.hideLoading();
									// window.location.href = requestConfig.redirectOpen;
									uni.navigateTo({
										url: `/pages/index/paySuccess`,
									});
									// res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
								} else {
									uni.hideLoading();
									uni.$u.toast("支付失败！");
								}
							},
						);
					} else {
						uni.$u.toast(res.msg);
					}
				}).catch((err) => {
					uni.hideLoading();
					let msg = err.msg || "";
					let errStr = msg.match(/[\u4e00-\u9fa5\/]+/)?.[0] || "支付失败";
					uni.$u.toast(errStr);
				});
		},

		GetQueryString(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
			var r = window.location.search.substr(1).match(reg);
			if (r != null) return unescape(r[2]);
			return null;
		},
		validateInput(e, num) {
			const inputTypeNum = /[^\d]/g; //数字
			// switch (num) {
			// 	case 1:
			// 		//要写nextTick 不然无效
			// 		this.$nextTick(() => {
			// 			this.price = e.replace(inputTypeNum, "");
			// 		});
			// 		break;
			// }
		},
	},
};
</script>

<style lang="scss">
.coupon-popup {
	padding: 30rpx;
	height: 1000rpx;
	box-sizing: border-box;
}

.popup-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	font-size: 32rpx;
	font-weight: bold;
	margin-bottom: 30rpx;
}

.coupon-list {
	height: calc(100% - 100rpx);
	overflow-y: auto;
}

.coupon-item {
	background: #fff;
	border-radius: 16rpx;
	padding: 24rpx;
	margin-bottom: 20rpx;
	display: flex;
	justify-content: space-between;
	align-items: center;
	border: 2rpx solid #eee;
}

.coupon-item.active {
	border-color: #ff4d4f;
	background: #fffbf4;
}

.coupon-item.disabled {
	opacity: 0.5;
}

.left {
	flex: 1;
}

.tag {
	font-size: 22rpx;
	color: #fff;
	padding: 4rpx 12rpx;
	border-radius: 6rpx;
	margin-bottom: 12rpx;
}

.independent {
	background: #5c8dff;
}

.chain {
	background: #ff7d00;
}

.price {
	display: flex;
	align-items: baseline;
	margin-bottom: 12rpx;
}

.yen {
	font-size: 28rpx;
	color: #ff4d4f;
}

.num {
	font-size: 44rpx;
	font-weight: bold;
	color: #ff4d4f;
	margin: 0 10rpx;
}

.tip {
	font-size: 24rpx;
	color: #999;
}

.info {
	font-size: 24rpx;
	color: #999;
	line-height: 1.6;
}

.right {
	padding-left: 20rpx;
}

::v-deep .u-cell__title {
	flex: none !important;
}

.main {
	width: 100%;
	background: white;
	height: calc(100% - 280rpx);
	border-radius: 40rpx;
}

.shopName {
	font-weight: bold;
	font-size: 36rpx;
	color: #333333;
}

/* 修改加载提示框的遮罩层 z-index */
.uni-loading__mask {
	z-index: 10076;
}

/* 修改加载提示框本身的 z-index */
.uni-loading {
	z-index: 10076;
}

.yzm {
	font-weight: 500;
	font-size: 28rpx;
	color: #00d36d;
}

.popup {
	height: 300px;
	padding: 20px;

	.footer {
		margin-top: 150rpx;

		.btn {
			width: 100%;
			background: linear-gradient(152deg, #6f9dfd 0%, #1b71f2 100%);
			border-radius: 46rpx;
			border: none;
		}
	}

	.main {
		margin-top: 20rpx;
	}

	.header {
		margin-bottom: 20rpx;
		text-align: center;

		.tip {
			margin-top: 20rpx;
			font-weight: 500;
			font-size: 26rpx;
			color: #ff8b2f;
		}
	}
}

.u-radio-group--row {
	justify-content: center;
}

.radioText {
	font-weight: 500;
	font-size: 24rpx;
	color: #999999;
	line-height: 34rpx;
	text-align: left;
	font-style: normal;
}

.login-main {
	background: url("../../static/images/payBack.png");
	background-size: 100% 100%;
	width: 100%;
	display: flex;
	flex-direction: column;
	/* justify-content: space-between; */
	align-items: center;
	height: 100vh;
}

.login-contont-button {
	text-align: center;

	.btn {
		margin-top: 20px;
		width: 95%;
		height: 90rpx;
		background: linear-gradient(152deg, #6f9dfd 0%, #1b71f2 100%);
		border-radius: 44rpx;
		color: #fff;
	}
}

.login-contont {
	width: 100%;
	height: 600rpx;
	margin-top: 100rpx;

	.uInput {
		padding: 10px 0px;
		border-bottom: 0.5px solid #97979740;
	}

	.login-contont-info {
		text-align: center;

		.text {
			margin-top: 10px;
			font-weight: 500;
			font-size: 28rpx;
			color: #999999;
			line-height: 40rpx;
			font-style: normal;
		}
	}
}

.radio {
	text-align: center;
	margin-top: 10px;
}

.login-main-content {
	display: flex;
	flex-direction: row;
	align-items: center;
	padding: 9px 26px;

	.img {
		// margin-top: 70px;
		margin-right: 20rpx;
		width: 44rpx;
		height: 44rpx;
		border-radius: 50%;
	}
}
</style>
