<template>
  <Layout>
    <div class="py-6">
      <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
        <PartnerNavigation title="Students" :subtitle="`${partner.center.name} - ${partner.name}`" />
        
        <Breadcrumb :breadcrumbs="[
          { name: 'Partner', icon: 'fas fa-user-tie', url: '/partner/dashboard' },
          { name: 'Students', icon: 'fas fa-users' }
        ]" />

        <!-- Filters -->
        <div class="bg-white shadow mb-6">
          <div class="px-6 py-4">
            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
              <div>
                <label class="block text-sm font-medium text-gray-700 mb-2">Search Students</label>
                <input v-model="filterForm.search" @input="applyFilters" type="text" placeholder="Name, email, or state code" class="w-full border-gray-300 rounded-md">
              </div>
              
              <div class="flex items-end">
                <button @click="clearFilters" class="bg-gray-500 hover:bg-gray-600 text-white font-bold py-2 px-4 rounded">
                  <i class="fas fa-refresh mr-1"></i>
                  Clear Filters
                </button>
              </div>
            </div>
          </div>
        </div>

        <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
          <div class="p-6">
            <div class="flex justify-between items-center mb-6">
              <h2 class="text-xl font-bold text-gray-900">Center Students</h2>
              <Link href="/partner/students/create" class="bg-orange-600 hover:bg-orange-700 text-white font-bold py-2 px-4 rounded">
                <i class="fas fa-plus mr-2"></i>Add New Student
              </Link>
            </div>

            <div class="overflow-x-auto">
              <table class="min-w-full divide-y divide-gray-200">
                <thead class="bg-gray-50">
                  <tr>
                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Name</th>
                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">State Code</th>
                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Email</th>
                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Phone</th>
                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Courses</th>
                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Enrollment Date</th>
                  </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                  <tr v-for="student in students.data" :key="student.id">
                    <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">{{ student.name }}</td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ student.state_code }}</td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ student.email }}</td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ student.phone }}</td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                      <div v-if="student.courses && student.courses.length > 0">
                        <span v-for="course in student.courses.slice(0, 2)" :key="course.id" class="inline-block bg-gray-100 text-gray-800 text-xs px-2 py-1 rounded mr-1 mb-1">
                          {{ course.name }}
                        </span>
                        <span v-if="student.courses.length > 2" class="text-xs text-gray-500">
                          +{{ student.courses.length - 2 }} more
                        </span>
                      </div>
                      <span v-else class="text-gray-400">No courses</span>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ formatDate(student.enrollment_date) }}</td>
                  </tr>
                </tbody>
              </table>
            </div>

            <!-- Pagination -->
            <div v-if="students.links && students.links.length > 3" class="mt-6">
              <nav class="flex justify-center">
                <div class="flex space-x-1">
                  <template v-for="link in students.links" :key="link.label">
                    <Link v-if="link.url" :href="link.url" 
                          :class="link.active ? 'bg-orange-500 text-white' : 'bg-white text-gray-700 hover:bg-gray-50'" 
                          class="px-3 py-2 text-sm border border-gray-300 rounded"
                          v-html="link.label">
                    </Link>
                    <span v-else 
                          class="px-3 py-2 text-sm border border-gray-300 rounded bg-gray-100 text-gray-400"
                          v-html="link.label">
                    </span>
                  </template>
                </div>
              </nav>
            </div>
          </div>
        </div>
      </div>
    </div>
  </Layout>
</template>

<script setup>
import Layout from '@/Components/Layout.vue'
import PartnerNavigation from '@/Components/PartnerNavigation.vue'
import Breadcrumb from '@/Components/Breadcrumb.vue'
import { Link, router } from '@inertiajs/vue3'
import { ref } from 'vue'

const props = defineProps({
  students: Object,
  partner: Object,
  filters: Object
})

const filterForm = ref({
  search: props.filters?.search || ''
})

const applyFilters = () => {
  router.get('/partner/students', filterForm.value, {
    preserveState: true,
    preserveScroll: true
  })
}

const clearFilters = () => {
  filterForm.value = { search: '' }
  router.get('/partner/students')
}

const formatDate = (dateString) => {
  if (!dateString) return 'N/A'
  return new Date(dateString).toLocaleDateString()
}
</script>