<?php
/**
 * SmartThink3 LMS - Site Content Management
 *
 * Admin interface to manage CMS-controlled content blocks for website pages.
 */

require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../classes/SiteContent.php';

// Ensure only admins can access
requireLogin();
requireAnyRole(['admin', 'super_admin']);

$siteContent = new SiteContent();
$db = Database::getInstance();

// Handle delete action
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_id'])) {
    $id = (int) $_POST['delete_id'];
    if ($id > 0) {
        $siteContent->deleteBlock($id);
        $_SESSION['flash']['success'] = 'Content block deleted successfully.';
        header('Location: site_content.php');
        exit;
    }
}

$pageFilter = isset($_GET['page']) ? trim($_GET['page']) : null;
$blocks = $siteContent->getBlocks($pageFilter ?: null);

$pageTitle = 'Site Content';
include __DIR__ . '/includes/header.php';
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="h4 mb-0">Site Content</h1>
        <p class="text-muted mb-0">Manage text and content blocks for your public website pages.</p>
    </div>
    <div>
        <a href="site_content_edit.php" class="btn btn-primary">
            <i class="fas fa-plus me-2"></i>Add Content Block
        </a>
    </div>
</div>

<div class="card">
    <div class="card-header d-flex justify-content-between align-items-center">
        <div>Content Blocks</div>
        <form method="get" class="d-flex align-items-center gap-2">
            <label for="page" class="mb-0 small text-muted">Filter by page:</label>
            <input type="text" name="page" id="page" class="form-control form-control-sm" placeholder="e.g. home, about" value="<?php echo htmlspecialchars($pageFilter ?? ''); ?>">
            <button type="submit" class="btn btn-sm btn-outline-secondary">Filter</button>
        </form>
    </div>
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-striped table-hover mb-0 datatable">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Page</th>
                        <th>Key</th>
                        <th>Label</th>
                        <th>Region</th>
                        <th>Updated At</th>
                        <th>Updated By</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($blocks)): ?>
                        <tr>
                            <td colspan="8" class="text-center text-muted py-4">No content blocks found. Click "Add Content Block" to create one.</td>
                        </tr>
                    <?php else: ?>
                        <?php foreach ($blocks as $block): ?>
                            <tr>
                                <td><?php echo (int) $block['id']; ?></td>
                                <td><code><?php echo htmlspecialchars($block['page_slug']); ?></code></td>
                                <td><code><?php echo htmlspecialchars($block['block_key']); ?></code></td>
                                <td><?php echo htmlspecialchars($block['label'] ?? ''); ?></td>
                                <td><?php echo htmlspecialchars($block['region']); ?></td>
                                <td><?php echo htmlspecialchars($block['updated_at']); ?></td>
                                <td><?php echo $block['updated_by'] ? (int) $block['updated_by'] : '-'; ?></td>
                                <td class="text-nowrap">
                                    <a href="site_content_edit.php?id=<?php echo (int) $block['id']; ?>" class="btn btn-sm btn-outline-primary me-1">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    <form method="post" action="site_content.php" class="d-inline">
                                        <input type="hidden" name="delete_id" value="<?php echo (int) $block['id']; ?>">
                                        <button type="submit" class="btn btn-sm btn-outline-danger" data-confirm="Are you sure you want to delete this content block? This cannot be undone.">
                                            <i class="fas fa-trash"></i>
                                        </button>
                                    </form>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php include __DIR__ . '/includes/footer.php'; ?>
