<template>
  <div class="login">
    <div class="login-form">
      <div class="faceLoginBtnWrap">
        <img class="faceLoginBtn" src="./../../assets/face.png" @click="takePhoto()">
      </div>
      <div style="width: 360px;">
        <h3 class="title" @click="toBigScreen">航玻生产管理系统</h3>
        <el-tabs v-model="activeName" :stretch="true">
          <el-tab-pane label="账号密码登录">
            <el-form
              ref="loginForm"
              :model="loginForm"
              :rules="loginRules"
            >
              <el-form-item prop="username">
                <el-input
                  ref="username"
                  v-model="loginForm.username"
                  placeholder="账号"
                  name="username"
                  type="text"
                  tabindex="1"
                  auto-complete="on"
                ><svg-icon
                  slot="prefix"
                  icon-class="userName"
                  class="el-input__icon input-icon"
                /></el-input>
              </el-form-item>

              <el-form-item prop="password">
                <el-input
                  :key="passwordType"
                  ref="password"
                  v-model="loginForm.password"
                  :type="passwordType"
                  placeholder="密码"
                  name="password"
                  tabindex="2"
                  auto-complete="on"
                  id="passwordInput"
                  @keyup.enter.native="handleLogin"
                ><svg-icon
                  slot="prefix"
                  icon-class="password"
                  class="el-input__icon input-icon"
                /></el-input>
                <span class="show-pwd" @click="showPwd">
                <svg-icon
                  :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
                />
              </span>
              </el-form-item>
              <el-button
                :loading="loading"
                type="primary"
                style="width: 100%; margin-bottom: 30px"
                @click.native.prevent="handleLogin"
              >登录</el-button
              >
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </div>
    </div>
    <el-dialog :visible.sync="limitedPhoto" @close="closeCamera" id="loginFaceWrap">
      <div style="font-size: 28px;color: #333333;text-align: center;font-weight: bold;">打卡</div>
      <div class="testTracking">
        <faceLogin ref="faceTracking" name="faceLogin" @func="getMsgFormSon"></faceLogin>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import faceLogin from '@/components/faceLogin/tracking.vue'
  export default {
    name: "Login",
    inject:['reload'],
    components:{
      faceLogin
    },
    data() {
      // const validateUsername = (rule, value, callback) => {
      //   if (!validUsername(value)) {
      //     callback(new Error('请输入正确账号!'))
      //   } else {
      //     callback()
      //   }
      // }
      const validatePassword = (rule, value, callback) => {
        if (value.length < 4) {
          callback(new Error("密码长度小于4位!"));
        } else {
          callback();
        }
      };
      return {
        timer: null,
        buttonmsg: "发送验证码",
        count: 60,
        loginForm: {
          username: "",
          password: "",
        },
        loginRules: {
          username: [{ required: true, trigger: "blur", message: "请输入账户" }],
          password: [
            {
              required: true,
              trigger: "blur",
              validator: validatePassword,
              message: "请输入密码",
            },
          ],
        },


        activeName: "",
        loading: false,
        passwordType: "password",
        redirect: undefined,
        limitedPhoto: false,
      };
    },
    created() {
      this.getUP();
    },
    watch: {
      $route: {
        handler: function (route) {
          this.redirect = route.query && route.query.redirect;
        },
        immediate: true,
      },
    },
    methods: {
      toBigScreen(){
        this.$router.push('/index')
      },
      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) {
            this.loading = true;
            this.$store
              .dispatch("user/login", this.loginForm)
              .then(() => {
                this.$store.dispatch("user/getCount", {});
                this.$router.push({ path: this.redirect || "/" }, () => {});
                this.loading = false;
                localStorage.setItem("rem_username", this.loginForm.username);
                localStorage.setItem("rem_password", this.loginForm.password);
              })
              .catch(() => {
                this.loading = false;
              });

          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },

      //读取cookie
      getUP() {

        this.loginForm.username = localStorage.getItem("rem_username");
        this.loginForm.password = localStorage.getItem("rem_password");
      },
      //人脸登录
      takePhoto(){
        this.limitedPhoto = true;
      },
      /*关闭相机*/
      closeCamera () {
        debugger;
        this.reload();
        this.$refs.faceTracking.closeCamera();
        // this.thisVideo.srcObject.getTracks()[0].stop();
      },
      getMsgFormSon(data){
        // this.limitedPhoto = data;
      },
    },
  };
</script>

<style lang="scss">
  .login {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    background-image: url("../../assets/bg-login.png");
    background-size: cover;
  }
  .title {
    margin: 30px auto 30px auto;
    text-align: center;
    color: #0174d7;
  }
  .logo {
    width: 350px;
    height: 140px;
  }

  .login-form {
    border-radius: 6px;
    background: #ffffff;
    width: 700px;
    padding: 25px;
    display: flex;
    justify-content: space-between;
    box-shadow: 0 0 10px 5px #eeeeee;

    .el-input {
      height: 45px;
      input {
        height: 45px;
      }
    }
    .input-icon {
      height: 39px;
      width: 14px;
      margin-left: 2px;
    }
  }
  .faceLoginBtnWrap{
    width: 250px;
    border-right: 1px dashed #409EFF;
    text-align: center;
  }
  .faceLoginBtn{
    width: 150px;
    margin-top: 90px;
  }
  #passwordInput{
    padding-right: 35px;
  }
  .show-pwd {
    height: 39px;
    margin-right: 2px;
  }
  .login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
  }
  .login-code {
    width: 33%;
    height: 38px;
    float: right;
    img {
      cursor: pointer;
      vertical-align: middle;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    cursor: pointer;
    user-select: none;
  }
  .login-code-img {
    height: 38px;
  }
  .testTracking{
    width:100%;
    height: 500px;
  }
</style>
