<template>
	<view class="content">

		<view class="location" @click="openLocation">
			<image class="location_icon" src="https://czg-qr-order.oss-cn-beijing.aliyuncs.com/menber/location.png"
				mode="aspectFill"></image>
			<view>{{ infoForn.shopUserInfo.shopName }}</view>
			<up-icon name="arrow-right" color="#575B66" size="16"></up-icon>
		</view>

		<view class="card_info flex-colum">
			<view class="title">账户余额(元)</view>
			<view class="card_info_con flex-between">
				<view class="balance">{{infoForn.userInfo.amount || '0.00'}}</view>
				<view class="card_info_con_right flex-end">
					<view class="card_info_con_right_item flex-colum" @click="handleClick(item)"
						v-for="(item,index) in cardManageList" :key="index">
						<image class="card_info_con_right_item_icon" :src="item.icon" mode="aspectFill"></image>
						<text class="card_info_con_right_item_text">{{item.name}}</text>
					</view>
				</view>
			</view>

		</view>
		<view class="customAmount" v-if="infoForn.shopUserInfo.isCustomAmount == 1">
			<view class="customAmount_left">
				<text class="customAmount_left_tip">￥</text>
				<input type="number" v-model="infoForn.amount" inputAlign="left"
					:placeholder="`充${infoForn.amount}送${infoForn.giftAmount}`" :customStyle="{border: '0'}"
					controlled="true" @input="amountChange"></input>
			</view>
			<text class="customAmount_right">自定义金额</text>
		</view>


		<view class="rechargeList">
			<view class="rechargeList_f" @click="clickinput(item,index)" v-for="(item,index) in infoForn.listdata"
				:key="index">
				<view class="rechargeList_item flex-colum " :class="index === infoForn.inputshow?'active':''">
					<view class="rechargeList_item_title">充值</view>
					<view class="flex-colum-start">
						<view class="rechargeList_item_amount">￥<text>{{item.amount}}</text></view>
						<view class="rechargeList_item_gift" v-show="index === infoForn.inputshow">
							<image class="rechargeList_item_gift_bg"
								src="https://czg-qr-order.oss-cn-beijing.aliyuncs.com/menber/gift_icon.png"
								mode="aspectFill"></image>
							<view class="rechargeList_item_gift_text">
								<text>赠</text>
								<text>￥{{ item.giftAmount }}</text>
							</view>
						</view>
						<view class="rechargeList_item_handsel" v-show="index !== infoForn.inputshow">
							赠送{{item.giftAmount}}元
						</view>
					</view>

				</view>
			</view>

		</view>

		<view class="gift" v-if="infoForn.giftList && infoForn.giftList.length > 0">
			<view class="gift_title">您将获得：</view>
			<view class="gift_list">
				<view class="gift_list_item" v-for="(item,index) in infoForn.giftList" :key="index">
					<text class="icon"></text>
					<text class="text"> {{ item }} </text>
				</view>
			</view>
		</view>

		<view class="explain">
			<view class="explain-top flex-between">
				<text class="explain-top_title">适用门店</text>
				<text class="shopName">{{ infoForn.shopUserInfo.shopName }}</text>
			</view>
			<view class="explain_content">
				<text class="explain_content_title">充值说明</text>
				<text class="explain_content_text">1.充值金额1家门店可用</text>
				<text class="explain_content_text">2.储值完成后不支持自助退款，可联系商家处理</text>
				<text class="explain_content_text">3.钱包余额不支持转赠，不可提现，长期有效</text>
			</view>
		</view>

		<view class="rechargeBox">
			<view class="agreement" @click="infoForn.isProtocol = !infoForn.isProtocol">
				<up-checkbox-group>
					<up-checkbox :checked="infoForn.isProtocol" shape="circle" activeColor="#E3AD7F"
						@change="radioChange" size="18" iconSize="10">
					</up-checkbox>
				</up-checkbox-group>
				<text>已同意</text>
				<text class="agreement_text" @click.stop="viewProtocol">《用户隐私协议》</text>
			</view>
			<view class="recharge" @tap="$u.debounce(userbalancerechangesub, 500)">立即充值</view>
		</view>
		<registermember :show="infoForn.shopUserInfo.isVip == 1? false : true" :shopUserInfo="infoForn.shopUserInfo">
		</registermember>
	</view>

</template>

