<template>
  <div class="login" >
    <div class="bg" v-if="erp"><img :src="bg" alt="" style="width:100%;height:100%;"></div>
    <div class="logo" v-if="erp">
      <img :src="logo" alt="">
      <span class="logo_txt">智慧园区场景管理</span>
    </div>
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" v-if="erp">
      <h3 class="title">用户登录 <span>LOGIN</span></h3>
      <el-form-item prop="username" >
        <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          :type="passwordtxt"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
          <svg-icon slot="prefix" icon-class="eye"  class="el-input__icon input-icon"  style="position:absolute;right:-340px"  @click="eyeTab" v-show="passwordtxt =='password'" />
          <svg-icon slot="prefix" icon-class="eye-open"  class="el-input__icon input-icon"  style="position:absolute;right:-340px"  @click="eyeTab" v-show="passwordtxt =='text'" />
        </el-input>

      </el-form-item>
      <el-form-item prop="code" v-if="captchaOnOff">
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img"/>
        </div>
      </el-form-item>
      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;"  @change="memory">记住密码</el-checkbox>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <!-- <div style="float: right;" v-if="register">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div> -->
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer" v-if="erp">
      <!-- <span>Copyright © 2018-2021 yongtian.vip All Rights Reserved.</span> -->
    </div>
    <div id="loader-wrapper" v-if="ERPloading">
        <div id="loader"></div>
        <div class="loader-section section-left"></div>
        <div class="loader-section section-right"></div>
        <div class="load_title">正在加载系统资源，请耐心等待</div>
    </div>
  </div>
</template>

<script>
import bgImg from "@/assets/images/bg.png";
import logoImg from "@/assets/logo/logo-b.png";
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      erp:false,
      ERPloading:true,
      passwordtxt:"password",
      bg:bgImg,
      logo:logoImg,
      codeUrl: "",
      cookiePassword: "",
      loginForm: {
        username: "admin",
        password: "12345678",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaOnOff: false,
      // 注册开关
      register: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getUrl()
  },
  methods: {
    getUrl(){
      let url = this.$route.query
      if(url.username && url.password){
        let data = {username:url.username,password:url.password}
        this.handleLoginERP(data)
      }else{
        this.getCode();
        this.getCookie();
      }
    },
    eyeTab(){
      if(this.passwordtxt == "password"){
        this.passwordtxt = "text"
      }else{

        this.passwordtxt = "password"
      }

    },
    getCode() {
      getCodeImg().then(res => {
        this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + res.data.img;
          this.loginForm.uuid = res.data.uuid;
          this.erp = true
          setTimeout(()=>{
            this.ERPloading = false
          },)
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username20220808");
      const password = Cookies.get("password20220808");
      const rememberMe = Cookies.get('rememberMe20220808')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.$store.dispatch("Login", this.loginForm).then((res) => {
            this.$router.push({ path: "/Middleware/bpmn"}).catch(()=>{});
          }).catch(() => {
            this.loading = false;
            if (this.captchaOnOff) {
              this.getCode();
            }
          });
        }
      });
    },
    memory(){
      if (this.loginForm.rememberMe) {
        Cookies.set("username20220808", this.loginForm.username, { expires: 30 });
        Cookies.set("password20220808", encrypt(this.loginForm.password), { expires: 30 });
        Cookies.set('rememberMe20220808', this.loginForm.rememberMe, { expires: 30 });
      } else {
        Cookies.remove("username20220808");
        Cookies.remove("password20220808");
        Cookies.remove('rememberMe20220808');
      }
    },
    // handleLoginERP(data) {//erp登录
    //   this.$store.dispatch("LoginERP", data).then((res) => {
    //     this.$router.push({ path: "/index"}).catch(()=>{});
    //   }).catch((err) => {
    //     location.href = '/center/#/';
    //   });
    // }
  }
};
</script>

<style  lang="scss" scoped>
.bg{
  width:100%;
  height:100%;
  overflow: hidden;
  position: fixed;
  top:0;
  left:0;
  z-index: -1;
  .bgimg{
    width:100%;
    height:100%;
    position: absolute;
    top:0;
    left:0;
    z-index: -1;
  }
}
.logo{
  width:100%;
  position: fixed;
  top:0;
  img{
    width:152px;
    margin:10px 2rem 0 20px;
    vertical-align: middle;
  }
  .logo_txt{
    font-size: 1.5rem;
    color:#fff;
    vertical-align: middle;
    letter-spacing: 4px;
  }
}
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  // background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: left;
  color: #000000;
  font-size: 20px;
  span{
    margin-left:10px;
    opacity: 0.25;
  }
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 456px;
  padding: 63px 35px;
  .el-input {
    height: 40px;
    font-size: 16px !important;
    input {
      height: 40px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 36px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
    width:100%;
    height: 36px;
    margin-top:-1px;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
::v-deep .el-input__inner{
  height:36px !important;
  line-height: 36px !important;
}
</style>
<style>
html,
body,
#app {
    height: 100%;
    margin: 0px;
    padding: 0px;
    background-color: transparent !important;

}
</style>
