<template>
  <div class="login-container">
    <el-card :body-style="{padding:0}" shadow="none">
      <div slot="header" class="clearfix">
        <span>数据资源中心</span>
<!--        <div style="float: right; color: #666666">-->
<!--          服务热线：<span style="color: #FF970F;font-weight: bold">400-698-5980</span>-->
<!--        </div>-->
      </div>

      <div style="min-height: 500px;">
        <el-row style="width: 1000px;margin: 99px auto 0 auto;overflow: hidden;">
          <el-col :span="16">
            <el-image src="http://lcadmin.tongyu99.com/statics/image/dl01.png" />
          </el-col>
          <el-col :span="8">
            <el-card shadow="always">

              <div slot="header" class="clearfix">
                <strong>用户登录</strong>
              </div>
              <el-form
                ref="loginForm"
                :model="loginForm"
                class="login-form"
                auto-complete="on"
                label-position="left"
              >
                <el-form-item prop="username">
                  <el-input
                    ref="username"
                    v-model="loginForm.username"
                    placeholder="请输入手机号/邮箱"
                    name="username"
                    type="text"
                    auto-complete="on"
                  >
                    <span slot="prepend" class="svg-container el-icon-user-solid" />
                  </el-input>
                </el-form-item>

                <el-form-item prop="password">
                  <el-input
                    :key="passwordType"
                    ref="password"
                    v-model="loginForm.password"
                    :type="passwordType"
                    placeholder="请输入登录密码"
                    name="password"
                    auto-complete="on"
                    @keyup.enter.native="handleLogin"
                  >
                    <span slot="prepend" class="svg-container el-icon-lock" />
                    <span slot="append" class="show-pwd" @click="showPwd">
                      <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
                    </span>
                  </el-input>
                </el-form-item>

                <el-row>
                  <el-col :span="16">
                    <el-form-item prop="password">

                      <el-input
                        ref="captcha"
                        v-model="loginForm.captcha"
                        placeholder="请输入验证码"
                        name="captcha"
                        type="text"
                      >
                        <span slot="prepend" class="svg-container el-icon-warning" />

                      </el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8" style="text-align: right">
                    <el-image
                      v-if="captcha && captcha!==''"
                      :src="captcha"
                      style="width: 80px"
                      class="show-pwd"
                      @click="refreshCaptcha"
                    />
                  </el-col>
                </el-row>

                <el-button
                  :loading="loading"
                  type="primary"
                  style="width: 100%; margin-bottom: 30px"
                  @click.native.prevent="handleLogin"
                >
                  登录
                </el-button>
              </el-form>
            </el-card>
          </el-col>
        </el-row>
      </div>

    </el-card>

    <div class="footer">
      ©2022 智慧旅游 版权所有 All Rights Reserved.
    </div>
  </div>
</template>

<script>
import { getCaptcha } from '@/api/oauth'

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
        captcha: ''
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      token: '',
      captcha: ''
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.refreshCaptcha()
  },
  methods: {
    refreshCaptcha() {
      getCaptcha().then((v) => {
        console.log(v)
        this.token = v.token
        this.captcha = v.captcha
      })
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', { ...this.loginForm, token: this.token })
            .then(() => {
              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="css">
.footer {
  height: 68px;
  line-height: 68px;
  color: #333;
  text-align: center;
  background: #eaeaea;
  font-size: 12px;
}

.footer a {
  color: #333;
}

.footer a:hover {
  color: #f08200;
}
</style>
