<template>
  <div class="mianBox" >
    <el-form class="login-form" auto-complete="on"  ref="loginForm" :model="loginForm" :rules="loginRules" v-if="login" :class="login?'opa1':'opa0'">
      <img src="../../assets/image/login/login.png" alt="" class="login">
      <el-form-item prop="username" width="460px">
        <el-input v-model="loginForm.username"
          ref="username"
          placeholder="请输入用户名"
          name="username"
          @keyup.enter.native="login_info()"
          type="text" auto-complete="off" >
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon z" />
        </el-input>
      </el-form-item>
      <el-form-item prop="password" style="position: relative;width:100%">
        <el-input
          ref="password"
          placeholder="请输入密码"
          name="password"
          @keyup.enter.native="login_info()"
          :type="passwordtxt"
          v-model="loginForm.password"
         >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon z" />
          
        </el-input>
        <div class="eyeWrap ">
          <svg-icon slot="prefix" icon-class="eye"  class="el-input__icon input-icon eye"    @click="eyeTab" v-show="passwordtxt =='password'" />
          <svg-icon slot="prefix" icon-class="eye-open"  class="el-input__icon input-icon  eye"   @click="eyeTab" v-show="passwordtxt =='text'" />
        </div>
        
      </el-form-item>
      <!-- <el-form-item prop="code">
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
        >
          <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-button
        type="primary"
        @click.native.prevent="login_info()"
        @keyup.enter = "login_info()"
        >
        <span v-if="!loading" >登 录</span>
        <span v-else>登 录 中...</span></el-button
      >
    </el-form>
    <van-loading
      size="24px"
      v-if="loading"
      color="#fff"
      vertical
      style="z-index: 2; position: fixed; top: 40%; left: 0; right: 0"
      >登录中...</van-loading
    >
    <van-overlay :show="loading" style="z-index: 1; position: fixed;background-color: rgba(0,0,0,0.6);" />
  </div>
</template>
<script>
import { Toast } from "vant";
import axios from "axios";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '../../utils/jsencrypt';
export default {
  data() {
    return {
      login:false,
      loading:false,
      passwordtxt:"password",
      codeUrl: "",
      cookiePassword: "",
      show_num:[],
      // 验证码开关
        // captchaOnOff: true,
      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: "请输入验证码" }]
      },
      appid:"wxd64360a4b8c50006",
      secret:"b7MTe45c89162542610509dafd9db7c3",
      code:undefined,
    };
  },
  mounted() {
    if(Cookies.get('rememberMeMT')){
      this.loginForm.rememberMe = Boolean(Cookies.get('rememberMeMT'))
    }
    if(Cookies.get("usernameMT")){
      this.loginForm.username = Cookies.get("usernameMT")
    }
    if(Cookies.get("passwordMT")){
      this.loginForm.password = decrypt(Cookies.get("passwordMT"))
    }
    if(Cookies.get("usernameMT") && Cookies.get("passwordMT")){
      this.loginForm.username = Cookies.get("usernameMT")
      this.loginForm.password = decrypt(Cookies.get("passwordMT"))
      this.login = false
      this.login_info()
    }else{
      this.login = true
    }
    
  },
  methods: {

    eyeTab(){//密码显示开关
      if(this.passwordtxt == "password"){
        this.passwordtxt = "text"
      }else{
        this.passwordtxt = "password"
      }
    },
    getCookie() {//缓存数据
      const username = Cookies.get("usernameMT");
      const password = Cookies.get("passwordMT");
      const rememberMe = Cookies.get('rememberMeMT')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    login_info() {
      if(Cookies.get("usernameMT") && Cookies.get("passwordMT")){
        this.$router.push({ path: "/index"})
      }else{
        this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          axios.get(`${process.env.VUE_APP_CONTROL_BASE_API}/api/thirdparty/v1/user/appLogin?name=${this.loginForm.username}&pass=${this.loginForm.password}`).then(res => {
            if (res?.data?.data?.data?.cipher) {
              let data = res.data.data.data
              //人员种类(00-正式人员、01-外协单位、02-租户)
              if(data.user.dmUser.type == "00"){
                //Toast('请使用"中国东信APP查看功能"')
                  Cookies.set("usernameMT", this.loginForm.username, { expires: 300 });
                  Cookies.set("rememberMeMT", this.loginForm.rememberMe, { expires: 300 });
                  Cookies.set("passwordMT", encrypt(this.loginForm.password), { expires: 300 });
                  Cookies.set("userIdMT", data.user.id, { expires: 300 });
                  Cookies.set("tokenMT", data.cipher, { expires: 300 });
                  Cookies.set("nonceMT", data.nonce, { expires: 300 });
                  Cookies.set("timestampMT", data.timestamp, { expires: 300 });
                  this.$router.push({ path: "/index"})

              }else if(data.user.dmUser.type == "01"){
                if(data.user.dmUser.status == false){
                  Toast("离职人员,暂无权限")
                  return
                }else{
                  Cookies.set("usernameMT", this.loginForm.username, { expires: 300 });
                  Cookies.set("rememberMeMT", this.loginForm.rememberMe, { expires: 300 });
                  Cookies.set("passwordMT", encrypt(this.loginForm.password), { expires: 300 });
                  Cookies.set("userIdMT", data.user.id, { expires: 300 });
                  Cookies.set("tokenMT", data.token, { expires: 300 });
                  Cookies.set("nonceMT", data.nonce, { expires: 300 });
                  Cookies.set("timestampMT", data.timestamp, { expires: 300 });
                  this.$router.push({ path: "/index"})
                }
              }else if(data.user.dmUser.type == "02"){
                Toast("暂无权限")
              }else{

              }
            } 
          }).catch(err =>{
            Toast(err.response.data.message)
          })
          setTimeout(()=>{
            this.loading = false
          },2000)
        }
      });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.mianBox{
    color: #000;
    position: fixed;
    top:0;
    left:0;
    width:100%;
    height:100%;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #fff;
}
.opa1{
  opacity: 1;
}
.opa0{
  opacity: 0;
}
.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 90%;
  position: absolute;
  top:8%;
  left:5%;
  .login{
    width:60%;
    margin:10% 20%;
  }
  .el-form-item__content{
    height:40px;
  }
  .login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
  }
  .login-code {
    width: 33%;
    height: 38px;
    float: right;
    margin-top:-2px;
    text-align: 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;
  }
  .login-code-img {
    height: 38px;
  }

  .el-button--primary{
    width:100%;
    margin-top:10px;
    color: #fff;
    background-color: rgba(242, 46, 60, 1);;
    border-color: rgba(242, 46, 60, 1);;
    border-radius: 4px;
  }
.eyeWrap {
  position: relative;
  width:10%;
  right:0;
  height:2.5rem !important;
  margin:-2.5rem 0 0 90%;;

}
.z{
  position: absolute;
  top:0.8rem !important;
}
.eye {
  position: absolute;
  right: 0.5rem;
  top: 0.8rem;
  fill: #ccc;
}
  .el-input__inner{
    padding-left:30px
  }
  .el-input__prefix{
    left:8px !important;
  }
    
}
</style>