<?php
/**
 * get_filter_options.php - جلب خيارات التصفية الذكية
 * @version 6.1.0
 */

require_once 'config.php';

// تعيين header JSON
header('Content-Type: application/json');

// التحقق من وجود نوع التصفية
$type = isset($_GET['type']) ? $_GET['type'] : '';

$data = [];
$response = ['success' => false, 'data' => [], 'message' => ''];

try {
    switch($type) {
        case 'captain':
            $result = $conn->query("SELECT id, name, phone FROM captains WHERE status = 'active' ORDER BY name");
            if ($result) {
                while($row = $result->fetch_assoc()) {
                    $data[] = [
                        'id' => $row['id'],
                        'name' => $row['name'],
                        'phone' => $row['phone'],
                        'display' => $row['name'] . ' - ' . $row['phone']
                    ];
                }
            }
            break;
            
        case 'customer':
            $result = $conn->query("SELECT id, name, phone FROM customers ORDER BY name LIMIT 100");
            if ($result) {
                while($row = $result->fetch_assoc()) {
                    $data[] = [
                        'id' => $row['id'],
                        'name' => $row['name'],
                        'phone' => $row['phone'],
                        'display' => $row['name'] . ' - ' . $row['phone']
                    ];
                }
            }
            break;
            
        case 'meal':
            $result = $conn->query("
                SELECT m.id, m.name, COALESCE(c.name, 'غير مصنف') as category 
                FROM meals m 
                LEFT JOIN categories c ON m.category_id = c.id 
                WHERE m.is_active = 1 
                ORDER BY m.name
            ");
            if ($result) {
                while($row = $result->fetch_assoc()) {
                    $data[] = [
                        'id' => $row['id'],
                        'name' => $row['name'],
                        'category' => $row['category'],
                        'display' => $row['name'] . ' (' . $row['category'] . ')'
                    ];
                }
            }
            break;
            
        case 'category':
            $result = $conn->query("SELECT id, name FROM categories ORDER BY name");
            if ($result) {
                while($row = $result->fetch_assoc()) {
                    $data[] = [
                        'id' => $row['id'],
                        'name' => $row['name'],
                        'display' => $row['name']
                    ];
                }
            }
            break;
            
        default:
            $response['message'] = 'نوع غير مدعوم';
            echo json_encode($response);
            exit;
    }
    
    $response['success'] = true;
    $response['data'] = $data;
    $response['message'] = 'تم تحميل البيانات بنجاح';
    
} catch (Exception $e) {
    $response['message'] = 'خطأ في قاعدة البيانات: ' . $e->getMessage();
}

echo json_encode($response);
exit;