<!DOCTYPE html>
<html lang="<?= app_locale() ?>" dir="ltr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="robots" content="noindex, nofollow">
    <title><?= esc($title ?? 'Admin') ?> — <?= esc(model('App\Models\SettingsModel')->getSetting('app_name','QR Profile CMS')) ?></title>

    <!-- Bootstrap 5 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css">
    <!-- Bootstrap Icons -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
    <!-- Google Fonts — Noto Sans for Unicode/Indian language support -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Sans:wght@300;400;500;600;700&family=Noto+Sans+Devanagari:wght@300;400;500;600;700&family=Noto+Sans+Tamil:wght@300;400;500;600;700&display=swap">
    <!-- SweetAlert2 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.min.css">
    <!-- Custom Admin CSS -->
    <link rel="stylesheet" href="<?= base_url('assets/css/admin.css') ?>">
    <!-- CSRF meta tag for AJAX -->
    <meta name="csrf-token" content="<?= csrf_hash() ?>">
    <meta name="csrf-name"  content="<?= csrf_token() ?>">
</head>
<body class="admin-body">

<!-- ===== SIDEBAR ===== -->
<nav id="sidebar" class="sidebar d-flex flex-column">
    <div class="sidebar-header px-3 py-3 d-flex align-items-center">
        <i class="bi bi-qr-code-scan fs-4 me-2 text-primary"></i>
        <span class="fw-bold fs-6 text-truncate"><?= esc(model('App\Models\SettingsModel')->getSetting('app_name','QR Profile CMS')) ?></span>
        <button class="btn btn-sm ms-auto d-lg-none" id="sidebarCloseBtn">
            <i class="bi bi-x-lg"></i>
        </button>
    </div>

    <div class="sidebar-user px-3 py-2 border-bottom">
        <div class="d-flex align-items-center gap-2">
            <div class="avatar-sm bg-primary text-white rounded-circle d-flex align-items-center justify-content-center">
                <?= mb_strtoupper(mb_substr(session()->get('admin_name') ?? 'A', 0, 1, 'UTF-8'), 'UTF-8') ?>
            </div>
            <div class="overflow-hidden">
                <div class="fw-semibold text-truncate small"><?= esc(session()->get('admin_name')) ?></div>
                <div class="text-muted" style="font-size:0.7rem"><?= esc(session()->get('admin_role') === 'super_admin' ? 'Super Admin' : 'Admin') ?></div>
            </div>
        </div>
    </div>

    <ul class="sidebar-nav nav flex-column px-2 pt-2 flex-grow-1">
        <li class="nav-item">
            <a href="<?= site_url('admin/dashboard') ?>" class="nav-link <?= active_nav('dashboard') ?>">
                <i class="bi bi-speedometer2 me-2"></i><?= lang('Admin.dashboard') ?>
            </a>
        </li>
        <li class="nav-item">
            <a href="<?= site_url('admin/users') ?>" class="nav-link <?= active_nav('admin/users') ?>">
                <i class="bi bi-person-lines-fill me-2"></i><?= lang('Admin.users') ?>
            </a>
        </li>
        <li class="nav-item">
            <a href="<?= site_url('admin/activity-logs') ?>" class="nav-link <?= active_nav('activity-logs') ?>">
                <i class="bi bi-journal-text me-2"></i><?= lang('Admin.activity_logs') ?>
            </a>
        </li>
        <?php if (session()->get('admin_role') === 'super_admin'): ?>
        <li class="nav-item">
            <a href="<?= site_url('admin/settings') ?>" class="nav-link <?= active_nav('settings') ?>">
                <i class="bi bi-gear me-2"></i><?= lang('Admin.settings') ?>
            </a>
        </li>
        <?php endif; ?>
    </ul>

    <!-- Language Switcher -->
    <!--<div class="px-3 py-2 border-top mt-auto">
        <small class="text-muted d-block mb-1"><?= lang('Admin.language') ?></small>
        <div class="d-flex gap-1 flex-wrap">
            <a href="<?= site_url('admin/lang/en') ?>" class="btn btn-xs btn-outline-secondary <?= app_locale()==='en' ? 'active' : '' ?>">EN</a>
            <a href="<?= site_url('admin/lang/hi') ?>" class="btn btn-xs btn-outline-secondary <?= app_locale()==='hi' ? 'active' : '' ?>">हि</a>
            <a href="<?= site_url('admin/lang/mr') ?>" class="btn btn-xs btn-outline-secondary <?= app_locale()==='mr' ? 'active' : '' ?>">म</a>
        </div>
    </div>-->

    <div class="px-3 py-2 border-top">
        <a href="<?= site_url('logout') ?>" class="btn btn-outline-danger btn-sm w-100">
            <i class="bi bi-box-arrow-right me-1"></i><?= lang('Admin.logout') ?>
        </a>
    </div>
