<!-- 修改密码 -->
<template>
	<view class="aircraft-setting">
		<view class="setting-item">
			<text>原密码</text>
			<u-input v-model.trim="form.oldPass" input-align="right" type="password"
			placeholder="请输入原密码" :clearable="false" />
		</view>
		<view class="setting-item">
			<text>新密码</text>
			<u-input v-model.trim="form.newPass" input-align="right" type="password"
			placeholder="请输入新密码" :clearable="false" />
		</view>
		<view class="setting-item">
			<text>确认密码</text>
			<u-input v-model.trim="form.comPass" input-align="right" type="password"
			placeholder="请输入确认密码" :clearable="false" />
		</view>
		<view class="setting-save" @click="submit">保存</view>
		<u-toast ref="uToast"></u-toast>
		<Loading :show="loading" />
	</view>
</template>

<script>
import { encrypt } from '@/utils/rsaEncrypt'
export default {
	// #ifdef MP
	options: {
		styleIsolation: 'shared'
	},
	// #endif
	data() {
		return {
			loading: false, // 加载
			form:{
				oldPass: '',
				newPass: '',
				comPass: ''
			},
			isPilot: this.$store.state.user_type == 1,
		}
	},
	methods:{
		// 提交
		async submit(){
			const { oldPass, newPass, comPass } = this.form;
			if(!oldPass||!newPass||!comPass){
				this.$refs.uToast.show({type: 'warning',title: "请完善信息！"});
			}else if(newPass !== comPass){
				this.$refs.uToast.show({type: 'warning',title: "新密码与确认密码不同！"});
			}else{
				try {
					this.loading = true;
					let res = await this.$api[this.isPilot?'aUpdatePass':'bUpdatePass']({
						oldPass: encrypt(oldPass),
						newPass: encrypt(newPass),
					});
					if(res.status&&res.status===400){
						this.$refs.uToast.show({type: 'error',title: "密码修改失败！"});
					}else{
						this.form = {
							oldPass: '',
							newPass: '',
							comPass: ''
						}
						this.$refs.uToast.show({type: 'success',title: "密码修改成功,请重新登录！"});
						setTimeout(async()=>{
							await this.$api.logout();
							this.$u.vuex('vuex_token', '');
							this.$u.vuex('user_message', {});
							uni.setStorageSync('current',0);
							uni.reLaunch({
								url: '/aircraft/server/my/child_pages/login',
							})
						},1000);
					}
				} catch (error) {
					this.$refs.uToast.show({type: 'error',title: "密码修改失败！"});
				} finally {
					this.loading = false;
				}
			}
		}
	},
}
</script>

<style scoped lang="scss">
.aircraft-setting {
	display: flex;
	flex-direction: column;
	min-height: 100vh;
	.setting-item{
		padding: 24rpx 0;
		margin: 0 32rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		border-bottom: 2rpx solid #EBEDF0;
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		font-size: 28rpx;
		color: #000000;
	}
	.setting-save{
		margin: auto 32rpx 52rpx;
		border-radius: 40rpx;
		background: #FBCB11;
		display: flex;
		align-items: center;
		justify-content: center;
		font-family: PingFang SC, PingFang SC;
		font-weight: bold;
		font-size: 28rpx;
		color: #020202;
		padding: 22rpx 0;
		&:active{
			margin: auto 42rpx 52rpx;
			padding: 18rpx 0;
			opacity: 0.8;
		}
	}
}
</style>