<template>
  <div class="login-container">
    <div class="loginBox">
      <!-- <div class="left">
        <div class="top">
          <i class="logo"></i>
          <div class="name">视频点位治理管理后台</div>
        </div>
        <div class="bottom"></div>
      </div> -->
      <div class="right">
        <h2 class="name">视频点位治理管理后台</h2>
        <div class="title">账号登录</div>
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
          <el-form-item prop="account_name">
            <i class="icon_user"></i>
            <el-input
              ref="account_name"
              v-model="loginForm.account_name"
              placeholder="请输入用户名"
              name="account_name"
              type="text"
              tabindex="1"
              autocomplete="on"
          /></el-form-item>
          <el-form-item prop="password">
            <i class="icon_password"></i>
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="请输入密码"
              name="password"
              tabindex="2"
              autocomplete="on"
              @keyup.native="checkCapslock"
              @blur="capsTooltip = false"
              @keyup.enter.native="handleLogin"
          /></el-form-item>
          <el-button
            :loading="loading"
            type="primary"
            style="
              width: 100%;
              margin-bottom: 30px;
              background: #164199;
              height: 50px;
            "
            @click.native.prevent="handleLogin"
            >登录</el-button
          >
        </el-form>
      </div>
    </div>
    <!-- <a
    href="https://beian.miit.gov.cn/"
    target="blank"
      style="
        display: flex;
        align-items: center;
        justify-content: center;
        color: #fff;
      "
    >
      蜀ICP备2023021382号-1
    </a> -->
  </div>
</template>

<script>
import SocialSign from "./components/SocialSignin";

export default {
  name: "Login",
  components: { SocialSign },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value.length < 4) {
        callback(new Error("账号格式错误，不能为空或小于4位"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 3) {
        callback(new Error("密码格式错误，不能为空或小于3位"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        account_name: "",
        password: "",
      },
      loginRules: {
        account_name: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
      },
      passwordType: "password",
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query;
        if (query) {
          this.redirect = query.redirect;
          this.otherQuery = this.getOtherQuery(query);
        }
      },
      immediate: true,
    },
  },
  created() {},
  mounted() {
    if (this.loginForm.account_name === "") {
      this.$refs.account_name.focus();
    } else if (this.loginForm.password === "") {
      this.$refs.password.focus();
    }
  },
  destroyed() {},
  methods: {
    checkCapslock(e) {
      const { key } = e;
      this.capsTooltip = key && key.length === 1 && key >= "A" && key <= "Z";
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        console.log(valid);
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(() => {
              // this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
              this.$router.push({ path: "/" });
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== "redirect") {
          acc[cur] = query[cur];
        }
        return acc;
      }, {});
    },
  },
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

// @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
//   .login-container .el-input input {
//     color: $cursor;
//   }
// }

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: #f2f7ff;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #000;
      height: 47px;
      caret-color: #000;

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

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: #f2f7ff; // rgba(0, 0, 0, 0.1);

    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  // background-color: $bg;
  background: url("../../assets/images/login_bg.png") no-repeat;
  background-size: cover;
  overflow: hidden;

  // .login-form {
  //   position: relative;
  //   width: 520px;
  //   max-width: 100%;
  //   padding: 160px 35px 0;
  //   margin: 0 auto;
  //   overflow: hidden;
  // }

  // .tips {
  //   font-size: 14px;
  //   color: #fff;
  //   margin-bottom: 10px;

  //   span {
  //     &:first-of-type {
  //       margin-right: 16px;
  //     }
  //   }
  // }

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

  // .thirdparty-button {
  //   position: absolute;
  //   right: 0;
  //   bottom: 6px;
  // }

  // @media only screen and (max-width: 470px) {
  //   .thirdparty-button {
  //     display: none;
  //   }
  // }
}
.loginBox {
  background: #edf7ff;
  width: 500px;
  height: 428px;
  margin: 120px auto;
  display: flex;
  border-radius: 24px;
  // padding: 36px 91px 0px 55px;
  box-sizing: border-box;

  .left {
    font-size: 24px;
    font-family: PingFang SC;
    font-weight: bold;
    color: #164199;
    background: #edf7ff;
    border-radius: 24px;
    .top {
      display: flex;
      align-items: center;
      width: 600px;
      margin-top: 36px;
      // margin-left: 55px;
      padding-left: 55px;
      .logo {
        background: url("../../assets/images/logo.png") no-repeat;
        background-size: cover;
        display: inline-block;
        width: 24px;
        height: 34px;
        margin-right: 12px;
      }
    }
    .bottom {
      background: url("../../assets/images/login_pic.png") no-repeat;
      background-size: cover;
      width: 399px;
      height: 313px;
      margin-top: 106px;
      margin-left: 55px;
    }
  }
  .right {
    border-radius: 24px;
    background: #fff;
    flex: 1;
    display: flex;
    flex-direction: column;
    // align-items: center;
    justify-content: center;
    padding: 0 92px;
    .title {
      font-size: 22px;
      font-family: PingFang SC;
      font-weight: 500;
      color: #272727;
      position: relative;
      margin-bottom: 50px;
      &::after {
        content: "";
        width: 38px;
        height: 5px;
        background: #164199;
        display: inline-block;
        position: absolute;
        left: 0;
        bottom: -10px;
      }
    }
  }
}
.icon_user {
  width: 15.8px;
  height: 16px;
  display: inline-block;
  background: url("../../assets/images/icon_username.png") no-repeat;
  background-size: cover;
  margin-left: 26px;
  position: relative;
  top: 2px;
}
.icon_password {
  width: 18px;
  height: 16px;
  display: inline-block;
  background: url("../../assets/images/icon_password.png") no-repeat;
  background-size: cover;
  margin-left: 26px;
  position: relative;
  top: 2px;
}
</style>
