<template>
  <div>
    <el-form :model="ruleForm" :rules="rules" ref="form" @submit.native.prevent>
      <el-form-item prop="account">
        <el-input v-model="ruleForm.account" placeholder="邮箱/手机号" @keyup.enter.native="handleSubmit">
          <i slot="prefix" class="el-input__icon el-icon-mobile-phone"></i>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="ruleForm.password" placeholder="请输入验证码" @keyup.enter.native="handleSubmit">
          <i slot="prefix" class="el-input__icon el-icon-unlock"></i>
          <countdown
            slot="suffix"
            size="mini"
            :disabled="disabledSend"
            @start="sendCodeRequest"
            ref="countdown"
          ></countdown>
        </el-input>
      </el-form-item>
      <el-checkbox v-model="ruleForm.RememberMe" style="margin-bottom: 10px">记住我</el-checkbox>
      <el-button type="primary" class="submit-button" @click="handleSubmit">登录</el-button>
    </el-form>
    <div class="login-footer">
      <router-link to="index">密码登录</router-link>
      <router-link to="../password">忘记密码</router-link>
    </div>
  </div>
</template>

<script>
import * as api from '@/api/account'
import countdown from './countdown.vue'

export default {
  name: 'LoginCode',
  components: { countdown },
  data() {
    return {
      ruleForm: { type: 2, account: '', password: '', RememberMe: false, service: 'https://h5.ezijing.com' },
      rules: {
        account: [{ required: true, message: '请输入邮箱/手机号', trigger: 'change' }],
        password: [{ required: true, message: '请输入验证码', trigger: 'change' }]
      },
      loading: false
    }
  },
  computed: {
    // 重定向地址
    nextUrl() {
      const nextUrl = window.sessionStorage.getItem('next_url')
      return nextUrl ? decodeURIComponent(nextUrl) : ''
    },
    disabledSend() {
      return !/^1[3-9]\d{9}$/.test(this.ruleForm.account)
    }
  },
  methods: {
    // 提交
    handleSubmit() {
      this.$refs.form.validate().then(this.handleRequest)
    },
    // 登录
    handleRequest() {
      this.loading = true
      api.login(this.ruleForm).then(response => {
        response.code === 0 ? this.handleSuccess(response) : this.$message.error(response.msg)
        this.loading = false
      })
    },
    // 登录成功
    handleSuccess(response) {
      this.$message({ type: 'success', message: '登录成功' })
      if (this.nextUrl) {
        window.location.href = this.nextUrl
      } else {
        this.$emit('success', response)
      }
    },
    // 验证码
    sendCodeRequest() {
      api
        .sendCode({ account: this.ruleForm.account })
        .then(response => {
          console.log(response)
          if (response.code === 0) {
            this.$message({ type: 'success', message: '验证码已发送' })
          } else {
            // 停止计时
            this.$refs.countdown.stop()
            this.$message({ type: 'error', message: response.msg })
          }
        })
        .catch(this.$refs.countdown.stop)
    }
  }
}
</script>
