<?php
require_once 'config.php';

// Jika sudah login, redirect ke dashboard masing-masing
if (isset($_SESSION['role'])) {
    if ($_SESSION['role'] === 'admin') header("Location: admin/dashboard.php");
    elseif ($_SESSION['role'] === 'petugas') header("Location: petugas/dashboard.php");
    elseif ($_SESSION['role'] === 'pelanggan') header("Location: pelanggan/dashboard.php");
    exit;
}

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username']);
    $password = $_POST['password'];
    
    if (!empty($username) && !empty($password)) {
        // 1. Cek Admin & Petugas
        $stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
        $stmt->execute([$username]);
        $user = $stmt->fetch();
        
        if ($user && !empty($user['password']) && password_verify($password, $user['password'])) {
            $_SESSION['id'] = $user['id'];
            $_SESSION['nama'] = $user['nama'];
            $_SESSION['role'] = $user['role'];
            
            if ($user['role'] === 'admin') {
                header("Location: admin/dashboard.php");
            } else {
                header("Location: petugas/dashboard.php");
            }
            exit;
        }
        
        // 2. Cek Pelanggan
        $stmt = $pdo->prepare("SELECT * FROM pelanggan WHERE username = ? AND status = 'aktif'");
        $stmt->execute([$username]);
        $pelanggan = $stmt->fetch();
        
        // TAMBAHAN: Cek !empty($pelanggan['password']) agar tidak error di PHP 8.3
        if ($pelanggan && !empty($pelanggan['password']) && password_verify($password, $pelanggan['password'])) {
            $_SESSION['id'] = $pelanggan['id'];
            $_SESSION['nama'] = $pelanggan['nama'];
            $_SESSION['role'] = 'pelanggan';
            $_SESSION['id_meteran'] = $pelanggan['id_meteran'];
            header("Location: pelanggan/dashboard.php");
            exit;
        }
        
        $error = "Username atau password salah, atau akun tidak aktif/tidak memiliki password.";
    } else {
        $error = "Username dan password harus diisi.";
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>Masuk - SPAM Desa Gemuruh</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root {
            --primary-sage: #3A7CA5;
            --dark-sage: #2C5F7D;
        }
        body {
            background-color: #f4f7fe;
            font-family: 'Segoe UI', sans-serif;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }
        .login-header {
            background: linear-gradient(135deg, var(--primary-sage) 0%, var(--dark-sage) 100%);
            color: white;
            padding: 40px 20px 60px;
            border-bottom-left-radius: 40px;
            border-bottom-right-radius: 40px;
            text-align: center;
        }
        .login-header h2 {
            font-weight: 800;
            font-size: 1.5rem;
            margin-bottom: 5px;
        }
        .login-header p {
            font-size: 0.9rem;
            opacity: 0.9;
            margin: 0;
        }
        .login-container {
            padding: 0 20px;
            margin-top: -35px;
            flex: 1;
        }
        .login-card {
            background: white;
            border-radius: 20px;
            box-shadow: 0 8px 20px rgba(0,0,0,0.06);
            padding: 25px;
        }
        .form-control {
            border-radius: 12px;
            padding: 12px 15px;
            border: 1px solid #e0e0e0;
            background: #f8f9fa;
        }
        .form-control:focus {
            border-color: var(--primary-sage);
            box-shadow: 0 0 0 0.25rem rgba(58, 124, 165, 0.25);
            background: white;
        }
        .form-label {
            font-weight: 700;
            font-size: 0.85rem;
            color: #4a5568;
            margin-bottom: 6px;
        }
        .btn-login {
            background: linear-gradient(135deg, var(--primary-sage) 0%, var(--dark-sage) 100%);
            color: white;
            border: none;
            border-radius: 12px;
            padding: 12px;
            font-weight: 700;
            font-size: 1rem;
            width: 100%;
            margin-top: 10px;
        }
        .btn-login:hover {
            opacity: 0.9;
            color: white;
        }
        .back-link {
            text-align: center;
            margin-top: 20px;
        }
        .back-link a {
            color: var(--primary-sage);
            text-decoration: none;
            font-weight: 600;
            font-size: 0.9rem;
        }
    </style>
</head>
<body>

    <div class="login-header">
        <i class="fa-solid fa-droplet fa-2x mb-2"></i>
        <h2>Masuk Aplikasi</h2>
        <p>SPAM Desa Gemuruh</p>
    </div>

    <div class="login-container">
        <div class="login-card">
            <?php if ($error): ?>
                <div class="alert alert-danger alert-dismissible fade show" role="alert" style="font-size: 0.85rem; border-radius: 10px;">
                    <i class="fa-solid fa-circle-exclamation me-1"></i> <?= htmlspecialchars($error) ?>
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
            <?php endif; ?>

            <form method="POST" action="">
                <div class="mb-3">
                    <label for="username" class="form-label">Username</label>
                    <div class="input-group">
                        <span class="input-group-text bg-light border-end-0" style="border-radius: 12px 0 0 12px; border-color: #e0e0e0;">
                            <i class="fa-solid fa-user text-muted"></i>
                        </span>
                        <input type="text" class="form-control border-start-0 ps-0" id="username" name="username" 
                               placeholder="Masukkan username" required value="<?= isset($_POST['username']) ? htmlspecialchars($_POST['username']) : '' ?>">
                    </div>
                </div>

                <div class="mb-4">
                    <label for="password" class="form-label">Password</label>
                    <div class="input-group">
                        <span class="input-group-text bg-light border-end-0" style="border-radius: 12px 0 0 12px; border-color: #e0e0e0;">
                            <i class="fa-solid fa-lock text-muted"></i>
                        </span>
                        <input type="password" class="form-control border-start-0 ps-0" id="password" name="password" 
                               placeholder="Masukkan password" required>
                    </div>
                </div>

                <button type="submit" class="btn btn-login">
                    <i class="fa-solid fa-right-to-bracket me-2"></i> Masuk
                </button>
            </form>

            <div class="back-link">
                <a href="index.php"><i class="fa-solid fa-arrow-left me-1"></i> Kembali ke Beranda</a>
            </div>
        </div>
        
        <div class="text-center mt-4 mb-3" style="font-size: 0.75rem; color: #a0aec0;">
            <p class="mb-0">Default Admin: <b>admin</b> | Petugas: <b>petugas</b><br>Password: <b>password</b></p>
        </div>
    </div>

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