<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="12" placeholder="请输入旧密码"   v-model="password"  type="password"></u-input>
				</u-form-item>
				<u-form-item   label="新密码"  :label-width="labelWidth">
					<u-input maxlength="12" placeholder="请输入新密码"   v-model="newPass"  type="password"></u-input>
				</u-form-item>
				<u-form-item   label="确认新密码"  :label-width="labelWidth">
					<u-input maxlength="12" placeholder="再次输入新密码"   v-model="confirmPass"  type="password"></u-input>
				</u-form-item>
			</u-form>
		</view>
		<view @click="submit"  class="footer-fixed cu-btn  bg-blue flex" style="padding: 45rpx;">
			确认修改
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//企业信息
				dataDetail:{},
				
				labelWidth:180,
				password:'',
				newPass:''
			}
		},
		onShow() {
			let dataDetail=getApp().globalData.userInfo
			if (this.$isEmpty(dataDetail)) {
				this.fetchUserInfo()
			}else{
				this.dataDetail=dataDetail
			}
		},
		methods: {
			/**
			 * 加载企业信息
			 * getApp().globalData.userInfo正常都会有值，一般不会来到这一步
			 */
			fetchUserInfo(){
				let creditCode=this.$cache.get('creditCode')
				this.$api.enterprise.detail({creditCode:creditCode}).then(res=>{
					this.dataDetail=res.data
				})
			},
			/**
			 * 修改密码
			 */
			submit(){
				if (this.$isEmpty(this.password)) {
					this.$u.toast("请输入旧密码")
					return
				}
				if (this.password!=this.dataDetail.password) {
					this.$u.toast("旧密码错误")
					return
				}
				if (this.$isEmpty(this.newPass)) {
					this.$u.toast('请输入新密码')
					return
				}
				if (this.newPass.length<6) {
					this.$u.toast('新密码密码不能少于6位')
					return
				}
				if (this.$isEmpty(this.confirmPass)) {
					this.$u.toast('请再次输入新密码')
					return
				}
				if (this.newPass!=this.confirmPass) {
					this.$u.toast('两次密码输入不一致')
					return
				}
				this.dataDetail.password=this.newPass
				this.$api.enterprise.submit(this.dataDetail).then(res=>{
					if (res.success) {
						this.$showModel('修改成功,请重新登陆！',false).then(res=>{
							this.$cache.clear()
							this.$Router.push('login')
						})
					}
				})
			}
		}
	}
</script>

<style>

</style>
