<template>
	<view class="container">
		<view class="card">
			<view class="info-wrap">
				<image class="cover" src="@/static/img_vipno_vip1-1.png" mode="aspectFill"></image>
				<text class="t">{{ type == 1 ? '设备商城' : '在线支付' }}</text>
			</view>
			<view class="input-wrap">
				<view class="input-cont">
					<text class="i">￥</text>
					<text class="num">{{ num }}</text>
				</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">
					<view class="label">
						<image class="icon" src="../../static/4.png" mode="aspectFit"></image>
						<text class="t">微信支付</text>
					</view>
					<uni-icons type="checkbox-filled" size="18" color="#54A347"></uni-icons>
				</view>
			</view>
			<view class="num-wrap">
				<text>实付：</text>
				<text class="t b">￥{{ num || 0 }}</text>
			</view>
		</view>
		<view class="btn-wrap">
			<view class="btn" @click="submitHandle"><text>确认支付</text></view>
		</view>
		<u-popup :show="show" :closeable="false" round mode="bottom">
			<view class="success-wrap">
				<view class="title">{{ payState == 1 ? '支付成功' : '支付失败，请返回APP重新下单' }}</view>
				<view class="content">
					<image class="img" src="@/static/success.png" mode="widthFix"></image>
				</view>
				<view class="popup-buttn">
					<button class="btn" type="primary" open-type="launchApp"><text>返回APP</text></button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	// import utils from '@/common/utils.js';
	export default {
		data() {
			return {
				type: 1,
				num: '',
				orderNumber: '',
				show: false,
				payState: 1,
				options: {}
			};
		},
		async onLoad(options) {
			// // #ifdef MP-WEIXIN
			// uni.login({
			// 	provider: 'weixin',
			// 	success: async (data) => {
			// 		try {
			// 			uni.cache.set('weixincode', data.code);
			// 			let res = await this.api.userlogin({
			// 				code: uni.cache.get('weixincode')
			// 			})
			// 			if (res) {
			// 				uni.cache.set('loginuser', res);
			// 				uni.cache.set('store_id', res.store_id) // 判断显示哪家的作品
			// 				uni.cache.set('openId', res.userinfo.open_id);
			// 			}
			// 		} catch (e) {
			// 			console.log(e)
			// 		}
			// 	}
			// });
			// // #endif
			//此处保存openid
			// uni.setStorageSync('openid', 'openid');
			// await this.$store.dispatch('getLoginInfo');
			this.orderNumber = options.orderNumber;
			this.num = options.num;
			if (options.type) {
				this.type = options.type;
			}
			if (options.type && options.type == 2) {} else if (options.type && options.type != 1) {
				this.options = options;
			}

			console.log({
				openid: uni.getStorageSync('openId'),
				orderNumber: this.orderNumber
			});

			// if (e.q) {
			// 	this.userAppId = utils.getQueryString(decodeURIComponent(e.q), 'userAppId');
			// 	this.getShopInfo();
			// }
		},
		methods: {
			// 付款
			async submitHandle() {
				try {
					uni.showLoading({
						title: '支付中...',
						mask: true
					});
					let res = '';
					if (this.type == 1) {
						res = await this.requestHandle('/wechat/orderPay', {
							openid: uni.getStorageSync('openId'),
							orderNumber: this.orderNumber,
							appId: 'wxcf9fdee37e8a4803'
						});
					} else if (this.type == 2) {
						// 加油支付
						res = await this.wxRequest('/order/orderPay', {
							openid: uni.getStorageSync('openId'),
							orderNumber: this.orderNumber
						});
					} else if (this.type == 3) {
						res = await this.wxRequest('/order/createorderApp', {
							openId: uni.getStorageSync('openId'),
							mobile: this.options.mobile,
							productId: this.options.productId,
							provinceName: this.options.provinceName,
							paymentType: this.options.paymentType,
							orderId: this.options.orderId
						});
					} else if (this.type == 4) {
						res = await this.wxRequest('/order/createshoporderApp', {
							openId: uni.getStorageSync('openId'),
							productId: this.options.productId,
							orderId: this.options.orderId
						});
					} else if (this.type == 5) {
						res = await this.wxRequest('/order/createoilshoporderApp', {
							openId: uni.getStorageSync('openId'),
							productId: this.options.productId,
							orderId: this.options.orderId
						});
					}

					console.log('options222222===', res);

					uni.requestPayment({
						provider: 'wxpay',
						timeStamp: res.data.payTimeStamp,
						nonceStr: res.data.paynonceStr,
						package: res.data.payPackage,
						signType: res.data.paySignType,
						paySign: res.data.paySign,
						success: (res) => {
							uni.hideLoading();
							this.show = true;
						},
						fail: (err) => {
							this.show = true;
							this.payState = 2;
							uni.hideLoading();
							uni.showToast({
								title: '支付失败',
								icon: 'none'
							});
						}
					});
				} catch (e) {
					this.show = true;
					this.payState = 2;
					uni.hideLoading();
					uni.showModal({
						title: '支付失败',
						content: e,
						showCancel: false
					});
					// uni.showToast({
					// 	title: '支付失败2:' + JSON.stringify(e),
					// 	icon: 'none'
					// });
				}
			},
			// 微信云托管请求封装
			wxRequest(api = '', data = {}, method = 'post') {
				console.log('data44444===', data);
				return new Promise((resolve, reject) => {
					uni.request({
						url: `https://ky.sxczgkj.cn/wap${url}`,
						method: method,
						header: {
							'Content-Type': 'application/x-www-form-urlencoded',
							sourceId: 2
						},
						data: data,
						success: (res) => {
							console.log('err2===', res);
							if (res.data.code == 0) {
								resolve(res.data);
							} else {
								uni.showToast({
									icon: 'none',
									title: res.data.message
								});
								reject(res.data.message);
							}
						},
						fail: (err) => {
							console.log('err1===', err);
							reject(JSON.stringify(err));
						}
					});
				});
			},
			// 封装请求
			requestHandle(url = '', data = {}, method = 'post') {
				return new Promise((resolve, reject) => {
					uni.request({
						url: `https://ky.sxczgkj.cn/wap${url}`,
						method: method,
						header: {
							'content-type': 'application/json'
						},
						data: data,
						success: (res) => {
							if (res.data.code == 200) {
								resolve(res.data);
							} else {
								uni.showToast({
									icon: 'none',
									title: res.data.message
								});
								reject(res.data.message);
							}
						},
						fail: (err) => {
							reject(JSON.stringify(err));
						}
					});
				});
			}
		}
	};
</script>

<style scoped lang="scss">
	.btn-wrap {
		width: 100%;
		position: fixed;
		bottom: 0;
		left: 0;
		padding: 28upx 28upx calc(env(safe-area-inset-bottom) + 28upx);

		.btn {
			padding: 20upx 0;
			border-radius: 100upx;
			background-color: #ffcc17;
			color: #000;
			font-size: 28upx;
			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;
				// &::after {
				// 	content: '';
				// 	width: 4upx;
				// 	height: 40upx;
				// 	background-color: #333;
				// 	position: absolute;
				// 	top: 50%;
				// 	margin-top: -24upx;
				// 	right: 0;
				// 	animation: ani 0.8s ease-in-out 1.4s infinite;
				// }
				// @keyframes ani {
				// 	0% {
				// 		opacity: 0;
				// 	}
				// 	100% {
				// 		opacity: 1;
				// 	}
				// }
			}
		}

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

			.input {
				flex: 1;
			}
		}
	}

	.pay-list {
		.item {
			display: flex;
			justify-content: space-between;
			padding: 28upx 0;

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

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

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

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