<?php
use Core\App\Models\HomeSections;
$sectionsModel = new HomeSections();
$sections = $sectionsModel->getAll();
$sectionLabels=[
    'top_bar'=>__('homepage.section.top_bar','Top Bar'),
    'hero_slider'=>__('homepage.section.hero','Hero / Slider'),
    'products_section'=>__('homepage.section.products','Products Section'),
    'ads_section'=>__('homepage.section.ads','Ads Section'),
    'faq_section'=>__('homepage.section.faq','FAQ Section'),
];
include __DIR__ . '/../header.php';
?>

<div class="content-wrapper">
<section class="content pt-3">
<div class="container-fluid pb-5">

<div class="d-flex flex-wrap justify-content-between align-items-start gap-3 mb-4">
    <div>
        <h3 class="mb-1 fw-bold"><i class="bi bi-layout-text-window-reverse me-2 text-primary"></i><?= __('homepage.settings','Homepage Settings') ?></h3>
        <p class="text-muted mb-0 small"><?= __('homepage.subtitle','Manage sections displayed on the homepage') ?></p>
    </div>
    <a href="/admin/home-sections/create" class="btn btn-primary">
        <i class="bi bi-plus-lg me-1"></i><?= __('homepage.add_section','Add Section') ?>
    </a>
</div>

<div class="card border-0 shadow-sm">
    <div class="card-header bg-white border-bottom d-flex justify-content-between align-items-center py-3">
        <h6 class="mb-0 fw-semibold"><i class="bi bi-layers me-2 text-muted"></i><?= __('homepage.sections_list','Sections') ?></h6>
        <span class="badge bg-secondary rounded-pill"><?= count($sections) ?></span>
    </div>
    <div class="card-body p-0">
        <?php if(empty($sections)): ?>
        <div class="text-center py-5 text-muted">
            <i class="bi bi-layout-text-window display-4 d-block mb-2 opacity-50"></i>
            <p class="mb-3"><?= __('homepage.no_sections','No sections added yet.') ?></p>
            <a href="/admin/home-sections/create" class="btn btn-primary btn-sm"><i class="bi bi-plus-lg me-1"></i><?= __('homepage.add_section','Add Section') ?></a>
        </div>
        <?php else: ?>
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead class="table-light">
                    <tr>
                        <th class="ps-3"><?= __('homepage.section_type','Section Type') ?></th>
                        <th><?= __('homepage.section_order','Order') ?></th>
                        <th><?= __('homepage.status','Status') ?></th>
                        <th class="text-end pe-3"><?= __('actions.actions','Actions') ?></th>
                    </tr>
                </thead>
                <tbody>
                <?php foreach($sections as $i=>$section): ?>
                <tr>
                    <td class="ps-3 fw-semibold">
                        <i class="bi bi-layout-sidebar me-2 text-muted"></i>
                        <?= htmlspecialchars($sectionLabels[$section['section_type']]??$section['section_type']) ?>
                    </td>
                    <td><span class="badge bg-light text-dark border"><?= $i+1 ?></span></td>
                    <td>
                        <?php if((int)$section['is_enabled']===1): ?>
                            <span class="badge bg-success-subtle text-success border border-success-subtle">
                                <i class="bi bi-check-circle me-1"></i><?= __('common.enabled','Enabled') ?>
                            </span>
                        <?php else: ?>
                            <span class="badge bg-secondary-subtle text-secondary border border-secondary-subtle">
                                <i class="bi bi-pause-circle me-1"></i><?= __('common.disabled','Disabled') ?>
                            </span>
                        <?php endif; ?>
                    </td>
                    <td class="text-end pe-3">
                        <a href="/admin/home-sections/edit/<?= (int)$section['id'] ?>" class="btn btn-sm btn-outline-primary"><i class="bi bi-pencil"></i></a>
                        <form method="post" action="/admin/home-sections/delete/<?= (int)$section['id'] ?>" class="d-inline" onsubmit="return confirm('<?= __('common.confirm_delete','Are you sure?') ?>')">
                            <?= \Core\System\Csrf::field() ?>
                            <button type="submit" class="btn btn-sm btn-outline-danger"><i class="bi bi-trash3"></i></button>
                        </form>
                    </td>
                </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php endif; ?>
    </div>
</div>

</div></section></div>
<?php include __DIR__ . '/../footer.php'; ?>