<template>
  <div class="reset">
		<el-form ref="registerForm" :model="registerForm" :rules="registerRules">
			<el-form-item class="register_field" prop="phone">
				<el-input ref="phone" v-model="registerForm.phone" type="text" :placeholder="$t('hivuiLogin_reset_phone')"></el-input>
			</el-form-item>
			<el-form-item class="register_field" prop="pw">
				<el-input ref="pw" v-model="registerForm.pw" type="password" :placeholder="$t('hivuiLogin_reset_password_tip')"></el-input>
				<pwStrength class="register_pwStrength" v-model="registerForm.pw" :minPw="minPw"></pwStrength>
			</el-form-item>
			<el-form-item class="register_field register_row" prop="captcha">
				<el-input ref="captcha" v-model="registerForm.captcha" type="text" :placeholder="$t('hivuiLogin_reset_captcha')"></el-input>
				<el-button class="register_capbtn" :loading="captchaLoading" type="primary" :disabled="captchaTimer==0?false:true" @click="getCaptcha">{{captchaTimer==0?$t('hivuiLogin_reset_getCaptcha'):(captchaTimer+$t('hivuiLogin_reset_getAgain'))}}</el-button>
			</el-form-item>
			<el-button class="register_submit" :loading="resetLoading" type="primary" @click="reset">{{$t('hivuiLogin_reset_reset')}}</el-button>
		</el-form>
  </div>
</template>

<script>
import { getToken } from "../utils/auth";
import { captcha,resetpw } from "../api/user";
import pwStrength from "../components/pwStrength";
import md5 from "js-md5";
export default {
  name: "resetPw",
  components: {
	  pwStrength
  },
  data() {
    return {
      registerForm: {
				phone: "",
				pw: "",
				captcha: "",
				email: "",
			},
			minPw:4,
			copyRight: window.customSysCofig?.copyright||this.$t('hivuiLogin_reset_def_copyright'),
			registerRules: {
				phone: [
					{ required: true, message: this.$t('hivuiLogin_reset_phone_tip'), trigger: "blur" },
					{ validator: (rule, value, callback)=>{
						if (!/^[1]([3-9])[0-9]{9}$/.test(value) && window?.customSysCofig.__checkPhone!="false") {
							callback(new Error(this.$t('hivuiLogin_reset_phone_msg')));
						} else {
							callback();
						}
					}, trigger: "blur" },
				],
				pw: [
					{ required: true, message: this.$t('hivuiLogin_reset_password_tip2'), trigger: "blur" },
					{ validator: (rule, value, callback)=>{
						if ((value+"").length<this.minPw) {
							callback(new Error(this.$t('hivuiLogin_reset_password_msg1')+this.minPw+this.$t('hivuiLogin_reset_password_msg2')));
						} else {
							callback();
						}
					}, trigger: "blur" },
				],
				captcha: [
					{ required: true, message: this.$t('hivuiLogin_reset_captcha_msg'), trigger: "blur" }
				],
				/*email: [
					{ required: true, message: "请填写邮箱地址", trigger: "blur" }
				]*/
			},
			captchaLoading:false,
			resetLoading:false,
			captchaTimer:0,
    };
  },
  model: {
    prop: "value",
    event: "valuechange"
  },
  props: {
    value: {
      type: Object
    }
  },
  computed: {},
  mounted() {},
  methods: {
		//获取验证码
		getCaptcha(){
			this.$refs.registerForm.validateField("phone",(errMsg) => {
				if (!errMsg) {
					let me=this;
					me.captchaTimer=60;
					me.captchaLoading=true;
					captcha({
					    "phone": me.registerForm.phone
					}).then(res=>{
						me.captchaLoading=false;
					}).catch(err=>{
						me.captchaLoading=false;
					});
					let _timer=setInterval(()=>{
						--me.captchaTimer;
						if(me.captchaTimer==0){
							clearInterval(_timer);
						}
					},1000);
				}
			});
		},
		//重置
		reset(){
			let me=this;
			this.$refs.registerForm.validate((valid) => {
				if (valid) {
					me.resetLoading=true;
					debugger;resetpw({
					    "phone":me.registerForm.phone,
					    "password":md5(me.registerForm.pw),
					    "captcha":me.registerForm.captcha,
					}).then(res=>{
						me.$message({
							message: me.$t('hivuiLogin_reset_reset_success'),
							type: 'success'
						});
						me.$emit("resetCb");
					}).catch(err=>{
						me.resetLoading=false;
					});
				}
			});
		},
  },
};
</script>
<style lang="scss" scoped>
	@function px2vw($px) {
		@return calc(#{$px} / 1920 * 100vw);
	}
	.reset{
		padding:px2vw(20) px2vw(50);
		.register_field{
			margin-bottom: px2vw(22);
			line-height:px2vw(48);
			::v-deep.el-input__inner{
				line-height:px2vw(48);
				height:px2vw(48);
			}
			.register_pwStrength{
				margin-top:px2vw(10);
			}
			&.register_row{
				::v-deep.el-form-item__content{
					display: flex;
					align-items:center;
				}
				
			}
		}
		.register_capbtn{
			width: px2vw(200);
			height: px2vw(48);
			line-height: px2vw(48);
			padding: 0;
		}
		.register_submit{
			height: px2vw(48);
			line-height: px2vw(48);
			padding-top: 0;
			width: 100%;
			margin-top:px2vw(10);
		}
		.register_tips{
			padding-top: px2vw(20);
			span{
				color:#3486d2;
				cursor: pointer;
			}
		}
	}
</style>
