<?php
// TEMPORARY DEBUG - remove after fixing
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

session_start();

if (!isset($_SESSION["user_id"]) || $_SESSION["role"] !== "admin") {
    header("Location: ../auth/login.php");
    exit;
}

require_once '../config/database.php';

echo "<pre style='font-family:monospace;background:#0f172a;color:#e2e8f0;padding:24px;font-size:13px;'>";
echo "=== DASHBOARD DEBUG ===\n\n";
echo "Session OK: user_id={$_SESSION['user_id']} role={$_SESSION['role']}\n\n";

/* Test each table individually */
$tables = [
    'customers'                => "SELECT COUNT(*) FROM customers",
    'partner_shops'            => "SELECT COUNT(*) FROM partner_shops",
    'credit_requests'          => "SELECT COUNT(*) FROM credit_requests WHERE status='pending'",
    'transactions'             => "SELECT COUNT(*) FROM transactions WHERE DATE(created_at)=CURDATE()",
    'companies'                => "SELECT COUNT(*) FROM companies WHERE status='active'",
    'company_staff'            => "SELECT COUNT(*) FROM company_staff",
    'hr_leave'                 => "SELECT COUNT(*) FROM hr_leave WHERE status='pending'",
];

foreach ($tables as $table => $sql) {
    try {
        $val = $pdo->query($sql)->fetchColumn();
        echo "✅ $table — count: $val\n";
    } catch (Throwable $e) {
        echo "❌ $table — ERROR: " . $e->getMessage() . "\n";
    }
}

echo "\n=== All checks done. Fix any ❌ errors above, then restore original dashboard.php ===";
echo "</pre>";