<?php
use exocet\bootstrap5md\MaterialAsset;
use exocet\bootstrap5md\widgets\NavBar;
use yii\bootstrap5\Nav;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\Breadcrumbs;
use app\models\Courier;
use app\models\Delivery;

$bucketCount = array_sum(Yii::$app->session->get('bucket', []));
$isAdmin = !Yii::$app->user->isGuest && method_exists(Yii::$app->user->identity, 'isAdmin') && Yii::$app->user->identity->isAdmin();

// Проверка, является ли пользователь курьером
$courier = null;
if (!Yii::$app->user->isGuest) {
    $courier = Courier::find()
            ->where(['user_id' => Yii::$app->user->id, 'status' => [Courier::STATUS_FREE, Courier::STATUS_BUSY]])
            ->one();
}

$this->registerCssFile('@web/css/site.css');
MaterialAsset::register($this);
$this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>" class="h-100">
<head>
    <meta charset="<?= Yii::$app->charset ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=1">
    <?= Html::csrfMetaTags() ?>
    <title><?= Html::encode($this->title) ?></title>
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <?php $this->head() ?>
</head>
<body class="d-flex flex-column min-vh-100">
<header>
    <nav class="navbar navbar-dark bg-primary py-2 fixed-top">
        <div class="container">
            <a class="navbar-brand d-flex align-items-center" href="<?= Yii::$app->homeUrl ?>">
                <img src="./imgs/logo.png" alt="FoodExpress" class="me-2" style="height:50px;">
                <span class="h5 fw-bold mb-0">FoodExpress</span>
            </a>
            <div class="nav-container">
                <?php
                $currentRoute = Yii::$app->controller->route;
                $isActive = function($routes) use ($currentRoute) {
                    if (is_array($routes)) {
                        foreach ($routes as $route) {
                            if (strpos($currentRoute, $route) === 0) {
                                return true;
                            }
                        }
                    } else {
                        return strpos($currentRoute, $routes) === 0;
                    }
                    return false;
                };

                $bucketLink = '<span class="bucket-link">
                    <i class="fas fa-shopping-cart"></i>
                    <span class="nav-text ms-2">Корзина</span>
                    ' . ($bucketCount > 0 ? '<span class="bucket-badge" id="bucket-badge">' . $bucketCount . '</span>' : '<span class="bucket-badge" id="bucket-badge" style="display: none;"></span>') . '
                </span>';
                ?>

                <ul class="navbar-nav flex-row align-items-center ms-auto" style="gap: 15px;">
                    <?php if (!$isAdmin && !$courier): ?>
                        <li class="nav-item">
                            <a class="nav-link d-flex align-items-center <?= $isActive('site/restaurants') ? 'active' : '' ?>"
                               href="<?= Url::to(['site/restaurants']) ?>">
                                <i class="fas fa-store me-2"></i>
                                <span class="nav-text">Рестораны</span>
                            </a>
                        </li>
                    <?php endif; ?>

                    <?php if ($courier): ?>
                        <li class="nav-item">
                            <a class="nav-link d-flex align-items-center <?= $isActive('courier/index') ? 'active' : '' ?>"
                               href="<?= Url::to(['/courier/index']) ?>">
                                <i class="fas fa-truck me-2"></i>
                                <span class="nav-text">Мои доставки</span>
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link d-flex align-items-center <?= $isActive('courier/available') ? 'active' : '' ?>"
                               href="<?= Url::to(['/courier/available']) ?>">
                                <i class="fas fa-box-open me-2"></i>
                                <span class="nav-text">Доступные заказы</span>
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link d-flex align-items-center <?= $isActive('courier/active') ? 'active' : '' ?>"
                               href="<?= Url::to(['/courier/active']) ?>">
                                <i class="fas fa-clipboard-list me-2"></i>
                                <span class="nav-text">Активные доставки</span>
                            </a>
                        </li>
                    <?php endif; ?>

                    <?php if (Yii::$app->user->isGuest): ?>
                        <li class="nav-item">
                            <a class="nav-link d-flex align-items-center <?= $isActive('auth/register') ? 'active' : '' ?>"
                               href="<?= Url::to(['/auth/register']) ?>">
                                <i class="fas fa-user-plus me-2"></i>
                                <span class="nav-text">Регистрация</span>
                            </a>
                        </li>
                    <?php else: ?>
                        <?php if (!$isAdmin && !$courier): ?>
                            <li class="nav-item">
                                <a class="nav-link d-flex align-items-center position-relative <?= $isActive('site/bucket') ? 'active' : '' ?>"
                                   href="<?= Url::to(['/site/bucket']) ?>">
                                    <?= $bucketLink ?>
                                </a>
                            </li>
                        <?php endif; ?>

                        <?php if (!$isAdmin && !$courier): ?>
                            <li class="nav-item">
                                <a class="nav-link d-flex align-items-center <?= $isActive('site/profile') ? 'active' : '' ?>"
                                   href="<?= Url::to(['/site/profile']) ?>">
                                    <i class="fas fa-user me-2"></i>
                                    <span class="nav-text">Профиль</span>
                                </a>
                            </li>
                        <?php endif; ?>
                    <?php endif; ?>

                    <li class="nav-item">
                        <?php if (Yii::$app->user->isGuest): ?>
                            <a class="nav-link d-flex align-items-center <?= $isActive('auth/login') ? 'active' : '' ?>"
                               href="<?= Url::to(['/auth/login']) ?>">
                                <i class="fas fa-sign-in-alt me-2"></i>
                                <span class="nav-text">Вход</span>
                            </a>
                        <?php elseif ($isAdmin): ?>
                            <a class="nav-link d-flex align-items-center <?= $isActive('admin/') ? 'active' : '' ?>"
                               href="<?= Url::to(['/admin/default']) ?>">
                                <i class="fas fa-cog me-2"></i>
                                <span class="nav-text">Админ-панель</span>
                            </a>
                        <?php endif; ?>
                    </li>

                    <?php if (!Yii::$app->user->isGuest): ?>
                        <li class="nav-item">
                            <a class="nav-link d-flex align-items-center"
                               href="<?= Url::to(['/auth/logout']) ?>"
                               data-method="post">
                                <i class="fas fa-sign-out-alt me-2"></i>
                                <span class="nav-text">Выход</span>
                            </a>
                        </li>
                    <?php endif; ?>

                    <li class="nav-item">
                        <button id="themeToggle" class="theme-toggle-btn" type="button" aria-label="Переключить тему">
                            <i class="fas fa-moon" id="themeIcon"></i>
                        </button>
                    </li>
                </ul>
            </div>
        </div>
    </nav>
