<template>
  <Layout>
    <div class="py-6">
      <div class="max-w-4xl mx-auto sm:px-6 lg:px-8">
        <PartnerNavigation title="Enroll New Student" :subtitle="partner.center.name" />
        
        <Breadcrumb :breadcrumbs="[
          { name: 'Partner', icon: 'fas fa-user-tie', url: '/partner/dashboard' },
          { name: 'Students', icon: 'fas fa-users', url: '/partner/students' },
          { name: 'Add Student', icon: 'fas fa-user-plus' }
        ]" />
        
        <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
          <div class="p-6 text-gray-900">
            <h2 class="text-xl font-bold mb-6">Student Information</h2>

            <form @submit.prevent="submit">
              <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
                <div>
                  <label class="block text-sm font-medium text-gray-700 mb-2">Student Name</label>
                  <input 
                    v-model="form.name" 
                    type="text" 
                    class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500"
                    required
                  >
                  <div v-if="errors.name" class="text-red-600 text-sm mt-1">{{ errors.name }}</div>
                </div>

                <div>
                  <label class="block text-sm font-medium text-gray-700 mb-2">State Code</label>
                  <input 
                    v-model="form.state_code" 
                    type="text" 
                    class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500"
                    required
                  >
                  <div v-if="errors.state_code" class="text-red-600 text-sm mt-1">{{ errors.state_code }}</div>
                </div>

                <div>
                  <label class="block text-sm font-medium text-gray-700 mb-2">Email</label>
                  <input 
                    v-model="form.email" 
                    type="email" 
                    class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500"
                    required
                  >
                  <div v-if="errors.email" class="text-red-600 text-sm mt-1">{{ errors.email }}</div>
                </div>

                <div>
                  <label class="block text-sm font-medium text-gray-700 mb-2">Phone</label>
                  <input 
                    v-model="form.phone" 
                    type="text" 
                    class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500"
                    required
                  >
                  <div v-if="errors.phone" class="text-red-600 text-sm mt-1">{{ errors.phone }}</div>
                </div>

                <div class="md:col-span-2">
                  <label class="block text-sm font-medium text-gray-700 mb-2">Enrollment Date</label>
                  <input 
                    v-model="form.enrollment_date" 
                    type="date" 
                    class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500"
                    required
                  >
                  <div v-if="errors.enrollment_date" class="text-red-600 text-sm mt-1">{{ errors.enrollment_date }}</div>
                </div>
              </div>

              <div class="mb-6">
                <h3 class="text-lg font-medium text-gray-900 mb-4">Course Enrollments</h3>
                <div v-for="(course, index) in form.courses" :key="index" class="bg-gray-50 p-4 rounded-lg mb-4">
                  <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                    <div>
                      <label class="block text-sm font-medium text-gray-700 mb-2">Course</label>
                      <select v-model="course.course_id" class="w-full border-gray-300 rounded-md" required>
                        <option value="">Select Course</option>
                        <option v-for="availableCourse in courses" :key="availableCourse.id" :value="availableCourse.id">
                          {{ availableCourse.name }} - ₦{{ availableCourse.price }}
                        </option>
                      </select>
                    </div>
                    
                    <div>
                      <label class="block text-sm font-medium text-gray-700 mb-2">Payment Status</label>
                      <select v-model="course.payment_status" class="w-full border-gray-300 rounded-md" required>
                        <option value="pending">Pending</option>
                        <option value="paid">Paid</option>
                        <option value="overdue">Overdue</option>
                      </select>
                    </div>
                    
                    <div class="flex items-end">
                      <button type="button" @click="removeCourse(index)" class="bg-red-500 hover:bg-red-600 text-white px-3 py-2 rounded">
                        <i class="fas fa-trash"></i>
                      </button>
                    </div>
                  </div>
                </div>
                
                <button type="button" @click="addCourse" class="bg-green-500 hover:bg-green-600 text-white font-bold py-2 px-4 rounded">
                  <i class="fas fa-plus mr-2"></i>Add Course
                </button>
                <div v-if="errors.courses" class="text-red-600 text-sm mt-1">{{ errors.courses }}</div>
              </div>

              <button 
                type="submit" 
                :disabled="true"
                class="w-full bg-orange-600 hover:bg-orange-700 text-white font-bold py-2 px-4 rounded disabled:opacity-50 cursor-not-allowed"
              >
              <i class="fas fa-lock mr-1"></i>
                {{ processing ? 'Enrolling...' : 'Enroll Student' }}
              </button>
              <!-- <button 
                type="submit" 
                :disabled="processing"
                class="w-full bg-orange-600 hover:bg-orange-700 text-white font-bold py-2 px-4 rounded disabled:opacity-50"
              >
                {{ processing ? 'Enrolling...' : 'Enroll Student' }}
              </button> -->
            </form>
          </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, useForm } from '@inertiajs/vue3'

defineProps({
  courses: Array,
  partner: Object,
  errors: Object
})

const form = useForm({
  name: '',
  state_code: '',
  email: '',
  phone: '',
  enrollment_date: new Date().toISOString().split('T')[0],
  courses: [{ course_id: '', payment_status: 'pending' }]
})

const addCourse = () => {
  form.courses.push({ course_id: '', payment_status: 'pending' })
}

const removeCourse = (index) => {
  if (form.courses.length > 1) {
    form.courses.splice(index, 1)
  }
}

const submit = () => {
  form.post('/partner/students/store')
}

const { processing } = form
</script>