<template>
  <div class="login">
    <div class="box-center">
      <div class="box-left">
        <div class="bai-size">百业兴智慧加油站收银台</div>
        <div class="bact-box">
          <div class="sanhh">活动营销</div>
          <div class="sanhh">节省人力</div>
          <div class="sanhh">精准定位</div>
        </div>
      </div>
      <div class="box-right">
        <div class="box-title">
          <div style="margin-right: 15px;cursor: pointer " v-for="(item,index) in dlyzm" :key="index" @click="actindex = item.type " >
            <div>{{item.text}}</div>
            <div class="gangs" v-if="item.type == actindex " ></div>
          </div>

        </div>
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" v-if="actindex == 0" >


          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              type="text"
              auto-complete="off"
              clearable
              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="密码"
              clearable
              @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="captchaCode" v-if="captchaOnOff">
            <div style="display: flex;align-items: center">

              <div>
                <el-input
                  v-model="loginForm.captchaCode"
                  auto-complete="off"
                  placeholder="请输入验证码"
                  style="width: 90%"
                  clearable
                  @keyup.enter.native="handleLogin"
                >
                  <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
                </el-input>
              </div>
              <div class="login-code">
                <img :src="codeUrl" @click="getCodeimg" class="login-code-img"/>
              </div>
            </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%;line-height: 24px;font-size: 16px;"
              @click.native.prevent="handleLogin"
            >
              <span v-if="!loading" >立即登录</span>
              <span v-else>登 录 中...</span>
            </el-button>
          </el-form-item>
        </el-form>
        <!--        验证码-->
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" v-if="actindex == 1" >

          <el-form-item prop="username">
            <el-input
              v-model="loginForm.tel"
              type="text"
              auto-complete="off"
              clearable
              placeholder="账号"
            >
              <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>

          <el-form-item prop="captchaCode" v-if="captchaOnOff">
            <div style="display: flex;align-items: center">

              <div>
                <el-input
                  v-model="loginForm.telcode"
                  auto-complete="off"
                  placeholder="请输入验证码"
                  style="width: 90%"
                  clearable
                  @keyup.enter.native="handleLogin"
                >
                  <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
                </el-input>
              </div>
              <div>
                <span class="anniulan" v-show="show" @click="getCode" >获取验证码</span>
                <span style="cursor: pointer;color: #00aaff " v-show="!show" class="count">{{count}}s后重新获取</span>
              </div>

            </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%;line-height: 24px;font-size: 16px;"
              @click.native.prevent="posttelcode()"
            >
              <span v-if="!loading">立即登录</span>
              <span v-else>登 录 中...</span>
            </el-button>
          </el-form-item>
        </el-form>


      </div>
    </div>

  </div>
