<template>
	<view class="paymentPwd-container">
		<text class="pwd-describe">{{nowSetp == 1 ? '请设置6位数字支付密码' : nowSetp == 2 ? '请再次输入密码' : ''}}</text>
		<view class="sms-item" @tap="showKeyboard=!showKeyboard">
			<u-message-input mode="bottomLine" :maxlength="6" v-model="value" :dot-fill="true" :disabled-keyboard="true"></u-message-input>
		</view>
		<view class="btn-con">
			<u-button class="handle-btn" type="info" size="medium" @click="cancel">取消</u-button>
			<u-button class="handle-btn" type="primary" size="medium" @click="submit">确定</u-button>
		</view>
		<!-- 数字键盘 -->
		<u-keyboard ref="uKeyboard" :dot-enabled="false" :mask="false" :tooltip="false" v-model="showKeyboard" @change="valChange" @backspace="backspace"></u-keyboard>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				nowSetp: 1,  //  当前第几次设置密码
				value: '', //输入的内容
				showKeyboard: false,  //  是否打开键盘
				oPwd: '',  //  第一次密码
				tPwd: '',  //  第二次密码
			}
		},
		onShow() {
		},
		onLoad() {
			//因为此时实例没有挂载完成，需要延迟操作
			setTimeout(() => {
			  this.showKeyboard = true
			}, 50)
		},
		methods:{
			// 按键被点击(点击退格键不会触发此事件)
			valChange(val) {
				// 将每次按键的值拼接到value变量中，注意+=写法
				if(this.value.length < 6){
					this.value += val
				}
			},
			// 退格键被点击
			backspace() {
				// 删除value的最后一个字符
				if(this.value.length) this.value = this.value.substr(0, this.value.length - 1)
			},
			//  确定
			submit(){
				if(!this.value || this.value.length < 6){
					uni.showToast({ title: '请输入6位数字支付密码', icon: 'none' })
				}else{
					if(this.nowSetp == 1){
						this.oPwd = this.value
						this.value = ''
						this.nowSetp++
						this.showKeyboard = true
					}else if(this.nowSetp == 2){
						this.tPwd = this.value
						this.checkPwd()
					}
				}
			},
			//  密码校验
			checkPwd(){
				if(this.oPwd == this.tPwd){
					//  请求接口
					uni.showToast({ title: '支付密码设置成功', icon: 'none' })
					setTimeout(()=>{
						uni.navigateBack({
						    delta: 1
						})
					}, 2000)
				}else{
					uni.showToast({ title: '两次密码不一致请重新输入', icon: 'none' })
					this.value = ''
					this.showKeyboard = true
				}
			},
			//  取消
			cancel(){
				this.oPwd = ''
				this.tPwd = ''
				this.value = ''
				this.nowSetp = 1
			}
		}
	}
</script>

<style lang="less">
	.paymentPwd-container{
		width: 100%;
		padding: 0 30rpx;
		box-sizing: border-box;
		background-color: #fff;
		.pwd-describe{
			display: block;
			color: #606266;
			padding: 80rpx 0 70rpx;
			text-align: center;
		}
		.btn-con{
			display: flex;
			justify-content: space-between;
			padding-top: 130rpx;
			.handle-btn{
				display: inline-block;
				width: 45%;
				margin: 0 auto;
				text-align: center;
			}
		}
	}
</style>

