<template>
  <div class="login">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" style="display:block">
      <h3 class="title">
        <img :src="$appVariant.SIDEBAR_TITLE_IMAGE_URI" alt="" srcset="" class="titleImg">
        {{ $appVariant.SIGN_UP_TITLE }}
      </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="type" auto-complete="off" placeholder="登陆密码"
          @keyup.enter.native="handleLogin">
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
          <img src="../assets/images/close.png" slot="suffix" alt="" srcset="" class="el-input__icon input-icon"
            v-if="show" style="height:18px;cursor:pointer" @click="type = 'input', show = false" />
          <img src="../assets/images/open.png" slot="suffix" alt="" srcset="" class="el-input__icon input-icon" v-else
            style="height:18px;cursor:pointer" @click="type = 'password', show = true" />
        </el-input>
      </el-form-item>



      <!-- <el-form-item prop="code" v-if="captchaEnabled">
        <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 prop="code">
        <el-input v-model="loginForm.code" auto-complete="off" placeholder="谷歌验证码" style="width: 100%"
          @keyup.enter.native="handleLogin">
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <!-- <img src="../assets/images/text.png" alt="" srcset="" class="el-input__icon input-icon doImg"
          style="width: 63px;height:17px;cursor:pointer" @click="go()" /> -->
        <!-- <div class="loginText">登录商户系统</div> -->
      </el-form-item>
      <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>


    <img src="../assets/images/lefttop.png" slot="suffix" alt="" class="lefttop" />
    <img src="../assets/images/rightbottom.png" slot="suffix" alt="" class="rightbottom" />
    <img src="../assets/images/leftbottom.png" slot="suffix" alt="" class="leftbottom" />
    <img src="../assets/images/righttop.png" slot="suffix" alt="" class="righttop" />
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'
export default {
  name: "Login",
  data() {
    var checkCode = (rule, value, callback) => {
      if (value == false) {
        callback(new Error('请进行人机验证'));
      } else {
        callback();
      }
    };
    return {
      guideUrl: '',
      show: true,
      type: 'password',
      codeUrl: "",
      cookiePassword: "",
      // S23PEOCHMUOKHWMG
      key: '6LcBoGUaAAAAABUnZINfh4j6FgqpQR-yHakZepIR',
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
        getValidateCode: false
      },
      loginRules: {
        username: [{
          required: true,
          trigger: "blur",
          message: "用户名不能为空"
        }],
        password: [{
          required: true,
          trigger: "blur",
          message: "密码不能为空"
        }],
        code: [{
          required: true,
          trigger: "change",
          message: "验证码不能为空"
        }],
        validateCode: [{
          validator: checkCode,
          trigger: 'change'
        }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    // this.getCode();
    this.getCookie();
  },
  methods: {
    go() {
      // window.open('https://pay-admin.bacddesi.com/execl/operation_guide.pdf')
    },
    getValidateCode(value) {
      this.loginForm.validateCode = value
    },
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          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');
          // }
          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 });

          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(() => { });
          }).catch(() => {
            // if (this.captchaEnabled) {
            //   this.loading = false;
            //   this.getCode();
            // }
            this.loading = false;
            // this.getCode();
          });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.leftbottom {
  position: absolute;
  left: 100px;
  bottom: 100px;
  width: 155x;
  height: 194px;
}

.righttop {
  position: absolute;
  right: 100px;
  top: 100px;
  width: 316x;
  height: 135px;
}

.lefttop {
  position: absolute;
  left: 0;
  top: 0;
  width: 479px;
  height: 424px;
}

.rightbottom {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 421px;
  height: 554px;
}

.loginText {
  font-weight: 400;
  font-size: 12px;
  line-height: 17px;
  color: #DCD7FF;
  text-align: right;
  margin-top: 12px;
  cursor: pointer;
}

.doImg {
  position: absolute;
  right: 4px;
  top: 0;
}

::v-deep .el-input--suffix .el-input__inner {
  padding-right: 80px;
}

::v-deep .el-button--medium {
  padding: 18px;
  background: #6D5EF8;
  border: none;
  color: #fff;
}

::v-deep .el-input__icon {
  transition: none;
}

::v-deep .el-button:hover,
.el-button:focus {
  background: #9389f5 !important;
}

::v-deep .el-input__inner {
  color: #000;
  padding-left: 40px;
  padding-right: 80px;
  background: #FFFFFF;
  border: 1px solid #DBDCE2;
  border-radius: 4px;
  height: 40px;
  line-height: 40px;
}

.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-size: contain;
  background-color: #F6F6F6;
  background-repeat: no-repeat;
  background-position: bottom;
  position: relative;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #000;
  font-weight: 600;
  font-size: 37px;
  display: flex;
  align-items: center;
  justify-content: center;


  .titleImg {
    width: 58px;
    height: 58px;
    margin-right: 10px;
  }
}

.login-form {
  border-radius: 6px;
  width: 400px;
  padding: 25px 25px 5px 25px;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 16px;
    width: 22px;
    margin-top: 12px;
  }
}

.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;
}

.login-code-img {
  height: 38px;
}



.login-form {
  border-radius: 6px;
  width: 400px;
  padding: 25px 25px 5px 25px;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }


}

.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;
}

.login-code-img {
  height: 38px;
}
</style>
