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

use frontend\modules\webcms\Module;
use yii\helpers\Url;

$isLoggedIn = Module::isLoggedIn();
$ctrl = Yii::$app->controller->id;
$action = Yii::$app->controller->action->id;
$this->beginPage();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?= yii\helpers\Html::encode($this->title) ?> — Website Manager</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <style>
        :root {
            --cms-sidebar: #0f172a;
            --cms-sidebar-hover: #1e293b;
            --cms-active: #2d6a4f;
            --cms-accent: #52b788;
            --cms-bg: #f8fafc;
        }
        * { box-sizing: border-box; }
        body { font-family: 'Inter', sans-serif; background: var(--cms-bg); margin: 0; color: #334155; }

        .cms-sidebar {
            width: 240px;
            min-height: 100vh;
            background: var(--cms-sidebar);
            position: fixed;
            top: 0; left: 0;
            display: flex;
            flex-direction: column;
            z-index: 100;
        }
        .cms-brand {
            padding: 1.25rem 1.3rem;
            border-bottom: 1px solid rgba(255,255,255,.07);
        }
        .cms-brand .title {
            color: #fff;
            font-size: 1rem;
            font-weight: 700;
            display: flex;
            align-items: center;
            gap: .5rem;
        }
        .cms-brand .subtitle {
            color: rgba(255,255,255,.35);
            font-size: .68rem;
            margin-top: .2rem;
            letter-spacing: .05em;
        }
        .cms-nav { flex: 1; padding: .75rem 0; }
        .cms-section {
            font-size: .62rem;
            text-transform: uppercase;
            letter-spacing: .1em;
            color: rgba(255,255,255,.3);
            padding: .9rem 1.3rem .3rem;
        }
        .cms-link {
            display: flex;
            align-items: center;
            gap: .55rem;
            color: rgba(255,255,255,.55);
            text-decoration: none;
            padding: .55rem .75rem;
            margin: 1px .6rem;
            border-radius: 8px;
            font-size: .875rem;
            font-weight: 500;
            transition: background .15s, color .15s;
        }
        .cms-link i { font-size: 1rem; width: 18px; text-align: center; }
        .cms-link:hover { background: var(--cms-sidebar-hover); color: #fff; }
        .cms-link.active { background: var(--cms-active); color: #fff; }
        .cms-footer-link {
            padding: .75rem 1rem;
            border-top: 1px solid rgba(255,255,255,.07);
        }

        .cms-main { margin-left: 240px; min-height: 100vh; display: flex; flex-direction: column; }
        .cms-topbar {
            background: #fff;
            border-bottom: 1px solid #e2e8f0;
            padding: .8rem 1.75rem;
            display: flex;
            align-items: center;
            justify-content: space-between;
            position: sticky;
            top: 0;
            z-index: 50;
        }
        .cms-topbar-title { font-weight: 600; font-size: 1rem; color: #0f172a; }
        .cms-content { padding: 2rem 1.75rem; flex: 1; }

        .cms-card {
            background: #fff;
            border-radius: 14px;
            border: 1px solid #e2e8f0;
            box-shadow: 0 1px 4px rgba(0,0,0,.05);
        }
        .cms-card-header {
            padding: 1.2rem 1.5rem;
            border-bottom: 1px solid #f1f5f9;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        .cms-card-header h5 { margin: 0; font-size: .95rem; font-weight: 600; color: #0f172a; }
        .cms-card-body { padding: 1.5rem; }

        .form-label-cms {
            font-size: .75rem;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: .08em;
            color: #64748b;
            margin-bottom: .4rem;
            display: block;
        }
        .form-hint {
            font-size: .75rem;
            color: #94a3b8;
            margin-top: .3rem;
        }
        input.cms-input, textarea.cms-input, select.cms-input {
            width: 100%;
            border: 1.5px solid #e2e8f0;
            border-radius: 8px;
            padding: .65rem .85rem;
            font-size: .9rem;
            color: #334155;
            background: #f8fafc;
            transition: border-color .2s, box-shadow .2s;
            outline: none;
            font-family: 'Inter', sans-serif;
        }
        input.cms-input:focus, textarea.cms-input:focus, select.cms-input:focus {
            border-color: var(--cms-active);
            box-shadow: 0 0 0 3px rgba(45,106,79,.1);
            background: #fff;
        }
        textarea.cms-input { resize: vertical; min-height: 80px; }

        .btn-cms-primary {
            background: var(--cms-active);
            color: #fff;
            border: none;
            border-radius: 8px;
            padding: .6rem 1.4rem;
            font-weight: 600;
            font-size: .875rem;
            cursor: pointer;
            transition: background .2s, transform .1s;
        }
        .btn-cms-primary:hover { background: #1e5c3f; }
        .btn-cms-secondary {
            background: #f1f5f9;
            color: #475569;
            border: none;
            border-radius: 8px;
            padding: .6rem 1.2rem;
            font-weight: 600;
            font-size: .875rem;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: .3rem;
            transition: background .2s;
        }
        .btn-cms-secondary:hover { background: #e2e8f0; color: #334155; }
        .btn-cms-danger {
            background: #fee2e2;
            color: #dc2626;
            border: none;
            border-radius: 8px;
            padding: .45rem .9rem;
            font-size: .8rem;
            font-weight: 600;
            cursor: pointer;
            transition: background .2s;
            text-decoration: none;
        }
        .btn-cms-danger:hover { background: #fecaca; color: #dc2626; }

        .cms-badge-active   { background: #dcfce7; color: #16a34a; border-radius: 50px; padding: .18rem .7rem; font-size: .72rem; font-weight: 600; }
        .cms-badge-inactive { background: #f1f5f9; color: #94a3b8; border-radius: 50px; padding: .18rem .7rem; font-size: .72rem; font-weight: 600; }

        .cms-section-tag {
            display: inline-block;
            background: #f0faf4;
            color: #2d6a4f;
            border-radius: 6px;
            padding: .15rem .55rem;
            font-size: .7rem;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: .06em;
        }
        .cms-alert-success {
            background: #f0fdf4;
            border: 1px solid #bbf7d0;
            color: #166534;
            border-radius: 10px;
            padding: .85rem 1.1rem;
            font-size: .88rem;
            margin-bottom: 1.5rem;
        }
        .cms-stat-card {
            background: #fff;
            border-radius: 14px;
            border: 1px solid #e2e8f0;
            padding: 1.5rem;
            display: flex;
            align-items: center;
            gap: 1rem;
        }
        .cms-stat-icon {
            width: 48px;
            height: 48px;
            border-radius: 12px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 1.3rem;
            flex-shrink: 0;
        }
    </style>
    <?php $this->head() ?>
</head>
<body>
<?php $this->beginBody() ?>

<?php if ($isLoggedIn): ?>
<!-- Sidebar -->
<aside class="cms-sidebar">
    <div class="cms-brand">
        <div class="title"><i class="bi bi-leaf" style="color:#52b788"></i> Website Manager</div>
        <div class="subtitle">SJ Nutrizione</div>
    </div>
    <nav class="cms-nav">
        <div class="cms-section">Overview</div>
        <a class="cms-link <?= $ctrl === 'default' && $action === 'index' ? 'active' : '' ?>"
           href="<?= Url::to(['/webcms/default/index']) ?>">
            <i class="bi bi-grid"></i> Dashboard
        </a>

        <div class="cms-section">Website Content</div>
        <a class="cms-link <?= $ctrl === 'default' && $action === 'content' ? 'active' : '' ?>"
           href="<?= Url::to(['/webcms/default/content']) ?>">
            <i class="bi bi-file-text"></i> Text Content
        </a>
        <a class="cms-link <?= $ctrl === 'testimonial' ? 'active' : '' ?>"
           href="<?= Url::to(['/webcms/testimonial/index']) ?>">
            <i class="bi bi-chat-quote"></i> Testimonials
        </a>
        <a class="cms-link <?= $ctrl === 'plan' ? 'active' : '' ?>"
           href="<?= Url::to(['/webcms/plan/index']) ?>">
            <i class="bi bi-tag"></i> Pricing & Plans
        </a>

        <div class="cms-section">Website</div>
        <a class="cms-link" href="<?= Url::to(['/site/index']) ?>" target="_blank">
            <i class="bi bi-box-arrow-up-right"></i> View Website
        </a>
    </nav>
    <div class="cms-footer-link">
        <a class="cms-link" href="<?= Url::to(['/webcms/default/logout']) ?>">
            <i class="bi bi-box-arrow-right"></i> Logout
        </a>
    </div>
</aside>
<?php endif; ?>

<!-- Main -->
<div class="<?= $isLoggedIn ? 'cms-main' : '' ?>">
    <?php if ($isLoggedIn): ?>
    <div class="cms-topbar">
        <div class="cms-topbar-title"><?= yii\helpers\Html::encode($this->title) ?></div>
        <a href="<?= Url::to(['/site/index']) ?>" target="_blank"
           style="font-size:.8rem;color:#64748b;text-decoration:none;display:flex;align-items:center;gap:.3rem">
            <i class="bi bi-eye"></i> View Website
        </a>
    </div>
    <?php endif; ?>

    <div class="cms-content">
        <?php if (Yii::$app->session->hasFlash('success')): ?>
        <div class="cms-alert-success">
            <i class="bi bi-check-circle me-2"></i><?= yii\helpers\Html::encode(Yii::$app->session->getFlash('success')) ?>
        </div>
        <?php endif; ?>
        <?= $content ?>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>
