<?php
session_start();

// Nonaktifkan cache untuk halaman login
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');

// Jika pengguna sudah login, arahkan langsung ke halaman admin
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true) {
    header('Location: /korandigital/admin/dashboard.php'); // Arahkan ke halaman admin
    exit;
}

// Ubah username dan password sesuai dengan kredensial admin Anda
$valid_username = 'admin';
$valid_password_hash = password_hash('password123', PASSWORD_DEFAULT); // Hash password (gunakan di pembuatan awal)

// Jika menerima POST request
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';

    // Validasi username dan password
    if ($username === $valid_username && password_verify($password, $valid_password_hash)) {
        $_SESSION['loggedin'] = true;
        header('Location: /korandigital/admin/dashboard.php'); // Redirect ke halaman admin setelah login sukses
        exit;
    } else {
        $error = 'Username atau password salah.';
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Login</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-dark">
<div class="container py-4">
    <div class="row justify-content-center">
        <div class="col-md-4 py-4">
            <div class="card bg-dark p-4 border-0 shadow rounded-4">
                <div class="card-body">
                    <h2 class="card-title fs-4 text-light">Sign to Dashboard</h2>
                    <?php if (isset($error)): ?>
                        <div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
                    <?php endif; ?>
                    <form action="" method="post">
                        <div class="mb-3">
                            <label for="username" class="form-label text-secondary">Username</label>
                            <input type="text" class="form-control" id="username" name="username" required>
                        </div>
                        <div class="mb-3">
                            <label for="password" class="form-label text-secondary">Password</label>
                            <input type="password" class="form-control" id="password" name="password" required>
                        </div>
                        <button type="submit" class="btn btn-danger">Sign In</button>
                    </form>
                </div>
            </div>
            <p class="mt-4 text-center text-light">Kembali ke  <a class="text-decoration-none text-warning" href="https://blthemes.com/korandigital/"><span class="fw-bold">KoraNews</span></p>
        </div>
    </div>
</div>
</body>
</html>
