<?php
// Pastikan config.php sudah ada session_start() di dalamnya
require_once '../includes/config.php'; 

$error_msg = ""; 

// --- LOGIKA LOGIN MANUAL ---
if (isset($_POST['login'])) {
    $email = trim($_POST['email']);
    $pass  = $_POST['password'];

    try {
        $stmt = $pdo->prepare("SELECT * FROM pelapor WHERE email = ?");
        $stmt->execute([$email]);
        $user = $stmt->fetch();

        if ($user) {
            // Cek apakah password cocok dengan yang di database (password_verify)
            if (password_verify($pass, $user['password'])) {
                $_SESSION['pelapor_id'] = $user['id'];
                $_SESSION['pelapor_nama'] = $user['nama_lengkap'];
                header("Location: index.php");
                exit;
            } else {
                $error_msg = "Password salah! Silakan coba lagi.";
            }
        } else {
            // DISINI PERUBAHANNYA: Tidak daftar otomatis, tapi minta user daftar dulu
            $error_msg = "Email belum terdaftar. Silakan buat akun baru!";
        }
    } catch (PDOException $e) {
        $error_msg = "Gagal menyambung ke database: " . $e->getMessage();
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Portal Pelapor - Tangkis BPS</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    
    <style>
        body { background: linear-gradient(135deg, #0d6efd 0%, #6610f2 100%); min-height: 100vh; display: flex; align-items: center; justify-content: center; font-family: 'Inter', sans-serif; }
        .card-login { background: white; border-radius: 20px; box-shadow: 0 15px 35px rgba(0,0,0,0.2); width: 100%; max-width: 420px; padding: 40px; }
        .btn-google { background: #ffffff; color: #1f1f1f; border: 1px solid #dadce0; border-radius: 8px; padding: 10px; font-weight: 600; display: flex; align-items: center; justify-content: center; text-decoration: none; transition: 0.2s; }
        .btn-google:hover { background: #f7f8f9; box-shadow: 0 1px 3px rgba(0,0,0,0.1); }
        .divider { display: flex; align-items: center; text-align: center; margin: 25px 0; color: #888; font-size: 0.85rem; text-transform: uppercase; }
        .divider::before, .divider::after { content: ''; flex: 1; border-bottom: 1px solid #eee; }
        .divider:not(:empty)::before { margin-right: 1.5rem; }
        .divider:not(:empty)::after { margin-left: 1.5rem; }
        .form-control { border-radius: 8px; padding: 12px; }
    </style>
</head>
<body>

    <div class="card-login text-center shadow-lg">
        <div class="mb-3">
            <i class="fas fa-shield-halved fa-3x text-primary"></i>
        </div>
        <h4 class="fw-bold mb-1">Portal Pelapor</h4>
        <p class="text-muted small mb-4">TANGKIS BPS Kabupaten Lampung Timur</p>

        <form method="POST">
            <div class="mb-3 text-start">
                <label class="form-label small fw-bold">Email</label>
                <input type="email" name="email" class="form-control" placeholder="nama@email.com" required>
            </div>
            <div class="mb-4 text-start">
                <label class="form-label small fw-bold">Password</label>
                <input type="password" name="password" class="form-control" placeholder="••••••••" required>
            </div>
            <button type="submit" name="login" class="btn btn-primary w-100 fw-bold shadow-sm mb-2">MASUK</button>
        </form>

        <div class="mt-4">
            <p class="small text-muted">Belum punya akun? <a href="register_pelapor.php" class="text-decoration-none fw-bold text-primary">Daftar Akun Baru</a></p>
        </div>
    </div>

    <script>
        <?php if($error_msg != ""): ?>
            Swal.fire({
                icon: 'error',
                title: 'Login Gagal',
                text: '<?= $error_msg ?>',
                confirmButtonColor: '#0d6efd'
            });
        <?php endif; ?>
    </script>

</body>
</html>