<style scoped>
.bg-img {
  background-image: linear-gradient(rgba(0, 0, 0, 0) 30%, rgba(0, 0, 0, 1) 90%), url("@images/auth/auth_img00.svg");
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 50rem;
  padding: 2rem 0;
}
.area {
  background-color: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(0.6rem);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 4rem;
  font-family: Abel;
  color: #000;
}
.card {
  background-color: #fff;
  border-radius: 0;
  padding: 1rem 4rem;
}
.card-header {
  background-color: transparent;
  border: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.card-header img {
  width: 12rem;
}
.card-body h1 {
  font-family: 'Noto Serif';
  font-size: 1.8rem;
  font-weight: bold;
  text-align: center;
  padding-top: 6rem;
}
.card-body h1 span {
  font-family: Outfit;
}
.card-body h5 {
  color: #3d3d3d;
  font-size: 0.8rem;
  font-weight: bold;
  text-align: center;
  padding-top: 0.8rem;
  padding-bottom: 2rem;
}
.row {
  --bs-gutter-x: 0;
}
label {
  font-size: 1rem;
  font-weight: bold;
}
.form-control {
  background-color: #f5f7fa;
  border: 0;
  font-size: 0.8rem;
  font-weight: bold;
  padding: 0.6rem 1rem;
}
.btn-login {
  width: 100%;
  background-color: #000;
  color: #fff;
  font-size: 1rem;
  font-weight: bold;
  border: 1px #000 solid;
  border-radius: 0.6rem;
  margin-top: 1.5rem;
  padding: 0.5rem 0;
  transition: 0.3s;
}
.btn-login:hover {
  background-color: #fff;
  color: #000;
}
.text {
  color: #000;
  font-size: 1rem;
  font-weight: bold;
}
.text2 {
  color: #000;
  font-size: 1rem;
}
a {
  color: #3d3d3d !important;
  font-size: 1rem;
  font-weight: bold;
}
#pwd-visible {
  position: absolute;
  top: 50%;
  right: 0.8rem;
  transform: translateY(-50%);
}
#password {
  padding-right: 3rem;
}
.register {
  color: #000 !important;
  -webkit-text-stroke: 0.5px #000;
}
.captcha-input {
  width: 8rem;
}
.captcha-image {
  width: 6rem;
}
.goto-area {
  margin-top: 6rem;
}
@media (max-width: 768px) {
  .area {
    padding: 4rem 2rem;
  }
  .card {
    padding: 1rem 2rem;
  }
}
/* #region ========= area2 */
.area2 {
  background-color: #000;
  min-height: 10rem;
}
/* #endregion */
</style>
<template>
  <div class="bg-img">
    <div class="area col-12 col-md-9 col-xl-7">
      <div class="card col-md-9 col-xl-7">
        <div class="card-header">
          <img src="@images/ida_logo_long.png" alt="logo" />
        </div>
        <div class="card-body p-0">
          <h1>
            <span>智慧化管理技術平台</span>
          </h1>
          <h5>請輸入電子郵件地址和密碼登入</h5>
          <form action="javascript:void(0)" class="row" method="post" @submit="login">
            <div class="mb-3 p-0" v-if="Object.keys(validationErrors).length > 0">
              <div class="alert alert-danger">
                <ul class="mb-0">
                  <li class="text" v-for="(value, key) in validationErrors" :key="key">
                    {{ value[0] }}
                  </li>
                </ul>
              </div>
            </div>

            <label for="email" class="p-0">電子郵件</label>
            <div class=" d-flex p-0">
              <input
                type="email"
                v-model="auth.email"
                name="email"
                id="email"
                class="form-control mt-1 mb-3"
                placeholder="電子郵件地址"
              />
            </div>

            <label for="password" class="p-0">密碼</label>
            <div class="position-relative d-flex p-0">
              <input
                :type="showPassword ? 'text' : 'password'"
                v-model="auth.password"
                name="password"
                id="password"
                class="form-control mt-1"
                autocomplete="off"
                placeholder="密碼"
              />
              <i id="pwd-visible" class="bi " :class="{ 'bi-eye-fill': showPassword, 'bi-eye-slash-fill': !showPassword }" @click="toggleShow"></i>
            </div>

            <label for="captcha" class="p-0 mt-3">驗證碼</label>
            <div class="d-flex p-0">
              <input
                type="text"
                v-model="auth.captcha"
                name="captcha"
                id="captcha"
                class="form-control mt-1 captcha-input"
                placeholder="請輸入驗證碼"
              />
              <img class="mt-1 ms-1 captcha-image" alt="captcha" :src="captchaSrc" @click="refreshCaptcha">
            </div>
            <!-- <div class="text-end">
              <a href="javascript:void(0);" @click="refreshCaptcha">刷新驗證碼</a>
            </div> -->

            <div class="text-center mt-3">
              <button
                type="submit"
                :disabled="processing"
                class="btn-login"
              >
                {{ processing ? "請稍等..." : "登入" }}
              </button>
            </div>
          </form>

          <div class="text-center goto-area"></div>
        </div>
      </div>
    </div>
  </div>
  <div class="area2"></div>
</template>
<script>
import { api } from "@/global/axios";
import { isAdmin, isParent, isSub } from "@/global/request";
export default {
  name: "login",
  data() {
    return {
      auth: {
        email: "",
        password: "",
        captcha: "",
      },
      captchaSrc: '/captcha/custom',
      validationErrors: [],
      processing: false,
      showPassword: false,
    };
  },
  created() {
    this.$store.commit("SET_LOADING", true);

    setTimeout(() => {
      this.$store.commit("SET_LOADING", false);
    }, 1000);
  },
  methods: {
    toggleShow() {
      this.showPassword = !this.showPassword;
    },
    refreshCaptcha() {
      this.captchaSrc = `/captcha/custom?${Math.random()}`;
    },
    async login() {
      this.processing = true;
      var vm = this;
      await api
        .post("/auth/login", this.auth)
        .then(({ data }) => {
          localStorage.setItem("APP_USER_TOKEN", data.data.api_token);

          vm.$store.commit("auth/SET_USER", data.data);
          vm.$store.commit("auth/SET_AUTHENTICATED", true);

          if (isAdmin()) {
            window.location.href = "/home/adminHome";
          }
          else{
            window.location.href = "/home/parentHome";
          }
        })
        .catch(({ response }) => {
          switch (response.status) {
            case 419:
              window.location = "/login";
              break;
            case 422:
            case 429:
              this.validationErrors = response.data.errors;
              break;
            case 500:
              this.validationErrors = response.data.errors;
              break;
            case 400:
              this.validationErrors = response.data.errors;

              const params = {
                email: this.auth.email
              };
              const encodedParams = btoa(encodeURIComponent(JSON.stringify(params)));

              this.$router.push({ path: `/email/send/${encodedParams}` });
              break;
            default:
              // this.validationErrors = {};
              this.validationErrors = response.data.errors;
              // alert(response.data.message);
          }
          this.refreshCaptcha();
        })
        .finally(() => {
          this.processing = false;
        });
    },
  },
};
</script>
