<template>
    <div
        v-if="network"
        class="content"
    >
        <FilterBar :fetch-releases="fetchNetwork" />

        <div class="network">
            <div class="sidebar">
                <a
                    :href="network.url"
                    target="_blank"
                    rel="noopener noreferrer"
                    class="title"
                >
                    <img
                        :src="`/img/logos/${network.slug}/network.png`"
                        class="logo"
                    >
                </a>

                <p
                    v-if="network.description"
                    class="description"
                >{{ network.description }}</p>

                <template v-if="sites.length">
                    <ul class="nolist sites">
                        <li
                            v-for="site in sites"
                            :key="`site-${site.id}`"
                        >
                            <SiteTile :site="site" />
                        </li>
                    </ul>
                </template>
            </div>

            <div class="content-inner">
                <Releases
                    :releases="releases"
                />
            </div>
        </div>
    </div>
</template>

<script>
import FilterBar from '../header/filter-bar.vue';
import Releases from '../releases/releases.vue';
import SiteTile from '../tile/site.vue';

async function fetchNetwork() {
    this.network = await this.$store.dispatch('fetchNetworks', this.$route.params.networkSlug);

    this.sites = this.network.sites
        .filter(site => !site.independent)
        .sort(({ name: nameA }, { name: nameB }) => nameA.localeCompare(nameB));

    this.releases = this.network.sites.map(site => site.releases).flat();
}

async function mounted() {
    await this.fetchNetwork();
    this.pageTitle = this.network.name;
}

export default {
    components: {
        FilterBar,
        Releases,
        SiteTile,
    },
    data() {
        return {
            network: null,
            sites: null,
            releases: [],
            pageTitle: null,
        };
    },
    mounted,
    methods: {
        fetchNetwork,
    },
};
</script>

<style lang="scss" scoped>
@import 'theme';

.network {
    display: flex;
    flex-direction: row;
    flex-grow: 1;
    justify-content: stretch;
    overflow-y: auto;
}

.sidebar {
    height: 100%;
    display: flex;
    flex-direction: column;
    background: $profile;
    color: $text-contrast;
    width: 25rem;
    overflow: hidden;
}

.logo {
    width: 100%;
    max-height: 8rem;
    object-fit: contain;
    box-sizing: border-box;
    padding: 1rem 2rem;
    border-bottom: solid 1px $highlight-hint;
    margin: 0;
    filter: $logo-highlight;
}

.sites {
    display: grid;
    grid-gap: 1rem;
    padding: 1rem;
    margin: 0;
    grid-template-columns: repeat(2, .5fr);
    overflow-y: auto;
}

@media(max-width: $breakpoint) {
    .network {
        flex-direction: column;
    }

    .sidebar {
        height: 20rem;
        overflow: hidden;
    }

    .sites {
        grid-template-columns: repeat(auto-fit, minmax(15rem, .5fr));
        overflow-y: auto;
    }
}
</style>
