<?php
// ── Router sederhana ──
session_start();
$page = $_GET['page'] ?? 'login';

// ── Halaman yang butuh login ──
$protected = [
    'dashboard','siswa','kelas','riwayat_siswa','alumni','tagihan','pembayaran','riwayat',
    'users','laporan','kwitansi','pengaturan','admin'
];
if (in_array($page, $protected) && !isset($_SESSION['admin_id'])) {
    header('Location: index.php?page=login');
    exit;
}

// ── Kalau sudah login, redirect dari login ke dashboard ──
if ($page === 'login' && isset($_SESSION['admin_id'])) {
    header('Location: index.php?page=dashboard');
    exit;
}

require_once 'config/db.php';
require_once 'config/helpers.php';

// ── View map (PHP 7.4 compatible) ──
$viewMap = [
    'login'          => 'views/login.php',
    'dashboard'      => 'views/dashboard.php',
    'siswa'          => 'views/siswa.php',
    'kelas'          => 'views/kelas.php',
    'riwayat_siswa'  => 'views/riwayat_siswa.php',
    'alumni'         => 'views/alumni.php',
    'tagihan'        => 'views/tagihan.php',
    'pembayaran'     => 'views/pembayaran.php',
    'riwayat'        => 'views/riwayat.php',
    'laporan'        => 'views/laporan.php',
    'kwitansi'       => 'views/kwitansi.php',
    'pengaturan'     => 'views/pengaturan.php',
    'admin'          => 'views/admin.php',
    'users'          => 'views/users.php',
    'logout'         => 'views/logout.php',
];

$view = isset($viewMap[$page]) ? $viewMap[$page] : 'views/404.php';

// ── API endpoint untuk Android (JSON) ──
if ($page === 'api') {
    require_once 'api/router.php';
    exit;
}

require $view;