<?php
/**
 * Admin/Staff/pending.php
 * جلب الحسابات المعلقة للإداريين والخدمة
 */
require_once __DIR__ . '/../../core/bootstrap.php';
require_once __DIR__ . '/../../functions.php';

try {
    $con = Database::get('main');
    
    // جلب الإداريين المعلقين
    $stmt1 = $con->query("SELECT id, name, phone, role, created_at, 'admin' as type FROM adminUser WHERE status = 'pending'");
    $admins = $stmt1->fetchAll(PDO::FETCH_ASSOC);

    // فك التشفير للأسماء والأرقام للإداريين
    foreach ($admins as &$admin) {
        $admin['name'] = $encryptionHelper->decryptData($admin['name']) ?: $admin['name'];
        $admin['phone'] = $encryptionHelper->decryptData($admin['phone']) ?: $admin['phone'];
    }

    // جلب موظفي الخدمة المعلقين
    $stmt2 = $con->query("SELECT id, first_name, last_name, phone, user_type as role, created_at, 'service' as type FROM users WHERE status = 'pending' AND user_type = 'service'");
    $services = $stmt2->fetchAll(PDO::FETCH_ASSOC);

    // فك التشفير لموظفي الخدمة
    foreach ($services as &$service) {
        $service['name'] = trim(($encryptionHelper->decryptData($service['first_name']) ?: $service['first_name']) . ' ' . ($encryptionHelper->decryptData($service['last_name']) ?: $service['last_name']));
        $service['phone'] = $encryptionHelper->decryptData($service['phone']) ?: $service['phone'];
    }

    $allPending = array_merge($admins, $services);

    printSuccess([
        "data" => $allPending
    ]);

} catch (Exception $e) {
    error_log("[Staff Pending Error] " . $e->getMessage());
    jsonError("خطأ في السيرفر: " . $e->getMessage());
}
exit();
