<script setup>
import Cookies from "js-cookie";

import { User, Lock } from "@element-plus/icons-vue";
import { ref } from "vue";

import { login } from '@/api/auth'

import { useRouter } from 'vue-router';
const router = useRouter();

import { useSettingStore } from '@/store/setting.js';
const settingStore = useSettingStore();

const formRef = ref(null);

const error = ref('');

const form = ref({
  username: "",
  password: "",
  remember: false,
});

const rules = ref({
  username: [
    { required: true, message: '請輸入帳號', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '請輸入密碼', trigger: 'blur' },
  ],
})  

const submitForm = async (formEl) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      let res = await login(form.value);
      if (res.code !== 200) {
        error.value = res.data;
        return;
      }

      // Cookies.set('Token', res.data.token);
      sessionStorage.setItem('token', res.data.token);
      router.push("/home");
    }
  })
}
</script>

<template>
  <div class="common-layout">
    <div class="content">
      <div class="top">
        <div class="header">
          <!-- <img alt="logo" class="logo" src="@/assets/img/logo.png" /> -->
          <span class="title">{{ settingStore.systemName }}</span>
        </div>
      </div>
      <div class="login">
        <el-form
          ref="formRef"
          :model="form"
          :rules="rules"
          class="ruleForm"
          status-icon
        >
          <el-alert
            type="error"
            :closable="true"
            v-if="error"
            :title="error"
            showIcon
            style="margin-bottom: 24px"
          />
          <el-form-item prop="username">
            <el-input
              autocomplete="autocomplete"
              size="large"
              v-model="form.username"
              placeholder="請輸入登入帳號"
              :prefix-icon="User"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              size="large"
              v-model="form.password"
              placeholder="請輸入登入密碼"
              :prefix-icon="Lock"
              show-password
            />
          </el-form-item>

          <!-- <div>
            <el-checkbox :checked="false">記往密碼</el-checkbox>
          </div> -->
          <el-form-item>
            <el-button
              style="width: 100%; margin-top: 24px"
              size="large"
              type="primary"
              @click="submitForm(formRef)"
            >
              登入
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <!-- <page-footer :link-list="footerLinks" :copyright="copyright"></page-footer> -->
  </div>
</template>

<style lang="less" scoped>
.common-layout {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: auto;
  background-color: var(--el-bg-color-page);
  background-image: url("https://gw.alipayobjects.com/zos/rmsportal/TVYTbAXWheQpRcWDaDMu.svg");
  background-repeat: no-repeat;
  background-position-x: center;
  background-position-y: 110px;
  background-size: 100%;

  .content {
    padding: 32px 0;
    flex: 1;

    @media (min-width: 768px) {
      padding: 112px 0 24px;
    }
  }

  .top {
    text-align: center;

    .header {
      margin: 20px 0;
      height: 44px;
      line-height: 44px;

      a {
        text-decoration: none;
      }

      .logo {
        height: 44px;
        vertical-align: top;
        margin-right: 16px;
      }

      .title {
        font-size: 33px;
        color: @title-color;
        // font-family: "Myriad Pro", "Helvetica Neue", Arial, Helvetica,
        //   sans-serif;
        font-weight: 600;
        position: relative;
        top: 2px;
      }
    }

    .desc {
      font-size: 14px;
      color: @text-color-second;
      margin-top: 12px;
      margin-bottom: 40px;
    }
  }

  .login {
    width: 368px;
    margin: 0 auto;

    @media screen and (max-width: 576px) {
      width: 95%;
    }

    @media screen and (max-width: 320px) {
      .captcha-button {
        font-size: 14px;
      }
    }

    .icon {
      font-size: 24px;
      color: @text-color-second;
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: @primary-color;
      }
    }
  }
}
</style>
