<template>
	<view class="pass">
		<view class="">
			<input class="uni-input" v-model="rawPass" placeholder="请输入原密码" />
			<!-- <text v-show="rawText">原密码不能为空</text> -->
		</view>
		<view class="">
			<input class="uni-input" v-model="newPass" placeholder="请输入新密码" />
			<!-- <text v-show="PassText">新密码不能为空</text> -->
		</view>
		<view class="">
			<input class="uni-input" v-model="newPass1" placeholder="请确认新密码" />
			<!-- <text v-show="Pass1Text">两次密码不一致</text> -->
		</view>
		<button type="warn" class="btn" @click="confirm">确定</button>
		<uni-popup ref="message" type="message">
			<uni-popup-message :type="msgType" :message="messageText" :duration="2000"></uni-popup-message>
		</uni-popup>
	</view>
</template>

<script>
	import {modifyPassword} from '../../api/user.js'
	import {
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				rawPass: '',
				newPass: '',
				newPass1: '',
				rawText: true,
				messageText: '',
				msgType: 'success'
			};
		},
		computed: {
			...mapState({
				user: store => store.user
			})
		},
		methods:{
			async confirm(){
				const res = await modifyPassword({
					Id: this.user._id,
					originalPassword: this.rawPass,
					newPassword: this.newPass,
					accessToken: uni.getStorageSync('tokenInfo'),
					lastUpdatedBy: {
						account: uni.getStorageSync('account'),
						person: this.user.name,
						sex: this.user.sex,
						IDCard: this.user.IDCard,
						factory: this.user.factory,
						warehouse: this.user.warehouse,
						line: this.user.line
					},
					account: uni.getStorageSync('account')
				})
				if(res.data.success === 1){
					this.msgType = 'error'
					this.messageText = res.data.message
					this.$refs.message.open()
				}else if(res.data.success === 0){
					this.msgType = 'success'
					this.messageText = res.data.message
					this.$refs.message.open()
					uni.removeStorage({
						key: 'username'
					});
					uni.removeStorage({
						key: 'passwork'
					});
					uni.removeStorage({
						key: 'tokenInfo'
					});
					uni.reLaunch({
						url: '/pages/login/login'
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.pass {
		padding: 50rpx 20rpx;

		&>view {
			margin-top: 20rpx;
			padding-bottom: 30rpx;
			position: relative;

			.uni-input {
				padding: 20rpx 10rpx;
				border-bottom: 1rpx solid #b4b4b4;
			}

			text {
				font-size: 22rpx;
				color: red;
				position: absolute;
				left: 10rpx;
				bottom: 0;
			}
		}

		.btn {
			width: 70%;
			margin-top: 50rpx;
		}
	}
</style>