<template>
  <div class="login">
    <div class="loginLogo"></div>
    <div class="loginText">
      <h2>数智牧场综合管理系统</h2>
      <div class="loginForm">
        <el-input
            style="height: 50px; line-height: 50px;"
            placeholder="请输入登录名"
            v-model="userName"
            size="large">
          <i class="el-icon-user-solid" slot="prefix" style="font-size: 28px; line-height: 50px; margin-left: 5px"></i>
        </el-input>
        <div style="height: 20px"></div>
        <el-input
            style="height: 50px"
            placeholder="请输入登录密码"
            v-model="passWord"
            type="password"
            size="large">
          <i class="pwd" slot="prefix"></i>
        </el-input>
        <div style="height: 20px"></div>
        <el-row :gutter="20">
          <el-col :span="15">
            <el-input
                style="height: 50px"
                placeholder="请输入验证码"
                v-model="code"
                size="large">
              <i class="pwd" slot="prefix"></i>
            </el-input>
          </el-col>
          <el-col :span="9">
            <img id="kaptchaImage" :src=" ip + '/admin/my/send'" @click="refset" alt="">
          </el-col>
        </el-row>
        <div style="height: 20px"></div>
        <el-button @click="onSubmit" style="width: 100%; height: 50px; background-color: #1ABC9C; color: #fff;" >登录</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { Login, getFarmId} from '../../utils/api';
import { mapState, mapActions} from 'vuex'
export default {
  name: "Login",
  data() {
    return {
      userName: '',
      passWord: '',
      code: '',
    }
  },
  computed: {
    ...mapState(['ip'])
  },
  methods: {
    ...mapActions(['setFarmIdAsync']),
    onSubmit() {
      let params = {
        accountName: this.userName,
        password: this.passWord,
        code: this.code,
      }
      Login(params).then(res => {
        if(res.code === 10000) {
          localStorage.setItem('accessToken', res.data.token);
          localStorage.setItem('UserName', res.data.accountName);
          localStorage.setItem('UserId', res.data.id);
          this.$message.success('登录成功');
          getFarmId().then(res => {
            if(res.code === 10000) {
              this.setFarmIdAsync(res.data);
              localStorage.setItem('lastFarmId', res.data);
              let url = this.$route.query.redirect;
              if(url) {
                this.$router.replace(url);
              } else {
                this.$router.replace('/');
              }
            }
          })
        } else {
          this.$message.error(res.message);
          this.refset();
        }
      })
    },
    // 刷新
    refset() {
      let img = document.getElementById('kaptchaImage');
      img.setAttribute('src', this.ip + '/admin/my/send?'+ Math.floor(Math.random() * 100));
    }
  }
}
</script>

<style scoped>
  /deep/.el-input__inner {
    height: 50px;
    line-height: 50px;
    padding: 0 0 0 50px;
  }
  .login {
    width: 100%;
    height: 100vh;
    background-color: #1ABC9C;
    position: relative;
  }
  .loginLogo {
    width: 100%;
    height: 400px;
    background-image: url("../../assets/images/login.png");
    background-size: 100% 100%;
    position: absolute;
    top: 50%;
    margin: -300px 0 0 0;
  }
  .loginText {
    width: 420px;
    height: 400px;
    background-color: #fff;
    border-radius: 10px;
    position: absolute;
    top: 50%;
    left: 50%;
    margin: -250px 0 0 -210px;
    box-sizing: border-box;
    padding: 10px;
    box-shadow: 0 0 10px rgb(0 0 0 / 35%);
    text-align: center;
  }
  h2 {
    font-size: 28px;
    color: #1ABC9C;
    font-weight: 600;
    padding-top: 10px;
  }
  .loginForm {
    width: 280px;
    margin: 40px auto 0;
    /*height: 300px;*/
  }
  .pwd {
    display: inline-block;
    width: 28px;
    height: 50px;
    font-size: 28px;
    background-image: url("../../assets/images/pwd.png");
    background-repeat: no-repeat;
    background-size: 100% 60%;
    background-position: 100% 42%;
    line-height: 50px;
    margin-left: 5px
  }
</style>
