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

        <!-- Search Filter -->
        <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 Courses</label>
                <input v-model="filterForm.search" @input="applyFilters" type="text" placeholder="Course name or description" 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 Search
                </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">Course Catalog</h2>
              <div class="text-sm text-gray-500">
                <i class="fas fa-info-circle mr-1"></i>Read-only view
              </div>
            </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">Course Name</th>
                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Description</th>
                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Price</th>
                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Duration</th>
                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Status</th>
                  </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                  <tr v-for="course in courses.data" :key="course.id">
                    <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
                      {{ course.name }}
                    </td>
                    <td class="px-6 py-4 text-sm text-gray-500">
                      {{ course.description || 'No description available' }}
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-green-600">
                      ₦{{ course.price?.toLocaleString() }}
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                      {{ course.duration_months }} month{{ course.duration_months !== 1 ? 's' : '' }}
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap">
                      <span :class="course.is_active ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800'" 
                            class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full">
                        {{ course.is_active ? 'Active' : 'Inactive' }}
                      </span>
                    </td>
                  </tr>
                </tbody>
              </table>
            </div>

            <!-- Pagination -->
            <div v-if="courses.links && courses.links.length > 3" class="mt-6">
              <nav class="flex justify-center">
                <div class="flex space-x-1">
                  <template v-for="link in courses.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({
  courses: Object,
  partner: Object,
  filters: Object
})

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

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

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