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

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

const from = ref(props.filters.from);
const to = ref(props.filters.to);

const apply = () => {
    router.get(route('admin.ranch.reports.index'), { from: from.value, to: to.value }, { preserveState: true });
};

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

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

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

        <div class="card mb-3">
            <div class="card-body row g-3 align-items-end">
                <div class="col-md-3">
                    <label class="form-label">Desde</label>
                    <input v-model="from" type="date" class="form-control" />
                </div>
                <div class="col-md-3">
                    <label class="form-label">Hasta</label>
                    <input v-model="to" type="date" class="form-control" />
                </div>
                <div class="col-md-2">
                    <button type="button" class="btn btn-primary" @click="apply">Filtrar</button>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-4">
                <div class="small-box text-bg-danger">
                    <div class="inner">
                        <h3 class="fs-4">{{ money(report.total_expenses) }}</h3>
                        <p>Gastos</p>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="small-box text-bg-success">
                    <div class="inner">
                        <h3 class="fs-4">{{ money(report.total_income) }}</h3>
                        <p>Ingresos por ventas</p>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="small-box" :class="report.net_profit >= 0 ? 'text-bg-primary' : 'text-bg-warning'">
                    <div class="inner">
                        <h3 class="fs-4">{{ money(report.net_profit) }}</h3>
                        <p>Utilidad neta</p>
                    </div>
                </div>
            </div>
        </div>

        <div class="card">
            <div class="card-header"><h3 class="card-title">Detalle</h3></div>
            <div class="card-body">
                <p><strong>Ventas:</strong> {{ report.sales_count ?? 0 }}</p>
                <p><strong>Muertes:</strong> {{ report.mortality_count ?? 0 }}</p>
                <h5 class="mt-3">Gastos por categoría</h5>
                <ul class="mb-0">
                    <li v-for="(amount, cat) in report.expenses_by_category || {}" :key="cat">
                        {{ report.expenses_by_category_labels?.[cat] || cat }}: {{ money(amount) }}
                    </li>
                    <li v-if="!Object.keys(report.expenses_by_category || {}).length" class="text-muted">Sin gastos en el periodo</li>
                </ul>
            </div>
        </div>
    </AuthenticatedLayout>
</template>