<script setup>
	import {
		ref,
		reactive,
		onMounted
	} from 'vue';
	import registermember from './components/registermember.vue'

	import {
		APIuseractivate,
		APIusershopInfodetail,
		APIshopUserInfo
	} from '@/common/api/member.js'

	// 状态管理
	import {
		Memberpay
	} from '@/stores/pay.js';
	const store = Memberpay();

	const cardManageList = [{
			name: "明细",
			url: "member/billDetails",
			icon: "https://czg-qr-order.oss-cn-beijing.aliyuncs.com/menber/detail.png"
		},
		{
			name: "管理",
			url: "member/storedManage",
			icon: "https://czg-qr-order.oss-cn-beijing.aliyuncs.com/menber/manage.png"
		}
	]

	const infoForn = reactive({
		inputshow: 0,
		memberOpen: false,
		isProtocol: false,
		listdata: [],
		amount: 0,
		giftAmount: 0,
		shopUserInfo: {},
		shopId: '',
		giftList: [],
		type: "",
		userInfo: ''

	})


	// * 获取充值金额列表
	const paygetActive = async () => {
		let res = await APIuseractivate({
			shopId: infoForn.shopId,
			page: 1,
			pageSize: 99
		})
		try {
			infoForn.listdata = res;
			infoForn.giftList = infoForn.listdata[0].gives;
			infoForn.giftAmount = infoForn.listdata[0].giftAmount
			if (infoForn.type == 'topUpActivity') {
				infoForn.amountChange()
			} else {
				infoForn.amount = infoForn.listdata[0].amount;
			}
		} catch (e) {
			//TODO handle the exception
		}
	}

	// * 拉起地图
	const openLocation = () => {
		uni.openLocation({
			latitude: Number(infoForn.shopUserInfo.lat), // 目的地的纬度，浮点数，范围为-90~90
			longitude: Number(infoForn.shopUserInfo.lng), // 目的地的经度，浮点数，范围为-180~180
			scale: 18, // 缩放比例，范围5~18
			name: infoForn.shopUserInfo.shopName, // 位置名
			address: infoForn.shopUserInfo.address, // 地址的详细说明
			success() {
				console.log('导航启动成功');

			},
			fail(error) {
				console.log('导航启动失败', error);
				if (error.errMsg == "openLocation:fail invalid_coordinate") {
					uni.showToast({
						title: "门店经纬度无效",
						icon: 'none'
					});
				}

			}
		});
	}

	// 监听金额修改
	const amountChange = () => {
		let item = infoForn.listdata.filter(item => infoForn.amount == item.amount)
		if (item.length > 0) {
			infoForn.listdata.forEach((v, e) => {
				if (infoForn.amount == v.amount) {
					infoForn.inputshow = e;
					infoForn.clickinput(v, e)
				}
			})
		} else {
			infoForn.inputshow = '';
			infoForn.giftList = [];
			infoForn.giftAmount = '';
		}

	}

	const shopInfo = async () => {
		let res = await this.api.shopUserInfo({
			shopId: this.shopId,
			userId: uni.cache.get('userInfo').id,
		})
		if (res.code == 0) {
			infoForn.shopUserInfo = res.data;
			uni.cache.remove('memberOpen', this.memberOpen)
			if (infoForn.shopUserInfo.isVip == 0) {
				this.memberOpen = true;
				uni.cache.set('memberOpen', this.memberOpen)
			}
		} else {
			let pages = getCurrentPages()
			if (pages.length > 1) {
				uni.navigateBack()
			} else {
				setTimeout(res => {
					uni.exitMiniProgram({
						success: function() {
							console.log('退出小程序成功');
						},
						fail: function(err) {
							console.log('退出小程序失败', err);
						}
					})
				}, 500)
			}
		}
	}

	// * 进入明细\管理
	const handleClick = (item) => {
		uni.navigateTo({
			url: `/pages/${item.url}?shopUserInfo=${JSON.stringify(infoForn.shopUserInfo)}`
		})
	}
	// * 是否同意协议
	const radioChange = (n) => {
		infoForn.isProtocol = n;
	}
	//查看协议
	const viewProtocol = () => {
		wx.openPrivacyContract({
			success: () => {}, // 打开成功
			fail: () => {}, // 打开失败
			complete: () => {}
		})
	}
	// 注册会员卡
	const getRegisterMember = (e) => {
		infoForn.memberOpen = e;
		this.shopInfo();
		this.paygetActive()
	}

	// 解码
	const getQueryString = (url, name) => {
		var reg = new RegExp('(^|&|/?)' + name + '=([^&|/?]*)(&|/?|$)', 'i')
		var r = url.substr(1).match(reg)
		if (r != null) {
			return r[2]
		}
		return null;
	}


	// 充值金额切换
	const clickinput = (a, b) => {
		infoForn.inputshow = b;
		infoForn.giftList = a.gives;
		infoForn.amount = a.amount;
		infoForn.giftAmount = a.giftAmount;
	}

	// 充值
	const userbalancerechangesub = async () => {
		if (!infoForn.isProtocol) {
			uni.showToast({
				title: '请勾选协议',
				icon: 'none'
			});
			return false;
		}
		console.log(infoForn.amount)
		if (infoForn.amount == null || infoForn.amount == '') {
			uni.showToast({
				title: '金额不能为空',
				icon: 'none'
			});
			return false;
		}
		if (infoForn.amount <= 0) {
			uni.showToast({
				title: '金额必须大于0',
				icon: 'none'
			});
			return false;
		}
		let res = await store.actionspayltPayVip({
			shopId: infoForn.shopId,
			shopUserId: infoForn.userInfo.id,
			amount: infoForn.amount, //金额
			returnUrl: '', //跳转地址
			buyerRemark: ''
		})
		console.log(res)
	}

	onMounted(async () => {
		// 获取当前页面栈
		const pages = getCurrentPages();
		// 获取当前页面实例
		const currentPage = pages[pages.length - 1];
		// 获取页面参数
		const options = currentPage.options;
		// // #ifdef MP-WEIXIN
		// if (options.q) {
		// 	infoForn.shopId = this.getQueryString(decodeURIComponent(options.q), 'shopId')
		// 	uni.cache.set('shopId', infoForn.shopId)
		// 	this.tokenShow = false;
		// }
		// // #endif
		// // #ifdef MP-ALIPAY
		// if (getApp().globalData.shopId) {
		// 	infoForn.shopId = getApp().globalData.shopId
		// }
		// uni.cache.set('shopId', infoForn.shopId)
		// // #endif


		if (options.shopId) {
			infoForn.shopId = options.shopId ? options.shopId : uni.cache.get('shopId')
			let resone = await APIusershopInfodetail({
				shopId: options.shopId
			})
			infoForn.shopUserInfo = resone.shopInfo

			// 根据传的shopInfo来返回上面的值
			infoForn.userInfo = await APIshopUserInfo({
				shopId: options.shopId
			})

			// shopUserInfo.shopExtend = res.shopExtend.member_bg

		}
		if (options.type) {
			infoForn.type = options.type
		}
		if (options.amount) {
			infoForn.amount = options.amount;
		}
		paygetActive()

	})
