<?php
use App\Entity\StationPlaylist;

$this->layout('main', ['title' => __('Playlists'), 'manual' => true]);

/** @var \App\Assets $assets */
$assets->load('fullcalendar');
?>

<div class="card">
    <div class="card-header ch-alt">
        <h2><?=__('Playlists') ?></h2>

        <a class="btn bgm-blue btn-float" role="button" title="<?=__('Add %s', __('Playlist')) ?>" href="<?=$router->fromHere('stations:playlists:add') ?>">
            <span class="sr-only"><?=__('Add %s', __('Playlist')) ?></span>
            <i class="zmdi zmdi-plus"></i>
        </a>
    </div>

    <div role="tabpanel">
        <ul class="tab-nav" role="tablist">
            <li class="p-l-10 active"><a role="tab" data-toggle="tab" aria-expanded="true" aria-controls="all-playlists" href="#all-playlists"><?=__('All Playlists') ?></a></li>
            <li><a role="tab" data-toggle="tab" aria-controls="schedule-view" href="#schedule-view"><?=__('Schedule View') ?></a></li>
        </ul>

        <div class="tab-content p-t-0 p-b-0">
            <div class="tab-pane active" id="all-playlists" role="tabpanel">

                <table class="table table-striped">
                    <colgroup>
                        <col width="30%">
                        <col width="30%">
                        <col width="28%">
                        <col width="12%">
                    </colgroup>
                    <thead>
                    <tr>
                        <th><?=__('Actions') ?></th>
                        <th><?=__('Playlist') ?></th>
                        <th><?=__('Scheduling') ?></th>
                        <th><?=__('# Songs') ?></th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach($playlists as $row): ?>
                        <tr class="vertical-align-middle">
                            <td>
                                <a class="btn btn-sm btn-primary" href="<?=$router->fromHere('stations:playlists:edit', ['id' => $row['id']]) ?>"><?=__('Edit') ?></a>

                            <?php if ($row['source'] === StationPlaylist::SOURCE_SONGS && $row['order'] === StationPlaylist::ORDER_SEQUENTIAL): ?>
                                <a class="btn btn-sm btn-default" href="<?=$router->fromHere('stations:playlists:reorder', ['id' => $row['id']]) ?>"><?=__('Reorder') ?></a>
                            <?php endif; ?>

                                <div class="btn-group">
                                    <button type="button" class="btn btn-sm btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                        <?=__('Export') ?> <span class="caret"></span>
                                    </button>
                                    <ul class="dropdown-menu">
                                        <?php foreach(['pls', 'm3u'] as $format): ?>
                                            <li><a href="<?=$router->fromHere('stations:playlists:export', ['id' => $row['id'], 'format' => $format]) ?>"><?=sprintf(__('Download %s'), strtoupper($format)) ?></a></li>                                        <?php endforeach; ?>
                                    </ul>
                                </div>

                                <a class="btn btn-sm btn-danger" href="<?=$router->fromHere('stations:playlists:delete', ['id' => $row['id'], 'csrf' => $csrf]) ?>"><?=__('Delete') ?></a>
                            </td>
                            <td>
                                <big>
                                    <a href="<?=$router->fromHere('stations:files:index').'#playlist:'.urlencode($row['name']) ?>"><?=$this->e($row['name']) ?></a>
                                </big>
                                <br>
                                <span class="label label-default"><?=(($row['source'] === StationPlaylist::SOURCE_SONGS) ? __('Song-based') : __('Remote URL')) ?></span>
                                <?php if ($row['source'] === StationPlaylist::SOURCE_SONGS && $row['order'] === StationPlaylist::ORDER_SEQUENTIAL): ?>
                                    <span class="label label-info"><?=__('Sequential') ?></span>
                                <?php endif; ?>
                                <?php if ($row['include_in_automation']): ?>
                                    <span class="label label-success"><?=__('Auto-Assigned') ?></span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <?php if (!$row['is_enabled']): ?>
                                    <?=__('Disabled') ?>
                                <?php elseif ($row['type'] === StationPlaylist::TYPE_DEFAULT): ?>
                                    <?=__('General Rotation') ?><br>
                                    <?=__('Weight') ?>: <?=(int)$row['weight'] ?> (<?=$row['probability'] ?>)
                                <?php elseif ($row['type'] === StationPlaylist::TYPE_SCHEDULED): ?>
                                    <?=__('Scheduled') ?><br>
                                    <?=sprintf(__('Plays between %s and %s'), $customization->formatTime(StationPlaylist::getTimestamp($row['schedule_start_time'])), $customization->formatTime(StationPlaylist::getTimestamp($row['schedule_end_time']))) ?>
                                <?php elseif ($row['type'] === StationPlaylist::TYPE_ONCE_PER_X_SONGS): ?>
                                    <?=sprintf(__('Once per %d Songs'), $row['play_per_songs']) ?>
                                <?php elseif ($row['type'] === StationPlaylist::TYPE_ONCE_PER_X_MINUTES): ?>
                                    <?=sprintf(__('Once per %d Minutes'), $row['play_per_minutes']) ?>
                                <?php elseif ($row['type'] === StationPlaylist::TYPE_ONCE_PER_DAY): ?>
                                    <?=__('Once per Day') ?><br>
                                    <?=sprintf(__('Plays at %s'), $customization->formatTime(StationPlaylist::getTimestamp($row['play_once_time']))) ?>
                                <?php elseif ($row['type'] === StationPlaylist::TYPE_ADVANCED): ?>
                                    <?=__('Custom') ?>
                                <?php endif; ?>
                            </td>
                            <td><?=$row['num_songs'] ?></td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>

            </div>
            <div class="tab-pane card-padding" id="schedule-view" role="tabpanel">
                <div id="playlist-calendar"></div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript" nonce="<?=$assets->getCspNonce() ?>">
$(function() {
    $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
        var target = $(e.target).attr("href");

        if (target == '#schedule-view') {

            $('#playlist-calendar').fullCalendar({
                themeSystem: 'bootstrap3',
                nowIndicator: true,
                defaultView: 'agendaWeek',
                locale: '<?=substr(strtolower($customization->getLocale()), 0, 2) ?>',
                header: false,
                footer: false,
                height: 'auto',
                now: '<?=$schedule_now ?>',
                events: '<?=$schedule_url ?>'
            });

        } else {

            $('#playlist-calendar').fullCalendar('destroy').removeClass().empty();

        }
    });
});
</script>
