<?php
/**
 * @var App\Entity\Station $station
 * @var App\Acl $acl
 * @var App\View\GlobalSections $sections
 */

$sections->appendStart('bodyjs');
?>
<script>
    $(document).on('station-needs-restart', function (e) {
        $('.btn-restart-station').removeClass('d-none');
    });

    $(document).on('click', '.api-call', function (e) {
        e.stopPropagation();

        var btn = $(this);

        var btn_original_text = btn.html();
        var trigger_reload = !btn.hasClass('no-reload');

        confirmDangerousAction(e.target).then(function (result) {
            if (result.value) {
                btn.text(<?=$this->escapeJs(__('Please wait...')) ?>);
                btn.addClass('disabled');

                $.ajax({
                    type: 'POST',
                    headers: {
                        "X-API-CSRF": App.api_csrf
                    },
                    url: btn.attr('href'),
                    success: function (data) {
                        // Only restart if the user isn't on a form page
                        if (trigger_reload && $('form.form').length === 0) {
                            setTimeout('location.reload()', 2000);
                        } else {
                            btn.removeClass('disabled').html(btn_original_text);
                        }

                        var notify_type = (data.success) ? 'success' : 'warning';
                        notify(data.formatted_message, notify_type);
                    },
                    error: function (response) {
                        data = jQuery.parseJSON(response.responseText);
                        notify(data.formatted_message, 'danger');

                        btn.removeClass('disabled').html(btn_original_text);
                    },
                    dataType: 'json'
                });
            }
        });

        return false;
    });

    $(function () {
        function updateClock() {
            let d = new Date();

            let timeConfig = {...App.time_config};
            timeConfig.timeZone = <?=$this->escapeJs($station->getTimezone()) ?>;
            timeConfig.timeStyle = 'long';

            let time = d.toLocaleString(App.locale_with_dashes, timeConfig);

            $('#station-time').text(time);
        }

        setInterval(updateClock, 1000);
        updateClock();
    });
</script>
<?php
$sections->end();
?>

<div class="navdrawer-header">
    <div class="d-flex align-items-center">
        <a class="navbar-brand px-0 flex-fill" href="<?= $router->fromHere('stations:profile:index') ?>">
            <div><?= $this->e($station->getName()) ?></div>
            <div class="xs" id="station-time" title="<?= $this->e(__('Station Time')) ?>">
                <?= date('H:i:s T') ?>
            </div>
        </a>
        <?php
        if ($acl->isAllowed(App\Enums\StationPermissions::Profile, $station)): ?>
            <a class="navbar-brand ml-0 flex-shrink-0" href="<?= $router->fromHere('stations:profile:edit') ?>">
                <i class="material-icons sm">edit</i>
                <span class="sr-only"><?= __('Edit Profile') ?>
            </a>
        <?php
        endif; ?>
    </div>
</div>

<?php
echo $this->fetch('partials/sidebar_menu', ['menu' => $menu, 'active' => $active]);
?>
