<template>
  <div class="main">
    <a-form
      id="formLogin"
      ref="formLogin"
      :form="form"
      class="user-layout-login"
      @submit="handleSubmit"
    >
      <a-form-item class="inputT">
        <a-input
          :placeholder="$t('user.login.mobile.placeholder')"
          size="large"
          type="text"
          oninput="value=value.replace(/[, ]/g,'')"
          onkeyup="value=value.replace(/[, ]/g,'')"
          onblur="value=value.replace(/\s+/g,'')"
          v-model="postData.phone"
        >
          <template v-slot:prefix>
            <a-icon :style="{ color: 'rgba(0,0,0,.25)' }" type="mobile" />
          </template>
        </a-input>
      </a-form-item>
      <!-- <a-form-item class="inputT">
            <a-button
              :disabled="getCodeBtn"
              class="login-button"
              size="large"
              type="primary"
              @click="getCaptcha"
            >
              获取验证码
            </a-button>
          </a-form-item> -->
      <a-form-item class="inputT">
        <a-input
          placeholder="请输入密码"
          size="large"
          type="password"
          oninput="value=value.replace(/[, ]/g,'')"
          onkeyup="value=value.replace(/[, ]/g,'')"
          onblur="value=value.replace(/\s+/g,'')"
          v-model="postData.passWord"
        >
          <template v-slot:prefix>
            <a-icon :style="{ color: 'rgba(0,0,0,.25)' }" type="lock" />
          </template>
        </a-input>
      </a-form-item>
      <a-form-item class="inputT">
        <a-input
          placeholder="请再次输入密码"
          size="large"
          type="password"
          oninput="value=value.replace(/[, ]/g,'')"
          onkeyup="value=value.replace(/[, ]/g,'')"
          onblur="value=value.replace(/\s+/g,'')"
          v-model="postData.passWordAgain"
        >
          <template v-slot:prefix>
            <a-icon :style="{ color: 'rgba(0,0,0,.25)' }" type="lock" />
          </template>
        </a-input>
      </a-form-item>
      <a-form-item class="inputT">
        <a-input
          placeholder="请输入验证码"
          size="large"
          oninput="value=value.replace(/[, ]/g,'')"
          onkeyup="value=value.replace(/[, ]/g,'')"
          onblur="value=value.replace(/\s+/g,'')"
          v-model="postData.code"
        >
          <template v-slot:prefix>
            <a-icon :style="{ color: 'rgba(0,0,0,.25)' }" type="security-scan" />
          </template>
        </a-input>
      </a-form-item>
      <a-form-item class="tologin">
        <a @click="getCaptcha" :class="codeButton ? '' : 'disable'">{{ codeText }}</a>
        <a class="login-form-forgot" @click="toLogin" >
          去登录
        </a>
      </a-form-item>
      <a-form-item class="inputT">
        <a-button
          :disabled="btn"
          class="login-button"
          htmlType="submit"
          size="large"
          type="primary"
        >
          确认
        </a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script>
// import { timeFix } from '@/utils/util'
import { getSmsCaptcha, forgetPassword } from '@/api/login'

export default {
  name: 'Login',
  data () {
    return {
      form: this.$form.createForm(this),
      postData: {
            phone: '',
            code: '',
            passWord: '',
            passWordAgain: ''
      },
      btn: true,
      codeButton: true,
      timer: 0,
      codeText: '获取验证码'
    }
  },
  watch: {
     postData: {
          handler (postData) {
            if (postData.phone.length === 11 && postData.code === '') {
              // 监听用户输入的账号和密码
               if (this.codeText === '获取验证码') {
                 this.codeButton = true
                  }
            } else {
              this.codeButton = false
            }
            if (
            postData.phone.length === 11 &&
            postData.code.length >= 4 &&
            postData.passWord.length >= 6 &&
            postData.passWordAgain.length >= 6
            ) {
              this.btn = false
              this.codeButton = false
            } else {
              if (postData.phone.length === 11 && postData.code.length > 0) {
                this.codeButton = false
              }
              this.btn = true
            }
          },
          deep: true,
          immediate: true
        }
  },
  methods: {
    handleSubmit (e) {
      e.preventDefault()

      forgetPassword({
        mobileNo: this.postData.phone,
        password: this.postData.passWord,
        password_confirmation: this.postData.passWordAgain,
        code: this.postData.code
      })
      .then((res) => this.loginSuccess(res))
      .catch(err => this.requestFailed(err))
      .finally(() => {
        this.loginBtn = false
      })
    },
    getCaptcha (e) {
      if (this.codeButton) {
        getSmsCaptcha({ mobileNo: this.postData.phone, type: 'FORGET' }).then(() => {
          this.$notification['success']({
            message: '提示',
            description: '验证码获取成功',
            duration: 8
          })
          this.codeButton = false
           var count = 60
           this.codeText = count + '秒后重新发送'
           if (this.timer) {
             clearInterval(this.timer)
              }
              this.timer = setInterval(() => {
                count--
                this.codeText = count + '秒后重新发送'
                if (count < 1) {
                  clearInterval(this.timer)
                  this.codeButton = true
                  this.codeText = '获取验证码'
                  }
                  }, 1000)
        }).catch(err => {
          this.requestFailed(err)
        })
      }
    },
    toLogin () {
      this.$router.push({ name: 'Login' })
    },
    loginSuccess (res) {
      this.toLogin()
      // 延迟 1 秒显示欢迎信息
      setTimeout(() => {
        this.$notification.success({
          message: '密码重置成功'
          // description: `${ timeFix() }，欢迎回来`
        })
      }, 300)
    },
    requestFailed (err) {
      this.$notification['error']({
        message: '错误',
        // description: ((err || {}).data || {}).message || '请求出现错误，请稍后再试',
        description: err.message || '请求出现错误，请稍后再试',
        duration: 4
      })
    }
  }
}
</script>

<style lang="less" scoped>
.user-layout-login {
  label {
    font-size: 14px;
  }

  .getCaptcha {
    display: block;
    width: 100%;
    height: 40px;
  }

  .forge-password {
    font-size: 14px;
  }

  button.login-button {
    padding: 0 15px;
    font-size: 16px;
    height: 40px;
    width: 100%;
  }

  .user-login-other {
    text-align: left;
    margin-top: 24px;
    line-height: 22px;

    .item-icon {
      font-size: 24px;
      color: rgba(0, 0, 0, 0.2);
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: #1890ff;
      }
    }

    .register {
      float: right;
    }
  }
}
.inputT{
  padding: 8px 24px !important;
}
.tologin{
  display: flex;
  justify-content: space-around;
  a{
    margin: 0 40px;
  }
}
.ant-form-item{
  margin: 0;
}
/deep/ .disable{
  color:#d9d9d9 !important;
}
// .ant-input-lg{
//   background-color: red !important;
// }
</style>
