<template>
  <div class="login-page">
    <div class="login-card">
      <div class="logo">
        <span class="icon">🗳️</span>
        <h1>Election 2026</h1>
      </div>
      <h2>Login to KCPS</h2>
      <p class="subtitle">Please enter your credentials to access the booth or admin panel.</p>

      <div class="form-group">
        <label>Username</label>
        <div class="input-wrapper">
          <input v-model="username" type="text" placeholder="Enter username" />
        </div>
      </div>

      <div class="form-group">
        <label>Password</label>
        <div class="input-wrapper">
          <input v-model="password" type="password" placeholder="••••••••" @keyup.enter="handleLogin" />
        </div>
      </div>

      <button @click="handleLogin" :disabled="loading" class="primary-btn">
        <span v-if="!loading">Sign In</span>
        <span v-else class="loader"></span>
      </button>

      <p v-if="error" class="error-message">{{ error }}</p>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { authService } from '../services/api';

const username = ref('');
const password = ref('');
const error = ref('');
const loading = ref(false);
const router = useRouter();

const handleLogin = async () => {
  if (!username.value || !password.value) {
    error.value = 'Fields cannot be empty';
    return;
  }
  loading.value = true;
  error.value = '';
  try {
    const response = await authService.login({ username: username.value, password: password.value });
    localStorage.setItem('token', response.data.token);
    localStorage.setItem('role', response.data.role);
    if (response.data.role === 'admin') router.push('/admin');
    else router.push('/vote');
  } catch (err) {
    error.value = 'Invalid username or password';
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: var(--bg-color);
  padding: 1rem;
}

.login-card {
  width: 100%;
  max-width: 420px;
  background-color: var(--card-bg);
  padding: 2.5rem;
  border-radius: 12px;
  border: 1px solid var(--border-color);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
}

.logo {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 2rem;
}

.logo .icon {
  font-size: 3rem;
  margin-bottom: 0.5rem;
}

.logo h1 {
  font-size: 1.5rem;
  font-weight: 700;
  margin: 0;
  color: var(--accent-color);
  text-transform: uppercase;
  letter-spacing: 2px;
}

h2 {
  font-size: 1.25rem;
  font-weight: 600;
  margin-bottom: 0.5rem;
  text-align: center;
  color: var(--text-primary);
}

.subtitle {
  color: var(--text-secondary);
  font-size: 0.9rem;
  text-align: center;
  margin-bottom: 2rem;
}

.form-group {
  margin-bottom: 1.5rem;
}

label {
  display: block;
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 0.5rem;
}

input {
  width: 100%;
  background: #252932;
  border: 1px solid var(--border-color);
  padding: 0.8rem 1rem;
  border-radius: 8px;
  color: var(--text-primary);
  font-size: 1rem;
  box-sizing: border-box;
}

input:focus {
  outline: none;
  border-color: var(--accent-color);
  box-shadow: 0 0 0 2px rgba(124, 58, 237, 0.2);
}

.primary-btn {
  width: 100%;
  background-color: var(--accent-color);
  color: #fff;
  padding: 1rem;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  margin-top: 1rem;
  transition: opacity 0.2s, transform 0.1s;
}

.primary-btn:hover:not(:disabled) {
  opacity: 0.9;
}

.primary-btn:active:not(:disabled) {
  transform: scale(0.98);
}

.primary-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.error-message {
  color: var(--danger-color);
  background: rgba(231, 76, 60, 0.1);
  padding: 0.8rem;
  border-radius: 6px;
  font-size: 0.85rem;
  margin-top: 1.5rem;
  text-align: center;
  border: 1px solid rgba(231, 76, 60, 0.2);
}

.loader {
  width: 20px;
  height: 20px;
  border: 3px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  border-top-color: #fff;
  animation: spin 1s ease-in-out infinite;
  display: inline-block;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}
</style>
