<template>
	<view class="Box">
		<view class="box_box">
			<!-- <view class="box_item flex-between">
				<text class="top_box_one_text">当前账号</text>
				<input type="number" v-model="form.mobile" placeholder="请输入手机号" maxlength="11" disabled="disabled" />
				<button v-if="!form.mobile" class="getPhone" open-type="getPhoneNumber" @getphonenumber="getPhone">
					<view class="text">获取手机号</view>
				</button>
			</view> -->
			<view class="u-flex">
				<view class="u-p-r-40">
					<view class="color-333 font-16 font-700">密码支付</view>
					<view class="font-12 color-999 u-m-t-4">关闭时，使用余额支付将会直接扣除，不再进行确认</view>
				</view>
				<up-switch v-model="form.usePayPwd" active-color="#FFD158" :inactive-value="0" @change="usePayPwdChange"
					:active-value="1"></up-switch>
			</view>
			<view class="color-333 font-16 font-700" style="margin-top: 60rpx;">密码支付</view>
			<view class="box_item flex-between">
				<text class="top_box_one_text">当前账号</text>
				<input type="number" v-model="form.mobile"  placeholder="请输入手机号" maxlength="11" :disabled="true" />
				<view class="top_box_one_texts">
					<view v-if="datalist.showText == true" class="yzm" @click="CodeRegister">{{ datalist.Recapture }}
					</view>
					<view v-else style="color: #ccc; background-color: #f9f9f9; border-radius:16px; padding: 10rpx;">
						{{ datalist.second }}s重新发送
					</view>
				</view>
			</view>
			<view class="box_item flex-between" style="position:relative;">
				<text class="top_box_one_text"></text>
				<input type="number" v-model="form.checkCode" placeholder="请输入验证码"
					style="padding-right: 140rpx;color: #333" />
				
			</view>
			<view class="box_item flex-between">
				<text class="top_box_one_text">新密码</text>
				<input type="number" v-model="form.password" placeholder="请输入6位数字交易密码" maxlength="6"
					:password="!passwords" />
			</view>
			<view class="box_item flex-between">
				<text class="top_box_one_text">确认密码</text>
				<input type="number" v-model="form.payPassword" placeholder="请再次输入密码" maxlength="6"
					:password="!payPasswords" />
			</view>

		</view>
		<view class="flex-colum">
			<view class="Box_bottom" :class="(form.checkCode!=''&&form.password!=''&&form.payPassword!='')?'active':''"
				@click="userInfosavePayPassword">确定</view>
		</view>
	</view>
</template>
<script setup>
	import {
		reactive,
		onMounted
	} from 'vue';

	import {
		APIuserpwd,
		APIusercode
	} from '@/common/api/api.js'
	import {
		APIshopUserInfo
	} from '@/common/api/member.js'

	import {
		productStore
	} from '@/stores/user.js';
	import {APIuser} from '@/common/api/api.js'
	const storeuser = productStore();
	const shopUserInfo = reactive({
		shopInfo: uni.cache.get('orderVIP'),
		shopId: ''
	})
	
	const userInfo=uni.cache.get('userInfo')

	const form = reactive({
		mobile: uni.cache.get('orderVIP').phone,
		password: '',
		payPassword: '',
		checkCode: '',
		usePayPwd: userInfo.usePayPwd
	})

	const datalist = reactive({
		isPwd: "",
		passwords: false,
		payPasswords: false,
		// 注册定时器   初始值
		second: 60,
		showText: true,
		Recapture: '获取验证码',
	})

	const CodeRegister = async () => {
		console.log('CodeRegister');
		const res = await APIusercode({
			// post  手机验证码
			phone: form.mobile
		});
		if (res) {
			uni.showToast({
				title: '验证码获取成功',
				icon: 'none'
			});
			// 定时器
			datalist.showText = false;
			datalist.Recapture = '重新获取';
			var interval = setInterval(() => {
				let times = --datalist.second;
				datalist.second = times < 10 ? '0' + times : times; //小于10秒补 0
			}, 1000);
			setTimeout(() => {
				clearInterval(interval);
				datalist.second = 60;
				datalist.showText = true;
			}, 60000);
		}
	}

	/**
	 * 获取手机号
	 * @param {Object} d
	 */
	const getPhone = async (d) => {
		if (d.detail.iv) {
			uni.login({
				provider: 'weixin',
				success: async (data) => {
					console.log(data)
					let res = await this.api.userwxlogins({
						code: data.code,
						encryptedData: d.detail.encryptedData,
						iv: d.detail.iv,
					})
					// form.mobile = res ? (res.slice(0, 3) + '****' + res.slice(7)) : '';
					form.mobile = res
				}
			})
		}
	}
	const loginwxuserInfo = async () => {
		let res = await this.api.loginwxuserInfo({
			userId: uni.cache.get('userInfo').id
		})
		if (res.code == 0) {
			uni.cache.set('userInfo', res.data);
		}
	}

	const userInfosavePayPassword = async () => {
		if (form.mobile.length != 11) {
			uni.showToast({
				title: '手机号必须是11位',
				icon: 'none'
			});
			return false;
		}
		if (form.password == null || form.password == '') {
			uni.showToast({
				title: '请输入密码',
				icon: 'none'
			});
			return false;
		}
		if (form.payPassword == null || form.payPassword == '') {
			uni.showToast({
				title: '请输入确认密码',
				icon: 'none'
			});
			return false;
		}
		if (form.password.length != 6 || form.payPassword.length != 6) {
			uni.showToast({
				title: '密码必须是6位',
				icon: 'none'
			});
			return false;
		}
		if (form.payPassword != form.password) {
			uni.showToast({
				title: '密码和确认密码不一致',
				icon: 'none'
			});
			return false;
		}
		if (form.checkCode == null || form.checkCode == '') {
			uni.showToast({
				title: '请输入验证码',
				icon: 'none'
			});
			return false;
		}

		let res = await APIuserpwd({
			checkPayPwd: form.payPassword,
			payPwd: form.password,
			code: form.checkCode,
			usePayPwd:form.usePayPwd
		})
		// * 获取会员信息
		await storeuser.actionsproductqueryProduct()
		if (res) {
			uni.navigateBack()
		}
	}
	
	async function usePayPwdChange(){
		uni.showLoading()
		await APIuserpwd({
			usePayPwd:form.usePayPwd
		})
		uni.showToast({
			title:'修改成功'
		})
	}
	onMounted(()=>{
		APIuser().then(res=>{
			uni.cache.set('userInfo', res);
			form.usePayPwd=res.usePayPwd
		})
	})
