<template>
	<view class="container" :style="[theme]">
		<view class="card">
			<view class="info-wrap flex-colum">
				<image class="cover" :src="list.store_avatar" mode="aspectFill"></image>
				<text class="t">{{list.store_nickname}}</text>
			</view>
			<view class="input-wrap">
				<view class="input-cont">
					<text class="i">￥</text>
					<input class="num" type="text" v-model="amount" :readonly="true" @input="checkNum($event)" />
				</view>
				<!-- <view class="tips"><input class="input" type="text" :maxlength="10" placeholder="添加付款备注（最多10个字）" v-model="remark" /></view> -->
			</view>
		</view>
		<view class="card">
			<view class="title"><text>支付方式</text></view>
			<view class="pay-list">
				<view class="item flex-between">
					<view class="label">
						<image class="icon" src="@/static/ye.png" mode="aspectFit"></image>
						<view class="t flex-colum-start">
							<text class="t_one">会员卡支付</text>
							<text class="t_tow" style="color: #5B8FF9;">当前余额：{{list.user_balance}}</text>
						</view>
					</view>
					<u-icon name="checkmark-circle-fill" size="18" color="#54A347" v-if="pay_type==2"></u-icon>
					<view @click="pay_typeclick(2)"
						style="border-radius: 50%; width:32rpx;height:32rpx;border: 1rpx solid #ccc;" v-else></view>
				</view>
			</view>
			<view class="pay-list">
				<view class="item flex-between">
					<view class="label">
						<image class="icon" src="@/static/4.png" mode="aspectFit"></image>
						<view class="t flex-colum-start">
							<text class="t_one">微信支付</text>
							<text class="t_tow">使用微信支付</text>
						</view>
					</view>
					<u-icon name="checkmark-circle-fill" size="20" color="#54A347" v-if="pay_type==1"></u-icon>
					<view @click="pay_typeclick(1)"
						style="border-radius: 50%; width:32rpx;height:32rpx;border: 1rpx solid #ccc;" v-else></view>
				</view>
			</view>
		</view>
		<view class="btn-wrap">
			<view class="btn" @click="showpopupclick"><text>确认支付</text></view>
		</view>
		<u-popup :show="showpopup" @close="closepopup" @open="openpopup" mode="center" :round="10">
			<view class="u-popupflex-colum flex-colum">
				<view class="u-popupflex-columview">
					请输入支付密码
				</view>
				<u-code-input v-model="ucodeinputvalue" :maxlength="6" dot :focus="true"
					@finish="finish"></u-code-input>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showpopup: false,
				ucodeinputvalue: '',
				amount: "",
				pay_type: 1,
				list: ''
			};
		},
		async onLoad(options) {
			this.reservationpaythebill()
		},
		computed: {
			theme() {
				return this.$store.getters.theme
			},
		},
		methods: {
			// 方法
			checkNum(e) {
				let val = e.target.value.replace(/(^\s*)|(\s*$)/g, "")
				console.log(val);
				if (!val) {
					this.amount = '';
					return
				}
				var reg = /[^\d.]/g

				// 只能是数字和小数点，不能是其他输入
				val = val.replace(reg, "")
				// // 保证第一位只能是数字，不能是点
				val = val.replace(/^\./g, "");
				// // 小数只能出现1位
				val = val.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
				// // 小数点后面保留2位
				val = val.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');
				console.log(val);
				this.$nextTick(() => {
					this.amount = val;
				})
			},
			pay_typeclick(e) {
				this.pay_type = e
			},
			async reservationpaythebill() {
				let res = await this.api.reservationpaythebill({
					store_id: uni.cache.get('store_id') // 判断显示哪家的作品
				})
				this.list = res
			},
			openpopup() {
				console.log('open');
			},
			closepopup() {
				this.ucodeinputvalue = ''
				this.showpopup = false
			},
			finish(e) {
				this.showpopup = false
				this.reservationmakenowsub()
			},
			async showpopupclick() {
				if (this.amount == null || this.amount == '') {
					uni.showToast({
						title: '支付金额不能为0',
						icon: 'none'
					});
					return false;
				}
				if (this.pay_type == 1) {
					this.reservationmakenowsub()
				}
				if (this.pay_type == 2) {
					let res = await this.api.useruserinfo() //p判断是否完成手机号
					uni.cache.set('loginuser', res);
					if (res.userinfo.mobile) {
						if (this.list.user_sec_password == 0) {
							uni.showModal({
								title: '提示',
								content: '您目前没有设置密码，请先设置支付密码',
								success: function(res) {
									if (res.confirm) {
										uni.redirectTo({
											url: '/pages/my/setup/repairpassword'
										});
									} else if (res.cancel) {}
								}
							});
						} else {
							this.showpopup = true
						}
					} else {
						uni.showModal({
							title: '提示',
							content: '确认订单需要获取您的手机号',
							success: function(res) {
								if (res.confirm) {
									uni.redirectTo({
										url: '/pages/my/setup/index'
									});
								} else if (res.cancel) {}
							}
						});
					}
				}
			},
			async reservationmakenowsub() {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				try {
					var res = await this.api.reservationpaythebillsub({
						store_id: uni.cache.get('store_id'), // 判断显示哪家的作品
						amount: this.amount,
						pay_type: this.pay_type,
						sec_password: this.ucodeinputvalue
					})
					this.ucodeinputvalue = ''
					if (res.pay_status == 0) {
						// #ifdef MP-WEIXIN
						uni.requestPayment({
							provider: 'wxpay', //支付类型-固定值
							timeStamp: res.pay_data.payTimeStamp, // 时间戳（单位：秒）
							nonceStr: res.pay_data.paynonceStr, // 随机字符串
							package: res.pay_data.payPackage, // 固定值
							signType: res.pay_data.paySignType, //固定值
							paySign: res.pay_data.paySign, //签名
							success: (res) => {
								uni.hideLoading()
								uni.showToast({
									title: "支付成功"
								})
								setTimeout(res => {
									uni.navigateBack()
								}, 1500)
							},
							fail: (err) => {
								setTimeout(res => {
									uni.showToast({
										icon: 'none',
										title: '支付失败'
									})
									uni.hideLoading()
								}, 2000)

							}
						});
						// #endif	
					} else {
						uni.hideLoading()
						uni.showToast({
							title: "支付成功"
						})
						setTimeout(res => {
							uni.navigateBack()
						}, 1500)
					}


				} catch (e) {

				}
			},
		}
	};
