<?php
/**
 * جلب طلبات المشرفين
 * api/admin/get_supervisors_requests.php
 */

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

if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    http_response_code(200);
    exit();
}

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

validateAdminToken();

$status = $_GET['status'] ?? 'all';

try {
    $database = new Database();
    $db = $database->getConnection();
    
    $sql = "SELECT * FROM supervisors_requests";
    if ($status !== 'all') {
        $sql .= " WHERE status = :status";
        $stmt = $db->prepare($sql);
        $stmt->execute([':status' => $status]);
    } else {
        $stmt = $db->query($sql);
    }
    $requests = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo json_encode([
        'success' => true,
        'data' => $requests
    ]);
    
} catch (PDOException $e) {
    http_response_code(500);
    echo json_encode(['error' => 'خطأ في قاعدة البيانات: ' . $e->getMessage()]);
}
?>