<?php
/**
 * support_ajax.php - معالجة طلبات AJAX للدعم الفني
 */

session_start();
require_once 'config.php';

if (!isset($_SESSION['admin_id'])) {
    http_response_code(403);
    echo json_encode(['success' => false, 'error' => 'Unauthorized']);
    exit;
}

$action = $_GET['action'] ?? '';

if ($action == 'get_messages') {
    $ticket_id = (int)($_GET['ticket_id'] ?? 0);
    
    if ($ticket_id > 0) {
        $messages = $conn->query("
            SELECT *, 
                   sender_type as sender,
                   DATE_FORMAT(created_at, '%H:%i') as time,
                   image
            FROM support_messages 
            WHERE ticket_id = $ticket_id 
            ORDER BY created_at ASC
        ");
        
        $data = [];
        while($msg = $messages->fetch_assoc()) {
            $data[] = $msg;
        }
        
        echo json_encode(['success' => true, 'messages' => $data]);
        exit;
    }
}

echo json_encode(['success' => false, 'error' => 'Invalid action']);