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

      <!-- Form Card -->
      <Card>
        <CardHeader>
          <CardTitle>Guardian Information</CardTitle>
          <CardDescription>
            Update the guardian's personal and contact information
          </CardDescription>
        </CardHeader>
        <CardContent>
          <form @submit.prevent="submit" class="space-y-6">
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
              <!-- Personal Information -->
              <div class="space-y-2">
                <Label for="first_name">First Name *</Label>
                <Input
                  id="first_name"
                  v-model="form.first_name"
                  type="text"
                  :class="{ 'border-destructive': form.errors.first_name }"
                />
                <p v-if="form.errors.first_name" class="text-sm text-destructive">
                  {{ form.errors.first_name }}
                </p>
              </div>

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

              <div class="space-y-2">
                <Label for="email">Email</Label>
                <Input
                  id="email"
                  v-model="form.email"
                  type="email"
                  :class="{ 'border-destructive': form.errors.email }"
                />
                <p v-if="form.errors.email" class="text-sm text-destructive">
                  {{ form.errors.email }}
                </p>
              </div>

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

              <div class="space-y-2">
                <Label for="occupation">Occupation</Label>
                <Input
                  id="occupation"
                  v-model="form.occupation"
                  type="text"
                  :class="{ 'border-destructive': form.errors.occupation }"
                />
                <p v-if="form.errors.occupation" class="text-sm text-destructive">
                  {{ form.errors.occupation }}
                </p>
              </div>

              <div class="space-y-2 md:col-span-2">
                <Label for="address">Address</Label>
                <Textarea
                  id="address"
                  v-model="form.address"
                  rows="3"
                  :class="{ 'border-destructive': form.errors.address }"
                />
                <p v-if="form.errors.address" class="text-sm text-destructive">
                  {{ form.errors.address }}
                </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 guardian
                  </Label>
                </div>
              </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('guardians.index')">
                  Cancel
                </Link>
              </Button>
              <Button type="submit" :disabled="form.processing">
                <span v-if="form.processing">Updating...</span>
                <span v-else">Update Guardian</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 { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'

interface Guardian {
  id: number
  first_name: string
  last_name: string
  phone: string
  address?: string
  occupation?: string
  is_active: boolean
  user?: {
    email: string
  }
}

const props = defineProps<{
  guardian: Guardian
}>()

const form = useForm({
  first_name: props.guardian.first_name || '',
  last_name: props.guardian.last_name || '',
  email: props.guardian.user?.email || '',
  phone: props.guardian.phone,
  address: props.guardian.address || '',
  occupation: props.guardian.occupation || '',
  is_active: props.guardian.is_active,
})

const submit = () => {
  form.put(route('guardians.update', props.guardian.id))
}
</script>
