<template>
	<view class="passwordset-content" :style="{'--status-bar-': statusBarHeight}">
		<view class="passwordset-content-info">
			<!-- 头部主要内容 开始 -->
			<view class="passwordset-content-header">	
				<customNavbar 
				title="支付密码设置" 
				bgColor="var(--gd-bgm-color)"
				:contentStyle="{color: '#fff'}"
				:leftStyle="{color: '#fff'}"
				></customNavbar>
			</view>
			<!-- 头部主要内容 结束 -->
			
			<!-- userInfo内容 开始 -->
			
			<view class="passwordset-content-password">
				<text>{{ tip[actionIndex] }}</text>
				<view>
					<u-code-input v-model="value" @finish="finish" :focus="true" dot></u-code-input>
				</view>
			</view>
			<!-- userInfo内容 结束 -->
		</view>
	</view>
</template>

<script>
	import { navigateTo } from "@/utils/util.js"
	export default {
		data() {
			return {
				title: '这是我的',
				statusBarHeight: 0, // 状态栏安全距离
				value: "",
				tip: ["输入当前密码，已确认身份","设置六位数数字密码","再次输入"],
				actionIndex: 0,
				password: '',
				withdrawInfo: {}
			}
		},
		onLoad() {

		},
		onShow() {
			this.statusBarHeight = getApp().globalData.statusBarHeight
			this.getWithdrawInfo()
		},
		methods: {
			/**
			 * @author ygh
			 * @data 2023-12-20
			 */
			navigateToFun(item){
				navigateTo(item.url)
			},
			finish(){
				if(this.actionIndex == 0) { // 认证密码
					this.marketPersons()
				}else if(this.actionIndex == 1) {
					uni.showLoading({
						title: "下一步"
					})
					this.password = this.value
					setTimeout(()=>{
						this.$set(this,'value', '')
						this.actionIndex += 1
						console.log("this.value==",this.value)
						uni.hideLoading()
					},500)
					
				}else if(this.actionIndex == 2) {
					if(this.password != this.value){
						uni.showToast({
							title: '两次密码不一样',
							icon: 'none'
						});
						this.getWithdrawInfo()
					}else {
						this.payset()
					}
				}
			},
			/**
			 * 获取用户密码信息
			 */
			async getWithdrawInfo() {
				try{
					this.password = ''
					this.value = ''
					let res = await this.$u.api.withdrawInfo({
						noSign: 1,
						userid: this.vuex_user_info.userId
					})
					
					if(res && res.code ===200) {
						this.withdrawInfo = res.data
						if(this.withdrawInfo.ifPaySet== 0) {
							this.actionIndex = 1
						}else {
							this.actionIndex = 0
						}
					} 
				}catch(e){
					//TODO handle the exception
					console.error("e===",e)
				}
			},
			/**
			 * 密码校验
			 */
			async marketPersons() {
				try{
					uni.showLoading({
						title: "认证中..."
					})
					let res = await this.$u.api.marketPersons({
						noSign: 1,
						userid: this.vuex_user_info.userId,
						oldPassword: this.value
					})
					
					if(res && res.code ===200) {
						this.value = ''
						this.actionIndex += 1
						uni.hideLoading()
						uni.showToast({
							title: '认证成功',
							icon: 'none'
						});
						
					} else {
						this.value = ''
						uni.showToast({
							title: res.msg,
							icon: 'none'
						});
					}
				}catch(e){
					//TODO handle the exception
					console.error("e===",e)
					this.value = ''
					uni.showToast({
						title: "认证失败",
						icon: 'none'
					});
				}
			},
			/**
			 * 设置密码
			 */
			async payset() {
				try{
					let res = await this.$u.api.payset({
						noSign: 1,
						userid: this.vuex_user_info.userId,
						newPassword: this.value
					})
					
					if(res && res.code ===200) {
						uni.showToast({
							title: "设置成功",
							icon: 'none'
						});
						uni.navigateBack({
							delta: 1
						})
					} else {
						uni.showToast({
							title: "设置失败",
							icon: 'none'
						});
						this.getWithdrawInfo()
					}
				}catch(e){
					//TODO handle the exception
					console.error("e===",e)
					uni.showToast({
						title: "设置失败",
						icon: 'none'
					});
					this.getWithdrawInfo()
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.passwordset-content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		--header-h: 90rpx;
		.passwordset-content-info {
			width: 100%;
			box-sizing: border-box;
		}
	}
	/** 头部主要内容 开始 */
	.passwordset-content-header {
		width: 100%;
		height: var(--header-h);
		box-sizing: border-box;
		::v-deep .u-search {
			padding: 0 30rpx !important;
		}
		::v-deep .u-search__action {
			color: #fff !important;
		}
	}
	/** 头部主要内容 结束  **/
	
	/** password内容 开始 */
	.passwordset-content-password {
		width: 100%;
		padding: 40rpx 40rpx 0;
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
		justify-content: center;
		>text {
			text-align: center;
		}
		>view {
			margin-top: 40rpx;
			display: flex;
			justify-content: center;
		}
	}
	/** password内容 结束  */
</style>
