<template>
  <main>
    <br>
    <center>
      <div class="is-6 column">
        <scroll-load
          class='category_widget__box'
          @loadNext='fetchData'
          :loading='loading'
          query-selector='.admin_users'
          :padding-bottom='100'
        >
          <h1>Users that have made Kaverti possible</h1>
          <div class="card" v-for='user in users' :key='"user-row" + user.username' v-show="user && user.contributor">
            <header class="card-header">
              <figure class="image is-48x48">
                <avatar-icon :user="user"/>
              </figure>
              <p class="card-header-title">
                <router-link :to='"/user/" + user.username' style="color: #000000;">{{user.username + ' '}}</router-link>
              </p>
            </header>
            <div class="card-content">
              <div class="content" v-if="user.description">
                {{user.description}}<br/>
                <time v-bind:datetime="user.createdAt">{{user.createdAt | formatDate}}</time>
              </div>
              <div class="content" v-if="!user.description">
                This user doesn't have a user description yet!<br/>
                <time v-bind:datetime="user.createdAt">{{user.createdAt | formatDate}}</time>
              </div>
            </div>
            <footer class="card-footer">
              <router-link :to='"/user/" + user.username' class="card-footer-item">View Profile</router-link>
            </footer>
          </div>
        </scroll-load>
      </div>
      <p name='fade' mode='out-in'>
      <center><loading-message key='loading' v-if='loading'></loading-message></center>
      <center><div class='overlay_message' v-if='!loading && !users.length'>
        Something went wrong while loading the users, check your internet connection, or check the <a href="https://status.troplo.com">Service Status</a>
      </div></center></p>
    </center>
  </main>
</template>
<script>
import LoadingMessage from '../LoadingMessage';
import AvatarIcon from '../AvatarIcon';
import throttle from 'lodash.throttle';
import AjaxErrorHandler from '../../assets/js/errorHandler';

export default {
  name: 'UserList',
  components: {
    LoadingMessage,
    AvatarIcon
  },
  data () {
    return {
      search: '',
      users: [],

      loading: true,
      offset: 0,
      limit: 15,

      roleOptions: [
        { name: 'Admins', value: 'admin' },
        { name: 'Users', value: 'user' }
      ],
      roleSelected: ['admin', 'user'],

      tableSort: {
        column: 'username',
        sort: 'desc'
      }
    }
  },
  methods: {
    fetchData () {
      if(this.offset === null) return;

      let url = process.env.VUE_APP_APIENDPOINT + process.env.VUE_APP_APIVERSION + `/` + `users/contributors?
					sort=${this.tableSort.column}
					&order=${this.tableSort.sort}
					&offset=${this.offset}
				`;
      if(this.roleSelected.length === 1) {
        url += '&role=' + this.roleSelected[0];
      }
      if(this.search.length) {
        url += '&search=' + encodeURIComponent(this.search.trim());
      }

      this.loading = true;
      this.axios
        .get(url)
        .then(res => {
          this.users.push(...res.data);
          this.loading = /*loading =*/ false;

          //If returned data is less than the limit
          //then there must be no more pages to paginate
          if(res.data.length < this.limit) {
            this.offset = null;
          } else {
            this.offset+= this.limit;
          }
        })
        .catch(e => {
          AjaxErrorHandler(this.$store)(e);
          this.loading = /*loading =*/ false;
        });
    },
    resetFetchData () {
      this.offset = 0;
      this.users = [];

      this.fetchData();
    }
  },
  getNewerUsers () {
    this.loadingNewer = true

    this.axios
      .get(process.env.VUE_APP_APIENDPOINT + process.env.VUE_APP_APIVERSION + '/' + 'users' + '?limit=' + this.newUsers)
      .then(res => {
        this.loadingNewer = false
        this.newUsers = 0

        this.threads.unshift(...res.data.Threads)
      })
      .catch((e) => {
        this.loadingNewer = false
        AjaxErrorHandler(this.$store)(e)
      })
  },
  mounted () {
    this.fetchData();
  },
  watch: {
    tableSort: 'resetFetchData',
    roleSelected: 'resetFetchData',
    search: throttle(function () {
      this.resetFetchData();
    }, 200)
  }
}
</script>
