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

defineProps({
    cattleOptions: { type: Array, default: () => [] },
    eventTypes: { type: Object, default: () => ({}) },
    outcomes: { type: Object, default: () => ({}) },
    materialTypes: { type: Array, default: () => [] },
});

const form = useForm({
    cattle_id: '',
    event_type: 'vaccination',
    event_date: new Date().toISOString().slice(0, 10),
    title: '',
    product_name: '',
    cost: '',
    outcome: '',
    material_type_id: '',
    notes: '',
});

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

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

    <AuthenticatedLayout>
        <template #header><h3 class="mb-0">Registrar evento de salud</h3></template>
        <template #breadcrumb>
            <li class="breadcrumb-item"><Link :href="route('dashboard')">Inicio</Link></li>
            <li class="breadcrumb-item"><Link :href="route('admin.ranch.health.index')">Salud</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">Animal</label>
                        <select v-model="form.cattle_id" class="form-select" required>
                            <option value="">Seleccione...</option>
                            <option v-for="c in cattleOptions" :key="c.id" :value="c.id">{{ c.ear_tag }}</option>
                        </select>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">Tipo</label>
                        <select v-model="form.event_type" class="form-select">
                            <option v-for="(label, key) in eventTypes" :key="key" :value="key">{{ label }}</option>
                        </select>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">Fecha</label>
                        <input v-model="form.event_date" type="date" class="form-control" required />
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Título</label>
                        <input v-model="form.title" class="form-control" required />
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">Producto</label>
                        <input v-model="form.product_name" class="form-control" />
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">Costo</label>
                        <input v-model="form.cost" type="number" step="0.01" class="form-control" />
                    </div>
                    <div v-if="form.event_type === 'illness'" class="col-md-4">
                        <label class="form-label">Resultado</label>
                        <select v-model="form.outcome" class="form-select">
                            <option value="">—</option>
                            <option v-for="(label, key) in outcomes" :key="key" :value="key">{{ label }}</option>
                        </select>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">Material salud</label>
                        <select v-model="form.material_type_id" class="form-select">
                            <option value="">—</option>
                            <option v-for="m in materialTypes" :key="m.id" :value="m.id">{{ m.name }}</option>
                        </select>
                    </div>
                    <div class="col-12">
                        <label class="form-label">Notas</label>
                        <textarea v-model="form.notes" class="form-control" rows="2" />
                    </div>
                </div>
                <div class="card-footer">
                    <button type="submit" class="btn btn-success" :disabled="form.processing">Guardar</button>
                    <Link :href="route('admin.ranch.health.index')" class="btn btn-secondary ms-2">Cancelar</Link>
                </div>
            </form>
        </div>
    </AuthenticatedLayout>
</template>
