<template>
  <div class="login-container">
    <div class="login-card">
      <div class="text-center mb-4">
        <i class="bi bi-clipboard-check display-1 text-primary"></i>
        <h2 class="mt-3">업무관리프로그램</h2>
        <p class="text-muted">로그인하여 시작하세요</p>
      </div>

      <!-- 비밀번호 로그인 폼 -->
      <div class="card mb-4">
        <div class="card-body">
          <form @submit.prevent="handlePasswordLogin">
            <div class="mb-3">
              <label class="form-label">이메일</label>
              <input type="email" class="form-control" v-model="email" placeholder="example@gmail.com" required />
            </div>
            <div class="mb-3">
              <label class="form-label">비밀번호</label>
              <input type="password" class="form-control" v-model="password" placeholder="비밀번호" required />
            </div>
            <button type="submit" class="btn btn-primary w-100" :disabled="isSubmitting">
              <span v-if="isSubmitting"><span class="spinner-border spinner-border-sm me-2"></span>로그인 중...</span>
              <span v-else><i class="bi bi-box-arrow-in-right me-2"></i>로그인</span>
            </button>
          </form>
          
          <!-- 소셜 로그인 구분선 -->
          <div class="d-flex align-items-center my-3">
            <hr class="flex-grow-1" /><span class="mx-3 text-muted small">또는</span><hr class="flex-grow-1" />
          </div>
          
          <!-- Google 로그인 -->
          <a href="/api/auth/google" class="btn btn-outline-secondary w-100 mb-2">
            <svg class="me-2" width="18" height="18" viewBox="0 0 24 24"><path fill="#4285F4" d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z"/><path fill="#34A853" d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"/><path fill="#FBBC05" d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z"/><path fill="#EA4335" d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z"/></svg>
            Google로 로그인
          </a>
          
          <!-- Synology 로그인 -->
          <a href="/api/auth/synology" class="btn btn-outline-dark w-100">
            <i class="bi bi-hdd-network me-2"></i>Synology로 로그인
          </a>
          
          <!-- 비밀번호 찾기 -->
          <div class="text-center mt-3">
            <NuxtLink to="/forgot-password" class="text-muted small">비밀번호를 잊으셨나요?</NuxtLink>
          </div>
        </div>
      </div>

      <!-- 에러 메시지 -->
      <div class="alert alert-danger mt-3" v-if="errorMessage">
        <i class="bi bi-exclamation-triangle me-2"></i>{{ errorMessage }}
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
definePageMeta({ layout: false })

const router = useRouter()

const email = ref('')
const password = ref('')
const isSubmitting = ref(false)
const errorMessage = ref('')

async function handlePasswordLogin() {
  if (!email.value || !password.value) return
  isSubmitting.value = true
  errorMessage.value = ''
  try {
    await $fetch('/api/auth/login-password', {
      method: 'POST',
      body: { email: email.value, password: password.value }
    })
    router.push('/')
  } catch (e: any) {
    errorMessage.value = e.data?.message || e.message || '로그인에 실패했습니다.'
  } finally {
    isSubmitting.value = false
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 2rem;
}
.login-card {
  background: white;
  border-radius: 1rem;
  padding: 2rem;
  width: 100%;
  max-width: 450px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
}
</style>
