<template>
  <view class="page">
    <l-title>修改密码</l-title>
	<l-input
	  @input="setValue('password.oldPwd', $event)"
	  :value="getValue('password.oldPwd')"
	  password
	  title="旧密码"
	/> 
	<l-input
	  @input="setValue('password.newPwd', $event)"
	  :value="getValue('password.newPwd')"
	  password
	  title="新密码"
	/> 
	<l-input
	  @input="setValue('password.confirmPwd', $event)"
	  :value="getValue('password.confirmPwd')"
	  password
	  title="确认输入"
	  placeholder="请再次输入新密码"
	/> 

	<view class="margin-sm">新密码必须8-20位同时包含1.[大小写字母]、2[数字]、3[特殊符号!@@#$%^&*]</view>

	<view class="margin-sm text-red" v-if="strongFlag">您的密码不满足强度要求，请您先修改密码后再执行系统其他操作！</view>
	
    <view class="padding">
      <l-button @click="submit" size="lg" color="blue" class="block" block>确认修改</l-button>
    </view>
	
	<view class="padding" v-if="strongFlag">
	  <l-button @click="logout" size="lg" color="red" class="block" block>退出</l-button>
	</view>
  </view>
</template>

<script>	
import get from 'lodash/get'
import set from 'lodash/set'
import customPageMixins from '@/common/custompage.js'
export default {
  mixins: [customPageMixins],
  
  data() {
    return {
	  strongFlag:false,
	  
	  // 表单数据 
	  current: {}, 
	  origin: {}, 
	    
	  // 表单项数据结构
	  scheme: { 
		  password:{
			  oldPwd: { type: 'text',title:'旧密码',verify:'NotNull' }, 
			  newPwd: { type: 'text',title:'新密码',verify:'NotNull' }, 
			  confirmPwd: { type: 'text',title:'确认密码',verify:'NotNull' }, 
		  }
	  }, 
	  
    }
  },

	async onLoad({}){
		await this.init();
	},

  methods: {
	  async init(){
		//判断是否是验证强密码打开的页面
		console.log(this.GET_STORAGE('pwd'))
		if(this.GET_STORAGE('pwd') == true){
			this.strongFlag=true
		}
		
		this.origin = await this.getDefaultForm()
		this.current=this.COPY(this.origin)
	  },
    // 提交修改
    async submit() {
		const verifyResult = this.verifyForm()
		if (verifyResult.length > 0) { 
		  this.CONFIRM('表单验证失败', verifyResult.join('\n')) 
		  return
		} 
		
		const postData = await this.getPostData()
		const postVal = JSON.parse(postData.strEntity)
		var reg = /^(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*,\.])[0-9a-zA-Z!@#$%^&*,\.]{8,20}$/;
		if(!reg.test(postVal.newPwd)){
			this.TOAST('新密码不满足强度要求');
			return ;
		}
		if(postVal.newPwd != postVal.confirmPwd){
			this.TOAST('新密码和确认密码输入不一致');
			return ;
		}

		  const success = await this.HTTP_POST(
			'learun/adms/user/modifypw',
			{
			  newpassword: this.MD5(postVal.newPwd),
			  oldpassword: this.MD5(postVal.oldPwd)
			},
			'未能成功修改密码'
		  )

		if (!success) {
			return
		}
		
		// this.NAV_BACK()
		this.TOAST('密码修改成功')
		this.CLEAR_GLOBAL()
		this.RELAUNCH_TO('/pages/login')
    },
	
	// 点击「注销登录」按钮
	async logout() {
	  if (!(await this.CONFIRM('注销确认', '确定要注销登录吗？', true))) {
	    return
	  }
	
	  this.CLEAR_GLOBAL()
	  this.RELAUNCH_TO('/pages/login')
	},
	
	// 获取表单值 
	getValue(path) { 
	  return get(this.current, path) 
	}, 
	  
	// 设置表单值 
	setValue(path, val) { 
	  set(this.current, path, val) 
	} ,
	
  }
}
</script>
