<script>
import Vue from 'vue'

export default Vue.extend({
  name: 'Index',
  data() {
    return {
      username: '',
      password: '',
      savePassword: false,
      loading: false,
      isLogin: true,
      env: process.env.NODE_ENV
    }
  },
  mounted() {
    this.getSavedAccount()
  },
  methods: {
    getSavedAccount() {
      const account = localStorage.getItem('savedAccount')
      if (account) {
        this.username = account.username
        this.password = account.password
        this.savePassword = true
      }
    },
    login() {
      if (!this.username) {
        this.$message.error('请输入用户名')
        return
      }
      if (!this.password) {
        this.$message.error('请输入密码')
        return
      }
      if (this.loading) {
        return
      }
      this.loading = true
      this.$store.dispatch('login', {
        username: this.username,
        password: this.password,
        login_type: 0,
        login_portal: 0,
        savePassword: this.savePassword
      }).then(res => {
        this.loading = false
        this.$router.push('/dashboard')
      }).catch(err => {
        this.loading = false
        this.$message.error(err.message)
      })
    }
  }
})
</script>

<template>
  <div class="body">
    <div class="login-cont">
      <div class="image-left">
        <img class="image" src="/static/image/login.webp" alt="">
        <div class="title">展会服务系统</div>
      </div>
      <div class="cont-right">
        <div class="title">
          <div @click="isLogin=true" :class="['item',isLogin?'active':'']">登录</div>
          <div @click="isLogin=false" :class="['item',isLogin?'':'active']">注册</div>
        </div>
        <el-input v-model="username" prefix-icon="el-icon-user" placeholder="请输入用户名" class="input" />
        <el-input v-model="password" prefix-icon="el-icon-unlock" placeholder="请输入密码" show-password class="input" />
        <el-checkbox v-model="savePassword">记住密码</el-checkbox>
        <el-button type="primary" class="button" @click="login">
          <span>登录</span>
          <span class="el-icon-loading" v-if="loading"></span>
        </el-button>
      </div>
    </div>
  </div>
</template>

<style scoped>
  .body{
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background-image: linear-gradient(120deg,#F7FAFF,#A6C5FE);
    .login-cont{
      background: white;
      overflow: hidden;
      display: grid;
      border-radius: 12px;
      box-shadow: 0 0 12px #00000011;
      grid-template-columns: 1fr 1fr;
      .image-left{
        padding: 40px;
        background: #123068;
        position: relative;
        .image{
          position: absolute;
          object-fit: cover;
          left: 0;
          top: 0;
          width: 100%;
          height: 100%;
        }
        .title{
          position: relative;
          color: white;
          font-size: 28px;
          font-weight: bold;
        }
      }
      .cont-right{
        padding: 40px;
        display: flex;
        flex-direction: column;
        grid-gap: 16px;
        .title{
          height: 36px;
          font-weight: bold;
          margin-bottom: 26px;
          display: flex;
          align-items: flex-end;
          gap: 20px;
          .item{
            line-height: 1.3;
            transition-duration: 300ms;
            font-size: 27px;
            cursor: pointer;
            display: flex;
            flex-direction: column;
            align-items: center;
            color: gray;
            &::after{
              transition-duration: 300ms;
              width: 40%;
              content: '';
              display: block;
              height: 4px;
              border-radius: 2px;
              background-color: lightgray;
            }
            &.active{
              font-size: 28px;
              color: black;
              &::after{
                width: 80%;
                background-color: #2563EB;
              }
            }
          }
        }
        .button{
          font-size: 18px;
          margin-top: 48px;
          width: 400px;
        }
        .test-button{
          margin-left: unset;
          font-size: 18px;
          width: 400px;
        }
        .input{
          font-size: 16px;
          width: 400px;
        }
      }
    }
  }
</style>
