<?php
// Landing page ID and Name fetch logic (add this at the top of your PHP file, before the HTML)
$landing_page_id = isset($_GET['id']) ? intval($_GET['id']) : (isset($_POST['landing_page_id']) ? intval($_POST['landing_page_id']) : null);
$landing_page_name = 'Untitled'; // Default

if ($landing_page_id && function_exists('get_landing_page_name')) { // Assuming you have a function to fetch name from DB
    $landing_page_name = get_landing_page_name($landing_page_id); // Replace with your actual DB query function
} elseif (!$landing_page_id) {
    $landing_page_id = 1; // Or redirect to list page if no ID: header('Location: landing_pages.php'); exit;
}

// If name is still default or empty, set a fallback
if (empty($landing_page_name) || $landing_page_name === 'Untitled') {
    $landing_page_name = 'Untitled Landing Page';
}
?>

<!-- SECTIONS ORDER (Drag & Drop) with Active/Inactive Toggles -->
<div class="form-card">
    <h3>Sections Order for Landing Page (ID: <?php echo htmlspecialchars($landing_page_id ?? 'N/A'); ?>) - <?php echo htmlspecialchars($landing_page_name ?? 'Untitled'); ?></h3>
    <p class="text-sm text-gray-600 mb-2">Landing Page ID: <?php echo htmlspecialchars($landing_page_id ?? 'N/A'); ?> | Name: <?php echo htmlspecialchars($landing_page_name ?? 'Untitled'); ?></p>
    <p class="text-sm text-gray-600 mb-2">Drag sections below to change their order on the landing page. Check/uncheck to activate/deactivate sections.</p>
    <ul id="sectionsOrder" class="sortable-section">
        <?php foreach ($sections_order as $section_key): ?>
            <li data-section="<?php echo htmlspecialchars($section_key); ?>">
                <span class="handle">⋮⋮</span>
                <label class="flex items-center gap-2 cursor-pointer flex-1">
                    <input type="checkbox" name="section_active[<?php echo htmlspecialchars($section_key); ?>]" <?php echo ($sections_status[$section_key] ? 'checked' : ''); ?> >
                    <span class="text-gray-800"><?php echo ucwords(str_replace('_', ' ', $section_key)); ?></span>
                </label>
                <input type="hidden" name="sections_order[]" value="<?php echo htmlspecialchars($section_key); ?>">
            </li>
        <?php endforeach; ?>
    </ul>
</div>