<template>
  <el-form class="panel" :model="form" @submit.stop label-width="70px">
    
    <div class="stop-psw">
      <input type="text">
      <input type="password" name="" id="">
    </div>
    
    <h2>重置密码</h2>
    <el-form-item class="panel-form-item" label="手机号">
      <p class="err-info">{{verification.phone}}</p>
      <el-input v-model.trim="form.phone" placeholder="请输入手机号码"></el-input>
    </el-form-item>
    <el-form-item class="panel-form-item" label="验证码">
      <p class="err-info">{{verification.code}}</p>
      <el-input v-model="form.code" placeholder="请输入验证码">
        <template v-slot:suffix>
          <el-button type="primary" plain class="input-inner-btn" @click="sendCode" :disabled="msgStatus.status !== 0">
            {{msgStatus.status === 2 ? `${msgStatus.miss}S后可重新发送` : '获取验证码' }}
          </el-button>
        </template>
      </el-input>
    </el-form-item>
    <el-form-item class="panel-form-item" label="重置密码">
      <p class="err-info">{{verification.psw}}</p>
      <el-input v-model="form.psw" placeholder="请输入8-16位数字、英文大小写组合密码" type="password"></el-input>
    </el-form-item>
    <el-form-item class="panel-form-item" label="确认密码">
      <p class="err-info">{{verification.regPsw}}</p>
      <el-input v-model="form.regPsw" placeholder="请确认密码" type="password"></el-input>
    </el-form-item>
    
    <div class="panel-form-item">
      <el-button type="primary" class="fill" @click="submitClick">提交</el-button>
    </div>

    <div class="more">
      <a @click="$router.replace({name: 'login'})">立即登录</a>
    </div>
  </el-form>
</template>

<script>
import { reactive, watch } from 'vue'
import axios from 'axios'
import { updatePsw, sendUserMsg } from '@/request/config'
import { PHONE, PSW } from '@/constant/REG'
import { openErrorMsg } from '@/request/errorMsg.js'
import { encryption, guid } from '@/util'

export default {
  name: 'login',
  setup() {
    const form = reactive({
      phone: '',
      code: '',
      psw: '',
      regPsw: '',
    })
    const verification = reactive({ 
      phone: '',
      code: '',
      psw: '',
      regPsw: '',
    })
    const msgStatus = reactive({status: 0, miss: 0})

    const checkForm = (isForce) => {
      verification.phone = ''
      verification.code = ''
      verification.psw = ''
      verification.regPsw = ''

      if (!form.phone) {
        isForce && (verification.phone = '请输入手机号')
      } else {
        verification.phone = PHONE.REG.test(form.phone) ? '': PHONE.tip
      }
      
      if (!form.code) {
        isForce && (verification.code = '请输入验证码')
      } else if (form.code.length !== 6){
        verification.code = '验证码不合法'
      }
      
      if (!form.psw) {
        isForce && (verification.psw = '请输入密码')
      } else {
        verification.psw = PSW.REG.test(form.psw) ? '': PSW.tip
      }
      
      if (!form.regPsw) {
        isForce && (verification.regPsw = '请输入确认密码')
      } else if (form.psw !== form.regPsw){
        verification.regPsw = '密码不一致'
      }
    }

    watch(form, () => checkForm())

    return {
      form,
      verification,
      msgStatus,
      checkForm
    }
  },
  methods: {
    async submitClick(ev) {
      this.checkForm(true)

      ev.stopPropagation()

      for (let val of Object.values(this.verification)) {
        console.log('verification',this.verification);
        if (val) return openErrorMsg(val, '提示')
      }

      try {
        let psw = encryption(this.form.psw)
        await axios.post(updatePsw, {
          userName: this.form.phone,
          key: this.imgKey,
          code: this.form.code,
          password: psw,
          confirmPwd: psw
        })

        this.$router.replace({ name: 'login' })
      } catch {
        // return this.refer()
      }


    },
    async sendCode() {
      if (!this.form.phone) {
        (this.verification.phone = '请输入手机号')
      } else {
        this.verification.phone = PHONE.REG.test(this.form.phone) ? '': PHONE.tip
      }

      if (this.msgStatus.status !== 0) {
        return;
      }

      this.msgStatus.status = 1
      try {
        await axios.get(sendUserMsg, {
          params: {
            areaNum: 86,
            phoneNum: this.form.phone,
            type: 2
          }
        })
        this.msgStatus.status = 2
        this.msgStatus.miss = 60
        let interval = setInterval(() => {
          if (--this.msgStatus.miss < 0) {
            clearInterval(interval)
            this.msgStatus.status = 0
          }
        }, 1000)

      } catch {
        this.msgStatus.status = 0
      }
    },
    refer() {
      this.imgKey = guid()
    }
  }
}
</script>

<style lang="sass">
@import "./style.scss"
</style>
