<script setup lang="ts">
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import InputText from 'primevue/inputtext';
import Password from 'primevue/password';
import Button from 'primevue/button';
import Card from 'primevue/card';
import Message from 'primevue/message';
import { useAuthStore } from '../auth.store';

const authStore = useAuthStore();
const router = useRouter();

const email = ref('');
const password = ref('');
const error = ref('');

async function onLogin() {
  error.value = '';
  try {
    await authStore.login(email.value, password.value);
    router.push('/dashboard');
  } catch (e: any) {
    error.value = e?.response?.data?.detail ?? '로그인에 실패했습니다. 이메일과 비밀번호를 확인하세요.';
  }
}
</script>

<template>
  <div class="login-page">
    <Card class="login-page__card">
      <template #content>
        <div class="login-page__content">
          <!-- Logo -->
          <div class="login-page__logo">
            <i class="pi pi-clock" style="font-size: 2.5rem; color: var(--p-primary-color);" />
            <h1 class="login-page__title">WTM</h1>
            <p class="login-page__subtitle">Work Time Manager</p>
          </div>

          <!-- Error -->
          <Message v-if="error" severity="error" :closable="false" style="width: 100%;">
            {{ error }}
          </Message>

          <!-- Form -->
          <form class="login-page__form" @submit.prevent="onLogin">
            <div class="form-field">
              <label class="form-field__label">이메일</label>
              <InputText
                v-model="email"
                type="email"
                placeholder="user@hanwha.com"
                fluid
                required
              />
            </div>

            <div class="form-field">
              <label class="form-field__label">비밀번호</label>
              <Password
                v-model="password"
                placeholder="비밀번호 입력"
                :feedback="false"
                toggleMask
                fluid
                :inputStyle="{ width: '100%' }"
              />
            </div>

            <Button
              type="submit"
              label="로그인"
              icon="pi pi-sign-in"
              :loading="authStore.loading"
              fluid
              class="login-page__submit"
            />
          </form>

          <div class="login-page__links">
            <router-link to="/forgot-password" class="login-page__link">
              비밀번호 찾기
            </router-link>
          </div>
        </div>
      </template>
    </Card>
  </div>
</template>

<style lang="scss" scoped>
@use '@/assets/styles/variables' as *;

.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: var(--p-surface-50);
  padding: $space-md;

  &__card {
    width: 100%;
    max-width: 420px;

    :deep(.p-card-content) {
      padding: 0;
    }
  }

  &__content {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: $space-lg;
  }

  &__logo {
    text-align: center;
  }

  &__title {
    font-size: $font-size-2xl;
    font-weight: 700;
    margin: $space-sm 0 0;
    color: $color-text;
  }

  &__subtitle {
    font-size: $font-size-sm;
    color: $color-text-muted;
    margin: $space-xs 0 0;
  }

  &__form {
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: $space-md;
  }

  &__submit {
    margin-top: $space-sm;
  }

  &__links {
    text-align: center;
  }

  &__link {
    font-size: $font-size-sm;
    color: $color-primary;
    text-decoration: none;

    &:hover {
      text-decoration: underline;
    }
  }
}
</style>
