<template>
  <div>
    <div>
      <SquareTop :static="true" :staticBannerData="staticSquareBanners" :staticSwiperData="staticSquarePreferredRepos">
      </SquareTop>
    </div>
    <div class="ui container">
      <SearchBar :static="true" :staticTopicsData="staticSquareTopics" ref="searchBarRef" type="square" :sort="``"
        :searchValue="reposListQurey" :topic="``" @change="searchBarChange"></SearchBar>
    </div>
    <div class="recommend-repos-c">
      <RecommendRepos :static="true" :staticSwiperData="staticSquareRecommendRepos"></RecommendRepos>
      <a name="search"></a>
    </div>
    <div class="ui container">
      <div class="ui grid">
        <div class="computer only ui two wide computer column">
          <ReposFilters ref="reposFiltersRef" @change="filtersChange"></ReposFilters>
        </div>
        <div class="ui sixteen wide mobile twelve wide tablet ten wide computer column">
          <ReposList ref="reposListRef" :sort="reposListSortType" :q="reposListQurey" :topic="reposListTopic"
            :page="page" :pageSize="pageSize" :pageSizes="pageSizes" @current-change="currentChange"
            @size-change="sizeChange">
          </ReposList>
        </div>
        <div class="computer only ui four wide computer column">
          <div>
            <ActiveUsers></ActiveUsers>
          </div>
          <div class="active-org-c">
            <ActiveOrgs></ActiveOrgs>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import SquareTop from '../components/SquareTop.vue';
import SearchBar from '../components/SearchBar.vue';
import RecommendRepos from '../components/RecommendRepos.vue';
import ReposFilters from '../components/ReposFilters.vue';
import ReposList from '../components/ReposList.vue';
import ActiveUsers from '../components/ActiveUsers.vue';
import ActiveOrgs from '../components/ActiveOrgs.vue';
import { getUrlSearchParams } from '~/utils';

const staticSquareBanners = JSON.stringify(window.staticSquareBanners || []);
const staticSquarePreferredRepos = window.staticSquarePreferredRepos || [];
const staticSquareTopics = JSON.stringify(window.staticSquareTopics || []);
const staticSquareRecommendRepos = window.staticSquareRecommendRepos || [];

export default {
  data() {
    return {
      reposListSortType: 'mostpopular',
      reposListQurey: '',
      reposListTopic: '',

      page: 1,
      pageSize: 15,
      pageSizes: [15, 30, 50],

      staticSquareBanners: staticSquareBanners,
      staticSquarePreferredRepos: staticSquarePreferredRepos,
      staticSquareTopics: staticSquareTopics,
      staticSquareRecommendRepos: staticSquareRecommendRepos,
    };
  },
  components: {
    SquareTop,
    SearchBar,
    RecommendRepos,
    ReposFilters,
    ReposList,
    ActiveUsers,
    ActiveOrgs,
  },
  methods: {
    filtersChange(condition) {
      this.page = 1;
      this.reposListSortType = condition.key;
      this.search();
    },
    searchBarChange(params) {
      this.page = 1;
      this.reposListQurey = params.q || '';
      this.reposListTopic = params.topic || '';
      this.search();
    },
    currentChange({ page, pageSize }) {
      this.page = page;
      this.search();
    },
    sizeChange({ page, pageSize }) {
      this.page = 1;
      this.pageSize = pageSize;
      this.search();
    },
    search() {
      window.location.href = `/explore/repos/square?q=${this.reposListQurey.trim()}&sort=${this.reposListSortType}&topic=${this.reposListTopic.trim()}&page=${this.page}&pageSize=${this.pageSize}`;
    }
  },
  beforeMount() {
    const urlParams = getUrlSearchParams();
    this.reposListQurey = urlParams.q || '';
    this.reposListTopic = urlParams.topic || '';
    this.reposListSortType = urlParams.sort || 'mostpopular';
    this.page = Number(urlParams.page) || 1;
    this.pageSize = this.pageSizes.indexOf(Number(urlParams.pageSize)) >= 0 ? Number(urlParams.pageSize) : 15;
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.reposFiltersRef.setDefaultFilter(this.reposListSortType);
      this.$refs.searchBarRef.setDefaultSearch({
        q: this.reposListQurey,
        topic: this.reposListTopic,
      });
      const urlParams = getUrlSearchParams();
      const page = Number(urlParams.page) || 1;
      const reposListSortType = urlParams.sort;
      if (page != 1 || reposListSortType) {
        window.location.href = '#search';
      }
      this.$refs.reposListRef.search();
    });
    window.addEventListener('pageshow', function (e) {
      if (e.persisted) {
        window.location.reload();
      }
    }, false);
  },
  beforeDestroy() { },
};
</script>

<style scoped lang="less">
.recommend-repos-c {
  margin: 0 0 54px;
}

.active-org-c {
  margin-top: 32px;
}
</style>
