<script setup>
import AppLayout from "@/Layouts/AppLayout.vue";
import { Link, router } from "@inertiajs/vue3";
import { ref, computed, watch } from "vue";
import DataTableServer from "@/Components/DataTable/DataTableServer.vue";

const props = defineProps({
  segment: Object,
  contracts: Object, // LengthAwarePaginator payload from Laravel
  clients: Array, // Full list of clients with contracts in this segment
});

// Initialize search and client filter from current URL so inputs reflect server filters
const urlParams = new URLSearchParams(window.location.search);
const search = ref(urlParams.get("search") || "");
const initialClient = urlParams.get("client") || urlParams.get("client_id") || "";
const selectedClient = ref(initialClient);

// Column definitions for the server-driven table
const columns = [
  { key: "reference", label: "Pogodba", sortable: true },
  { key: "client_case", label: "Primer" },
  { key: "client", label: "Stranka" },
  { key: "type", label: "Vrsta" },
  { key: "start_date", label: "Začetek", sortable: true },
  { key: "end_date", label: "Konec", sortable: true },
  { key: "account", label: "Stanje", align: "right" },
];

// Build client options from the full list provided by the server, so the dropdown isn't limited by current filters
const clientOptions = computed(() => {
  const list = Array.isArray(props.clients) ? props.clients : [];
  const opts = list.map((c) => ({
    value: c.uuid || "",
    label: c.name || "(neznana stranka)",
  }));
  return opts.sort((a, b) => (a.label || "").localeCompare(b.label || ""));
});

// React to client selection changes by visiting the same route with updated query
watch(selectedClient, (val) => {
  const query = { search: search.value };
  if (val) {
    query.client = val;
  }
  router.get(
    route("segments.show", { segment: props.segment?.id ?? props.segment }),
    query,
    { preserveState: true, preserveScroll: true, only: ["contracts"], replace: true }
  );
});

function formatDate(value) {
  if (!value) {
    return "-";
  }
  const d = new Date(value);
  if (isNaN(d)) return value;
  return `${String(d.getDate()).padStart(2, "0")}.${String(d.getMonth() + 1).padStart(
    2,
    "0"
  )}.${d.getFullYear()}`;
}

function formatCurrency(value) {
  if (value === null || value === undefined) return "-";
  const n = Number(value);
  if (isNaN(n)) return String(value);
  return (
    n.toLocaleString("sl-SI", { minimumFractionDigits: 2, maximumFractionDigits: 2 }) +
    " €"
  );
}
</script>

<template>
  <AppLayout :title="`Segment: ${segment?.name || ''}`">
    <template #header> </template>
    <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
      <div class="bg-white overflow-hidden shadow-xl sm:rounded-lg p-4">
        <h2 class="text-lg">{{ segment.name }}</h2>
        <div class="text-sm text-gray-600 mb-4">{{ segment?.description }}</div>

        <!-- Filters -->
        <div class="mb-4 flex flex-col sm:flex-row sm:items-end gap-3">
          <div class="flex-1">
            <label class="block text-sm font-medium text-gray-700 mb-1">Stranka</label>
            <div class="flex items-center gap-2">
              <select
                v-model="selectedClient"
                class="block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 text-sm"
              >
                <option value="">Vse stranke</option>
                <option
                  v-for="opt in clientOptions"
                  :key="opt.value || opt.label"
                  :value="opt.value"
                >
                  {{ opt.label }}
                </option>
              </select>
              <button
                type="button"
                class="text-sm text-gray-600 hover:text-gray-900"
                @click="selectedClient = ''"
                v-if="selectedClient"
              >
                Počisti
              </button>
            </div>
          </div>
        </div>

        <DataTableServer
          :columns="columns"
          :rows="contracts?.data || []"
          :meta="contracts || {}"
          v-model:search="search"
          route-name="segments.show"
          :route-params="{ segment: segment?.id ?? segment }"
          :query="{ client: selectedClient || undefined }"
          :only-props="['contracts']"
          :page-size-options="[10, 25, 50]"
          empty-text="Ni pogodb v tem segmentu."
          row-key="uuid"
        >
          <!-- Primer (client_case) cell with link when available -->
          <template #cell-client_case="{ row }">
            <Link
              v-if="row.client_case?.uuid"
              :href="
                route('clientCase.show', {
                  client_case: row.client_case.uuid,
                  segment: segment?.id ?? segment,
                })
              "
              class="text-indigo-600 hover:underline"
            >
              {{ row.client_case?.person?.full_name || "Primer stranke" }}
            </Link>
            <span v-else>{{ row.client_case?.person?.full_name || "-" }}</span>
          </template>

          <!-- Stranka (client) name -->
          <template #cell-client="{ row }">
            {{ row.client?.person?.full_name || "-" }}
          </template>

          <!-- Vrsta (type) -->
          <template #cell-type="{ row }">
            {{ row.type?.name || "-" }}
          </template>

          <!-- Dates formatted -->
          <template #cell-start_date="{ row }">
            {{ formatDate(row.start_date) }}
          </template>
          <template #cell-end_date="{ row }">
            {{ formatDate(row.end_date) }}
          </template>

          <!-- Account balance formatted -->
          <template #cell-account="{ row }">
            <div class="text-right">
              {{ formatCurrency(row.account?.balance_amount) }}
            </div>
          </template>
        </DataTableServer>
      </div>
    </div>
  </AppLayout>
</template>
