<template>
	<view>
		<u-navbar title="修改密码" autoBack placeholder></u-navbar>
		<view class="card_password">
			<view class="item_pass">
				<view class="pass_title">旧密码<span style="color: red;">*</span></view>
				<u-input v-show="isText1 === true" placeholder="请填写密码" border="surround" v-model="userInfo.oldCode"
					class="input_class_pwd" :password="true">
					<template slot="suffix" v-if="userInfo.oldCode">
						<u-icon name="eye-off" @click="isText1 = false" size="20"></u-icon>
					</template>
				</u-input>
				<u-input v-show="isText1 === false" placeholder="请填写密码" border="surround" v-model="userInfo.oldCode"
					class="input_class_pwd" :password="false">
					<template slot="suffix" v-if="userInfo.oldCode">
						<u-icon name="eye-fill" @click="isText1 = true" size="20"></u-icon>
					</template>
				</u-input>
			</view>
			<view class="item_pass">
				<view class="pass_title">新密码<span style="color: red;">*</span></view>
				<u-input v-show="isText2 === true" placeholder="请填写新密码" border="surround" v-model="userInfo.sedNewCode"
					class="input_class_pwd" :password="true">
					<template slot="suffix" v-if="userInfo.sedNewCode">
						<u-icon name="eye-off" @click="isText2 = false" size="20"></u-icon>
					</template>
				</u-input>
				<u-input v-show="isText2 === false" placeholder="请填写新密码" border="surround" v-model="userInfo.sedNewCode"
					class="input_class_pwd" :password="false">
					<template slot="suffix" v-if="userInfo.sedNewCode">
						<u-icon name="eye-fill" @click="isText2 = true" size="20"></u-icon>
					</template>
				</u-input>
			</view>
			<view class="item_pass">
				<view class="pass_title">再次输入新密码<span style="color: red;">*</span></view>
				<u-input v-show="isText3 === true" placeholder="请再次填写新密码" border="surround" v-model="userInfo.newCode"
					class="input_class_pwd" :password="true">
					<template slot="suffix" v-if="userInfo.newCode">
						<u-icon name="eye-off" @click="isText3 = false" size="20"></u-icon>
					</template>
				</u-input>
				<u-input v-show="isText3 === false" placeholder="请再次填写新密码" border="surround" v-model="userInfo.newCode"
					class="input_class_pwd" :password="false">
					<template slot="suffix" v-if="userInfo.newCode">
						<u-icon name="eye-fill" @click="isText3 = true" size="20"></u-icon>
					</template>
				</u-input>
			</view>
		</view>
		<view class="card_btn">
			<u-button type="primary" :disabled="disabled" text="提交" @click="amend"></u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: {
					oldCode: '',
					sedNewCode: '',
					newCode: ''
				},
				isText1: true,
				isText2: true,
				isText3: true,
			}
		},
		computed: {
			disabled() {
				if (this.userInfo.oldCode == '' || this.userInfo.sedNewCode == '' || this.userInfo.newCode == '') {
					return true;
				} else {
					return false;
				}
			},
		},
		methods: {
			amend() {
				if (this.userInfo.sedNewCode === this.userInfo.newCode) {
					uni.showLoading({
						mask: true
					});
					this.$api.put('/api/user/pwd/set', {
						oldPassword: this.userInfo.oldCode,
						newPassword: this.userInfo.newCode,
					}).then(res => {
						if (res.code == 200) {
							this.$cache.removeToken()
							this.$cache.removeCache('userInfo')
							uni.redirectTo({
								url: '/pages/login'
							})
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							});
						}
						uni.hideLoading();
					}).catch(() => {
						uni.hideLoading();
					})
				} else {
					uni.showToast({
						title: '输入两次密码不一样',
						icon: 'none'
					});
				}
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #fff !important;
	}

	.card_password {
		padding: 20rpx 40rpx 40rpx 40rpx;
	}

	.item_pass {
		margin-top: 30rpx;
	}

	.pass_title {
		margin-bottom: 20rpx;
	}

	.card_btn {
		margin: 40rpx;
	}
</style>