<script setup>
defineProps(['objects']);
import { ref } from 'vue';
import { departments, getDepartments, isLoading, getDepartmentName} from "@/components/pagination/departmentTabs.vue";
import { getUserOrder } from "@/components/shop/Orders.vue";

const redirectUserObjectPage = (objectId) => {
  // Send the user to the object page
  window.location.href = `/user/orders/${objectId}`;
};

// Get the departments (If the departments are not already loaded)
if (departments.value.length === 0) {
  getDepartments();
}

// If the screen is mobile, set the is small variable to true
const isSmall = ref(window.innerWidth < 1024);

// When the window is resized, check if the screen is mobile
window.addEventListener('resize', () => {
  isSmall.value = window.innerWidth < 1024;
});

</script>

<template>
  <div>
  <table class="table is-fullwidth" :class="isSmall ? 'is-hidden' : ''">
    <thead>
    <tr>
      <th>ID</th>
      <th>Afdeling</th>
      <th>Reg. Nr. 1</th>
      <th>Reg. Nr. 2</th>
      <th>Reg. Nr. 3</th>
      <th>Dato</th>
      <th>Handlinger</th>
    </tr>
    </thead>
    <tbody>
    <tr v-for="object in objects" :key="object.id">
      <td>{{ object.id }}</td>
      <td>{{ getDepartmentName(object.department_id) }}</td>
      <td>{{ object.reg_1 }}</td>
      <td>{{ object.reg_2 }}</td>
      <td>{{ object.reg_3 }}</td>
      <td>{{ object.created_at }}</td>
      <td>
        <div class="buttons">
          <button class="button is-small is-dark" @click="redirectUserObjectPage(object.id)">Vis</button>
        </div>
      </td>
    </tr>
    </tbody>
    <tfoot>
    <tr>
      <td colspan="10">Viser {{ objects.length }} transaktioner</td>
    </tr>
    </tfoot>
  </table>
    <!-- Mobile cards -->
    <div class="columns is-multiline is-hidden-desktop mt-6 my-6" :class="isSmall ? '' : 'is-hidden'" v-if="objects.length > 0">
      <div class="column is-full" v-for="object in objects" :key="object.id">
        <div class="card">
          <div class="card-content">
            <div class="is-pulled-right">
              <button class="button is-small is-dark" @click="redirectUserObjectPage(object.id)">
                <span class="icon">
                  <i class="fas fa-eye"></i>
                </span>
                <span>Vis</span>
              </button>
            </div>
            <div class="content">
              <p><strong>ID:</strong> {{ object.id }}</p>
              <p><strong>Afdeling:</strong> {{ getDepartmentName(object.department_id) }}</p>
              <p><strong>Reg. Nr. 1:</strong> {{ object.reg_1 }}</p>
              <p><strong>Reg. Nr. 2:</strong> {{ object.reg_2 }}</p>
              <p><strong>Reg. Nr. 3:</strong> {{ object.reg_3 }}</p>
              <p><strong>Dato:</strong> {{ object.created_at }}</p>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- If there are no objects, show a message -->
    <div class="notification is-dark mt-6 mb-6" v-if="objects.length === 0 && isSmall">
      Der er ingen transaktioner at vise
    </div>
  </div>
</template>

<style scoped>

</style>