<template>
  <div class="login flex row">
    <img class="left" src="../../../static/images/loginbg_left.png" />
    <div class="right flex column">
      <img class="logo" src="../../../static/images/logo.png" />
      <span class="welcome">欢迎登录</span>
      <el-form class="form" ref="loginForm" :model="loginForm" label-width="80px" label-position="top"
        :rules="loginRules" hide-required-asterisk>
        <el-form-item label="账号:" prop="account">
          <el-input v-model="loginForm.account" @keyup.enter.native="handleLogin" placeholder="请输入账号" maxlength="20" clearable></el-input>
        </el-form-item>
        <el-form-item label="密码:" prop="password">
          <el-input v-model="loginForm.password" @keyup.enter.native="handleLogin" placeholder="请输入密码" maxlength="20" show-password clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="loginbtn blackbtn" :loading="loading" @click.native.prevent="handleLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  import {
    login
  } from "@/api/login";
  import store from '@/store/index'
  export default {
    name: "Login",
    data() {
      return {
        loginForm: {
          account: "",
          password: "",
        },
        loginRules: {
          account: [{
            required: true,
            trigger: "blur",
            message: "账号不能为空"
          }],
          password: [{
            required: true,
            trigger: "blur",
            message: "密码不能为空"
          }]
        },
        loading: false,
      };
    },
    watch: {
      $route: {
        handler: function (route) {
          this.redirect = route.query && route.query.redirect;
        },
        immediate: true
      }
    },
    created() {
      store.commit('CLEAR')
    },
    methods: {
      // 登录
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true;
            let params = {
              account: this.loginForm.account,
              password: this.$md5(this.loginForm.password)
            }
            login(params).then(res => {
              if (res.code == 0) {
                store.commit('SET_USER_INFO',res.data)
                store.commit('SET_USER_TOKEN',res.data.token)
                this.$router.push("/goods");
              }
            }).catch(()=>{
              this.loading=false
            });
          }
        });
      }
    }
  };
</script>

<style lang="less">
  .login {
    padding: 0px 0px 0px 0px !important;
    margin: 0px 0px 0px 0px !important;
    background-color: white !important;
    height: 100% !important;
  }
  .left {
    width: 33%;
    height: 100%;
  }

  .right {
    width: 67%;
    background: url("../../../static/images/loginbg_right.png") no-repeat 100% 0%;
    background-size: contain;
    display: flex;
    flex-direction: column;
    padding-left: 70px;
    padding-top: 70px;
    .logo {
      width: 100px;
      height: auto;
    }
    .form {
      font-weight: bold;
      margin-top: 50px;
      margin-left: 50px;
      .el-input {
        width: 400px;
      }
      .loginbtn {
        width: 200px;
        margin-top: 20px;
      }
    }
    .welcome {
      color: #060101;
      font-size: x-large;
      font-weight: bold;
      margin-top: 10px;
    }
  }
</style>