<script setup>
import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout.vue';
import { Head, Link, useForm } from '@inertiajs/vue3';

defineProps({
    mothers: { type: Array, default: () => [] },
    pastures: { type: Array, default: () => [] },
    genders: { type: Object, default: () => ({}) },
    earTagTypes: { type: Object, default: () => ({}) },
});

const form = useForm({
    mother_id: '',
    ear_tag: '',
    ear_tag_type: 'internal',
    gender: 'female',
    breed: '',
    calving_date: new Date().toISOString().slice(0, 10),
    birth_weight_kg: '',
    pasture_id: '',
    notes: '',
    calving_notes: '',
});

const submit = () => form.post(route('admin.ranch.calvings.store'));
</script>

<template>
    <Head title="Registrar parto" />

    <AuthenticatedLayout>
        <template #header><h3 class="mb-0">Registrar parto</h3></template>
        <template #breadcrumb>
            <li class="breadcrumb-item"><Link :href="route('dashboard')">Inicio</Link></li>
            <li class="breadcrumb-item"><Link :href="route('admin.ranch.calvings.index')">Partos</Link></li>
            <li class="breadcrumb-item active">Nuevo</li>
        </template>

        <div class="card card-success">
            <form @submit.prevent="submit">
                <div class="card-body row g-3">
                    <div class="col-md-4">
                        <label class="form-label">Madre</label>
                        <select v-model="form.mother_id" class="form-select" required>
                            <option value="">Seleccione...</option>
                            <option v-for="m in mothers" :key="m.id" :value="m.id">{{ m.ear_tag }}</option>
                        </select>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">Arete de la cría</label>
                        <input v-model="form.ear_tag" class="form-control" required />
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">Tipo arete</label>
                        <select v-model="form.ear_tag_type" class="form-select">
                            <option v-for="(label, key) in earTagTypes" :key="key" :value="key">{{ label }}</option>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">Sexo</label>
                        <select v-model="form.gender" class="form-select">
                            <option v-for="(label, key) in genders" :key="key" :value="key">{{ label }}</option>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">Fecha parto</label>
                        <input v-model="form.calving_date" type="date" class="form-control" required />
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">Peso al nacer</label>
                        <input v-model="form.birth_weight_kg" type="number" step="0.01" class="form-control" />
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">Potrero</label>
                        <select v-model="form.pasture_id" class="form-select">
                            <option value="">—</option>
                            <option v-for="p in pastures" :key="p.id" :value="p.id">{{ p.name }}</option>
                        </select>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Raza</label>
                        <input v-model="form.breed" class="form-control" />
                    </div>
                    <div class="col-12">
                        <label class="form-label">Notas</label>
                        <textarea v-model="form.calving_notes" class="form-control" rows="2" />
                    </div>
                </div>
                <div class="card-footer">
                    <button type="submit" class="btn btn-success" :disabled="form.processing">Registrar</button>
                    <Link :href="route('admin.ranch.calvings.index')" class="btn btn-secondary ms-2">Cancelar</Link>
                </div>
            </form>
        </div>
    </AuthenticatedLayout>
</template>
