<?php
// ajax/get_import_details.php
session_start();
require_once '../layouts/dbconn.php';
require_once '../layouts/session.php';

header('Content-Type: application/json');

// Check permission
if (!hasPermission('manage_patients')) {
    echo json_encode(['success' => false, 'message' => 'Unauthorized']);
    exit();
}

$activityId = isset($_GET['activity_id']) ? intval($_GET['activity_id']) : 0;

if ($activityId === 0) {
    echo json_encode(['success' => false, 'message' => 'Invalid activity ID']);
    exit();
}

$stmt = $conn->prepare("
    SELECT 
        al.activity_id,
        al.user_id,
        al.action,
        al.module,
        al.details,
        al.ip_address,
        al.created_at,
        u.full_name as imported_by
    FROM activity_logs al
    LEFT JOIN users u ON al.user_id = u.user_id
    WHERE al.activity_id = ? AND al.module = 'patients' AND al.action = 'IMPORT'
");

$stmt->bind_param("i", $activityId);
$stmt->execute();
$result = $stmt->get_result();

if ($row = $result->fetch_assoc()) {
    echo json_encode([
        'success' => true,
        'data' => $row
    ]);
} else {
    echo json_encode([
        'success' => false,
        'message' => 'Import details not found'
    ]);
}

$stmt->close();
?>