</script>

<style lang="scss" scoped>
	
	.Box {
		padding: 80rpx 28rpx 28rpx;

		.box_one {
			margin-top: 26rpx;
			font-size: 24rpx;
			font-family: Source Han Sans CN-Regular, Source Han Sans CN;
			font-weight: 400;
			color: #999999;
		}

		.box_box {
			background: #fff;

			.box_item {
				// border-bottom: 1rpx solid #E5E5E5;
				position: relative;

				.top_box_one_text {
					font-family: Source Han Sans CN, Source Han Sans CN;
					font-weight: 500;
					font-size: 28rpx;
					color: #333333;
				}

				input {
					padding: 26rpx 0;
					width: 75%;
					font-size: 28rpx;
					font-family: Source Han Sans CN-Regular, Source Han Sans CN;
					font-weight: 400;
					color: #333;
				}


				.top_box_one_texts {
					position: absolute;
					right: 0;
					margin: auto;
					font-size: 24rpx;
					font-family: Source Han Sans CN-Regular, Source Han Sans CN;
					font-weight: 400;
					color: #5082fd;
					z-index: 1;
					.yzm {
						padding: 8rpx 20rpx;
						background: #E5E5E5;
						border-radius: 24rpx;
						font-weight: 500;
						font-size: 24rpx;
						color: #333333;
					}
				}
			}

			.box_item:nth-child(1) {
				border-bottom: none;
				border-top: 1rpx solid #E5E5E5;

				input {
					border-bottom: 1rpx solid #E5E5E5;
					color: #333;
				}
			}
		}


		.flex-colum {
			width: 100%;
			margin-top: 56rpx;

			.Box_bottom {
				width: 100%;
				height: 96rpx;
				line-height: 96rpx;
				background-color: #999;
				border-radius: 48rpx;
				font-size: 32rpx;
				font-family: Source Han Sans CN-Regular, Source Han Sans CN;
				font-weight: 400;
				color: #ffffff;
				text-align: center;
			}

			.Box_bottom.active {
				background-color: #E3AD7F;
			}
		}

		.getPhone {
			line-height: initial;
			background-color: none;
			border: 2rpx solid #E3AD7F;
			color: #E3AD7F;
			font-size: 28rpx;
			padding: 5rpx 10rpx;
			position: absolute;
			right: 10rpx;
			z-index: 2;
		}
	}
	.Box .box_box .box_item uni-input{
		border-bottom: 1rpx solid #E5E5E5;
	}
	.Box .box_box .box_item input{
		border-bottom: 1rpx solid #E5E5E5;
	}
	:deep(.Box .box_box .box_item uni-input){
		border-bottom: 1rpx solid #E5E5E5;
	}
</style>