</template>
<script>
import { getCodeImg ,gettelcode} from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      show: true,
      count: '',
      timer: null,
      dlyzm:[
        {text:'登录',type:'0'},
        // {text: "验证码",type:'1'}
      ],
      actindex:0,
      codeUrl: "",
      pwdType: 'password',
      systemName : process.env.VUE_APP_TITLE,
      loginForm: {
        tel:null,
        telcode:null,
        username: "",
        password: "",
        rememberMe: false,
        captchaCode: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        captchaCode: [{ required: true, trigger: "change", message: "请输入验证码" }, { min: 4, max: 6, message: '请输入验证码', trigger: 'blur' }]
      },
      loading: false,
      // 验证码开关
      captchaOnOff: true,
      // 注册开关
      register: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCodeimg();
    this.getCookie();
  },
  methods: {
    getCode() {

      /*if(!this.loginForm.tel){
        this.$message.error("手机号不能为空")
        return
      }*/

      const TIME_COUNT = 60;
      if (!this.timer) {
        this.count = TIME_COUNT;
        this.show = false;
        this.timer = setInterval(() => {
          if (this.count > 0 && this.count <= TIME_COUNT) {
            this.count--;
          } else {
            this.show = true;
            clearInterval(this.timer);
            this.timer = null;
          }
        }, 1000)
      }
      let data = {
        mobile:this.loginForm.tel
      }
      gettelcode(data).then(res =>{
        if(res.code == 200){
          this.$message.success("操作成功")
        }else{
          this.$message.error("操作失败请稍后再试")
        }
      })
    },
    posttelcode(){
      const data = {
        mobile:this.loginForm.tel,
        code:this.loginForm.telcode
      }
      this.$store.dispatch("codeLogin", data).then(() => {
        // this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
        this.$router.push({ path:"/homeindex" || "/" }).catch(()=>{});
      }).catch(() => {
        this.loading = false;

      });

    },
    getCodeimg() {
      const app = this
      getCodeImg().then(response => {
        app.codeUrl = response.data.captcha
        app.loginForm.uuid = response.data.uuid
      })
    },
    // getCode() {
    //   const app = this
    //   getCodeImg().then(response => {
    //     app.codeUrl = response.data.captcha;
    //     app.loginForm.uuid = response.data.uuid;
    //   })
    // },
    showPwd() {
      if (this.pwdType === "password") {
        this.pwdType = "";
      } else {
        this.pwdType = "password";
      }
    },
    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),
        uuid: this.loginForm.uuid
      };
    },
    handleLogin() {
      const app = this;
      if (app.loading) {
          return false;
      }
      this.$refs.loginForm.validate(valid => {
        if (valid) {
            app.loading = true;
            if (app.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');
            }
          console.log('/homeindex')
            app.$store.dispatch("Login", this.loginForm).then(() => {
            // app.$router.push({ path: "/homeindex" }).catch(()=>{});
              this.$router.push({
                path:"/homeindex",
                query:{
                  id:0
                }
              })
          }).catch(() => {
              app.loading = false;
              if (app.captchaOnOff) {
                  // app.getCode();
              }
          });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg: #2d3a4b;
$light_gray: #eee;
$dark_gray: #889aa4;
$light_gray: #eee;
.login {
  height: 100vh;
  width: 100%;
  position: relative;
  background-image: url("../../assets/images/backbbj.png");
  //background-image: url("../assets/images/backbbj.png");
  overflow: hidden;
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
}
.box-center{
  width: 1000px;
  height: 580px;
  border-radius: 8px;
  background: #FFFFFF;
  box-shadow: 0px 4px 10px 0px rgba(20,61,129,0.1);
  border-radius: 16px 16px 16px 16px;
  opacity: 1;
  margin: 150px auto;
  overflow: hidden;
  display: flex;

}
.gangs{
  width: 100%;
  height: 5px;
  background: #00aaff;
  border-radius: 50px;
}
.box-left{
  width: 50%;
  height: 100%;
  background: #00aaff;
  background-image: url("../../assets/images/box-left.png");
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
}
.box-right{
  width: 50%;
  box-sizing: border-box;
  padding: 80px;
}
.box-title{
  width: 100%;
  //text-align: center;
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}
.bai-size{
  width: 100%;
  text-align: center;
  font-size: 40px;
  font-weight: bold;
  color: #FFFFFF;
  margin-top: 65px;

}
.bact-box{
  width: 85%;
  margin: 15px auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 40px;

}
.sanhh{
  width: 112px;
  height: 34px;
  background: linear-gradient(90deg, #E1EBFF 0%, #FFFFFF 100%);
  border-radius: 2px 2px 2px 2px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-family: PingFang SC, PingFang SC;
  font-weight: 400;
  color: #1755D0;
}

/* reset element-ui css */
.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  background-image: url("../../assets/images/bg.png");
  background-position: center;
  color: #ffffff;

  ::v-deep .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;

      &:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: #fff !important;
      }
    }
  }

  ::v-deep .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }

  .login-from-box {
    position: relative;
    .login-form {
      position: absolute;
      left: 0;
      right: 0;
      width: 450px;
      padding: 35px 35px 15px 35px;
      margin: 100px auto;
      align-items: center;
      color: white;
      backdrop-filter: saturate(180%) blur(20px);
      background: rgba(0, 0, 0, .65);
      border-radius: 10px;
      box-shadow: 0 0.4px 0.6px rgba(0, 0, 0, 0.141),
      0 1px 1.3px rgba(0, 0, 0, 0.202), 0 1.9px 2.5px rgba(0, 0, 0, 0.25),
      0 3.4px 4.5px rgba(0, 0, 0, 0.298), 0 6.3px 8.4px rgba(0, 0, 0, 0.359),
      0 15px 20px rgba(0, 0, 0, .26);
      .login-btn {
        .btn {
          position: relative;
          width: 100%;
          padding: 6px 0;
          margin: 10px 0 36px 0;
          font-size: 1.2em;
          color: white;
          background: transparent;
          border: 2px solid #ffffff;
          outline: none;
          cursor: pointer;
          overflow: hidden;
          transition: 0.5s;

          &::before {
            position: absolute;
            content: "";
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: linear-gradient(120deg,
              transparent,
              hsla(204, 70%, 53%, 0.5),
              transparent);
            transform: translateX(-100%);
            transition: 0.5s;
          }

          &:hover {
            box-shadow: 0 0 20px 10px hsla(204, 70%, 53%, 0.5);
          }

          &:hover::before {
            transform: translateX(100%);
          }
        }
      }
      .login-code {
        width: 100px;
        height: 48px;
        float: right;
        img {
          cursor: pointer;
          vertical-align: middle;
          height: 48px;
          border-radius: 1px;
        }
      }
    }

    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;
      text-align: center;
    }

    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
      &_login {
        font-size: 20px;
      }
    }

    .title {
      font-size: 26px;
      font-weight: 400;
      color: $light_gray;
      margin: 0px auto 20px auto;
      text-align: center;
      font-weight: bold;
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
  }
}
</style>
