<template>
  <AppLayout title="Create Student">
    <template #header>
      <h2 class="font-semibold text-xl text-gray-800 leading-tight">
        Create New Student
      </h2>
    </template>

    <div class="py-12">
      <div class="max-w-4xl mx-auto sm:px-6 lg:px-8">
        <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
          <div class="p-6 bg-white border-b border-gray-200">
            <form @submit.prevent="submit">
              <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <!-- Personal Information -->
                <div class="col-span-2">
                  <h3 class="text-lg font-medium text-gray-900 mb-4">Personal Information</h3>
                </div>

                <!-- First Name -->
                <div>
                  <label for="first_name" class="block text-sm font-medium text-gray-700">First Name</label>
                  <input
                    id="first_name"
                    v-model="form.first_name"
                    type="text"
                    class="mt-1 block w-full border-gray-300 rounded-md shadow-sm focus:border-indigo-500 focus:ring-indigo-500"
                    :class="{ 'border-red-500': form.errors.first_name }"
                    required
                  />
                  <div v-if="form.errors.first_name" class="mt-2 text-sm text-red-600">
                    {{ form.errors.first_name }}
                  </div>
                </div>

                <!-- Last Name -->
                <div>
                  <label for="last_name" class="block text-sm font-medium text-gray-700">Last Name</label>
                  <input
                    id="last_name"
                    v-model="form.last_name"
                    type="text"
                    class="mt-1 block w-full border-gray-300 rounded-md shadow-sm focus:border-indigo-500 focus:ring-indigo-500"
                    :class="{ 'border-red-500': form.errors.last_name }"
                    required
                  />
                  <div v-if="form.errors.last_name" class="mt-2 text-sm text-red-600">
                    {{ form.errors.last_name }}
                  </div>
                </div>

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

                <!-- Phone -->
                <div>
                  <label for="phone" class="block text-sm font-medium text-gray-700">Phone</label>
                  <input
                    id="phone"
                    v-model="form.phone"
                    type="tel"
                    class="mt-1 block w-full border-gray-300 rounded-md shadow-sm focus:border-indigo-500 focus:ring-indigo-500"
                    :class="{ 'border-red-500': form.errors.phone }"
                  />
                  <div v-if="form.errors.phone" class="mt-2 text-sm text-red-600">
                    {{ form.errors.phone }}
                  </div>
                </div>

                <!-- Date of Birth -->
                <div>
                  <label for="date_of_birth" class="block text-sm font-medium text-gray-700">Date of Birth</label>
                  <input
                    id="date_of_birth"
                    v-model="form.date_of_birth"
                    type="date"
                    class="mt-1 block w-full border-gray-300 rounded-md shadow-sm focus:border-indigo-500 focus:ring-indigo-500"
                    :class="{ 'border-red-500': form.errors.date_of_birth }"
                    required
                  />
                  <div v-if="form.errors.date_of_birth" class="mt-2 text-sm text-red-600">
                    {{ form.errors.date_of_birth }}
                  </div>
                </div>

                <!-- Gender -->
                <div>
                  <label for="gender" class="block text-sm font-medium text-gray-700">Gender</label>
                  <select
                    id="gender"
                    v-model="form.gender"
                    class="mt-1 block w-full border-gray-300 rounded-md shadow-sm focus:border-indigo-500 focus:ring-indigo-500"
                    :class="{ 'border-red-500': form.errors.gender }"
                    required
                  >
                    <option value="">Select Gender</option>
                    <option value="male">Male</option>
                    <option value="female">Female</option>
                    <option value="other">Other</option>
                  </select>
                  <div v-if="form.errors.gender" class="mt-2 text-sm text-red-600">
                    {{ form.errors.gender }}
                  </div>
                </div>

                <!-- Address -->
                <div class="col-span-2">
                  <label for="address" class="block text-sm font-medium text-gray-700">Address</label>
                  <textarea
                    id="address"
                    v-model="form.address"
                    rows="3"
                    class="mt-1 block w-full border-gray-300 rounded-md shadow-sm focus:border-indigo-500 focus:ring-indigo-500"
                    :class="{ 'border-red-500': form.errors.address }"
                  ></textarea>
                  <div v-if="form.errors.address" class="mt-2 text-sm text-red-600">
                    {{ form.errors.address }}
                  </div>
                </div>

                <!-- Academic Information -->
                <div class="col-span-2 mt-6">
                  <h3 class="text-lg font-medium text-gray-900 mb-4">Academic Information</h3>
                </div>

                <!-- Student ID -->
                <div>
                  <label for="student_id" class="block text-sm font-medium text-gray-700">Student ID</label>
                  <input
                    id="student_id"
                    v-model="form.student_id"
                    type="text"
                    class="mt-1 block w-full border-gray-300 rounded-md shadow-sm focus:border-indigo-500 focus:ring-indigo-500"
                    :class="{ 'border-red-500': form.errors.student_id }"
                    required
                  />
                  <div v-if="form.errors.student_id" class="mt-2 text-sm text-red-600">
                    {{ form.errors.student_id }}
                  </div>
                </div>

                <!-- Admission Date -->
                <div>
                  <label for="admission_date" class="block text-sm font-medium text-gray-700">Admission Date</label>
                  <input
                    id="admission_date"
                    v-model="form.admission_date"
                    type="date"
                    class="mt-1 block w-full border-gray-300 rounded-md shadow-sm focus:border-indigo-500 focus:ring-indigo-500"
                    :class="{ 'border-red-500': form.errors.admission_date }"
                    required
                  />
                  <div v-if="form.errors.admission_date" class="mt-2 text-sm text-red-600">
                    {{ form.errors.admission_date }}
                  </div>
                </div>

                <!-- Guardian Selection -->
                <div class="col-span-2" v-if="guardians.length > 0">
                  <label for="guardian_ids" class="block text-sm font-medium text-gray-700">Guardians (Optional)</label>
                  <div class="mt-2 space-y-2">
                    <div v-for="guardian in guardians" :key="guardian.id" class="flex items-center">
                      <input
                        :id="`guardian_${guardian.id}`"
                        v-model="form.guardian_ids"
                        :value="guardian.id"
                        type="checkbox"
                        class="h-4 w-4 text-indigo-600 focus:ring-indigo-500 border-gray-300 rounded"
                      />
                      <label :for="`guardian_${guardian.id}`" class="ml-2 block text-sm text-gray-900">
                        {{ guardian.user.first_name }} {{ guardian.user.last_name }} ({{ guardian.user.email }})
                      </label>
                    </div>
                  </div>
                  <div v-if="form.errors.guardian_ids" class="mt-2 text-sm text-red-600">
                    {{ form.errors.guardian_ids }}
                  </div>
                </div>

                <!-- Active Status -->
                <div class="col-span-2">
                  <div class="flex items-center">
                    <input
                      id="is_active"
                      v-model="form.is_active"
                      type="checkbox"
                      class="h-4 w-4 text-indigo-600 focus:ring-indigo-500 border-gray-300 rounded"
                    />
                    <label for="is_active" class="ml-2 block text-sm text-gray-900">
                      Active Student
                    </label>
                  </div>
                  <div v-if="form.errors.is_active" class="mt-2 text-sm text-red-600">
                    {{ form.errors.is_active }}
                  </div>
                </div>
              </div>

              <!-- Form Actions -->
              <div class="mt-6 flex items-center justify-end space-x-3">
                <Link :href="route('students.index')" class="bg-gray-300 hover:bg-gray-400 text-gray-800 font-bold py-2 px-4 rounded">
                  Cancel
                </Link>
                <button
                  type="submit"
                  :disabled="form.processing"
                  class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded disabled:opacity-50"
                >
                  <span v-if="form.processing">Creating...</span>
                  <span v-else>Create Student</span>
                </button>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
  </AppLayout>
</template>

<script setup lang="ts">
import { useForm, Link, usePage } from '@inertiajs/vue3'
import { useToast } from '@/composables/useToast'
import { onMounted } from 'vue'
import AppLayout from '@/layouts/AppLayout.vue'

const props = defineProps({
  academicYears: {
    type: Array,
    default: () => []
  },
  guardians: {
    type: Array,
    default: () => []
  }
})

const page = usePage()
const toast = useToast()

const form = useForm({
  first_name: '',
  last_name: '',
  email: '',
  phone: '',
  date_of_birth: '',
  gender: '',
  address: '',
  student_id: '',
  admission_date: new Date().toISOString().split('T')[0],
  guardian_ids: [],
  is_active: true
})

const submit = () => {
  form.post(route('students.store'), {
    onError: (errors) => {
      // Show toast for validation errors
      const firstError = Object.values(errors)[0]
      const errorMessage = Array.isArray(firstError) ? firstError[0] : firstError
      toast.error(errorMessage || 'Please check the form for errors.')
    }
  })
}

// Show success toast if redirected with success message
onMounted(() => {
  if (page.props.success) {
    toast.success(page.props.success)
  }
})
</script>
