<template>
  <div class="login center_in">
    <div class="card_login_form">
      <div class="form_left center_in">
        <div class="form_login">
          <h3>宝智达-冷链物流运输</h3>
          <div class="card_form">
            <el-input v-model="form.name" size="medium" placeholder="请输入账号"></el-input>
          </div>
          <div class="card_form">
            <el-input v-model="form.password" size="medium" show-password placeholder="请输入密码"></el-input>
          </div>
          <div class="card_form_code">
            <el-input v-model="form.captcha" size="medium" placeholder="请输入验证码"></el-input>
            <div style="margin-left: 5px;cursor: pointer;" @click="refreshCode">
              <identifyingCode :identify-code="identifyCode" :contentWidth="140"></identifyingCode>
            </div>
          </div>
          <div class="card_check">
            <el-checkbox v-model="checked">记住密码</el-checkbox>
          </div>
          <el-button type="primary" size="medium" :loading="loginLoading" @click="getLogin">立即登录</el-button>
        </div>
      </div>
      <div class="image_card">
        <div class="triangle"></div>
        <el-image style="height: 320px;" :src="require('@/assets/images/logistics1.png')"></el-image>
      </div>
    </div>
  </div>
</template>

<script>
  import identifyingCode from '@/components/identifyingCode'
  import {
    mapActions
  } from 'vuex'
  export default {
    name: 'LoginView',
    components: {
      identifyingCode
    },
    data() {
      return {
        form: {
          name: '',
          password: '',
          captcha: ''
        },
        checked: false,
        loginLoading: false,
        identifyCode: "", // 验证码
        identifyCodes: '1234567890', //验证码随机范围
      }
    },
    created() {
      this.getuserpwd()
    },
    mounted() {
      this.refreshCode()
    },
    methods: {
      ...mapActions(['Login', 'Logout']),
      getLogin() {
        if (!this.form.captcha) {
          this.$message({
            message: '请输入验证码',
            type: 'warning'
          });
        } else {
          if (this.identifyCode != this.form.captcha) {
            this.refreshCode()
            this.$message({
              message: '请输入正确的验证码',
              type: 'warning'
            });
          } else {
            const {
              Login
            } = this
            if (this.form.name && this.form.password) {
              this.loginLoading = true
              Login({
                type: 1,
                username: this.form.name,
                password: this.form.password,
              }).then((res) => {
                if (this.checked) {
                  let arrList = JSON.stringify(this.form)
                  localStorage.setItem("userData", arrList);
                }
                // 登录成功，跳转至首页
                this.$router.push({
                  path: '/'
                })
                this.loginLoading = false
              }).catch(() => {
                this.loginLoading = false
              })
            } else {
              this.$message({
                message: '账号密码不能为空',
                type: 'warning'
              });
            }
          }
        }
      },
      getuserpwd() {
        const arrList = localStorage.getItem('userData')
        if (arrList) {
          const arrData = JSON.parse(arrList)
          this.form.name = arrData.name
          this.form.password = arrData.password
          this.checked = true
        }
      },
      // 验证码点击刷新方法
      refreshCode() {
        this.identifyCode = ''
        // 4位验证码可以根据自己需要来定义验证码位数
        this.makeCode(this.identifyCodes, 4)
      },
      // 随机数
      randomNum(min, max) {
        max = max + 1
        return Math.floor(Math.random() * (max - min) + min)
      },
      // 随机生成验证码字符串
      makeCode(data, len) {
        for (let i = 0; i < len; i++) {
          this.identifyCode += data[this.randomNum(0, data.length - 1)]
        }
        console.log("随机验证码:", this.identifyCode)
      },
    }
  }
</script>

<style lang="scss">
  .login {
    -webkit-touch-callout: none;
    /* iOS Safari */
    -webkit-user-select: none;
    /* Chrome/Safari/Opera */
    -khtml-user-select: none;
    /* Konqueror HTML */
    -moz-user-select: none;
    /* Firefox */
    -ms-user-select: none;
    /* Internet Explorer/Edge */
    user-select: none;
    height: 100vh;
    background-color: #f3f7f9;
  }

  .card_login_form {
    display: flex;
    width: 800px;
    height: 450px;
    box-shadow: 0 0 20px 0 rgba(12, 1, 4, .05);
    background: #fff;
    border-radius: 10px;
    overflow: hidden;
  }

  .form_left {
    flex: 1;
  }

  .form_login {
    width: 100%;
    display: flex;
    flex-direction: column;
    padding: 0 60px;
  }

  .card_form {
    margin-bottom: 20px;
  }

  .card_form_code {
    display: flex;
  }

  .image_card {
    position: relative;
    display: flex;
    align-items: flex-end;
    justify-content: flex-end;
    flex: 1;
    background: linear-gradient(to bottom, #46c4bd, #cdefed);
  }

  .triangle {
    position: absolute;
    left: 0;
    width: 0px;
    height: 0px;
    border-width: 0px 0px 450px 60px;
    border-style: solid;
    border-left-color: #fff;
    border-bottom-color: transparent;
  }

  .card_check {
    margin-bottom: 20px;
    display: flex;
  }
</style>
