<template>
  <div
    class="login-container"
    :style="{backgroundImage: 'url(' + img + ')'}"
  >
    <div class="out-div">
      <div class="title-container">
        <h3 class="title">{{loginTitle}}</h3>
      </div>
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        auto-complete="on"
        label-position="left"
      >
        <el-form-item prop="username">

          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="请输入账号"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </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"
            @keyup.enter.native="handleLogin"
          />
          <span
            class="show-pwd"
            @click="showPwd"
          >
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>

        <div class="select">
          <div
            class="select-left"
            @click="remberPwd"
          >
            <img
              v-show="!isShow"
              src="@/assets/image/select.png"
              alt=""
              srcset=""
            >
            <img
             v-show="isShow"
              src="@/assets/image/selected.png"
              alt=""
              srcset=""
            >
            <div class="selected">记住密码</div>
          </div>
          <div class="select-right">忘记密码？</div>
        </div>

        <el-button
          class="submit"
          :loading="loading"
          type="primary"
          style="width:100%;margin-bottom:30px;"
          @click.native.prevent="handleLogin"
        >登录</el-button>

        <div class="tips">
          <span style="margin-right:20px;">用户名: admin</span>
          <span> 密码: admin</span>
        </div>

      </el-form>
    </div>
  </div>
</template>

<script>
import { getNationList } from "@/api/personnel";
import { validUsername } from "@/utils/validate";
import { STATUS_CODES } from "http";
const defaultSettings = require('../../settings.js')

export default {
  name: "Login",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入正确的用户名"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 4) {
        callback(new Error("密码不得小于4位"));
      } else {
        callback();
      }
    };
    return {
      img: require("@/assets/image/background.jpg"),
      isShow: false,
      loginTitle:defaultSettings.loginTitle,
      loginForm: {
        username: "",
        password: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername }
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword }
        ]
      },
      loading: false,
      passwordType: "password",
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  methods: {
    async getNationList() {
      //获取民族
      await getNationList().then(res => {
        //console.log(res.data['App\Repositories\Enums\NationEnum'], "班级列表")
        console.log(res["data"], "获取民族");
        sessionStorage.setItem("status", JSON.stringify(res["data"]));
      });
    },
    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;
          let data = {
            username: this.loginForm.username,
            password: btoa(this.loginForm.password)
          };
          this.$store
            .dispatch("user/login", data)
            .then(() => {
              this.$router.push({ path: this.redirect || "/" });

              this.loading = false;
              this.getNationList();
            })
            .catch(err => {
              this.loading = false;
              console.log(err, "ppppoopioio");
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //记住密码
    remberPwd() {
      this.isShow = !this.isShow;
    }
  }
};
</script>

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

$bg: #283443;
/*$bg:#e2291d;*/
$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: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #e44030;
      height: 47px;
      caret-color:#e44030;
    }
    input:-internal-autofill-selected {
      background-color: #ffff !important;
    }
  }

  .el-form-item {
    border: 1px solid #999999;
    background: $light_gray;
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
/*登陆页面背景颜色*/
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #c30d23;
.out-div {
  margin-top: 10%;
}
.login-container {
  background-size: 100% 100%;
  background-repeat: no-repeat;
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  .login-form {
    position: relative;
    width: 515px;
    max-width: 100%;
    padding: 35px 35px 0;
    margin: 0 auto;
    background-color: #ffffff;
    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: 64px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-family: STXinwei;
      font-weight: bold;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
.submit {
  width: 424px;
  height: 59px;
  background: #e44030;
  border-radius: 4px;
  border: none !important;
}
.select {
  display: flex;
  font-size: 14px;
  height: 30px;
  line-height: 30px;
  font-weight: 400;
  color: #333333;
  margin-bottom: 25px;
  .select-left {
    flex: 1;
    img {
      width: 20px;
      margin-top: 5px;
      height: 20px;
    }
    .selected {
      display: inline-block;
      vertical-align: top;
      margin-left: 10px;
    }
  }
  .select-right {
    felx: 1;
    text-align: right;
  }
}
</style>
