{% extends 'base.html.twig' %}
{% block title "#{parent()} - #{provider.name}" %}
{% block manifest path('webapp_manifest', { provider: provider.identifier }) %}

{% block body %}
    <div class="row">
        <div class="col-md-8 order-md-last">
            <section class="section messages" v-show="messages.count > 0">
                <header class="section__title flex">
                    <h2>
                        <fa :icon="['fal', 'bullhorn']" fixed-width class="mr-2"></fa>
                        Komunikaty <span class="ml-2 badge badge-pill badge-dark">{{ '{{ messages.count }}' }}</span>
                    </h2>
                    <button class="btn btn-action flex-space-left" ref="settings-messages" @click="visibility.messages = true">
                        <tooltip>ustawienia</tooltip>
                        <fa :icon="['fal', 'cog']" fixed-width></fa>
                    </button>
                    <button class="btn btn-action" @click="updateMessages" ref="btn-messages-refresh">
                        <tooltip>odśwież</tooltip>
                        <fa :icon="['fal', 'sync']" :spin="messages.state === 'fetching'" fixed-width></fa>
                    </button>
                    <button class="btn btn-action" @click="sections.messages = !sections.messages">
                        <tooltip>
                            {{ '{{ ' }} sections.messages ? 'zwiń' : 'rozwiń' {{ '}}' }}
                            <span class="sr-only">sekcję komunikatów</span>
                        </tooltip>
                        <fa :icon="['fal', sections.messages ? 'chevron-up' : 'chevron-down']" fixed-width/>
                    </button>

                    <popper reference="settings-messages" v-if="visibility.messages" arrow placement="left-start" @leave="visibility.messages = false">
                        <h3 class="popper__heading flex">
                            <fa :icon="['far', 'cog']"></fa>
                            <label class="text" for="messages-auto-refresh">autoodświeżanie</label>
                            <input type="checkbox" class="flex-space-left" id="messages-auto-refresh" v-model="autorefresh.messages.active"/>
                        </h3>
                        <div class="flex" v-show="autorefresh.messages.active">
                            <span class="text">co</span>
                            <label class="sr-only" for="messages-auto-refresh-interval">częstotliwość odświeżania</label>
                            <input type="text" class="form-control form-control-sm" id="messages-auto-refresh-interval" v-model="autorefresh.messages.interval"/>
                            <span class="text">s</span>
                        </div>
                    </popper>
                </header>
                <fold :visible="sections.messages">
                    <messages></messages>
                </fold>
            </section>

            <section class="section">
                <header class="section__title flex">
                    <h2>
                        <fa :icon="['fal', 'clock']" fixed-width></fa>
                        <span class="text">Odjazdy</span>
                    </h2>

                    <button class="btn btn-action flex-space-left" ref="settings-departures" @click="visibility.departures = true">
                        <tooltip>ustawienia</tooltip>
                        <fa :icon="['fal', 'cog']" fixed-width></fa>
                    </button>
                    <button class="btn btn-action" @click="updateDepartures({ stops })">
                        <tooltip>odśwież</tooltip>
                        <fa :icon="['fal', 'sync']" :spin="departures.state === 'fetching'" fixed-width></fa>
                    </button>
                    <portal to="popups">
                        <popper reference="settings-departures" v-if="visibility.departures" arrow placement="left-start" @leave="visibility.departures = false">
                            <h3 class="popper__heading flex">
                                <fa :icon="['far', 'sync']" fixed-width></fa>
                                <label class="text" for="messages-auto-refresh">autoodświeżanie</label>
                                <input type="checkbox" class="flex-space-left" id="messages-auto-refresh" v-model="autorefresh.departures.active"/>
                            </h3>
                            <div class="flex" v-show="autorefresh.messages.active">
                                <span class="text">co</span>
                                <label class="sr-only" for="messages-auto-refresh-interval">częstotliwość odświeżania</label>
                                <input type="text" class="form-control form-control-sm form-control-simple" id="messages-auto-refresh-interval" v-model="autorefresh.departures.interval"/>
                                <span class="text">s</span>
                            </div>
                        </popper>
                    </portal>
                </header>
                <departures :stops="stops"></departures>
                {% if provider.attribution %}
                    <div class="attribution">
                        <fa :icon="['fal', 'info-circle']"></fa>
                        Pochodzenie danych: {{ provider.attribution|raw }}
                    </div>
                {% endif %}
            </section>
        </div>
        <div class="col-md-4 order-md-first">
            <section class="section picker" v-if="stops.length > 0">
                <header class="section__title flex">
                    <h2>
                        <fa :icon="['fal', 'sign']" fixed-width></fa>
                        <span class="text">Przystanki</span>
                    </h2>
                    <button class="btn btn-action flex-space-left" @click="clear">
                        <tooltip>usuń wszystkie</tooltip>
                        <fa :icon="['fal', 'trash-alt']" fixed-width></fa>
                    </button>
                </header>

                <ul class="picker__stops list-underlined">
                    <li v-for="stop in stops" :key="stop.id" class="d-flex align-items-center">
                        <button @click="remove(stop)" class="btn btn-action align-self-start">
                            <tooltip>usuń przystanek</tooltip>
                            <fa :icon="['fal', 'times']"></fa>
                        </button>
                        <picker-stop :stop="stop" class="flex-grow-1"></picker-stop>
                    </li>
                </ul>

                <div class="d-flex mt-2">
                    <button class="btn btn-action btn-sm flex-space-left" @click="visibility.save = true" ref="save">
                        <fa :icon="['fal', 'star']" fixed-width></fa>
                        zapisz jako...
                    </button>
                </div>

                <popper reference="save" v-if="visibility.save" arrow tabindex="-1" @leave="visibility.save = false" placement="bottom-end">
                    <favourites-adder @saved="visibility.save = false"/>
                </popper>
            </section>
            <section class="section picker">
                <header class="section__title flex">
                    <template v-if="visibility.picker === 'search'">
                        <h2 class="flex-grow-1">
                            <fa :icon="['fal', 'search']" fixed-width class="mr-1"></fa>
                            Wybierz przystanki
                        </h2>
                        <button class="btn btn-action" @click="visibility.picker = 'favourites'">
                            <tooltip>Zapisane</tooltip>
                            <fa :icon="['fal', 'star']" fixed-witdth></fa>
                        </button>
                    </template>
                    <template v-else>
                        <h2 class="flex-grow-1">
                            <fa :icon="['fal', 'star']" fixed-width class="mr-1"></fa>
                            Zapisane
                        </h2>
                        <button class="btn btn-action" @click="visibility.picker = 'search'">
                            <tooltip>Wybierz przystanki</tooltip>
                            <fa :icon="['fal', 'search']" fixed-witdth></fa>
                        </button>
                    </template>
                </header>
                <div class="transition-box" style="overflow: hidden;">
                    <transition name="fade">
                        <stop-finder @select="add" :blacklist="stops" v-if="visibility.picker === 'search'"></stop-finder>
                        <favourites v-else-if="visibility.picker === 'favourites'"></favourites>
                    </transition>
                </div>
            </section>
        </div>
    </div>

    <portal-target name="popups" multiple></portal-target>
{% endblock %}

{% block javascripts %}
    <script>
        window.data = {
            provider: {{ provider.identifier|json_encode|raw }}
        };

        window.czydojade = {};
        window.czydojade.state = {{ state|json_encode|raw }};
    </script>
{% endblock %}