</header>

<main id="main" class="flex-shrink-0" role="main" style="background-color:rgb(248, 248, 248);">
    <div class="container">
        <?php if (!empty($this->params['breadcrumbs'])): ?>
            <?= Breadcrumbs::widget(['links' => $this->params['breadcrumbs']]) ?>
        <?php endif ?>
        <?= $content ?>
    </div>
</main>

<footer class="bg-primary text-white mt-auto" style="padding-top:4%; padding-bottom:4%">
    <div class="container">
        <div class="row align-items-start">
            <div class="col-md-3 mb-3 mb-md-0">
                <div class="d-flex align-items-center mb-3">
                    <img src="./imgs/logo.png" alt="FoodExpress" class="me-2" style="height: 50px;">
                    <span class="h5 fw-bold mb-0">FoodExpress</span>
                </div>
                <p class="text-light mb-0 small">Быстрая доставка еды из любимых ресторанов</p>
            </div>

            <?php if (!$isAdmin): ?>
                <div class="col-md-3 mb-3 mb-md-0">
                    <h6 class="fw-bold mb-3">Для клиентов</h6>
                    <ul class="list-unstyled mb-0">
                        <li class="mb-2">
                            <a href="<?= \yii\helpers\Url::to(['/site/restaurants']) ?>"
                               class="text-light text-decoration-none hover-text-white small">
                                Рестораны
                            </a>
                        </li>
                        <li class="mb-2">
                            <a href="<?= \yii\helpers\Url::to(['/site/stocks']) ?>"
                               class="text-light text-decoration-none hover-text-white small">
                                Акции
                            </a>
                        </li>
                        <li class="mb-2">
                            <a href="#"
                               class="text-light text-decoration-none hover-text-white small">
                                Помощь
                            </a>
                        </li>
                    </ul>
                </div>
            <?php endif; ?>

            <div class="col-md-3 mb-3 mb-md-0">
                <h6 class="fw-bold mb-3">Для партнеров</h6>
                <ul class="list-unstyled mb-0">
                    <li class="mb-2">
                        <a href="<?= \yii\helpers\Url::to(['/auth/restaurant-register']) ?>"
                           class="text-light text-decoration-none hover-text-white small">
                            Ресторанам
                        </a>
                    </li>
                    <li class="mb-2">
                        <a href="<?= \yii\helpers\Url::to(['/auth/courier-register']) ?>"
                           class="text-light text-decoration-none hover-text-white small">
                            Курьерам
                        </a>
                    </li>
                </ul>
            </div>

            <div class="col-md-3">
                <h6 class="fw-bold mb-3">Контакты</h6>
                <div class="text-light">
                    <p class="mb-2 small d-flex align-items-center">
                        <i class="fas fa-phone me-2" style="width: 20px;"></i> +7 (999) 123-45-67
                    </p>
                    <p class="mb-0 small d-flex align-items-center">
                        <i class="fas fa-envelope me-2" style="width: 20px;"></i> support@foodexpress.ru
                    </p>
                </div>
            </div>
        </div>
    </div>
</footer>

<script>
    function updateBucketBadge(count) {
        const badge = document.getElementById('bucket-badge');
        if (badge) {
            if (count > 0) {
                badge.textContent = count;
                badge.style.display = 'flex';
            } else {
                badge.style.display = 'none';
            }
        }
    }

    function applyTheme(theme) {
        const themeIcon = document.getElementById('themeIcon');
        const main = document.getElementById('main');

        if (theme === 'dark') {
            document.documentElement.classList.add('dark-mode');
            if (themeIcon) {
                themeIcon.classList.replace('fa-moon', 'fa-sun');
            }
            if (main) {
                main.style.backgroundColor = '#1a1a1a';
            }
        } else {
            document.documentElement.classList.remove('dark-mode');
            if (themeIcon) {
                themeIcon.classList.replace('fa-sun', 'fa-moon');
            }
            if (main) {
                main.style.backgroundColor = 'rgb(248, 248, 248)';
            }
        }
    }

    document.addEventListener('DOMContentLoaded', function() {
        const savedTheme = localStorage.getItem('theme') || 'light';
        applyTheme(savedTheme);

        const themeToggle = document.getElementById('themeToggle');
        if (themeToggle) {
            themeToggle.addEventListener('click', function() {
                const html = document.documentElement;
                const isDarkMode = html.classList.contains('dark-mode');

                if (isDarkMode) {
                    localStorage.setItem('theme', 'light');
                    applyTheme('light');
                } else {
                    localStorage.setItem('theme', 'dark');
                    applyTheme('dark');
                }
            });
        }
    });
</script>

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