<?php
$title = 'Homepage Blocks CMS';
?>

<div class="container-fluid py-4">

    <div class="d-flex justify-content-between align-items-center mb-4">

        <div>

            <h2 class="fw-bold mb-1">
                Homepage Blocks CMS
            </h2>

            <div class="text-muted">
                Dynamic enterprise homepage brochure blocks
            </div>

        </div>

        <div class="d-flex gap-2">

            <a
                href="/easychair/conference/<?= urlencode(
                    $conference['slug']
                    ?? $_SESSION['conference_slug']
                    ?? ''
                ) ?>"
                target="_blank"
                class="btn btn-dark rounded-pill"
            >

                <i class="fa fa-eye"></i>
                Preview Homepage

            </a>

            <a
                href="/easychair/admin/conference-home/blocks/create"
                class="btn btn-primary rounded-pill"
            >

                <i class="fa fa-plus"></i>
                Add Block

            </a>

        </div>

    </div>

    <div class="card border-0 shadow-lg rounded-4">

        <div class="card-body p-0">

            <div class="table-responsive">

                <table class="table align-middle mb-0">

                    <thead class="table-light">

                        <tr>

                            <th>ID</th>

                            <th>Block</th>

                            <th>Type</th>

                            <th>Sort</th>

                            <th>Status</th>

                            <th width="320">
                                Actions
                            </th>

                        </tr>

                    </thead>

                    <tbody>

                    <?php foreach (($blocks ?? []) as $b): ?>

                        <?php

                        $type =
                            $b['block_type']
                            ?? 'panel';

                        $badge = [

                            'hero_slide' => 'danger',
                            'panel' => 'primary',
                            'important_date' => 'warning',
                            'travel' => 'success',
                            'media' => 'dark',
                            'participation' => 'info',
                            'analytics' => 'secondary',
                            'ai' => 'purple',
                            'networking' => 'primary',
                            'virtual' => 'dark',
                            'operations' => 'success',
                            'proceedings' => 'info',
                            'feature' => 'warning',
                            'cta' => 'danger'

                        ];

                        ?>

                        <tr
                            data-id="<?= (int)$b['id'] ?>"
                            data-sort-order="<?= (int)$b['sort_order'] ?>"
                        >

                            <td>

                                <span
                                    class="drag-handle me-2"
                                    style="cursor:grab"
                                >

                                    <i class="fa fa-grip-vertical"></i>

                                </span>

                                <?= (int)$b['id'] ?>

                            </td>

                            <td>

                                <div class="fw-bold">

                                    <?= htmlspecialchars(
                                        $b['title_en']
                                        ?? '-'
                                    ) ?>

                                </div>

                                <div class="small text-muted">

                                    <?= htmlspecialchars(
                                        $b['title_ar']
                                        ?? '-'
                                    ) ?>

                                </div>

                                <div class="mt-2">

                                    <span class="badge bg-dark">

                                        <?= htmlspecialchars(
                                            $b['block_key']
                                            ?? '-'
                                        ) ?>

                                    </span>

                                </div>

                            </td>

                            <td>

                                <span
                                    class="badge bg-<?= htmlspecialchars(
                                        $badge[$type]
                                        ?? 'secondary'
                                    ) ?>"
                                >

                                    <?= htmlspecialchars($type) ?>

                                </span>

                            </td>

                            <td>

                                <span class="badge bg-dark">

                                    <?= (int)$b['sort_order'] ?>

                                </span>

                            </td>

                            <td>

                                <?php if (!empty($b['is_active'])): ?>

                                    <span class="badge bg-success">
                                        Active
                                    </span>

                                <?php else: ?>

                                    <span class="badge bg-danger">
                                        Disabled
                                    </span>

                                <?php endif; ?>

                            </td>

                            <td>

                                <div class="d-flex gap-2 flex-wrap">

                                    <a
href="/easychair/admin/conference-home/blocks/up?id=<?= (int)$b['id'] ?>"
class="btn btn-sm btn-light rounded-pill">

                                        <i class="fa fa-arrow-up"></i>

                                    </a>

                                    <a
href="/easychair/admin/conference-home/blocks/down?id=<?= (int)$b['id'] ?>"
class="btn btn-sm btn-light rounded-pill">

                                        <i class="fa fa-arrow-down"></i>

                                    </a>

                                    <a
href="/easychair/admin/conference-home/blocks/edit?id=<?= (int)$b['id'] ?>"
class="btn btn-sm btn-warning rounded-pill">

                                        <i class="fa fa-edit"></i>
                                        Edit

                                    </a>

                                    <a
href="/easychair/admin/conference-home/blocks/toggle?id=<?= (int)$b['id'] ?>"
class="btn btn-sm btn-secondary rounded-pill">

                                        <i class="fa fa-power-off"></i>

                                        <?= !empty($b['is_active'])
                                            ? 'Disable'
                                            : 'Enable' ?>

                                    </a>

                                    <a
href="/easychair/admin/conference-home/blocks/delete?id=<?= (int)$b['id'] ?>"
class="btn btn-sm btn-danger rounded-pill"
onclick="return confirm('Delete block permanently?')">

                                        <i class="fa fa-trash"></i>

                                    </a>

                                </div>

                            </td>

                        </tr>

                    <?php endforeach; ?>

                    </tbody>

                </table>

            </div>

        </div>

    </div>

</div>
