<script setup>
import { ref, onMounted, watch } from 'vue';
import DataTable from 'primevue/datatable';
import Column from 'primevue/column';
import InputText from 'primevue/inputtext';
import Select from 'primevue/select';
import Tag from 'primevue/tag';
import api from '../api.js';

const rows = ref([]);
const productGroups = ref([]);
const loading = ref(true);
const filters = ref({ product_group_id: null, search: '' });

async function loadFilters() {
  const { data } = await api.getProductGroups();
  productGroups.value = data;
}

async function loadStock() {
  loading.value = true;
  const params = {};
  if (filters.value.product_group_id) params.product_group_id = filters.value.product_group_id;
  if (filters.value.search) params.search = filters.value.search;
  const { data } = await api.getStock(params);
  rows.value = data;
  loading.value = false;
}

let searchTimer = null;
watch(filters, () => {
  clearTimeout(searchTimer);
  searchTimer = setTimeout(loadStock, 250);
}, { deep: true });

function availabilitySeverity(qty) {
  if (qty <= 0) return 'danger';
  if (qty < 100) return 'warn';
  return 'success';
}

onMounted(async () => {
  await loadFilters();
  await loadStock();
});
</script>

<template>
  <div class="page module-accent" style="--accent-color: var(--module-stock)">
    <div class="page-header">
      <div>
        <h1>Stock</h1>
        <p class="page-subtitle">On-hand and allocated stock by style and warehouse bin.</p>
      </div>
    </div>

    <div class="filter-bar card-block">
      <Select
        v-model="filters.product_group_id"
        :options="productGroups"
        optionLabel="name"
        optionValue="id"
        placeholder="All Product Groups"
        showClear
        style="min-width: 220px"
      />
      <InputText v-model="filters.search" placeholder="Search by style code or name..." style="min-width: 260px" />
    </div>

    <div class="card-block">
      <DataTable :value="rows" :loading="loading" dataKey="id" stripedRows responsiveLayout="scroll">
        <Column field="style_code" header="Style Code">
          <template #body="{ data }"><span class="mono">{{ data.style_code }}</span></template>
        </Column>
        <Column field="style_name" header="Style Name" />
        <Column field="colour" header="Colour" />
        <Column field="warehouse" header="Warehouse" />
        <Column field="bin_location" header="Bin">
          <template #body="{ data }"><span class="mono">{{ data.bin_location }}</span></template>
        </Column>
        <Column field="quantity_on_hand" header="On Hand">
          <template #body="{ data }"><span class="mono">{{ data.quantity_on_hand }}</span></template>
        </Column>
        <Column field="quantity_allocated" header="Allocated">
          <template #body="{ data }"><span class="mono">{{ data.quantity_allocated }}</span></template>
        </Column>
        <Column field="quantity_available" header="Available">
          <template #body="{ data }">
            <Tag :value="data.quantity_available" :severity="availabilitySeverity(data.quantity_available)" />
          </template>
        </Column>
        <template #empty>
          <div class="empty-state">No stock records match these filters.</div>
        </template>
      </DataTable>
    </div>
  </div>
</template>
