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

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

                    <Icon
                        v-if="network.url"
                        icon="new-tab"
                        class="icon-href"
                    />
                </a>

                <p class="description">{{ network.description }}</p>
            </div>

            <template v-if="sites.length">
                <h3 class="heading">Sites</h3>

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

            <Releases
                :releases="releases"
                :context="network.name"
            />
        </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';

.header {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: top;
    margin: 0 0 2rem 0;
}

.title {
    display: inline-flex;
    align-items: top;
    margin: 0 1rem 0 0;

    &:hover .icon {
        fill: $primary;
    }
}

.logo {
    width: 20rem;
    max-height: 8rem;
    object-fit: contain;
    margin: 0 .5rem 0 0;
}

.sites {
    display: grid;
    grid-gap: 1rem;
    margin: 0 0 2rem 0;
}

.sites {
    grid-template-columns: repeat(auto-fit, 15rem);
}

@media(max-width: $breakpoint) {
    .sites {
        grid-template-columns: repeat(auto-fit, minmax(15rem, 1fr));
    }
}
</style>
