<template>
  <div class="login-container">
    <div class="formBox">
      <div class="formLeft">
        <!-- Welcome -->
        <div class="text">
          <p class="p1">欢迎登陆</p>
          <p class="p2">智能营销管理系统</p>
        </div>
      </div>
      <div class="formRight">

        <div class="title">
          LOGIN
        </div>
        <div class="tabs">
          <div class="tabLeft" :class="tabNum == 0 ? 'active' : ''" @click="tabClick(0)">账号登录</div>
          <!-- <div class="tabRight" :class="tabNum==1?'active':''" @click="tabClick(1)">面部登录</div> -->
        </div>
        <div v-if="tabNum == 1">
          <renLian @loginClick="loginClick" />
        </div>
        <div v-else>
          <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on"
            label-position="left">
            <el-form-item prop="username">
              <span class="svg-container">
                <svg-icon icon-class="user" />
              </span>
              <el-input ref="username" style="width:85%;margin-left: 10px" v-model="loginForm.username" placeholder="用户名"
                name="username" type="text" tabindex="1" auto-complete="on" @blur="getCode" />
            </el-form-item>
            <el-form-item prop="password">
              <span class="svg-container">
                <svg-icon icon-class="password" />
              </span>
              <el-input style="width:85%;margin-left: 10px" ref="password" v-model="loginForm.password" show-password
                type="password" placeholder="密码" name="password" tabindex="2" auto-complete="on" />
              <!-- <span class="show-pwd" @click="showPwd">
                <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
              </span> -->
            </el-form-item>
            <el-form-item prop="imgCode" class="login-code">
              <div class="codeBox">
                <div class="codeLeft">
                  <el-input v-model="loginForm.imgCode" placeholder="验证码" />
                </div>
                <div class="codeRight">
                  <img v-if="imgurl" :src="imgurl" alt="" @click="getCode" />
                </div>
              </div>

            </el-form-item>
            <div class="loginBtn">
              <el-button type="primary" style="width: 100%; margin-bottom: 30px" @click.native.prevent="handleLogin">
                登录
              </el-button>
            </div>
          </el-form>
        </div>
      </div>
    </div>
    <el-dialog title="获取验证码" width="450px" :visible.sync="codeShow" :close-on-press-escape="false"
      :close-on-click-modal="false" :show-close="false">
      <div class="getCode">
        <input type="text" v-model="vcode" placeholder="验证码" maxlength="6" />
        <span class="code" :class="{ code: 1, number: codeState.state }" @click="getCheck()">{{ codeState.state ?
          codeState.time + "s" : "重新获取" }}</span>
      </div>
      <div style="width: 80%;margin:10px auto;color: #909090;">已向{{ filterPhone(phone) }}发送验证码,请注意查收</div>
      <div class="loginBtn">
        <el-button type="primary" style="width: 100%; margin-bottom: 30px" @click="btnClick">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { puk } from "../../api/common";
