<script setup lang="ts">
import AppLayout from '@/layouts/AppLayout.vue'
import type { BreadcrumbItemType } from '@/types'
import { Button } from '@/components/ui/button';
import { router, Link, Head } from '@inertiajs/vue3';
import { ChevronLeft, ChevronRight } from 'lucide-vue-next'

defineOptions({
  layout: AppLayout,
})

const props = defineProps<{
  product_backlogs: any
  breadcrumbs: BreadcrumbItemType[]
}>()

// Calculate row number with pagination offset
const getRowNumber = (index: number) => {
  const currentPage = props.product_backlogs.meta?.current_page || 1
  const perPage = props.product_backlogs.meta?.per_page || 10
  return (currentPage - 1) * perPage + index + 1
}

// Inertia pagination function
const visitPage = (url: string | null) => {
  if (url) {
    router.visit(url)
  }
}
</script>

<template>
  <Head title="NFR Backlogs" />
  <div class="p-6">
    <div class="flex justify-between">
        <h1 class="text-2xl font-bold mb-4">Non-Functional Requirement Backlogs</h1>
        <div class="flex space-x-2">
            <Button variant="secondary">
              <Link href="/product-backlogs">
                Functional User Stories
              </Link>
            </Button>
            <Button variant="secondary">
              <Link href="/product-backlogs/sprint-backlogs">
                Sprint Backlogs
              </Link>
            </Button>
        </div>
        
    </div>

    <table class="w-full border text-left">
      <thead class="bg-gray-100 dark:bg-neutral-900">
        <tr>
          <th class="p-2 border">No</th>
          <th class="p-2 border">Projects</th>
          <th class="p-2 border">NFR</th>
          <th class="p-2 border">Description</th>
          <th class="p-2 border">Updated at</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(requirement, index) in product_backlogs.data" :key="requirement.id">
          <td class="p-2 border">{{ getRowNumber(index) }}</td>
          <td class="p-2 border">
            {{ requirement.project.name }}
          </td>
          <td class="p-2 border">{{ requirement.name }}</td>
          <td class="p-2 border">{{ requirement.description }}</td>
          <td class="p-2 border">{{ requirement.updated_at }}</td>
        </tr>
        
        <!-- Show message if no requirements -->
        <tr v-if="product_backlogs.data.length === 0">
          <td colspan="7" class="p-4 border text-center text-gray-500">
            No requirements found.
          </td>
        </tr>
      </tbody>
    </table>

    <!-- Simple Pagination -->
    <div v-if="product_backlogs.meta && product_backlogs.meta.last_page > 1" class="mt-6 flex items-center justify-center space-x-4">
      <Button
        variant="outline"
        size="sm"
        :disabled="!product_backlogs.links.prev"
        @click="visitPage(product_backlogs.links.prev)"
      >
        <ChevronLeft class="h-4 w-4" />
        Previous
      </Button>

      <span class="text-sm text-gray-700">
        Page {{ product_backlogs.meta.current_page }} of {{ product_backlogs.meta.last_page }}
      </span>

      <Button
        variant="outline"
        size="sm"
        :disabled="!product_backlogs.links.next"
        @click="visitPage(product_backlogs.links.next)"
      >
        Next
        <ChevronRight class="h-4 w-4" />
      </Button>
    </div>

    <!-- Results counter -->
    <div v-if="product_backlogs.meta" class="mt-4 text-center text-sm text-gray-600">
      Showing {{ product_backlogs.meta.from }} to {{ product_backlogs.meta.to }} of {{ product_backlogs.meta.total }} results
    </div>
  </div>
</template>

<style scoped>
button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
</style>