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

defineProps({
    materialTypes: { type: Array, default: () => [] },
    lots: { type: Array, default: () => [] },
    categories: { type: Object, default: () => ({}) },
});

const form = useForm({
    material_type_id: '',
    expense_date: new Date().toISOString().slice(0, 10),
    quantity: '',
    unit_cost: '',
    fattening_lot_id: '',
    notes: '',
});

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

<template>
    <Head title="Nuevo gasto" />

    <AuthenticatedLayout>
        <template #header><h3 class="mb-0">Registrar gasto</h3></template>
        <template #breadcrumb>
            <li class="breadcrumb-item"><Link :href="route('dashboard')">Inicio</Link></li>
            <li class="breadcrumb-item"><Link :href="route('admin.ranch.expenses.index')">Gastos</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-6">
                        <label class="form-label">Material</label>
                        <select v-model="form.material_type_id" class="form-select" required>
                            <option value="">Seleccione...</option>
                            <option v-for="m in materialTypes" :key="m.id" :value="m.id">
                                {{ m.name }} ({{ categories[m.category] || m.category }})
                            </option>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">Fecha</label>
                        <input v-model="form.expense_date" type="date" class="form-control" required />
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">Lote (opcional)</label>
                        <select v-model="form.fattening_lot_id" class="form-select">
                            <option value="">—</option>
                            <option v-for="l in lots" :key="l.id" :value="l.id">{{ l.name }}</option>
                        </select>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">Cantidad</label>
                        <input v-model="form.quantity" type="number" step="0.001" class="form-control" required />
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">Costo unitario</label>
                        <input v-model="form.unit_cost" type="number" step="0.01" class="form-control" required />
                    </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.expenses.index')" class="btn btn-secondary ms-2">Cancelar</Link>
                </div>
            </form>
        </div>
    </AuthenticatedLayout>
</template>
