<template>
  <div class="login">
    <div class="loginIcon">
      <img :src="require('@/assets/images/loginIcon.png')" alt=""/>
    </div>
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <div class="imgDiv" @click="isComputer = !isComputer">
        <img class="computer" v-show="!isComputer" src="@/assets/images/computer.png" alt=""/>
        <img class="ewm" v-show="isComputer" src="@/assets/images/ewm.png" alt=""/>
        <div class="zz"></div>
      </div>
      <h3 class="title">门店拜访管理系统</h3>
      <div v-show="isComputer">
        <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="password"
            auto-complete="off"
            placeholder="密码"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
          </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;">记住密码</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>
      </div>
      <div class="ewmDiv" v-show="!isComputer">
        <div id="wx_login"></div>
<!--        <div class="info">请使用企业微信扫描二维码登录</div>-->
      </div>
    </el-form>

    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2024-2025 </span>
    </div>
  </div>
</template>

<script>
import { getCodeImg,getWorkWeixinInfo,getQrCodeLogin } from "@/api/login";
import { setToken } from '@/utils/auth'
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        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: true,
      // 注册开关
      register: false,
        redirect: undefined,
        isComputer:false,
        baseUrl:undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
          this.baseUrl = window.location.href.split(route.path)[0];
          this.path = route.path;
        this.redirect = route.query && route.query.redirectTo;
         if(route.query.code){
             this.scancodeLogin(route.query.code)
         }
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  mounted(){
      this.ewmCreated();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      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;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
          }).catch(() => {
            this.loading = false;
            if (this.captchaOnOff) {
              this.getCode();
            }
          });
        }
      });
    },
      ewmCreated(){
          let that = this;
          getWorkWeixinInfo().then(res => {
              var wwLogin = new WwLogin({
                  "id": "wx_login",
                  "appid": res.data.appid,
                  "agentid": res.data.agentId,
                  "redirect_uri": encodeURIComponent(res.data.redirectUrl+"/ewmRedirect?type=1&redirectTo="+that.redirect),
                  "state": "",
                  "href": "data:text/css;base64,LmltcG93ZXJCb3ggLnRpdGxle2Rpc3BsYXk6IG5vbmV9",
                  "lang": "zh",
              });
          });

      },
      scancodeLogin(code){
          getQrCodeLogin({code:code}).then(res => {
              setToken(res.token)
              let redirect;
              if(this.redirect == '/ewmRedirect'){
                  redirect = 'undefined';
              }else{
                  redirect = this.redirect;
              }
              if(redirect != 'undefined' && redirect != '/ewmRedirect' ){
                  // this.$router.push({ path: redirect}).catch(()=>{});
                  window.location.href = this.baseUrl + '/'+redirect;
              }else{
                  // this.$router.push({ path: "/"}).catch(()=>{});
                  window.location.href = this.baseUrl;
              }
              // this.$router.push({ path: redirect || "/" }).catch(()=>{});
          });
      },
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  /*background-image: url("../assets/images/login-background.jpg");*/
  /*background-size: cover;*/
  background: #6BB1F7;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}
.loginIcon{
  width: 35%;
  margin-right: 5%;
}
.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 350px;
  height: 380px;
  padding: 25px 25px 5px 25px;
  position: relative;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.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;
  background: #52A7FA;
}
.login-code-img {
  height: 38px;
}
  .imgDiv{
    height: 52px;
    width: 52px;
    box-sizing: border-box;
    position: absolute;
    top: 0;
    right: 0;
    border: 4px;
    img{

      &.ewm{
        height: 50px;
        width: 50px;
        position: absolute;
        top: 0px;
        right: 0px;
      }
      &.computer{
        height: 36px;
        width: 36px;
        position: absolute;
        top: 4px;
        right: 4px;
      }
    }
    .zz{
      width:0;
      height:0;
      border:25px solid;
      border-color:transparent transparent #FFF #FFF;
      position: absolute;
      top: 2px;
      right: 2px;
      z-index: 9;
    }
  }
  .ewmDiv{
    height: 282px;
    /*text-align: center;*/
    #wx_login{
      width: 100%;
      /*height: 260px;*/
      /*margin-top: 25px;*/
      display: inline-block;
      text-align: center;
      iframe{
        width: 65%;
        /*height: 200px;*/
      }
    }
    .info{
      text-align: center;
      margin-top: 20px;
      font-size: 14px;
      color: #6c6c6c;
    }
  }
</style>