</script>

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

	.content {
		padding: 16rpx 20rpx 245rpx 20rpx;

		.location {
			display: flex;
			align-items: center;
			background-color: #fff;
			padding: 16rpx 28rpx 18rpx 28rpx;
			margin-bottom: 32rpx;
			border-radius: 12rpx;
			font-weight: 400;
			font-size: 28rpx;
			color: #333333;

			.location_icon {
				width: 20rpx;
				height: 26rpx;
				margin-right: 20rpx;
			}
		}

		.card_info {
			width: 100%;
			height: 182rpx;
			background: linear-gradient(132deg, #D6B68D 0%, #E6D6BC 100%);
			border-radius: 12rpx;
			padding: 32rpx 28rpx;
			align-items: initial;
			margin-bottom: 26rpx;

			.title {
				font-weight: 500;
				font-size: 32rpx;
				color: #333333;
			}

			.card_info_con {
				align-items: flex-end;

				.balance {
					font-weight: 400;
					font-size: 48rpx;
					color: #333333;
				}

				.card_info_con_right {
					.card_info_con_right_item {
						margin-left: 64rpx;

						.card_info_con_right_item_icon {
							width: 40rpx;
							height: 36rpx;
							margin-bottom: 5rpx;
						}

						.card_info_con_right_item_text {
							font-weight: 400;
							font-size: 24rpx;
							color: #333333;
						}
					}
				}
			}
		}

		.customAmount {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 32rpx;
			padding: 22rpx 16rpx 22rpx 32rpx;
			background-color: #fff;
			border-radius: 8rpx;

			.customAmount_left {
				width: 100%;
				display: flex;
				align-items: center;

				.customAmount_left_tip {
					font-weight: 400;
					font-size: 28rpx;
					color: #333333;
				}

				input {
					width: 100%;
					font-weight: 500;
					font-size: 24rpx;
					color: #333;
					padding-left: 16rpx;
				}
			}

			.customAmount_right {
				font-weight: 500;
				font-size: 28rpx;
				color: #666666;
				flex-shrink: 0;
			}
		}

		.rechargeList {
			width: 100%;
			display: flex;
			flex-wrap: wrap;

			.rechargeList_f {
				width: 33.333%;
				margin-bottom: 30rpx;
				padding-right: 20rpx;
				padding-left: 0;

				.rechargeList_item {
					border-radius: 0rpx 48rpx 0rpx 0rpx;
					border: 4rpx solid #E5E5E5;
					align-items: initial;
					padding: 26rpx 22rpx;
					justify-content: space-between;
					position: relative;

					.rechargeList_item_title {
						margin-bottom: 20rpx;
					}

					.rechargeList_item_title,
					.rechargeList_item_handsel {
						font-weight: 400;
						font-size: 20rpx;
						color: #999999;
					}

					.rechargeList_item_amount {
						font-weight: bold;
						font-size: 28rpx;
						color: #666666;

						text {
							font-size: 44rpx;
							font-weight: bold;
							color: #666666;
						}
					}

					.rechargeList_item_handsel {}

					.rechargeList_item_gift {
						width: 92.52rpx;
						height: 88.74rpx;
						position: absolute;
						top: 12rpx;
						right: 12rpx;

						.rechargeList_item_gift_bg {
							width: 92.52rpx;
							height: 88.74rpx;
							position: absolute;
						}

						.rechargeList_item_gift_text {
							width: 92.52rpx;
							height: 88.74rpx;
							position: absolute;
							display: flex;
							flex-direction: column;
							align-items: center;
							justify-content: center;

							text:nth-child(1) {
								font-weight: bold;
								font-size: 20rpx;
								color: #fff;
							}

							text:nth-child(2) {
								font-weight: 400;
								font-size: 24rpx;
								color: #fff;
							}
						}
					}
				}

				.rechargeList_item.active {
					border: 4rpx solid #DCC19E;
					background: linear-gradient(133deg, #F9F6ED 0%, #FFFFFF 100%);

					.rechargeList_item_title {
						color: #F7664E;
						margin-bottom: 47rpx;
					}

					.rechargeList_item_amount {
						font-weight: bold;
						font-size: 28rpx;
						color: #FB604A;

						text {
							font-size: 44rpx;
							font-weight: bold;
							color: #FB604A;
						}
					}
				}
			}

			.rechargeList_f:nth-child(3n-1) {
				padding-right: 10rpx;
				padding-left: 10rpx;
			}

			.rechargeList_f:nth-child(3n) {
				padding-right: 0;
				padding-left: 20rpx;
			}
		}

		.gift {
			display: flex;
			flex-direction: column;
			padding: 0 22rpx;
			margin-bottom: 48rpx;

			.gift_title {
				font-weight: bold;
				font-size: 32rpx;
				color: #333333;
				margin-bottom: 16rpx;
			}

			.gift_list {
				display: flex;
				flex-direction: column;

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

					.icon {
						width: 24rpx;
						height: 24rpx;
						border-radius: 50%;
						background-color: #F7853D;
						margin-right: 8rpx;
						flex-shrink: 0;
					}

					.text {
						font-weight: 400;
						font-size: 28rpx;
						color: #333333;
					}
				}
			}
		}

		.explain {
			background-color: #fff;
			border-radius: 12rpx;
			display: flex;
			flex-direction: column;
			padding: 22rpx 24rpx;
			margin-bottom: 30rpx;

			.explain-top {
				// align-items: center;
				margin-bottom: 18rpx;

				.explain-top_title {
					font-weight: bold;
					font-size: 32rpx;
					color: #333333;
				}

				.shopName {
					font-weight: 400;
					font-size: 24rpx;
					color: #333333;
				}
			}

			.explain_content {
				display: flex;
				flex-direction: column;

				.explain_content_title {
					font-weight: bold;
					font-size: 32rpx;
					color: #333333;
					margin-bottom: 20rpx;
				}

				.explain_content_text {
					font-weight: 400;
					font-size: 24rpx;
					color: #666666;
					margin-bottom: 14rpx;
				}
			}
		}

		.rechargeBox {
			width: 100%;
			padding: 0 28rpx 84rpx 28rpx;
			background-color: #fff;
			position: fixed;
			left: 0;
			bottom: 0;

			.agreement {
				display: flex;
				align-items: center;
				justify-content: center;
				padding-top: 16rpx;
				padding-bottom: 20rpx;

				text {
					font-size: 24rpx;
				}

				.agreement_text {
					color: #E3AD7F;
				}
			}

			.recharge {
				width: 100%;
				height: 90rpx;
				line-height: 90rpx;
				text-align: center;
				font-weight: bold;
				font-size: 36rpx;
				color: #FFFFFF;
				border-radius: 12rpx;
				background: #E3AD7F;
			}
		}

	}
</style>