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

defineOptions({
  layout: AppLayout,
})

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

const page = usePage()
const role = page.props.auth?.user?.role ?? 'user'

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

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

// Get badge variant based on status
const getStatusVariant = (status: string) => {
  return status === 'done' ? 'default' : 'secondary'
}
</script>

<template>
  <Head title="Sprint Backlogs" />
  <div class="p-6">
    <!-- Header and actions -->
    <div class="flex justify-between">
      <h1 class="text-2xl font-bold mb-4">Sprint Backlogs</h1>
      <div class="flex space-x-2">
        <Button variant="secondary" as-child>
          <Link href="/product-backlogs">
            Functional User Stories
          </Link>
        </Button>
        <Button variant="secondary" as-child>
          <Link href="/product-backlogs/non-functional-requirements">
            Non-Functional Requirements
          </Link>
        </Button>
      </div>
    </div>

    <!-- Sprint Backlogs Table -->
    <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">Project</th>
          <th class="p-2 border">Sprint</th>
          <th class="p-2 border">Updated at</th>
        </tr>
      </thead>
      <tbody>
        <!-- Loop over project_sprints -->
        <tr v-for="(item, index) in sprint_backlogs.data" :key="item.id">
          <td class="p-2 border">{{ getRowNumber(index) }}</td>
          <td class="p-2 border">{{ item.project?.name }}</td>
          <td class="p-2 border">{{ item.sprint?.name }}</td>
          <td class="p-2 border">{{ item.updated_at }}</td>
        </tr>

        <!-- Show message if no data -->
        <tr v-if="sprint_backlogs.data.length === 0">
          <td colspan="6" class="p-4 border text-center text-gray-500">
            No sprint assignments found.
          </td>
        </tr>
      </tbody>
    </table>

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

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

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

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

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