<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>
    <section v-if="!$store.state.experimentsStore.marketplace" class="hero is-info is-large">
      <div class="hero-body">
        <div class="container">
          <h2>
            Kaverti Marketplace
          </h2>
          <h1>
            The Kaverti Marketplace is coming soon
          </h1>
          <h2 v-if="$store.state.developerMode">
            However, you're currently opted into Experiments, you can enable the Marketplace experiment in User Settings.
          </h2>
        </div>
      </div>
    </section>
    <section v-if='$store.state.experimentsStore.marketplace' class="hero is-info">
      <div class="hero-body" style="padding: 1rem 1rem !important;">
        <div class="mobile-container">
          <div class="container">
            <p style="text-align: center;">Teams are currently in development, expect missing features.</p>
          </div>
        </div>
      </div>
    </section>
    <div class="section" v-if="$store.state.experimentsStore.marketplace">
      <div class="">
        <div class="column">
          <b-button tag="router-link"
                    to="/marketplace/create"
                    type="is-link is-primary">
            Create Marketplace Item
          </b-button>
        </div>
        <tab-view
            :tabs='["Hats", "Faces", "Shirts", "Pants"]'
            v-model="showSettingTab"
            padding='true'
            slot='main'
        >
        </tab-view>
        <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.picture">
                  <img v-if="user.picture === 'default' && $store.state.theme === 'light' && user.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.approved" width="128px" height="128px" src="https://cdn.kaverti.com/teams/unknown-dark.png">
                  <img v-if="$store.state.theme === 'light' && !user.approved" width="128px" height="128px" src="https://cdn.kaverti.com/teams/pending-light.png">
                  <img v-if="$store.state.theme === 'dark' && !user.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.id">{{user.name}}</router-link></p>
                  <p class="subtitle is-6">Created by <router-link :to="'/u/' + user.User.username"> @{{user.User.username}}</router-link></p>
                </div>
              </div>

              <div class="content limit">
                {{user.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'>
        No items to display at the moment.
      </div></center></p>
    </div>
  </main>
</template>
<script>
import LoadingMessage from '../LoadingMessage';
import ScrollLoad from '../ScrollLoad';
import TabView from '../TabView'
import throttle from 'lodash.throttle';
import AjaxErrorHandler from '../../assets/js/errorHandler';

export default {
  name: 'Marketplace',
  components: {
    LoadingMessage,
    ScrollLoad,
    TabView
  },
  data () {
    return {
      search: '',
      users: [],

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

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

      tableSort: {
        column: 'username',
        sort: 'desc'
      }
    }
  },
  computed: {
    showSettingTab: {
      get() {
        return this.marketplaceTabs
      },
      set(index) {
        this.marketplaceTabs = index
      }
    }
  },
  methods: {
    fetchData () {
      if(this.offset === null) return;

      let url = process.env.VUE_APP_APIENDPOINT + process.env.VUE_APP_APIVERSION + '/' + 'marketplace/' + this.showSettingTab + `?
					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.Items);
          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 + '/' + 'marketplace/shirts' + '?limit=' + this.newUsers)
      .then(res => {
        this.loadingNewer = false
        this.newUsers = 0

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