</nav>

<!-- ===== MAIN CONTENT ===== -->
<div class="main-wrapper" id="mainWrapper">

    <!-- Top Navbar -->
    <header class="top-navbar d-flex align-items-center px-3 gap-3">
        <button class="btn btn-sm btn-outline-secondary" id="sidebarToggle" title="Toggle Sidebar">
            <i class="bi bi-list fs-5"></i>
        </button>

        <nav aria-label="breadcrumb" class="d-none d-md-block">
            <ol class="breadcrumb mb-0 small">
                <li class="breadcrumb-item"><a href="<?= site_url('admin/dashboard') ?>"><?= lang('Admin.dashboard') ?></a></li>
                <?php if (isset($breadcrumb)): foreach ($breadcrumb as $bc): ?>
                <li class="breadcrumb-item <?= $bc['active'] ? 'active' : '' ?>">
                    <?= $bc['active'] ? esc($bc['label']) : '<a href="'.esc($bc['url']).'">'.esc($bc['label']).'</a>' ?>
                </li>
                <?php endforeach; endif; ?>
            </ol>
        </nav>

        <div class="ms-auto d-flex align-items-center gap-2">
            <span class="badge bg-light text-dark border small">
                <i class="bi bi-person me-1"></i><?= esc(session()->get('admin_name')) ?>
            </span>
        </div>
    </header>

    <!-- Flash Messages -->
    <div class="px-4 pt-3">
        <?php if (session()->getFlashdata('success')): ?>
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            <i class="bi bi-check-circle me-2"></i><?= esc(session()->getFlashdata('success')) ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
        <?php endif; ?>
        <?php if (session()->getFlashdata('error')): ?>
        <div class="alert alert-danger alert-dismissible fade show" role="alert">
            <i class="bi bi-exclamation-triangle me-2"></i><?= esc(session()->getFlashdata('error')) ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
        <?php endif; ?>
        <?php if ($errors = session()->getFlashdata('errors')): ?>
        <div class="alert alert-warning alert-dismissible fade show" role="alert">
            <i class="bi bi-exclamation-circle me-2"></i><strong>Please fix these errors:</strong>
            <ul class="mb-0 mt-1">
                <?php foreach ((array)$errors as $err): ?>
                <li><?= esc($err) ?></li>
                <?php endforeach; ?>
            </ul>
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
        <?php endif; ?>
    </div>

    <!-- Page Content -->
    <main class="content-area px-4 pb-4">
        <?= $this->renderSection('content') ?>
    </main>

    <footer class="text-center text-muted py-3 border-top small">
        &copy; <?= date('Y') ?> <?= esc(model('App\Models\SettingsModel')->getSetting('app_name','QR Profile CMS')) ?> &mdash; Admin Panel
    </footer>
</div><!-- /main-wrapper -->

<!-- ===== SCRIPTS ===== -->
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.all.min.js"></script>
<script src="<?= base_url('assets/js/admin.js') ?>"></script>

<?= $this->renderSection('scripts') ?>
</body>
</html>
