<template>
  <div class="login" :style="{ backgroundImage: `url(${bg})` }">
    <div class="title">
      <!-- <div class="login-logo">
        <van-image width="48" height="48" :src="require('@/assets/imgs/logo.png')" />
        <span style="margin-left: 6px">融通危险源辨识系统</span>
      </div> -->
      <!-- <div class="login-name">Enterprise Business Data Monitoring</div> -->
    </div>

    <div class="con" :style="{ backgroundImage: `url(${conBg})` }">
      <div class="hello">融通危险源辨识系统</div>
      <div class="welcome">用户登录<span></span></div>
      <div class="login-form">
        <van-form @submit="onSubmit" :show-error-message="false" validate-trigger="onSubmit">
          <div class="username-wrap">
            <div class="username-icon">
              <van-image :src="require('@/assets/login/login-username.png')" />
            </div>
            <van-field v-model="username" name="username" label="" placeholder="账号"
              :rules="[{ required: true, message: '请填写账号' }]" />
          </div>
          <div class="passworld-wrap">
            <div class="passworld-icon">
              <van-image :src="require('@/assets/login/login-passworld.png')" />
            </div>
            <van-field v-model="password" type="password" name="password" placeholder="密码"
              :rules="[{ required: true, message: '请填写密码' }]" />
          </div>
          <div style="margin: 16px 0">
            <van-checkbox v-model="remember" shape="square">记住密码</van-checkbox>
          </div>
          <div style="margin: 16px">
            <van-button round block type="info" native-type="submit">登录</van-button>
          </div>
        </van-form>
      </div>
    </div>
  </div>
</template>

<script>
import { getFun, postFun } from "@/service/table";
import { setToken } from "@/utils/auth";
import {
  setUserInfo,
  getUserInfo,
  getLocalUserInfo,
  setLocalUserInfo,
} from "@/utils/userInfo";
//引入
const Base64 = require("js-base64").Base64;
export default {
  data() {
    return {
      bg: require("@/assets/login/login-bg.png"),
      conBg: require("@/assets/login/login-center.png"),
      username: "",
      password: "",
      remember:
        localStorage.getItem("isRemember") &&
          localStorage.getItem("isRemember") !== "false"
          ? true
          : false,
    };
  },
  watch: {
    remember(newVal, oldVal) {
      if (newVal) {
        localStorage.setItem("isRemember", true);
      } else {
        const useInfo = getLocalUserInfo();
        localStorage.setItem("isRemember", false);
        setLocalUserInfo({
          ...useInfo,
          loginPassword: ''
        })
      }
    },
  },
  mounted() {
    const store = window.localStorage;
    if (
      store.getItem("isRemember") &&
      store.getItem("isRemember") !== "false"
    ) {
      if (getLocalUserInfo()) {
        this.username = getLocalUserInfo().loginName;
        this.password = Base64.decode(getLocalUserInfo().loginPassword);
      }
    }
    let height = document.documentElement.clientHeight; //获取当前可视区域的高度
    document.getElementsByClassName("login")[0].style.height = height + "px"; //给根布局设置高度
    // 判断localStorage中是否有用户信息
    if (getLocalUserInfo()) {
      this.username = getLocalUserInfo().loginName;
      // 获取解密后的密码
      // console.log(Base64.decode(getLocalUserInfo().loginPassword)); // base64解密
    }
  },
  methods: {
    onSubmit(values) {
      this.$toast.loading({
        message: "登录中...",
        forbidClick: true,
        loadingType: "spinner",
        duration: 0,
      });
      postFun("/login", values).then((Response) => {
        if (Response.code == 200) {
          this.$toast.clear();
          const localUserInfo = {
            loginName: values.username,
            loginPassword: window.localStorage.getItem("isRemember") ? Base64.encode(values.password) : '',
          }
          // setLocalUserInfo(localUserInfo);
          setToken(Response.token);
          getFun("/getInfo").then((Response2) => {
            console.log(
              "%c [ Response2 ]-89",
              "font-size:13px; background:pink; color:#bf2c9f;",
              Response2
            );
            var userInfo = {
              ...Response2.user,
              ...localUserInfo
            };
            /* 存储用户信息 */
            setUserInfo(userInfo);
            setLocalUserInfo(userInfo)
            // if(!Response2.data.initializePassword){
            //     this.$router.push('/save-workbench')
            // }else{
            //     this.$router.push('/reset-pas-two')
            // }
            this.$router.push("/save-workbench");
          });
          /* 存储用户信息 */
        } else {
          this.$toast.clear();
          this.$toast.fail({
            message: Response.msg,
            duration: 2000,
          });
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>
/* @import url(); 引入css类 */
.login {
  background-size: 100% auto;
  background-repeat: no-repeat;
  background-attachment: fixed;
  height: 100vh;
  overflow: auto;
  position: relative;
  background-color: #fff;

  .title {
    .login-logo {
      margin: 0 auto;
      font-size: 26px;
      text-align: center;
      color: white;
      font-weight: bolder;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .login-name {
      width: 100%;
      text-align: center;
      margin-top: 20px;
      color: #fafbfe;
      font-size: 18px;
    }
  }

  .con {
    width: 320px;
    height: 340px;
    margin: auto;
    padding: 35px 25px;
    box-sizing: border-box;
    background-size: cover;
    background-repeat: no-repeat;
    box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
    border-radius: 10px;
    position: absolute;
    top: 50vh;
    left: 50%;
    transform: translate(-50%, -120px);

    .hello {
      color: #004fb1;
      font-size: 24px;
      font-weight: bold;
      text-align: center;
    }

    .welcome {
      margin: 15px 0;
      color: #7795e0;
      text-align: center;
      font-size: 18px;

      span {
        color: #71aaf2;
      }
    }

    .login-form {
      .username-wrap {
        position: relative;
        border-bottom: 1px solid #d2dcf5;

        .username-icon {
          width: 20px;
          position: absolute;
          left: 5px;
          top: 9px;
          z-index: 100;
        }

        /deep/ .van-field__body {
          padding-left: 20px;
          box-sizing: border-box;
        }

        /deep/ .van-field__error-message {
          padding-left: 20px;
        }
      }

      .passworld-wrap {
        position: relative;
        border-bottom: 1px solid #d2dcf5;

        .passworld-icon {
          width: 20px;
          position: absolute;
          left: 5px;
          top: 9px;
          z-index: 100;
        }

        /deep/ .van-field__body {
          padding-left: 20px;
          box-sizing: border-box;
        }

        /deep/ .van-field__error-message {
          padding-left: 20px;
        }
      }
    }
  }
}
</style>
