<template>
  <el-row type="flex" justify="center">
    <el-col :xs="24" :sm="18" :md="12" :lg="9" :xl="6">
      <el-form ref="setAccountform" :model="setAccount" :rules="accountRules">
        <el-form-item prop="user">
          <el-input class="self-input" v-model="setAccount.user" type="text" placeholder="手机号" @keyup.enter.native="onSubmitSetAccount">
          </el-input>
        </el-form-item>
        <el-form-item prop="pwd">
          <el-input v-model="setAccount.pwd" type="password" placeholder="短信验证码" @keyup.enter.native="onSubmitSetAccount">
            <el-button slot="suffix" size="mini" :disabled="isSendCode" @click="sendCode">{{sendBtnText}}</el-button>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="login-btn" @click="onSubmitSetAccount">登录</el-button>
        </el-form-item>
        <div class="text">
          <span class="code-login">
            <router-link class="router-link-class" active-class="router-link-active-class" :to="{ name: 'normalLogin' }">密码登录</router-link>
          </span>
          <span class="forget-pwd">
            <router-link class="router-link-class" active-class="router-link-active-class" :to="{ name: 'forgetPwd' }">忘记密码</router-link>
          </span>
        </div>
      </el-form>
    </el-col>
  </el-row>
</template>

<script>
  export default {
    data () {
      return {
        isSendCode: false, // 是否已发送验证码
        timeInterval: null, // 定时器，倒计时
        sendBtnText: '发送验证码', // 按钮 文字
        setAccount: {},
        accountRules: {
          'user': [
            { required: true, message: '请输入手机号', trigger: 'change' },
            { pattern: /^1[3-9]\d{9}$/, message: '请输入正确格式的手机号', trigger: 'change' }
          ],
          'pwd': [
            { required: true, message: '请输入短信验证码', trigger: 'change' }
          ]
        }
      }
    },
    beforeMount () {
    },
    beforeDestroy () {
      /* 清空倒计时 */
      this.clearTime()
    },
    methods: {
      /* 发送验证码 */
      sendCode () {
        /* 发送验证码不管是否成功，都开始倒计时 */
        let time = 60
        this.isSendCode = true
        this.sendBtnText = '60s后重发'
        this.timeInterval = setInterval(() => {
          if (time-- > 0) {
            this.sendBtnText = time + 's后重发'
          } else {
            this.isSendCode = false
            this.sendBtnText = '发送验证码'
            clearInterval(this.timeInterval)
          }
        }, 1000)
      },
      /* 清空倒计时 */
      clearTime () {
        this.isSendCode = true
        this.sendBtnText = '发送验证码'
        clearInterval(this.timeInterval)
      },
      onSubmitSetAccount () {
        this.$refs['setAccountform'].validate((valid) => {
          if (valid) {
            if (this.setAccount.user === 'admin@qq.com' && this.setAccount.pwd === '123456') {
              /* 跳转 */
              this.$router.push({ path: '/app/my-learn/course' })
            } else {
              this.$message.error('用户名或密码错误')
            }
            /* 重置密码 */
            this.$refs['setAccountform'].resetFields()
          } else {
            this.$message.error('请检查输入项，确认无误后，重新提交')
            return false
          }
        })
      }
    }
  }
</script>
