<style>
.vertical-alt {
  margin: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  -ms-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}
.limit{
  margin-top: 0.5rem;
  word-break: break-all;
}
</style>
<template>
  <main>
    <div class="section">
      <div class="">
        <scroll-load
          key='user-row'
          class='columns is-multiline'
          v-if='users.length'
          :loading='loading'
          @loadNext='fetchData'
        >
          <div class="column is-4" v-for='user in users' :key='"user-row" + user.name' v-show="user && !user.hidden"><div class="card">
            <div class="card-content">
              <div class="media-left">
                <figure class="image is-128x128">
                  <img v-if="user.picture !== 'default' && user.approved" width="128px" height="128px" :src="user.Item.picture">
                  <img v-if="user.picture === 'default' && $store.state.theme === 'light' && user.Item.approved" width="128px" height="128px" src="https://cdn.kaverti.com/teams/unknown-light.png">
                  <img v-if="user.picture === 'default' && $store.state.theme === 'dark' && user.Item.approved" width="128px" height="128px" src="https://cdn.kaverti.com/teams/unknown-dark.png">
                  <img v-if="$store.state.theme === 'light' && !user.Item.approved" width="128px" height="128px" src="https://cdn.kaverti.com/teams/pending-light.png">
                  <img v-if="$store.state.theme === 'dark' && !user.Item.approved" width="128px" height="128px" src="https://cdn.kaverti.com/teams/pending-dark.png">
                </figure>
              </div>
              <br>
              <div class="media">
                <div class="media-content">
                  <p class="title is-4"><router-link :to="'/m/' + user.Item.id">{{user.Item.name}}</router-link></p>
                  <p class="subtitle is-6">Created by <router-link :to="'/u/' + user.Item.User.username"> @{{user.Item.User.username}}</router-link></p>
                </div>
              </div>

              <div class="content limit">
                {{user.Item.description | truncate(70)}}
              </div>
            </div>
          </div>
          </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'>
        This user doesn't have any purchased Marketplace Items yet.
      </div></center></p>
    </div>
  </main>
</template>
<script>
import LoadingMessage from '../LoadingMessage';
import ScrollLoad from '../ScrollLoad';
import throttle from 'lodash.throttle';
import AjaxErrorHandler from '../../assets/js/errorHandler';

export default {
  name: 'Inventory',
  components: {
    LoadingMessage,
    ScrollLoad
  },
  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 + '/' + 'user/' + this.$route.params.username + `?
					sort=${this.tableSort.column}
					&order=${this.tableSort.sort}
					&offset=${this.offset}
					&inventory=true
				`;
      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.Inventories);
          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 + '/' + 'user/' + this.$route.params.username + '?limit=' + this.newUsers + '&inventory=true')
      .then(res => {
        this.loadingNewer = false
        this.newUsers = 0

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