<?php
/**
 * Layout back-office (Tabler).
 * @var string $content
 * @var string $pageTitle
 */

use App\Core\Auth;
use App\Core\Csrf;
use App\Core\Settings;

$siteTitle = (string) Settings::get('site_title', 'DANIEL TIZIANI');
$lang      = (string) Settings::get('default_lang', 'fr');
$reqPath   = parse_url((string) ($_SERVER['REQUEST_URI'] ?? '/'), PHP_URL_PATH) ?: '/';

$nav = static function (string $href, bool $exact = false) use ($reqPath): string {
    $path   = '/' . trim($href, '/');
    $active = $exact ? ($reqPath === $path) : str_starts_with($reqPath, $path);
    return $active ? ' active' : '';
};
?><!doctype html>
<html lang="<?= e($lang) ?>">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
    <meta name="robots" content="noindex,nofollow">
    <meta name="csrf-token" content="<?= e(Csrf::token()) ?>">
    <title><?= e($pageTitle ?? 'Administration') ?> — <?= e($siteTitle) ?></title>
    <link rel="stylesheet" href="<?= e(asset('vendor/tabler/tabler.min.css')) ?>">
    <link rel="stylesheet" href="<?= e(asset('vendor/tabler/tabler-icons.min.css')) ?>">
    <link rel="stylesheet" href="<?= e(asset('css/admin.css')) ?>">
</head>
<body class="antialiased">
<div class="page">
    <header class="navbar navbar-expand-md sticky-top admin-navbar d-print-none" data-bs-theme="dark">
        <div class="container-xl">
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbar-menu" aria-label="Menu">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="navbar-brand pe-0 pe-md-3">
                <a href="<?= e(base_url('admin')) ?>" class="text-white text-decoration-none fw-bold">
                    <i class="ti ti-photo me-1"></i>CMS &middot; <?= e($siteTitle) ?>
                </a>
            </div>
            <div class="navbar-nav flex-row order-md-last align-items-center">
                <a href="<?= e(base_url('admin/account')) ?>" class="d-none d-md-flex align-items-center text-white-50 me-3 small text-decoration-none" title="Mon compte">
                    <i class="ti ti-user me-1"></i><?= e(Auth::name() !== '' ? Auth::name() : 'Admin') ?>
                </a>
                <form method="post" action="<?= e(base_url('admin/logout')) ?>" class="m-0">
                    <?= Csrf::field() ?>
                    <button type="submit" class="btn btn-sm btn-outline-light"><i class="ti ti-logout me-1"></i>Déconnexion</button>
                </form>
            </div>
            <div class="collapse navbar-collapse" id="navbar-menu">
                <div class="navbar-nav">
                    <div class="nav-item"><a class="nav-link<?= $nav('admin', true) ?>" href="<?= e(base_url('admin')) ?>"><span class="nav-link-icon"><i class="ti ti-layout-dashboard"></i></span><span class="nav-link-title">Tableau de bord</span></a></div>
                    <div class="nav-item"><a class="nav-link<?= $nav('admin/galleries') ?>" href="<?= e(base_url('admin/galleries')) ?>"><span class="nav-link-icon"><i class="ti ti-photo"></i></span><span class="nav-link-title">Galeries</span></a></div>
                    <div class="nav-item"><a class="nav-link<?= $nav('admin/pages') ?>" href="<?= e(base_url('admin/pages')) ?>"><span class="nav-link-icon"><i class="ti ti-file-text"></i></span><span class="nav-link-title">Pages</span></a></div>
                    <div class="nav-item"><a class="nav-link<?= $nav('admin/redirects') ?>" href="<?= e(base_url('admin/redirects')) ?>"><span class="nav-link-icon"><i class="ti ti-arrow-guide"></i></span><span class="nav-link-title">Redirections</span></a></div>
                    <div class="nav-item"><a class="nav-link<?= $nav('admin/settings') ?>" href="<?= e(base_url('admin/settings')) ?>"><span class="nav-link-icon"><i class="ti ti-settings"></i></span><span class="nav-link-title">Paramètres</span></a></div>
                </div>
            </div>
        </div>
    </header>

    <div class="page-wrapper">
        <div class="page-body">
            <div class="container-xl">
                <?= $content ?>
            </div>
        </div>
    </div>
</div>

<!-- Modale de suppression réutilisable -->
<div class="modal modal-blur fade" id="deleteModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-sm modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h3 class="modal-title"><i class="ti ti-alert-triangle text-danger me-2"></i>Confirmer la suppression</h3>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Fermer"></button>
            </div>
            <div class="modal-body text-center py-4">
                <p class="mb-2 text-secondary">Voulez-vous vraiment supprimer&nbsp;:</p>
                <strong id="deleteItemLabel" class="d-block mb-2 fs-3"></strong>
                <span class="text-danger">Cette action est définitive.</span>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-link link-secondary" data-bs-dismiss="modal">Annuler</button>
                <form method="post" id="deleteForm" class="m-0">
                    <?= Csrf::field() ?>
                    <button type="submit" class="btn btn-danger"><i class="ti ti-trash me-1"></i>Oui, supprimer</button>
                </form>
            </div>
        </div>
    </div>
</div>

<?php if (!empty($flash)): ?>
<div class="toast-container position-fixed bottom-0 end-0 p-3" style="z-index: 1090;">
    <div id="flashToast" class="toast align-items-center text-white bg-success border-0" role="alert" aria-live="assertive" aria-atomic="true">
        <div class="d-flex">
            <div class="toast-body"><i class="ti ti-circle-check me-1"></i><?= e($flash) ?></div>
            <button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Fermer"></button>
        </div>
    </div>
</div>
<?php endif; ?>

<script src="<?= e(asset('vendor/tabler/tabler.min.js')) ?>"></script>
<script src="<?= e(asset('vendor/sortable/Sortable.min.js')) ?>"></script>
<script src="<?= e(asset('js/admin.js')) ?>"></script>
</body>
</html>
