<?php
session_start();

header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET');
header('Access-Control-Allow-Headers: Content-Type');

error_reporting(0); ini_set('display_errors', 0);

require_once '../includes/db_config.php';

if (!isset($_SESSION['user_id']) || !isset($_SESSION['user_role']) || $_SESSION['user_role'] !== 'admin'){
    echo json_encode(['success'=>false,'error'=>'Unauthorized access']);
    exit;
}

try {
    $db = getDBConnection();

    $q1 = "SELECT COUNT(*) as total FROM delivery_staff";
    $st1 = $db->prepare($q1); $st1->execute();
    $r1 = $st1->fetch();
    $tot = $r1 ? (int)$r1['total'] : 0;

    $q2 = "SELECT COUNT(*) as available FROM delivery_staff WHERE status = 'available'";
    $st2 = $db->prepare($q2); $st2->execute();
    $r2 = $st2->fetch();
    $avail = $r2 ? (int)$r2['available'] : 0;

    echo json_encode(['success'=>true,'total_staff'=>$tot,'available_staff'=>$avail]);

} catch (PDOException $e){
    echo json_encode(['success'=>false,'error'=>'Database error: '.$e->getMessage()]);
} catch (Exception $e){
    echo json_encode(['success'=>false,'error'=>'Error: '.$e->getMessage()]);
}
?>

