<template>
  <div class="user-login">
    <div class="user-login-bg"></div>
    <div class="content-wrapper">
      <!-- <h2 class="slogan">
        欢迎使用 <br />
        在线考试管理系统
      </h2> -->
      <div class="form-container">
        <div class="form-body">
          <div class="text-center">
            <div class="school-logo">
              <img :src="schoolLogo" alt="学校logo" />
            </div>
            <h1 class="text-center login-title">在线考试平台后台管理系统</h1>
          </div>
          <el-form ref="form" :model="user" label-width="0">
            <div class="form-items">
              <el-row class="form-item">
                <el-col>
                  <el-form-item
                    prop="username"
                    :rules="[{ required: true, message: '用户名不能为空' }]"
                  >
                    <div class="form-line">
                      <el-input
                        size="large"
                        prefix-icon="icon icon-user"
                        placeholder="请输入用户名"
                        v-model="user.username"
                        clearable
                      ></el-input>
                    </div>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row class="form-item">
                <el-col>
                  <el-form-item
                    prop="password"
                    :rules="[{ required: true, message: '密码不能为空' }]"
                  >
                    <div class="form-line">
                      <el-input
                        size="large"
                        prefix-icon="icon icon-password"
                        type="password"
                        placeholder="请输入密码"
                        v-model="user.password"
                        clearable
                      ></el-input>
                    </div>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row class="form-item error-info">
                <el-col>
                  <el-form-item>
                    <div v-show="user.errorInfo">
                      <i class="el-icon-error"></i>{{ user.errorInfo }}
                    </div>
                  </el-form-item>
                </el-col>
              </el-row>
              <!-- <el-row class="form-item">
                <el-col>
                  <el-form-item>
                    <el-checkbox class="checkbox">记住账号</el-checkbox>
                  </el-form-item>
                </el-col>
              </el-row> -->
              <el-row class="form-item">
                <el-button
                  size="large"
                  type="primary"
                  class="submit-btn"
                  @click="submitBtn"
                  :loading="loading"
                >
                  登 录
                </el-button>
              </el-row>
            </div>
          </el-form>
        </div>
      </div>
    </div>
    <div class="footer">
      Copyright ©
      <a href="http://www.qmth.com.cn" target="_blank">www.qmth.com.cn</a>, All
      Rights Reserved.
    </div>
  </div>
</template>

<script>
import { LOGIN_BY_USERNAME } from "../../store/action-types";
import { ORG_CODE } from "../../constant/constants";
import { getLogo } from "@/api/login";

export default {
  // components: { BasicContainer },
  name: "Login",
  data() {
    return {
      schoolLogo: "",
      user: {
        username: "",
        password: "",
        errorInfo: "",
      },
      loading: false,
    };
  },
  async created() {
    const res = await getLogo(ORG_CODE);
    this.schoolLogo = res.data.data.logo;
    this.$store.commit("setSchoolLogo", this.schoolLogo);
  },
  methods: {
    async submitBtn() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          try {
            this.loading = true;
            await this.$store.dispatch(LOGIN_BY_USERNAME, {
              loginName: this.user.username,
              password: this.user.password,
              code: ORG_CODE,
            });
            // console.log(res);
            // localStorage.setItem("themis-user", res.data);

            const user = this.$store.state.user;
            const needLiveUrl =
              user.roleCodes.includes("SUPER_ADMIN") ||
              user.roleCodes.includes("ADMIN") ||
              user.roleCodes.includes("INSPECTION") ||
              user.roleCodes.includes("INVIGILATE");
            if (needLiveUrl)
              await this.$store.dispatch("invigilation/fetchLiveDomains");

            this.$message({
              message: "登录成功",
              type: "success",
              duration: 1.5,
              showClose: true,
            });
            this.$router.push("/home");
          } catch (error) {
            // console.log(error?.response?.data?.message);
            this.user.errorInfo = error?.response?.data?.message || "";
          } finally {
            this.loading = false;
          }
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.user-login {
  .user-login-bg {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-size: cover;
    background-image: url(./login-bg.png);
  }
  .el-checkbox__label {
    color: #999;
    font-size: 13px;
  }
  .content-wrapper {
    position: absolute;
    top: -100px;
    left: 0;
    right: 0;
    bottom: 0;
    max-width: 1080px;
    margin: 0 auto;
    display: flex;
    justify-content: space-around;
    align-items: center;
    .slogan {
      text-align: center;
      color: blue;
      font-size: 36px;
      letter-spacing: 2px;
      line-height: 48px;
    }
  }
  .form-container {
    display: flex;
    justify-content: center;
    flex-direction: column;
    width: 400px;
    border: 6px solid rgba(0, 0, 0, 0.1);
    border-radius: 20px;
  }
  .form-body {
    padding: 55px 56px 50px;
    background-color: #fff;
    border-radius: 20px;
  }
  .school-logo {
    height: 60px;
    position: relative;

    > img {
      display: block;
      position: absolute;
      margin: auto;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      max-height: 100%;
      max-width: 100%;
    }
  }
  .login-title {
    color: #626a82;
    font-size: 18px;
    font-weight: 700;
    line-height: 25px;
    margin: 6px 0 40px;
  }

  .el-form-item {
    margin-bottom: 15px;
  }
  .form-Item {
    position: relative;
    flex-direction: column;
  }
  .form-line {
    position: relative;
    display: flex;
    align-items: center;
    &:after {
      content: "";
      position: absolute;
      bottom: 3px;
      left: 0;
      width: 100%;
      box-sizing: border-box;
    }
  }
  .el-input {
    width: 100%;
  }
  .input-icon {
    color: #999;
  }
  .checkbox {
    margin-left: 5px;
  }
  .submit-btn {
    width: 100%;
  }

  .link {
    color: #999;
    text-decoration: none;
    font-size: 13px;
  }
  .line {
    color: #dcd6d6;
    margin: 0 8px;
  }
}

@media screen and (max-width: 720px) {
  .user-login {
    .content-wrapper {
      margin: 20px auto;
      top: 40px;
      max-width: 300px;
      display: block;
      .slogan {
        color: #666;
        font-size: 22px;
        line-height: 30px;
      }
    }
  }
}
.form-item {
  margin-bottom: 5px;
}
.error-info {
  height: 30px;
  overflow: hidden;
  color: red;
}

.footer {
  bottom: 10px;
  position: absolute;
  font-size: 12px;
  text-align: center;
  width: 100%;
  color: #fff;
  a {
    color: #fff;
  }
}
</style>
<style lang="scss">
.user-login {
  .el-input--prefix {
    .el-input__inner {
      padding-left: 40px;
    }
    .el-input__prefix {
      padding-top: 4px;
      left: 16px;
    }
  }
}
</style>