</script>

<style scoped lang="scss">
	page {
		background: #f8f7f7;
	}

	.u-popupflex-colum {
		padding: 80rpx 40rpx;
		border-radius: 50rpx;
		background: #FFFFFF;

		.u-popupflex-columview {
			font-family: SourceHanSansCN-Bold-, SourceHanSansCN-Bold;
			font-weight: normal;
			color: #000;
			font-weight: bold;
			margin-bottom: 50rpx;
		}
	}

	.btn-wrap {
		width: 100%;
		position: fixed;
		bottom: 50rpx;
		left: 0;
		padding: 28upx 28upx calc(env(safe-area-inset-bottom) + 28upx);

		.btn {
			padding: 20upx 0;
			background: var(--bg-color-button);
			border-radius: 34rpx;
			font-size: 32rpx;
			font-family: PingFang SC-Bold, PingFang SC;
			font-weight: bold;
			color: #FFFFFF;
			display: flex;
			justify-content: center;
		}
	}

	.container {
		padding: 28upx;
		padding-bottom: 640upx;
	}

	.card {
		padding: 28upx;
		border-radius: 28upx;
		background-color: #fff;
		margin-bottom: 28upx;

		.title {
			font-size: 32upx;
		}
	}

	.info-wrap {
		display: flex;
		align-items: center;

		.cover {
			$size: 80upx;
			width: $size;
			height: $size;
			border-radius: 50%;
			margin-right: 20upx;
			background-color: #efefef;
		}

		.t {
			font-size: 32upx;
		}
	}

	.input-wrap {
		padding: 28upx 0 0;

		.input-cont {
			display: flex;
			align-items: center;
			border-bottom: 1upx solid #ececec;

			.i {
				font-size: 42upx;
				font-weight: bold;
				margin-right: 12upx;
				position: relative;
				top: -4upx;
			}

			.num {
				font-size: 48upx;
				height: 80upx;
				font-weight: bold;
				position: relative;
				padding-right: 14upx;
				overflow: hidden;
				white-space: nowrap;
				text-overflow: ellipsis;
			}
		}

		.tips {
			display: flex;
			padding-top: 24upx;

			.input {
				flex: 1;
			}
		}
	}

	.pay-list {
		.item {
			padding: 28upx 0;

			.label {
				display: flex;
				align-items: center;

				.icon {
					$size: 42upx;
					width: $size;
					height: $size;
					margin-right: 20upx;
				}

				.t {
					.t_one {
						font-size: 28rpx;
						font-family: SourceHanSansCN-Regular, SourceHanSansCN;
						font-weight: 400;
						color: #333333;
					}

					.t_tow {
						font-size: 24rpx;
						font-family: SourceHanSansCN-Regular, SourceHanSansCN;
						font-weight: 400;
						color: #999999;
					}
				}
			}
		}
	}

	.num-wrap {
		display: flex;
		align-items: center;
		justify-content: flex-end;

		.t {
			font-size: 32upx;
		}

		.b {
			font-weight: bold;
		}
	}

	.number-key-wrap {
		width: 100%;
		position: fixed;
		left: 0;
		bottom: 0;
		background-color: #efefef;
		z-index: 999;
		padding: 28upx 28upx calc(env(safe-area-inset-bottom) + 28upx);
		border-radius: 28upx 28upx 0 0;
		$gap: 12upx;
		$radius: 14upx;
		$itemH: 120upx;

		.number-key {
			display: flex;

			.item {
				display: flex;
				font-size: 48upx;
				font-weight: bold;
				align-items: center;
				justify-content: center;
				border-radius: $radius;
				background-color: #fff;

				&.active {
					background-color: #d9d9d9;
				}
			}

			.left {
				flex: 1;

				.num-list {
					display: grid;
					grid-template-columns: 1fr 1fr 1fr;
					grid-template-rows: $itemH $itemH $itemH;
					grid-gap: $gap;
				}

				.num-foot {
					padding-top: $gap;
					display: grid;
					grid-gap: $gap;
					grid-template-columns: 2fr 1fr;
					grid-template-rows: $itemH;
				}
			}

			.right {
				width: 160upx;
				display: flex;
				flex-direction: column;
				margin-left: $gap;

				.item {
					flex: 1;

					&:last-child {
						margin-top: $gap;
						font-size: 32upx;
					}

					&.confirm {
						background-color: #ffcc17;

						&.active {
							background-color: #e1b516;
						}
					}
				}
			}
		}
	}

	.success-wrap {
		padding: 0 28upx calc(env(safe-area-inset-bottom) + 28upx);

		.title {
			padding: 28upx 0;
			font-size: 32upx;
			font-weight: bold;
			display: flex;
			justify-content: center;
		}

		.content {
			.img {
				width: 100%;
				height: auto;
			}
		}

		.btn {
			padding: 12upx 0;
			border-radius: 100upx;
			font-size: 28upx;
			background: #ffcc17;
			color: #000;
		}
	}
</style>