<template>
  <div class='login'>
    <div v-if="resource.mode === 'image'" class="login-bg animated  fast login-in"
         :style="{'background-image': 'url(' + resource.image + ')'}"></div>

    <div v-if="resource.mode === 'video'">
      <div class="login-video-div">
        <video id="video" :poster="resource.videoPoster" loop autoplay muted/>
      </div>
      <div class="login-video-progress"
           v-if="videoPercent !== 0 && videoPercent !== 100 && resource.mode === 'video'">
        <el-progress :percentage="videoPercent" :show-text="false"/>
      </div>
      <img class="login-top-left-logo" :src="resource.logoImage" alt="" v-if="resource.logoMode === 'image'">
      <div class="login-top-left-logo" v-if="resource.logoMode === 'text'">{{resource.logoText}}</div>
      <img class="login-bottom-logo" :src="resource.bottomLogo" alt="" v-if="resource.bottomLogo">
    </div>

    <div class="login-con animated fadeIn">
      <el-form class="login-form" :model="loginForm" :rules="rules" ref="loginForm">
        <h1>
          <i class="el-icon-fa el-icon-fa-sign-in" style="margin-right: 10px;"/>欢迎登录</h1>
        <div class="input-con">
          <el-form-item prop="userid">
            <el-input type="text" v-model="loginForm.userid" placeholder="账号" size="large" @keyup.native.enter='handleSubmit'>
              <i slot="prepend" class="el-icon-fa el-icon-fa-user"/>
            </el-input>
          </el-form-item>
          <el-form-item prop="passwd">
            <el-input type="password" v-model="loginForm.passwd" placeholder="密码" size="large"
                      @keyup.native.enter='handleSubmit'>
              <i slot="prepend" class="el-icon-fa el-icon-fa-lock"/>
            </el-input>
          </el-form-item>
          <el-form-item prop="captcha" v-if="needCaptcha">
            <el-input type="text" v-model="loginForm.captcha" placeholder="验证码" size="large" maxlength="5">
              <img slot="prepend" class="captcha" @click="changeCaptcha" :src="captchaUrl" @keyup.native.enter='handleSubmit'>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button class="btnSubmit blue" @click.native.prevent="handleSubmit" :loading="loading">登录</el-button>
          </el-form-item>
        </div>
      </el-form>
    </div>
  </div>
</template>
<script>
  import config from "../config";
  import AcesAPI from "../api/base/sysm/AcesAPI.js";

  export default {
    computed: {},
    data() {
      return {
        resource: config.login,

        videoPercent: 0,

        loading: false,
        needCaptcha: false,
        captchaUrl: `${config.ajaxUrl}/anon/captcha.jpg`,
        loginForm: {
          userid: "",
          passwd: "",
          captcha: ""
        },
        rules: {
          userid: [{required: true, message: "请输入账号"}],
          passwd: [{required: true, message: "请输入密码"}],
          captcha: [{required: true, message: "验证码不能为空"}]
        }
      };
    },
    async mounted() {
      //取出是否需要验证码
      this.needCaptcha = window.localStorage.getItem("login-captcha") === "true";
      this.initLoginVideo();
    },
    methods: {
      //切换验证码
      changeCaptcha() {
        this.captchaUrl = `${config.ajaxUrl}/anon/captcha.jpg?_dc=${new Date().getTime()}`;
      },
      // 处理登陆按钮
      async handleSubmit() {
        await this.$refs.loginForm.validate();
        this.loading = true;

        try {
          await AcesAPI.logck(this.loginForm.userid);
          await this.doLogin();
        } catch (e) {
          if (e.data.respCode === "020110") {
            if (window.confirm(e.data.respDesc)) {
              this.doLogin();
            }
          }
        } finally {
          this.loading = false;
        }
      },
      //执行最终登录
      async doLogin(confirm) {
        try {
          let data = await AcesAPI.login(this.loginForm).then();
          window.localStorage.setItem(config.loginToken, "true");
          window.localStorage.removeItem("login-captcha");
          this.$store.state.base.loginTimeout = false;
          this.$store.state.base.loginChangePwd = data.changepwd;
          this.$tabView.removeAll();
          await this.$router.push("/");
        } catch (e) {
          this.needCaptcha = e.data.captcha;
          window.localStorage.setItem("login-captcha", this.needCaptcha);
          this.changeCaptcha();
        }
      },
      initLoginVideo() {
        if (this.resource.mode === 'video') {
          let xhr = new XMLHttpRequest();
          xhr.open("GET", this.resource.video, true);
          xhr.addEventListener("progress", evt => {
            if (evt.lengthComputable) {
              let percentComplete = evt.loaded / evt.total;
              this.videoPercent = percentComplete * 100;

            }
          }, false);
          xhr.responseType = "blob";
          xhr.onload = e => {
            if (e.target.status === 200) {
              let b = e.target.response;
              let vid = (window.webkitURL || window.URL).createObjectURL(b);
              let video = document.getElementById("video");
              video.src = vid;
              video.play();
            }
          };
          xhr.send();
        }
      }
    }
  };
</script>
<style lang="scss">
  .login {
    position: absolute;
    width: 100%;
    height: 100%;
    overflow: hidden;

    .login-video-div {
      position: fixed;
      height: 70%;
      width: 100%;
      top: calc((100vh - 70%) / 2);
      overflow: hidden;

      video {
        width: 100%;
      }
    }

    .login-video-progress {
      position: fixed;
      width: 100%;
      top: calc(100vh - 15%);

      .el-progress-bar__outer {
        border-radius: 0;
        height: 2px !important;
      }

      .el-progress-bar__inner {
        border-radius: 0;
      }
    }

    .login-top-left-logo {
      background-size: 523px 60px;
      height: 60px;
      width: 523px;
      position: fixed;
      top: calc(100vh - 70% - 15% + -70px);
      left: 160px;
      font-size: 36px;
      color: #0068b5;
    }

    .login-bottom-logo {
      width: 299px;
      height: 38.5px;
      background-size: 299px 38.5px;
      position: fixed;
      top: calc(100vh - 15% + 28px);
      right: 160px;
    }
  }

  .login-bg {
    background-size: cover;
    position: absolute;
    width: 100%;
    height: 100%;
  }

  @keyframes login-in {
    100% {
      transform: scale(1, 1);
      opacity: 1;
    }
    0% {
      transform: scale(1.4, 1.4);
      opacity: 0;
    }
  }

  .login-in {
    animation-name: login-in;
  }

  .login-con {
    position: absolute;
    right: 160px;
    top: 50%;
    -webkit-transform: translateY(-60%);
    transform: translateY(-60%);
    width: 300px;
    min-height: 200px;
    background-color: #fff;
    border-radius: 4px;

    .login-form {
      box-shadow: none;

      h1 {
        height: 30px;
        line-height: 30px;
        margin: 0;
        font-size: 12px;
        padding-left: 30px;
        padding-bottom: 5px;
        padding-top: 5px;
        border-bottom: 1px solid #ccc;
      }

      .input-con {
        padding: 15px 20px;
      }

      .el-form-item {
        border-radius: 4px;
        margin-bottom: 1em;
      }

      .el-icon-fa::before {
        font-size: 15px;
      }

      input {
        height: 30px;
        line-height: 30px;
        background-color: #fff !important;
        padding-left: 12px;
      }

      .el-input-group__prepend {
        width: 20px;
        text-align: center;
      }

      .btnSubmit {
        height: 30px;
        width: 100%;
      }
    }

    .captcha {
      cursor: pointer;
      height: 20px;
    }
  }
</style>
