<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('loginPwd.account.placeholder')" @keyup.enter="handleSubmit">
          <template #prefix>
            <el-icon class="el-input__icon"><User /></el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password" v-model="ruleForm.password" :placeholder="$t('loginPwd.password.placeholder')" @keyup.enter="handleSubmit">
          <template #prefix>
            <el-icon class="el-input__icon"><Lock /></el-icon>
          </template>
        </el-input>
      </el-form-item>
      <router-link :to="{ path: '../password', query: $route.query }" class="forget-password-text" v-if="hasForgetPassword">
        {{ $t('login.forgetPassword') }}
      </router-link>
      <!-- <el-checkbox v-if="hasRemember" v-model="ruleForm.RememberMe" style="margin-bottom: 10px">{{ $t('login.rememberMe') }}</el-checkbox> -->
      <el-button type="primary" class="submit-button" :loading="loading" @click="handleSubmit">
        {{ $t('login.submitText') }}
      </el-button>
      <router-link :to="{ path: 'code', query: $route.query }" class="login-method-text" v-if="hasCodeLogin">
        {{ $t('login.codeLogin') }}
      </router-link>
    </el-form>
  </div>
</template>

<script>
import * as api from '@/api/account'
import { User, Lock } from '@element-plus/icons-vue'

export default {
  name: 'LoginAccount',
  components: { User, Lock },
  props: {
    hasCodeLogin: { type: Boolean, default: true },
    hasForgetPassword: { type: Boolean, default: true },
    hasRemember: { type: Boolean, default: true },
    headers: { type: Object, default: () => ({}) }
  },
  data() {
    return {
      ruleForm: { type: 1, account: '', password: '', RememberMe: true, service: 'https://h5.ezijing.com' },
      rules: {
        account: [{ required: true, message: this.$t('loginPwd.account.rule'), trigger: 'change' }],
        password: [{ required: true, message: this.$t('loginPwd.password.rule'), trigger: 'change' }]
      },
      loading: false
    }
  },
  computed: {
    // 重定向地址
    nextUrl() {
      const nextUrl = window.sessionStorage.getItem('next_url')
      return nextUrl ? decodeURIComponent(nextUrl) : ''
    }
  },
  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('loginPwd.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.sessionStorage.clear()
    }
  }
}
</script>
