<?php

/** @var \yii\web\View $this */
/** @var string $content */

use backend\assets\AppAsset;
use yii\bootstrap5\Html;

AppAsset::register($this);

$currentController = Yii::$app->controller->id;
?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <?php $this->registerCsrfMetaTags() ?>
    <title><?= Html::encode($this->title) ?> — SJ Nutrizione</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
    <?php $this->head() ?>
    <style>
        :root { --sidebar-w: 240px; --sidebar-bg: #1a2035; }
        body { background: #f4f6f9; }
        .sn-sidebar { width: var(--sidebar-w); min-height: 100vh; background: var(--sidebar-bg); position: fixed; top: 0; left: 0; z-index: 1030; overflow-y: auto; display: flex; flex-direction: column; }
        .sn-brand { padding: 1rem 1.25rem; border-bottom: 1px solid #2d3a55; }
        .sn-brand .logo { color: #fff; font-size: 1.15rem; font-weight: 700; }
        .sn-brand small { color: #8896ae; font-size: .68rem; }
        .sn-nav { flex: 1; padding: .5rem 0; }
        .sn-section { color: #5a6a85; font-size: .65rem; text-transform: uppercase; letter-spacing: .08em; padding: .8rem 1.25rem .2rem; }
        .sn-nav .nav-link { color: #a0aec0; padding: .5rem .75rem; border-radius: 6px; margin: 1px .625rem; font-size: .875rem; display: flex; align-items: center; gap: 8px; transition: background .15s, color .15s; }
        .sn-nav .nav-link:hover { background: #2d3a55; color: #fff; }
        .sn-nav .nav-link.active { background: #3b4e72; color: #fff; }
        .sn-nav .nav-link i { font-size: 1rem; width: 18px; text-align: center; }
        .sn-main { margin-left: var(--sidebar-w); min-height: 100vh; display: flex; flex-direction: column; }
        .sn-topbar { background: #fff; border-bottom: 1px solid #e2e8f0; padding: .75rem 1.5rem; display: flex; align-items: center; justify-content: space-between; position: sticky; top: 0; z-index: 100; }
        .sn-content { padding: 1.5rem; flex: 1; }
        .card { border: none; box-shadow: 0 1px 3px rgba(0,0,0,.08); border-radius: 10px; }
        .table thead th { background: #f8fafc; font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; color: #64748b; border-bottom: 2px solid #e2e8f0; }
        .badge { font-size: .72em; }
        @media(max-width:768px) { .sn-sidebar{transform:translateX(-100%);} .sn-main{margin-left:0;} }
    </style>
</head>
<body>
<?php $this->beginBody() ?>

<!-- Sidebar -->
<aside class="sn-sidebar">
    <div class="sn-brand">
        <div class="logo"><i class="bi bi-leaf"></i> SJ Nutrizione</div>
        <small>ERP &amp; Nutrition System</small>
    </div>
    <nav class="sn-nav">
        <div class="sn-section">Overview</div>
        <a class="nav-link <?= $currentController === 'dashboard' ? 'active' : '' ?>"
           href="<?= \yii\helpers\Url::to(['/dashboard/index']) ?>">
            <i class="bi bi-speedometer2"></i> Dashboard
        </a>

        <div class="sn-section">CRM</div>
        <a class="nav-link <?= $currentController === 'lead' ? 'active' : '' ?>"
           href="<?= \yii\helpers\Url::to(['/lead/index']) ?>">
            <i class="bi bi-funnel"></i> Leads
        </a>
        <a class="nav-link <?= $currentController === 'customer' ? 'active' : '' ?>"
           href="<?= \yii\helpers\Url::to(['/customer/index']) ?>">
            <i class="bi bi-people"></i> Customers
        </a>
        <a class="nav-link <?= $currentController === 'plan' ? 'active' : '' ?>"
           href="<?= \yii\helpers\Url::to(['/plan/index']) ?>">
            <i class="bi bi-card-list"></i> Plans
        </a>

        <div class="sn-section">Finance</div>
        <a class="nav-link <?= $currentController === 'invoice' ? 'active' : '' ?>"
           href="<?= \yii\helpers\Url::to(['/invoice/index']) ?>">
            <i class="bi bi-receipt"></i> Invoices
        </a>
        <a class="nav-link <?= ($currentController === 'invoice' && \Yii::$app->controller->action->id === 'outstanding') ? 'active' : '' ?>"
           href="<?= \yii\helpers\Url::to(['/invoice/outstanding']) ?>">
            <i class="bi bi-exclamation-circle"></i> Outstanding
        </a>

        <div class="sn-section">Nutrition</div>
        <a class="nav-link <?= $currentController === 'diet-plan' ? 'active' : '' ?>"
           href="<?= \yii\helpers\Url::to(['/diet-plan/index']) ?>">
            <i class="bi bi-clipboard2-pulse"></i> Diet Plans
        </a>
        <a class="nav-link <?= $currentController === 'food-library' ? 'active' : '' ?>"
           href="<?= \yii\helpers\Url::to(['/food-library/index']) ?>">
            <i class="bi bi-basket"></i> Food Library
        </a>
    </nav>
    <div class="pb-3 px-1">
        <div class="sn-section">Account</div>
        <?php if (!Yii::$app->user->isGuest): ?>
        <?php echo Html::beginForm(['/site/logout'], 'post', ['style' => 'margin: 1px .625rem']) ?>
        <?php echo Html::submitButton('<i class="bi bi-box-arrow-right"></i> Logout (' . Html::encode(Yii::$app->user->identity->username) . ')',
            ['class' => 'btn btn-link nav-link w-100 text-start p-2', 'style' => 'color:#a0aec0']) ?>
        <?php echo Html::endForm() ?>
        <?php endif; ?>
    </div>
</aside>

<!-- Main area -->
<div class="sn-main">
    <!-- Top bar -->
    <div class="sn-topbar">
        <h6 class="mb-0 fw-semibold text-dark"><?= Html::encode($this->title) ?></h6>
        <div class="d-flex align-items-center gap-2">
            <?php if (!Yii::$app->user->isGuest): ?>
                <span class="text-muted small"><i class="bi bi-person-circle"></i>
                    <?= Html::encode(Yii::$app->user->identity->username) ?>
                </span>
            <?php endif; ?>
        </div>
    </div>

    <!-- Alerts -->
    <div class="px-4 pt-3">
        <?php foreach (['success', 'error', 'warning', 'info'] as $type): ?>
            <?php if (Yii::$app->session->hasFlash($type)): ?>
                <div class="alert alert-<?= $type === 'error' ? 'danger' : $type ?> alert-dismissible fade show small" role="alert">
                    <?= Html::encode(Yii::$app->session->getFlash($type)) ?>
                    <button type="button" class="btn-close btn-sm" data-bs-dismiss="alert"></button>
                </div>
            <?php endif; ?>
        <?php endforeach; ?>
    </div>

    <!-- Content -->
    <main class="sn-content">
        <?= $content ?>
    </main>

    <footer class="text-center text-muted py-3 border-top small">
        &copy; <?= date('Y') ?> SJ Nutrizione
    </footer>
</div>

<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>
