<?php
/**
 * @var League\Plates\Template\Template $this
 * @var App\Auth $auth
 * @var App\Acl $acl
 * @var App\Http\Router $router
 * @var App\Session\Flash $flash
 * @var App\Customization $customization
 * @var App\Version $version
 * @var App\Http\ServerRequest $request
 * @var App\Environment $environment
 * @var App\Entity\User $user
 * @var App\View\GlobalSections $sections
 */

$manual ??= false;
$title ??= null;
$header ??= null;
?>
<!DOCTYPE html>
<html data-theme="<?= $customization->getTheme()->value ?>">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title><?= $customization->getPageTitle($title) ?></title>

    <?= $this->fetch('partials/head') ?>

    <script src="<?= $this->assetUrl('dist/lib/sweetalert2/sweetalert2.min.js') ?>" defer></script>

    <?= $sections->get('head') ?>

    <style>
        <?=$customization->getCustomInternalCss() ?>
    </style>
</head>
<body class="page-full <?= $page_class ?? '' ?> <?php
if ($sections->has('sidebar')): ?>has-sidebar<?php
endif; ?>">

<?= $this->fetch('partials/bodyjs') ?>

<script>
    <?php
    $csrfJson = 'null';
    $csrf = $request->getAttribute(App\Http\ServerRequest::ATTR_SESSION_CSRF);
    if ($csrf instanceof App\Session\Csrf) {
        $csrfToken = $csrf->generate(App\Middleware\Auth\ApiAuth::API_CSRF_NAMESPACE);
        $csrfJson = json_encode($csrfToken, JSON_THROW_ON_ERROR);
    }
    ?>

    App.api_csrf = <?=$csrfJson ?>;
</script>

<?= $sections->get('bodyjs') ?>

<a class="sr-only sr-only-focusable" href="#content"><?= __('Skip to main content') ?></a>

<header class="navbar bg-primary-dark shadow-sm fixed-top">
    <?php
    if ($sections->has('sidebar')): ?>
        <button aria-controls="sidebar" aria-expanded="false" aria-label="<?= __(
            'Toggle Sidebar'
        ) ?>" class="navbar-toggler d-inline-flex d-lg-none mr-3" data-breakpoint="lg" data-target="#sidebar"
                data-toggle="navdrawer" data-type="permanent">
            <span class="navbar-toggler-icon"></span>
        </button>
    <?php
    endif; ?>

    <a class="navbar-brand ml-0 mr-auto" href="<?= $router->named('dashboard') ?>">
        azura<strong>cast</strong> <?php
        if (!empty($customization->getInstanceName())): ?>
            <small><?= $this->e($customization->getInstanceName()) ?></small><?php
        endif; ?>
    </a>

    <div id="radio-player-controls"></div>

    <div class="dropdown ml-3 d-inline-flex align-items-center">
        <div class="mr-2">
            <?= $user->getDisplayName() ?>
        </div>

        <button aria-expanded="false" aria-haspopup="true" class="navbar-toggler" aria-label="<?= __(
            'Toggle Menu'
        ) ?>" data-toggle="dropdown" type="button">
            <i class="material-icons" aria-hidden="true">menu_open</i>
        </button>
        <ul class="dropdown-menu dropdown-menu-right">
            <li>
                <a class="dropdown-item" href="<?= $router->named('dashboard') ?>">
                    <i class="material-icons" aria-hidden="true">home</i>
                    <?= __('Dashboard') ?>
                </a>
            </li>
            <li class="dropdown-divider">&nbsp;</li>
            <?php
            if ($acl->isAllowed(App\Enums\GlobalPermissions::View)): ?>
                <li>
                    <a class="dropdown-item" href="<?= $router->named('admin:index:index') ?>">
                        <i class="material-icons" aria-hidden="true">settings</i>
                        <?= __('System Administration') ?>
                    </a>
                </li>
            <?php
            endif; ?>
            <li>
                <a class="dropdown-item" href="<?= $router->named('profile:index') ?>">
                    <i class="material-icons" aria-hidden="true">account_circle</i>
                    <?= __('My Account') ?>
                </a>
            </li>
            <li>
                <a class="dropdown-item" href="<?= $router->named('profile:theme') ?>">
                    <i class="material-icons" aria-hidden="true">invert_colors</i>
                    <?= __('Switch Theme') ?>
                </a>
            </li>
            <li>
                <a class="dropdown-item" href="https://docs.azuracast.com/en/user-guide/troubleshooting"
                   target="_blank">
                    <i class="material-icons" aria-hidden="true">help</i>
                    <?= __('Help') ?>
                </a>
            </li>
            <li class="dropdown-divider">&nbsp;</li>
            <?php
            if ($auth->isMasqueraded()): ?>
                <li>
                    <a class="dropdown-item" href="<?= $router->named('account:endmasquerade') ?>">
                        <i class="material-icons" aria-hidden="true">exit_to_app</i>
                        <?= __('End Session') ?>
                    </a>
                </li>
            <?php
            else: ?>
                <li>
                    <a class="dropdown-item" href="<?= $router->named('account:logout') ?>">
                        <i class="material-icons" aria-hidden="true">exit_to_app</i>
                        <?= __('Sign Out') ?></a></li>
            <?php
            endif; ?>
        </ul>
    </div>
</header>

<?php
if ($sections->has('sidebar')): ?>
    <div class="navdrawer navdrawer-permanent-lg navdrawer-permanent-clipped" id="sidebar" tabindex="-1">
        <nav class="navdrawer-content">
            <?= $sections->get('sidebar') ?>
        </nav>
    </div>
<?php
endif; ?>

<section id="main">
    <section id="content" <?php
    if (!$sections->has('sidebar')): ?>class="content-alt"<?php
    endif; ?> role="main">
        <div class="container">
            <?php
            if ($manual): ?>
                <?=$this->section('content')?>
            <?php
            else: ?>
                <?php
                if ($header): ?>
                    <div class="block-header">
                        <h2><?=$header?></h2>
                    </div>
                <?php
                endif; ?>
                <div class="card mb-3" role="region">
                    <div class="card-header bg-primary-dark">
                        <h3 class="card-title"><?=$title?></h3>
                    </div>
                    <div class="card-body">
                        <?=$this->section('content')?>
                    </div>
                </div>
            <?php
            endif; ?>
        </div>
    </section>
</section>

<footer id="footer" <?php
if (!$sections->has('sidebar')): ?>class="footer-alt"<?php
endif; ?> role="contentinfo">
    <?= sprintf(
        __('Powered by %s'),
        '<a href="https://www.azuracast.com/" target="_blank">' . $environment->getAppName(
        ) . '</a> &bull; ' . $version->getVersionText() . ' &bull; ' . ($environment->isDocker(
        ) ? 'Docker' : 'Ansible') . ' &bull; PHP ' . \PHP_MAJOR_VERSION . '.' . \PHP_MINOR_VERSION
    ) ?>
    <br>
    <?= __('Like our software?') ?> <a href="https://docs.azuracast.com/en/contribute/donate"><?= __(
            'Donate to support AzuraCast!'
        ) ?></a>
</footer>

<div id="radio-player"></div>
</body>
</html>
