<?php
session_start();
require_once 'config.php';

$error = ""; // Inisialisasi variabel error agar tidak error saat dipanggil

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $input_user = $_POST['username']; 
    $input_pass = $_POST['password']; 

    // 1. CEK USER (ADMIN & KADER berada di tabel users)
    $stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
    $stmt->execute([$input_user]);
    $user = $stmt->fetch();

    if ($user && password_verify($input_pass, $user['password'])) {
        $_SESSION['role'] = $user['role'];
        $_SESSION['nama'] = $user['nama_lengkap'];
        $_SESSION['posyandu'] = $user['id_posyandu'];

        // PENGECEKAN ROLE UNTUK REDIRECT
        if ($user['role'] === 'admin') {
            header("Location: admin/dashboard.php");
        } else {
            header("Location: kader/dashboard.php");
        }
        exit;
    } 

    // 2. CEK IBU (Username & Password menggunakan NIK Ibu dari tabel balita)
    $stmt_ibu = $pdo->prepare("SELECT * FROM balita WHERE nik_ibu_kunci = ? LIMIT 1");
    $stmt_ibu->execute([$input_user]);
    $dataIbu = $stmt_ibu->fetch();

    if ($dataIbu && $input_pass === $dataIbu['nik_ibu_kunci']) {
        $_SESSION['role'] = 'ibu';
        $_SESSION['nik_ibu_kunci'] = $dataIbu['nik_ibu_kunci']; 
        $_SESSION['nama_balita'] = $dataIbu['nama_balita'];
        header("Location: ibu/dashboard.php");
        exit;
    }

    // Jika semua pengecekan di atas gagal
    $error = "Username atau Password salah!";
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login - Sistem Informasi Posyandu Desa Gemuruh</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body { 
            background: linear-gradient(135deg, #f0f4f8 0%, #d7e3ec 100%); 
            height: 100vh; display: flex; align-items: center; 
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        .login-card { border: none; border-radius: 25px; box-shadow: 0 15px 35px rgba(0,0,0,0.1); background: white; }
        .btn-primary { border-radius: 12px; padding: 12px; font-weight: bold; background: #0d6efd; border: none; }
        .form-control { border-radius: 10px; padding: 12px; background-color: #f8fafc; }
    </style>
</head>
<body>

<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-5 col-lg-4 px-4">
            <div class="card login-card p-4">
                <div class="text-center mb-4">
                    <h3 class="fw-bold text-primary mb-1">POSYANDU</h3>
                    <p class="text-muted small">Lestari Widodo - Desa Gemuruh</p>
                </div>
                
                <?php if ($error): ?>
                    <div class="alert alert-danger py-2 small text-center"><?= $error ?></div>
                <?php endif; ?>

                <form method="POST">
                    <div class="mb-3">
                        <label class="form-label small fw-bold text-secondary">Username</label>
                        <input type="text" name="username" class="form-control" placeholder="Masukkan username" required autocomplete="off">
                    </div>
                    <div class="mb-4">
                        <label class="form-label small fw-bold text-secondary">Password</label>
                        <input type="password" name="password" class="form-control" placeholder="Masukkan password" required>
                    </div>
                    <div class="d-grid">
                        <button type="submit" class="btn btn-primary shadow-sm">MASUK APLIKASI</button>
                    </div>
                </form>
                
                <div class="text-center mt-4">
                    <hr class="text-muted">
                    <p class="mb-0 text-muted" style="font-size: 0.6rem;">Digitalisasi Pelayanan Posyandu v.1.2 Desa Gemuruh</p>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>