import urls from "../../api/urls";
import renLian from './renlian.vue'
import {
  setToken,
} from '../../utils/auth'
import {
  post,
  get
} from '@/api/common'
export default {
  name: "Login",
  components: {
    renLian
  },
  data () {
    return {
      tabNum: 0,
      loginModel: 1,
      loginForm: {
        username: "",
        password: "",
        imgCode: "",
      },
      loginRules: {
        username: [
          {
            required: true,
            trigger: "blur",
            message: "请输入用户名",
          },
        ],
        password: [
          {
            required: true,
            trigger: "blur",
            message: "请输入密码",
          },
        ],
        imgCode: [
          {
            required: true,
            trigger: "blur",
            message: "请输入验证码",
          },
        ],
      },
      passwordType: "password",
      redirect: "/",
      encrypt: null,
      imgurl: "",
      phone: "",
      codeShow: false,
      codeState: {
        state: false,
        time: 60,
        task: null,
      },
      vcode: "",
    };
  },
  created () {
    puk().then((res) => {
      const encrypt = new JSEncrypt();
      encrypt.setPublicKey(res.data);
      this.encrypt = encrypt;
    });
    if (this.loginForm.username) {
      this.getCode();
    }
    console.log(this.$store)
  },
  methods: {
    tabClick (num) {
      this.tabNum = num
    },
    filterPhone: (phone) => {
      phone = phone.toString();
      return phone.substr(0, 3) + "****" + phone.substr(7, 11);
    },
    loginClick (phone) {
      this.phone = phone;
      this.codeShow = true;
      setTimeout(() => {
        this.getCheck();
      }, 500);
    },
    getCode () {
      this.imgurl = `${process.env.VUE_APP_BASE_API}${urls.getCode}?userName=${this.loginForm.username
        }&refrech=${this.imgCodeRefrech}&times=${Date.now()}`;
    },
    showPwd () {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin () {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          const username = this.encrypt.encrypt(this.loginForm.username);
          const password = this.encrypt.encrypt(this.loginForm.password);
          const imgCode = this.loginForm.imgCode;
          this.$post("/sysUser/login", {
            username,
            password,
            imgCode,
          }).then((res) => {
            if (res.code === 10000) {
              if (res.data.phone !== "") {
                this.phone = res.data.phone;
                this.codeShow = true;
                setTimeout(() => {
                  this.getCheck();
                }, 500);

                // this.$store.commit('user/SET_TOKEN', res.data.token)
                // setToken(res.data.token)
                // this.$router.go(0)
                // this.$router.replace({
                //   path: '/kongbai',
                // })
              }
            }
          }).catch((error) => {
            this.getCode()
          });
          // this.$store
          //   .dispatch('user/login', { username, password, imgCode })
          //   .then(() => {
          //     this.$router.push({ path: this.redirect || '/' })
          //   })
          //   .catch(() => {

          //   })
        }
      });
    },
    getCheck () {
      this.$post("/sysUser/login/smscode", {
        phone: this.phone,
      }).then((res) => {
        this.codeState.state = true;
        this.codeState.task = setInterval(() => {
          if (this.codeState.time <= 0) {
            clearInterval(this.codeState.task);
            this.codeState.task = null;
            this.codeState.state = false;
            this.codeState.time = 60;
          } else {
            this.codeState.time--;
          }
        }, 1000);
      });
    },
    btnClick () {
      this.$store.dispatch("user/login", {
        phone: this.phone,
        vcode: this.vcode,
      }).then(() => {
        get('/sysUser/info', {}).then(res => {
          let nowTime = new Date().getTime()
          let num = new Date(res.data.overdueTime).getTime() - nowTime
          let day = num / (1000 * 60 * 60 * 24)
          localStorage.setItem('dayNum', day)
          // this.$router.push({ path: this.redirect || "/" });
          this.$router.go(0)
          this.$router.replace({
            path: '/kongbai',
          })
        })
      });
      // this.$store
      //   .dispatch("/sysUser/login/valismscode", {
      //     phone: this.phone,
      //     vcode: this.vcode,
      //   })
      //   .then(() => {
      //     this.$router.push({ path: this.redirect || "/" });
      //   })
    },
  },
};
</script>
<style lang="scss">
.el-form-item__error {
  left: 40px !important;
}
</style>
<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  // background-color: $bg;
  overflow: hidden;
  // background-color: #409eff;
  background-image: url("./bg.jpg");
  background-repeat: no-repeat;
  background-size: cover;
  background-position: 100% 100%;

  .formBox {
    width: 1000px;
    margin: 0 auto;
    margin-top: 251px;
    position: relative;

    .formLeft {
      width: 100%;
      height: 400px;
      background: #000;
      color: #fff;
      position: relative;
      opacity: 0.6;

      .text {
        width: 180px;
        position: absolute;
        top: 30px;
        left: 30px;

        .p1 {
          font-size: 18px;
        }

        .p2 {
          font-size: 28px;
        }
      }
    }

    .formRight {
      width: 400px;
      height: 550px;
      position: absolute;
      top: -75px;
      right: 70px;
      z-index: 1;
      // flex: 1;
      background-color: #f1f1f1;

      .login-form {
        width: 100%;
        padding: 20px;
        // margin-top: 30px;
      }

      .title {
        font-size: 20px;
        color: #409eff;
        text-align: center;
        margin: 20px;
      }

      .tabs {
        width: 70%;
        display: flex;
        margin: 0 auto;
        margin-top: 30px;
        justify-content: space-between;
        color: #909090;
        cursor: pointer;

        .active {
          color: #409eff;
          border-bottom: 1px solid #409eff;
        }
      }
    }
  }

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

  // .title-container {
  //   position: relative;

  //   .title {
  //     font-size: 26px;
  //     color: $light_gray;
  //     margin: 0px auto 40px 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;
  }
}

.getCode {
  width: 80%;
  height: 45px;
  background: #ffffff;
  border: 1px solid #9e9e9e;
  border-radius: 10px;
  margin: 0 auto;
  // margin-top: 22px;
  box-sizing: border-box;
  padding-left: 31px;
  padding-right: 40px;
  display: flex;
  flex-direction: row;
  justify-content: space-between;

  input {
    margin: 0;
    padding: 0;
    border: none;
    outline: none;
    width: 60%;
    height: 45px;
    line-height: 45px;
    // color: #d3d3d3;
    background-color: transparent;
  }

  .code {
    display: block;
    line-height: 45px;
    text-align: center;
    color: #409eff;
    // position: absolute;
    // right: 20px;
    // bottom: 24px;
  }
}

.loginBtn {
  width: 80%;
  margin: 0 auto;
  margin-top: 60px;
}

.codeBox {
  width: 100%;
  height: 45px;
  display: flex;
  justify-content: space-between;

  .codeLeft {
    width: 55%;
    height: 40px;
    margin-left: 40px;
  }

  .codeRight {
    width: 30%;
    height: 40px;

    img {
      width: 100%;
      height: 40px;
    }
  }
}
</style>
