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

defineProps({
    expenses: { type: Object, required: true },
    filters: { type: Object, default: () => ({}) },
});

const money = (n) =>
    new Intl.NumberFormat('es-MX', { style: 'currency', currency: 'MXN' }).format(Number(n || 0));
</script>

<template>
    <Head title="Gastos — Control Ganadero" />

    <AuthenticatedLayout>
        <template #header><h3 class="mb-0">Gastos</h3></template>
        <template #breadcrumb>
            <li class="breadcrumb-item"><Link :href="route('dashboard')">Inicio</Link></li>
            <li class="breadcrumb-item active">Gastos</li>
        </template>

        <div class="card">
            <div class="card-header">
                <h3 class="card-title">Materiales y costos</h3>
                <div class="card-tools">
                    <Link :href="route('admin.ranch.expenses.create')" class="btn btn-sm btn-success">
                        <i class="fa-solid fa-plus me-1"></i> Nuevo gasto
                    </Link>
                </div>
            </div>
            <div class="card-body table-responsive p-0">
                <table class="table table-hover mb-0">
                    <thead>
                        <tr>
                            <th>Fecha</th>
                            <th>Material</th>
                            <th>Cantidad</th>
                            <th>Costo unit.</th>
                            <th>Total</th>
                            <th>Lote</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr v-for="e in expenses.data" :key="e.id">
                            <td>{{ e.expense_date }}</td>
                            <td>{{ e.material_type?.name }}</td>
                            <td>{{ e.quantity }}</td>
                            <td>{{ money(e.unit_cost) }}</td>
                            <td><strong>{{ money(e.total_cost) }}</strong></td>
                            <td>{{ e.fattening_lot?.name || '—' }}</td>
                        </tr>
                        <tr v-if="!expenses.data?.length">
                            <td colspan="6" class="text-center text-muted py-4">Sin gastos</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </AuthenticatedLayout>
</template>
