<template>
	<view class="body">
		<view class="header-bg">

		</view>
		<view class="header">
			<view class="title">
				设置支付密码
			</view>
		</view>

		<view class="main">
			<view class="input-item inline-form">
				<label class="label" for="">手机号码</label>
				<input class="input" type="text" placeholder="" v-model="form.mobile" disabled="true">

			</view>
			<view class="input-item inline-form">
				<label class="label" for="">验证码</label>
				<input class="input" type="text" placeholder="请输入验证码" v-model="form.captcha">
				<button class="send-vcode" @tap="sendSms()"
					:disabled="!sendable">{{sendable?'获取验证码':countdown+'秒'}}</button>
			</view>
			<view class="input-item inline-form">
				<label class="label" for="">新密码</label>
				<input class="input" type="password" placeholder="请输入新密码" maxlength="6" v-model="form.newsafepass">
			</view>
			<view class="input-item inline-form">
				<label class="label" for="">确认密码</label>
				<input class="input" type="password" placeholder="请输入新密码" maxlength="6" v-model="form.comfirmpass">
			</view>
		</view>
		<view style="margin: 10upx 40upx;font-size: 26upx;color: red;">*支付密码必须为6位数字</view>
		<view class="submit-btn-wrapper">
			<button class="btn submit-btn" type="default" @tap="submit">确认修改</button>
		</view>
	</view>
</template>

<script>
	let timeInterval = null;
	const time = 120;

	export default {
		data() {
			return {
				sendable: true,
				countdown: time,
				form: {
					type: "mobile",
					mobile: "",
					captcha: "",
					newsafepass: "",
					comfirmpass: ""
				}
			}
		},
		onLoad() {
			this.form.mobile = this.$store.state.user.mobile
		},
		methods: {
			sendSms() {
				if (this.sendable && this.$verified.mobile(this.form.mobile)) {
					this.sendable = false;

					this.$http.get({
						url: '/sms/send',
						data: {
							mobile: this.form.mobile,
							event: "resetsafepwd"
						},
						success: (res) => {
							uni.showToast({
								title: res.data.msg,
								icon: "none"
							})
							timeInterval = setInterval(() => {
								if (this.countdown-- === 0) {
									clearInterval(timeInterval)
									this.countdown = time;
									this.sendable = true;
								}
							}, 1000)
						},
						fail: () => {
							this.sendable = true;
						}
					})
				}
			},
			submit() {
				if (this.form.newsafepass !== this.form.comfirmpass) {
					uni.showToast({
						icon: "none",
						title: "两次输入的密码不一致"
					})
					return
				}
				if (this.$verified.safepass(this.form.newsafepass) && this.$verified.required(this.form.captcha,
					"请输入验证码")) {
					this.$http.post({
						url: "/user/resetsafepwd",
						data: this.form,
						success: (res) => {
							uni.showToast({
								title: "支付密码修改成功",
								icon: "none",
								success() {
									setTimeout(uni.navigateBack(), 1000)
								}
							})
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.body {
		position: relative;
		overflow: hidden;
	}

	.header-bg {
		background: $primary-color;
		height: 100upx;
		position: absolute;
		top: 0;
		width: 100%;
	}

	.header {
		margin: 20upx;
		background: white;
		text-align: center;
		border-radius: 20upx;
		position: relative;
		z-index: 1;

		.title {
			height: 100upx;
			font-size: 32upx;
			line-height: 100upx;
			// font-weight: bold;
		}
	}

	.main {
		margin: 20upx;
		background: white;
		border-radius: 10upx;
	}

	.input-item.inline-form {
		display: flex;
		align-items: center;
		height: 80upx;
		font-size: 28upx;
		padding: 0 20upx;
		position: relative;
		color: #999999;

		.label {
			width: 160upx;
			// flex: 1 1 160upx;

		}

		.input {
			flex-grow: 1;
			font-size: 28upx;
		}
	}

	.send-vcode {
		background: $primary-color;
		color: white;
		font-size: 24upx;
		position: absolute;
		padding: 5upx 0;
		width: 160upx;
		line-height: normal;
		right: 0upx;
		top: 20upx;
	}

	.submit-btn-wrapper {
		margin-top: 80upx;

		.submit-btn {
			font-size: 30upx;
			line-height: 60upx;
			width: 250upx;
			background: $primary-color;
			color: white;
		}
	}
</style>
