<script setup>
	// 支付密码
	import {
		computed,
		defineExpose,
		ref,
		getCurrentInstance,
		defineEmits
	} from 'vue';
	import {
		useStore
	} from 'vuex'
	import util from '@/common/js/util.js'
	// 加密
	import CryptoJS from 'crypto-js';

	// 
	const {
		proxy
	} = getCurrentInstance()
	// 
	const emit = defineEmits(['defineEmits'])
	// 仓库
	const store = useStore()
	// 密码
	const pwd = ref('')
	// 用户信息
	const userinfo = computed(() => {
		return store.state.userinfo
	})

	// 打开弹窗
	function open() {
		// 如果用户没有二级密码
		if (userinfo.value.hasSecondCipher) {
			proxy.$refs.pwdRef.open()
		} else {
			uni.navigateTo({
				url: '/pages/mine/setting/secondPwd'
			})
		}
	}

	// 关闭弹窗
	function close() {
		proxy.$refs.pwdRef.open()
	}

	// 确认
	function handleConfirm() {
		if (!/^\d{6}$/.test(pwd.value)) {
			util.alert('二级密码不正确')
			return
		}
		// md5加密
		emit('confirm', CryptoJS.MD5(pwd.value).toString())
	}

	// 
	defineExpose({
		open,
		close,
	})
</script>

<template>
	<uni-popup ref="pwdRef" type="center">
		<view class="pwdAlt popMid plr30 bfff">
			<view class="title mtb30 f30">验证二级密码</view>

			<view class="inputBox mtb20 plr30">
				<input v-model="pwd" :maxlength="6" type="number" placeholder="输入二级密码" />
			</view>

			<view class="btns mtb30 rows">
				<view class="btn lg cancel plr40 f1" @click="$refs.pwd.close()">取消</view>
				<view class="btn lg black plr40 f1" @click="handleConfirm">验证</view>
			</view>
		</view>
	</uni-popup>
</template>

<style>
</style>