<template>
  <AppLayout title="Edit Exam Type">
    <div class="space-y-6">
      <!-- Header -->
      <div class="flex items-center justify-between">
        <div>
          <h1 class="text-3xl font-bold tracking-tight">Edit Exam Type</h1>
          <p class="text-muted-foreground">
            Update exam type information and weight
          </p>
        </div>
      </div>

      <!-- Form Card -->
      <Card>
        <CardHeader>
          <CardTitle>Exam Type Information</CardTitle>
          <CardDescription>
            Update the exam type details and weight percentage
          </CardDescription>
        </CardHeader>
        <CardContent>
          <form @submit.prevent="submit" class="space-y-6">
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
              <div class="space-y-2">
                <Label for="academic_year_id">Academic Year *</Label>
                <Select v-model="form.academic_year_id">
                  <SelectTrigger :class="{ 'border-destructive': form.errors.academic_year_id }">
                    <SelectValue placeholder="Select academic year" />
                  </SelectTrigger>
                  <SelectContent>
                    <SelectItem 
                      v-for="academicYear in academicYears" 
                      :key="academicYear.id" 
                      :value="academicYear.id.toString()"
                    >
                      {{ academicYear.name }}
                    </SelectItem>
                  </SelectContent>
                </Select>
                <p v-if="form.errors.academic_year_id" class="text-sm text-destructive">
                  {{ form.errors.academic_year_id }}
                </p>
              </div>

              <div class="space-y-2">
                <Label for="name">Exam Type Name *</Label>
                <Input
                  id="name"
                  v-model="form.name"
                  type="text"
                  placeholder="e.g., Midterm Exam"
                  :class="{ 'border-destructive': form.errors.name }"
                />
                <p v-if="form.errors.name" class="text-sm text-destructive">
                  {{ form.errors.name }}
                </p>
              </div>

              <div class="space-y-2">
                <Label for="weight">Weight (%) *</Label>
                <Input
                  id="weight"
                  v-model="form.weight"
                  type="number"
                  min="0"
                  max="100"
                  step="0.01"
                  placeholder="e.g., 30.00"
                  :class="{ 'border-destructive': form.errors.weight }"
                />
                <p v-if="form.errors.weight" class="text-sm text-destructive">
                  {{ form.errors.weight }}
                </p>
                <p class="text-sm text-muted-foreground">
                  Weight percentage for calculating final grades (0-100%)
                </p>
              </div>

              <div class="space-y-2">
                <Label>Status</Label>
                <div class="flex items-center space-x-2">
                  <Checkbox 
                    id="is_active" 
                    v-model:checked="form.is_active"
                  />
                  <Label for="is_active" class="text-sm font-normal">
                    Active exam type
                  </Label>
                </div>
                <p class="text-sm text-muted-foreground">
                  Only active exam types can be used for creating results
                </p>
              </div>

              <div class="space-y-2 md:col-span-2">
                <Label for="description">Description</Label>
                <Textarea
                  id="description"
                  v-model="form.description"
                  placeholder="Enter exam type description (optional)"
                  rows="3"
                  maxlength="500"
                  :class="{ 'border-destructive': form.errors.description }"
                />
                <p v-if="form.errors.description" class="text-sm text-destructive">
                  {{ form.errors.description }}
                </p>
                <p class="text-sm text-muted-foreground">
                  Optional description (max 500 characters)
                </p>
              </div>
            </div>

            <!-- Exam Type Preview -->
            <div v-if="form.name && form.academic_year_id && form.weight" class="mt-6 p-4 bg-muted rounded-lg">
              <h3 class="font-medium mb-2">Exam Type Preview</h3>
              <div class="space-y-1 text-sm">
                <p><strong>Name:</strong> {{ form.name }}</p>
                <p><strong>Academic Year:</strong> {{ getSelectedAcademicYearName() }}</p>
                <p><strong>Weight:</strong> {{ form.weight }}%</p>
                <p><strong>Status:</strong> {{ form.is_active ? 'Active' : 'Inactive' }}</p>
                <p v-if="form.description"><strong>Description:</strong> {{ form.description }}</p>
              </div>
            </div>

            <!-- Form Actions -->
            <div class="flex items-center justify-end space-x-3 pt-6 border-t">
              <Button variant="outline" type="button" as-child>
                <Link :href="route('exam-types.index')">
                  Cancel
                </Link>
              </Button>
              <Button type="submit" :disabled="form.processing">
                <span v-if="form.processing">Updating...</span>
                <span v-else>Update Exam Type</span>
              </Button>
            </div>
          </form>
        </CardContent>
      </Card>
    </div>
  </AppLayout>
</template>

<script setup lang="ts">
import { useForm, Link } from '@inertiajs/vue3'
import AppLayout from '@/layouts/AppLayout.vue'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { Checkbox } from '@/components/ui/checkbox'
import { Textarea } from '@/components/ui/textarea'
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'

interface ExamType {
  id: number
  academic_year_id: number
  name: string
  weight: number
  description?: string
  is_active: boolean
}

interface AcademicYear {
  id: number
  name: string
}

const props = defineProps<{
  examType: ExamType
  academicYears: AcademicYear[]
}>()

const form = useForm({
  academic_year_id: props.examType.academic_year_id.toString(),
  name: props.examType.name,
  weight: props.examType.weight.toString(),
  description: props.examType.description || '',
  is_active: props.examType.is_active,
})

const getSelectedAcademicYearName = () => {
  const academicYear = props.academicYears.find(ay => ay.id.toString() === form.academic_year_id)
  return academicYear?.name || ''
}

const submit = () => {
  form.put(route('exam-types.update', props.examType.id))
}
</script>
