<script setup>
import UsersTable from "@/components/displays/superuser/tables/usersTable.vue";

let props = defineProps(["setCustomerFilter", "hideSearch", "autoLoad"]);
import { defineProps } from "vue";
import { useRouter } from "vue-router";
import {
  isLoaded,
  isLoading,
  list,
  loadList,
  metaCurrentPage,
  metaItemsPerPage,
  metaTotalItems,
  setEndpoint,
  setMetaItemsPerPage,
  setPage,
  metaSearch,
  search,
  setFilter,
  setOrder,
  hideSearchField,
  setHideSearchField,
} from "@/components/pagination/paginatedList.vue";
import PaginationNavigation from "@/components/displays/pagination/PaginationNavigation.vue";

import LoadButtonWhileAwait from "@/components/request/LoadButtonWhileAwait.vue";
import PaginationDisplay from "@/components/displays/pagination/PaginationDisplay.vue";
import OrdersTable from "@/components/displays/department/pos/orders/ordersTable.vue";
const router = useRouter();
setEndpoint("/users", false);
setFilter("customer_number", 0, false);

// Hide the search field, if the hideSearch prop is set
if (props.hideSearch) {
  setHideSearchField(true);
}

if (props.autoLoad) {
  loadList();
}

</script>

<template>
  <input @input="search($event.target.value)" class="input" type="text" placeholder="Søg efter bruger" v-if="!hideSearchField"/>
  <PaginationDisplay :metaItemsPerPage="metaItemsPerPage" :loadFunction="loadList" :isLoading="isLoading" :setMetaItemsPerPage="setMetaItemsPerPage">
    <template #paginationColumns>
      <!-- Sort by created_at -->
      <div class="column is-narrow my-3">
        <label class="label">Rækkefølge</label>
        <div class="control">
          <div class="select">
            <select @change="setOrder('created_at', $event.target.value); loadList();">
              <option value="asc">Ascending (Oldest first)</option>
              <option value="desc" selected>Descending (Newest first)</option>
            </select>
          </div>
        </div>
      </div>
    </template>
  </PaginationDisplay>
  <UsersTable :objects="list" />
  <PaginationNavigation :currentPage="metaCurrentPage" :totalPages="Math.ceil(metaTotalItems / metaItemsPerPage)" :loadFunction="loadList" :setPage="setPage" :isLoading="isLoading" />
  <LoadButtonWhileAwait class="is-dark" :isLoading="isLoading" :loadFunction="loadList" icon="fas fa-sync-alt">Reload</LoadButtonWhileAwait>
</template>

<style scoped>

</style>