<?php
$title = $title ?? app_name();
$currentUser = auth_user();

$breadcrumbs = match ($title) {
    'Produk Admin' => [
        ['label' => 'Dashboard', 'url' => url('/admin/dashboard')],
        ['label' => 'Produk', 'url' => null],
    ],
    'User Admin' => [
        ['label' => 'Dashboard', 'url' => url('/admin/dashboard')],
        ['label' => 'User', 'url' => null],
    ],
    'Pembelian Admin' => [
        ['label' => 'Dashboard', 'url' => url('/admin/dashboard')],
        ['label' => 'Pembelian', 'url' => null],
    ],
    'Pengaturan Admin' => [
        ['label' => 'Dashboard', 'url' => url('/admin/dashboard')],
        ['label' => 'Pengaturan', 'url' => null],
    ],
    default => [
        ['label' => 'Dashboard', 'url' => null],
    ],
};
?>
<!doctype html>
<html lang="id">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="Dashboard admin">
    <title><?= e($title) ?> - <?= e(app_name()) ?></title>
    <link rel="stylesheet" href="<?= e(asset('/assets/app.css')) ?>">
</head>
<body>
<div class="page-shell admin-shell">
    <aside class="admin-sidebar" aria-label="Navigasi admin">
        <div class="admin-sidebar-brand">
            <a class="brand" href="<?= e(url('/admin/dashboard')) ?>">
                <?php if ($logo = company_logo()): ?>
                    <span class="brand-logo" aria-hidden="true">
                        <img src="<?= e(url('/media?path=' . rawurlencode($logo))) ?>" alt="" loading="lazy">
                    </span>
                <?php else: ?>
                    <span class="brand-mark" aria-hidden="true"></span>
                <?php endif; ?>
                <span><?= e(company_name()) ?></span>
            </a>
            <p class="admin-sidebar-note">Kelola produk, user, pembelian, dan pengaturan perusahaan.</p>
        </div>

        <nav class="admin-sidebar-nav">
            <a class="admin-sidebar-link <?= e(($title ?? '') === 'Dashboard Admin' ? 'admin-sidebar-link-active' : '') ?>" href="<?= e(url('/admin/dashboard')) ?>">Dashboard</a>
            <a class="admin-sidebar-link <?= e(($title ?? '') === 'Produk Admin' ? 'admin-sidebar-link-active' : '') ?>" href="<?= e(url('/admin/products')) ?>">Produk</a>
            <a class="admin-sidebar-link <?= e(($title ?? '') === 'User Admin' ? 'admin-sidebar-link-active' : '') ?>" href="<?= e(url('/admin/users')) ?>">User</a>
            <a class="admin-sidebar-link <?= e(($title ?? '') === 'Pembelian Admin' ? 'admin-sidebar-link-active' : '') ?>" href="<?= e(url('/admin/purchases')) ?>">Pembelian</a>
            <a class="admin-sidebar-link <?= e(($title ?? '') === 'Pengaturan Admin' ? 'admin-sidebar-link-active' : '') ?>" href="<?= e(url('/admin/settings')) ?>">Pengaturan</a>
        </nav>

        <div class="admin-sidebar-footer">
            <div class="admin-user-card">
                <span class="admin-user-label">Masuk sebagai</span>
                <strong><?= e($currentUser['nama'] ?? 'Admin') ?></strong>
                <span><?= e($currentUser['role'] ?? 'admin') ?></span>
            </div>
            <form class="logout-form" action="<?= e(url('/logout')) ?>" method="post">
                <?= csrf() ?>
                <button class="button-secondary admin-logout-button" type="submit">Logout</button>
            </form>
        </div>
    </aside>

    <main class="admin-main">
        <header class="admin-page-header">
            <div>
                <span class="admin-page-kicker">Panel Admin</span>
                <nav class="admin-breadcrumbs" aria-label="Breadcrumb">
                    <?php foreach ($breadcrumbs as $index => $crumb): ?>
                        <?php if (($crumb['url'] ?? null) !== null): ?>
                            <a class="admin-breadcrumb" href="<?= e($crumb['url']) ?>"><?= e($crumb['label']) ?></a>
                        <?php else: ?>
                            <span class="admin-breadcrumb-current"><?= e($crumb['label']) ?></span>
                        <?php endif; ?>
                        <?php if ($index < count($breadcrumbs) - 1): ?>
                            <span class="admin-breadcrumb-separator" aria-hidden="true">/</span>
                        <?php endif; ?>
                    <?php endforeach; ?>
                </nav>
                <h1 class="admin-page-title"><?= e($title) ?></h1>
            </div>
            <div class="admin-page-meta">
                <span class="admin-page-meta-label">Masuk sebagai</span>
                <strong><?= e($currentUser['nama'] ?? 'Admin') ?></strong>
            </div>
        </header>

        <?= $content ?>

        <footer class="admin-footer">
            <div>
                <strong><?= e(company_name()) ?></strong>
                <p>Dashboard admin untuk memantau produk, user, pembelian, dan pengaturan perusahaan.</p>
            </div>
            <span class="admin-footer-note">Sidebar layout aktif</span>
        </footer>
    </main>
</div>
</body>
</html>
