<template>
  <div class="login-container">
    <div class="login-form-wrap">
      <el-form autoComplete="on" :model="loginForm" :rules="loginRules" ref="loginForm" label-position="left" label-width="0px" class="card-box login-form">
        <!-- <div class="title">{{title}}</div> -->
        <div class="title">成师后勤管理系统</div>
        <el-form-item class="loginFormItem" prop="username">
          <span class="svg-container svg-container_login">
            <svg-icon icon-class="user" />
          </span>
          <el-input name="username" type="text" v-model="loginForm.name" autoComplete="on" placeholder="username" />
        </el-form-item>
        <el-form-item class="loginFormItem" prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password"></svg-icon>
          </span>
          <el-input name="password" :type="pwdType" @keyup.enter.native="handleLogin" v-model="loginForm.password" autoComplete="on" placeholder="password"></el-input>
          <span class="show-pwd" @click="showPwd">
            <svg-icon icon-class="eye" />
          </span>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width:100%;" :loading="loading" @click.native.prevent="handleLogin">
            登录
          </el-button>
        </el-form-item>
        <!-- <div class="tips">
        <span style="margin-right:20px;">username: admin</span>
        <span> password: admin</span>
      </div> -->
      </el-form>
    </div>
  </div>
</template>

<script>
import { isvalidUsername } from '@/utils/validate';
import { mapActions, mapGetters } from 'vuex';
import { PAGE_TITLE } from '@/config';

export default {
  name: 'login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!isvalidUsername(value)) {
        callback(new Error('请输入正确的用户名'));
      } else {
        callback();
      }
    };
    const validatePass = (rule, value, callback) => {
      if (value.length == 0) {
        callback(new Error('请输入密码'));
      } else {
        callback();
      }
    };
    return {
      title: PAGE_TITLE,
      loginForm: {
        name: '',
        password: '',
      },
      loginRules: {
        name: [
          { required: true, trigger: 'blur', validator: validateUsername },
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePass },
        ],
      },
      pwdType: 'password',
    };
  },
  computed: {
    ...mapGetters(['loading']),
  },
  methods: {
    ...mapActions(['Login']),
    showPwd() {
      if (this.pwdType === 'password') {
        this.pwdType = '';
      } else {
        this.pwdType = 'password';
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.Login(this.loginForm)
            .then(path => {
              console.log('login done');
              this.$router.push({ path: path });
            })
            .catch(err => {
              console.log(err);
            });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
  },
};
</script>

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

.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background-color: $bg;
  background-image: url('../../assets/images/login/login_bg@3x.png');
  background-size: cover;
  input:-webkit-autofill {
    box-shadow: 0 0 0px 1000px #fff inset !important;
    -webkit-text-fill-color: #666 !important;
  }
  input {
    background: transparent;
    border: 0px;
    -webkit-appearance: none;
    border-radius: 0px;
    padding: 12px 5px 12px 15px;
    color: #666;
    height: 47px;
  }
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
  }
  .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: 40px;
    color: $light_gray;
    margin: 0px auto 70px auto;
    text-align: center;
  }
  .login-form-wrap {
    width: 1290px;
    height: 730px;
    background: url('../../assets/images/login/login_screen_bg@3x.png') center
      no-repeat;
    margin: 100px auto 0;
    position: relative;
  }
  .login-form {
    position: absolute;
    top: 200px;
    right: 230px;
    width: 336px;
  }
  .loginFormItem {
    height: 52px;
    background-color: #fff;
    border-radius: 8px;
    margin-bottom: 28px;
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    // background: rgba(0, 0, 0, 0.1);
    // border-radius: 5px;
    color: #454545;
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .thirdparty-button {
    position: absolute;
    right: 35px;
    bottom: 28px;
  }
}
@media screen and (max-width: 1334px) {
  .login-container {
    background-image: url('../../assets/images/login/login_bg@2x.png');
    input {
      padding: 12px 5px 12px 15px;
      height: 31px;
    }
    .el-input {
      height: 31px;
    }
    .login-form-wrap {
      width: 860px;
      height: 487px;
      background: url('../../assets/images/login/login_screen_bg@2x.png') center;
    }
    .login-form {
      top: 133.333px;
      right: 152.333px;
      width: 224px;
    }
    .title {
      font-size: 26px;
      margin: 0px auto 46px auto;
    }
    .loginFormItem {
      height: 34px;
      background-color: #fff;
      border-radius: 8px;
      margin-bottom: 28px;
      .el-form-item__content {
        line-height: 22px;
      }
    }
    .svg-container {
      padding: 6px 5px 6px 15px;
      width: 20px;
      &_login {
        font-size: 14px;
      }
    }
  }
}
</style>
