<template>
  <div>
    <el-form :model="ruleForm" :rules="rules" ref="form" @submit.prevent>
      <el-form-item prop="account">
        <el-input v-model="ruleForm.account" :placeholder="$t('loginCode.account.placeholder')" @keyup.enter="handleSubmit">
          <template #prefix>
            <el-icon class="el-input__icon"><Iphone /></el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="ruleForm.password" :placeholder="$t('loginCode.code.placeholder')" @keyup.enter="handleSubmit">
          <template #prefix>
            <el-icon class="el-input__icon"><Unlock /></el-icon>
          </template>
          <template #suffix>
            <countdown size="small" :disabled="disabledSend" @start="sendCodeRequest" ref="countdown"></countdown>
          </template>
        </el-input>
      </el-form-item>
      <!-- <el-checkbox v-model="ruleForm.RememberMe" style="margin-bottom: 10px">{{ $t('login.rememberMe') }}</el-checkbox> -->
      <el-button type="primary" class="submit-button" @click="handleSubmit">
        {{ $t('login.submitText') }}
      </el-button>
      <router-link :to="{ path: 'index', query: $route.query }" class="login-method-text">
        {{ $t('login.pwdLogin') }}
      </router-link>
    </el-form>
  </div>
</template>

<script>
import * as api from '@/api/account'
import countdown from './countdown.vue'
import { Iphone, Unlock } from '@element-plus/icons-vue'

export default {
  name: 'LoginCode',
  props: {
    sendcode: { type: Object, default: () => ({}) },
    headers: { type: Object, default: () => ({}) }
  },
  components: { Iphone, Unlock, countdown },
  data() {
    return {
      ruleForm: { type: 2, account: '', password: '', RememberMe: false, service: 'https://h5.ezijing.com' },
      rules: {
        account: [{ required: true, message: this.$t('loginCode.account.rule'), trigger: 'change' }],
        password: [{ required: true, message: this.$t('loginCode.code.rule'), trigger: 'change' }]
      },
      loading: false
    }
  },
  computed: {
    // 重定向地址
    nextUrl() {
      const nextUrl = window.localStorage.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
      const params = Object.assign({}, this.$route.query, this.ruleForm)
      api.login(params, { headers: this.headers }).then(response => {
        response.code === 0 ? this.handleSuccess(response) : this.$message.error(response.msg)
        this.loading = false
      })
    },
    // 登录成功
    handleSuccess(response) {
      this.$message({ type: 'success', message: this.$t('loginCode.successMessage') })
      const { info } = response.data
      if (this.nextUrl) {
        let queryString = ''
        if (info) {
          const temp = `id=${info.id}&username=${info.username}&mobile=${info.mobile}`
          queryString = this.nextUrl.includes('?') ? `&${temp}` : `?${temp}`
        }
        window.location.href = this.nextUrl + queryString
      } else {
        this.$emit('success', response)
      }
      // window.localStorage.clear()
    },
    // 验证码
    sendCodeRequest() {
      const params = Object.assign({}, this.sendcode, { account: this.ruleForm.account })
      api
        .sendCode(params, { headers: this.headers })
        .then(response => {
          console.log(response)
          if (response.code === 0) {
            this.$message({ type: 'success', message: this.$t('loginCode.sendCodeMessage') })
          } else {
            // 停止计时
            this.$refs.countdown.stop()
            this.$message({ type: 'error', message: response.msg })
          }
        })
        .catch(this.$refs.countdown.stop)
    }
  }
}
</script>
