<?php
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/includes/auth.php';
require_once __DIR__ . '/includes/db.php';

// Ensure DB exists (simple check)
$mysqli = db();

if (!current_user()) {
  header('Location: ' . url_for('login.php'));
  exit;
}

$page = $_GET['page'] ?? 'dashboard';
$allowed = ['stock_detail_report','employees','payroll','finance_dashboard','finance_realtime_dashboard','finance_manager_dashboard','bank_reconciliation','petty_cash','cash_book_entry','transaction_history','bank_accounts','attendance','dashboard','users','suppliers','purchase_orders','materials','types','colors','finishes','ship_modes','delivery_terms','payment_terms','shop_settings','services','expenditures','expenditure_to_cashbook','journal_entries','salary_management','import_raw_materials','polishing','finished_products','slab_cutting','cutting_inventory','customers','sales','invoice_print','reports','stock_summary_report','production_report','competitor_analysis','branches','store_locations','production','designations','backups','company_licenses'];
if (!in_array($page, $allowed, true)) $page = 'dashboard';

include __DIR__ . '/templates/header.php';
?>
<div id="app-wrapper">
  <?php include __DIR__ . '/templates/sidebar.php'; ?>
  <main id="content" class="p-0 w-100">
    <?php include __DIR__ . '/pages/' . $page . '.php'; ?>
  </main>
</div>
<?php include __DIR__ . '/templates/footer.php'; ?>
