<?php
session_start();

if (isset($_SESSION['user_id'])) {
    header('Location: /dashboard.php');
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Caregiver Deluxe Login</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    body {
      font-family: Arial, sans-serif;
      background: #f4f7fb;
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
      margin: 0;
    }
    .card {
      background: #fff;
      padding: 32px;
      width: 100%;
      max-width: 400px;
      border-radius: 14px;
      box-shadow: 0 10px 30px rgba(0,0,0,0.10);
    }
    h1 {
      margin-top: 0;
      margin-bottom: 10px;
      font-size: 28px;
    }
    p {
      color: #666;
      margin-bottom: 24px;
    }
    input {
      width: 100%;
      padding: 12px;
      margin-bottom: 14px;
      border: 1px solid #ccc;
      border-radius: 8px;
      box-sizing: border-box;
    }
    button {
      width: 100%;
      padding: 12px;
      border: 0;
      border-radius: 8px;
      background: #2563eb;
      color: #fff;
      font-size: 16px;
      cursor: pointer;
    }
    button:hover {
      background: #1d4ed8;
    }
    #message {
      margin-top: 14px;
      font-size: 14px;
      color: #c00;
      min-height: 20px;
    }
  </style>
</head>
<body>
  <div class="card">
    <h1>Caregiver Deluxe</h1>
    <p>Sign in to continue</p>

    <form id="loginForm">
      <input type="email" id="email" name="email" placeholder="Email" required>
      <input type="password" id="password" name="password" placeholder="Password" required>
      <button type="submit">Sign In</button>
    </form>

    <div id="message"></div>
  </div>

  <script>
    document.getElementById('loginForm').addEventListener('submit', async function(e) {
      e.preventDefault();

      const formData = new FormData();
      formData.append('email', document.getElementById('email').value);
      formData.append('password', document.getElementById('password').value);

      const response = await fetch('/api/session-login.php', {
        method: 'POST',
        body: formData
      });

      const data = await response.json();

      if (data.success) {
        window.location.href = data.redirect;
      } else {
        document.getElementById('message').textContent = data.message || 'Login failed';
      }
    });
  </script>
</body>
</html>