<template>
	<view>
		<view class="bg-white padding-left-40 padding-right-40">
			<u-form   ref="uForm" >
				<u-form-item   label="旧密码"  :label-width="labelWidth">
					<u-input maxlength="8" placeholder="请输入旧密码"   v-model="oldPassword"  type="password"></u-input>
				</u-form-item>
				<u-form-item   label="新密码"  :label-width="labelWidth">
					<u-input maxlength="8" placeholder="请输入新密码"   v-model="newPassword"  type="password"></u-input>
				</u-form-item>
				<u-form-item   label="确认新密码"  :label-width="labelWidth">
					<u-input maxlength="8" placeholder="再次输入新密码"   v-model="confirmPassword"  type="password"></u-input>
				</u-form-item>
			</u-form>
		</view>
		<view @click="submit"  class=" footer-fixed cu-btn  base-bg-color flex " style="padding: 45rpx;" :style="{marginBottom:safeAreaBottom}">
			确认修改
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loginType:'',
				
				account:'',
				
				dataDetail:{},
				
				labelWidth:180,
				oldPassword:'',
				newPassword:'',
				confirmPassword:''
			}
		},
		onLoad() {
			this.$loginType=this.$cache.get('loginType')
			if (!this.$cache.get('loginAccount')) {
				uni.showModal({
					content:"系统异常",
					showCancel:false,
					success() {
						uni.navigateBack({
							delta:1
						})
					}
				})
			}else{
				this.account=this.$cache.get('loginAccount')
			}
		},
		computed: {
		  //ios底部安全区域
		  safeAreaBottom() {
		    let info = uni.getSystemInfoSync()
		    let safe = 20
		    if (
		      info &&
		      ['devtools', 'ios'].includes(info.platform) &&
		      info.statusBarHeight > safe
		    ) {
		      return info.statusBarHeight - safe+'px'
		    }
		    return 0
		  }
		},
		methods: {
			/**
			 * 修改密码
			 */
			submit(){
				if (this.$isEmpty(this.oldPassword)) {
					this.$u.toast("请输入旧密码")
					return
				}
				if (this.$isEmpty(this.newPassword)) {
					this.$u.toast('请输入新密码')
					return
				}
				if (this.newPassword.length<6) {
					this.$u.toast('新密码不能少于6位')
					return
				}
				if (this.$isEmpty(this.confirmPassword)) {
					this.$u.toast('请再次输入新密码')
					return
				}
				if (this.newPassword!=this.confirmPassword) {
					this.$u.toast('两次密码输入不一致')
					return
				}
				let loginType=''
				
				if (this.loginType==this.$loginType.AGENCY) {
					//园区
					loginType=2
				}else{
					//企业
					loginType=1
				}
				
				let params={
					loginType,
					account:this.account,
					oldPassword:this.oldPassword,
					newPassword:this.newPassword
				}
				this.$api.updatePassword(this.$u.queryParams(params)).then(res=>{
					if (res.data==true) {
						this.$showModel('修改成功,请重新登陆！',false).then(res=>{
							this.$cache.clear()
							uni.reLaunch({
								url:"/pages/login/login"
							})
						})
					}else{
						this.$u.toast(res.data)
					}
				})
				if (this.loginType==this.$loginType.AGENCY) {
					//修改园区密码
					this.$api.agency.submit(this.dataDetail).then(res=>{
						if (res.success) {
							
						}
					})
					
				}else if (this.loginType==this.$loginType.ENTERPRISE) {
					//修改企业密码
					this.$api.enterprise.submit(this.dataDetail).then(res=>{
						if (res.success) {
							this.$showModel('修改成功,请重新登陆！',false).then(res=>{
								this.$cache.clear()
								uni.navigateTo({
									url:"/pages/login/login"
								})
							})
						}
					})
				}
			}
		}
	}
</script>

<style>

</style>
