<template>
  <div>
    <div class="ui container">
      <SearchBar ref="searchBarRef" type="search" :sort="reposListSortType" :searchValue="reposListQurey"
        :topic="reposListTopic" @change="searchBarChange"></SearchBar>
    </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">
          </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 SearchBar from '../components/SearchBar.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';

export default {
  data() {
    return {
      reposListSortType: 'mostpopular',
      reposListQurey: '',
      reposListTopic: '',
    };
  },
  components: {
    SearchBar,
    ReposFilters,
    ReposList,
    ActiveUsers,
    ActiveOrgs,
  },
  methods: {
    filtersChange(condition) {
      this.reposListSortType = condition.key;
      this.search();
    },
    searchBarChange(params) {
      this.reposListQurey = params.q || '';
      this.reposListTopic = params.topic || '';
      this.search();
    },
    search() {
      this.$nextTick(() => {
        this.$refs.reposListRef.search();
      });
    }
  },
  mounted() {
    const urlParams = getUrlSearchParams();
    this.reposListQurey = urlParams.q || '';
    this.reposListTopic = urlParams.topic || '';
    this.reposListSortType = urlParams.sort || 'mostpopular';
    this.search();
    this.$refs.reposFiltersRef.setDefaultFilter(this.reposListSortType);
    this.$refs.searchBarRef.setDefaultSearch({
      q: this.reposListQurey,
      topic: this.reposListTopic,
    });
  },
  beforeDestroy() { },
};
</script>

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

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