<template>
  <AppLayout title="Create Academic Year">
    <div class="space-y-6">
      <!-- Header -->
      <div class="flex items-center justify-between">
        <div>
          <h1 class="text-3xl font-bold tracking-tight">Create Academic Year</h1>
          <p class="text-muted-foreground">
            Add a new academic year to the system
          </p>
        </div>
      </div>

      <!-- Form Card -->
      <Card>
        <CardHeader>
          <CardTitle>Academic Year Information</CardTitle>
          <CardDescription>
            Enter the details for the new academic year
          </CardDescription>
        </CardHeader>
        <CardContent>
          <form @submit.prevent="submit" class="space-y-6">
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
              <!-- Academic Year Name -->
              <div class="space-y-2">
                <Label for="name">Academic Year Name *</Label>
                <Input
                  id="name"
                  v-model="form.name"
                  type="text"
                  placeholder="e.g., 2024-2025"
                  :class="{ 'border-destructive': form.errors.name }"
                />
                <p v-if="form.errors.name" class="text-sm text-destructive">
                  {{ form.errors.name }}
                </p>
                <p class="text-sm text-muted-foreground">
                  Enter the academic year name (e.g., "2024-2025" or "2024")
                </p>
              </div>

              <!-- Status -->
              <div class="space-y-2">
                <Label for="is_active">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">
                    Set as active academic year
                  </Label>
                </div>
                <p class="text-sm text-muted-foreground">
                  Only one academic year can be active at a time
                </p>
              </div>

              <!-- Start Date -->
              <div class="space-y-2">
                <Label for="start_date">Start Date *</Label>
                <Input
                  id="start_date"
                  v-model="form.start_date"
                  type="date"
                  :class="{ 'border-destructive': form.errors.start_date }"
                />
                <p v-if="form.errors.start_date" class="text-sm text-destructive">
                  {{ form.errors.start_date }}
                </p>
              </div>

              <!-- End Date -->
              <div class="space-y-2">
                <Label for="end_date">End Date *</Label>
                <Input
                  id="end_date"
                  v-model="form.end_date"
                  type="date"
                  :class="{ 'border-destructive': form.errors.end_date }"
                />
                <p v-if="form.errors.end_date" class="text-sm text-destructive">
                  {{ form.errors.end_date }}
                </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('academic-years.index')">
                  Cancel
                </Link>
              </Button>
              <Button type="submit" :disabled="form.processing">
                <span v-if="form.processing">Creating...</span>
                <span v-else>Create Academic Year</span>
              </Button>
            </div>
          </form>
        </CardContent>
      </Card>
    </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'
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 { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'

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

const form = useForm({
  name: '',
  start_date: '',
  end_date: '',
  is_active: false
})

const submit = () => {
  form.post(route('academic-years.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>
