@extends('layouts.app')

@section('content')
    <x-common.page-breadcrumb :pageTitle="$title ?? 'Laporan Laba Rugi'" />

    @php
        $formatRupiah = fn ($value) => 'Rp ' . number_format((float) $value, 0, ',', '.');
        $__profitLossRound1000 = function ($amount) {
            $amount = max((float) ($amount ?? 0), 0);
            return $amount > 0 ? (float) (ceil($amount / 1000) * 1000) : 0;
        };

        // Pendapatan disamakan dengan aturan invoice/kasir: nilai tagihan tampil dibulatkan ke kelipatan Rp1.000.
        // Biaya tidak ikut dibulatkan agar HPP/expense tetap sesuai data pengeluaran asli.
        $revenue = $__profitLossRound1000($summary['revenue'] ?? 0);
        $purchaseCost = (float) ($summary['purchase_cost'] ?? 0);
        $operationalExpense = (float) ($summary['operational_expense'] ?? 0);
        $salaryExpense = (float) ($summary['salary_expense'] ?? 0);
        $totalOperatingExpense = (float) ($summary['total_operating_expense'] ?? ($operationalExpense + $salaryExpense));
        $grossProfit = $revenue - $purchaseCost;
        $netProfit = $grossProfit - $totalOperatingExpense;
        $totalOrders = (int) ($summary['total_orders'] ?? 0);

        $periodLabel = \Carbon\Carbon::parse($startDate)->format('d M Y') . ' s/d ' . \Carbon\Carbon::parse($endDate)->format('d M Y');
    @endphp

    <div class="mb-6 rounded-2xl border border-brand-200 bg-brand-50 p-5 dark:border-brand-900/40 dark:bg-brand-900/10">
        <div class="flex items-start gap-4">
            <div class="flex h-10 w-10 items-center justify-center rounded-xl bg-brand-500 text-white">
                <svg class="size-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z"/>
                </svg>
            </div>

            <div class="flex-1">
                <div class="flex flex-wrap items-center gap-2">
                    <h4 class="text-sm font-semibold text-gray-900 dark:text-white/90">Ringkasan Laba Rugi</h4>
                    <span class="rounded-full bg-white/80 px-2 py-0.5 text-xs font-medium text-gray-700 dark:bg-white/10 dark:text-white/70">
                        Periode: {{ $periodLabel }}
                    </span>
                </div>

                <p class="mt-2 text-sm text-gray-700 dark:text-white/70">
                    Laporan ini memakai <span class="font-semibold">penjualan dari order berbayar</span>,
                    <span class="font-semibold">pembelian dari tabel purchases</span>,
                    <span class="font-semibold">beban operasional dari expenses</span>,
                    dan <span class="font-semibold">gaji dari salaries</span> bila tersedia.
                </p>
            </div>
        </div>
    </div>

    <div class="mb-6 rounded-2xl border border-gray-200 bg-white p-5 dark:border-gray-800 dark:bg-white/[0.03]">
        <form method="GET" class="grid grid-cols-1 items-end gap-4 sm:grid-cols-2 lg:grid-cols-4">
            <div class="w-full">
                <x-form.date-picker
                    name="start_date"
                    label="Dari Tanggal"
                    placeholder="Pilih tanggal"
                    :defaultDate="$startDate"
                />
            </div>

            <div class="w-full">
                <x-form.date-picker
                    name="end_date"
                    label="Sampai Tanggal"
                    placeholder="Pilih tanggal"
                    :defaultDate="$endDate"
                />
            </div>

            <div class="flex w-full gap-2 lg:col-span-2">
                <button type="submit" class="h-11 flex-1 rounded-lg bg-brand-500 px-5 text-sm font-medium text-white hover:bg-brand-600">
                    Filter
                </button>
                <a href="{{ route('reports.profit-loss') }}" class="h-11 flex items-center justify-center rounded-lg border border-gray-300 px-4 text-sm font-medium text-gray-700 hover:bg-gray-50 dark:border-gray-700 dark:text-gray-300">
                    Reset
                </a>
            </div>
        </form>
    </div>

    <div class="mb-6 grid grid-cols-1 gap-4 sm:grid-cols-2 xl:grid-cols-5">
        <div class="rounded-2xl border border-gray-200 bg-white p-5 dark:border-gray-800 dark:bg-white/[0.03]">
            <div class="text-sm font-medium text-gray-500 dark:text-gray-400">Total Penjualan</div>
            <div class="mt-2 text-2xl font-bold text-gray-800 dark:text-white/90">{{ $formatRupiah($revenue) }}</div>
            <div class="mt-2 text-xs text-gray-500 dark:text-gray-400">{{ $totalOrders }} order berbayar</div>
        </div>

        <div class="rounded-2xl border border-gray-200 bg-white p-5 dark:border-gray-800 dark:bg-white/[0.03]">
            <div class="text-sm font-medium text-gray-500 dark:text-gray-400">Pembelian / HPP</div>
            <div class="mt-2 text-2xl font-bold text-error-600 dark:text-error-400">{{ $formatRupiah($purchaseCost) }}</div>
            <div class="mt-2 text-xs text-gray-500 dark:text-gray-400">Diambil dari purchases</div>
        </div>

        <div class="rounded-2xl border border-gray-200 bg-white p-5 dark:border-gray-800 dark:bg-white/[0.03]">
            <div class="text-sm font-medium text-gray-500 dark:text-gray-400">Laba Kotor</div>
            <div class="mt-2 text-2xl font-bold {{ $grossProfit < 0 ? 'text-error-600 dark:text-error-400' : 'text-success-600 dark:text-success-400' }}">
                {{ $formatRupiah($grossProfit) }}
            </div>
            <div class="mt-2 text-xs text-gray-500 dark:text-gray-400">Penjualan - Pembelian/HPP</div>
        </div>

        <div class="rounded-2xl border border-gray-200 bg-white p-5 dark:border-gray-800 dark:bg-white/[0.03]">
            <div class="text-sm font-medium text-gray-500 dark:text-gray-400">Beban Operasional</div>
            <div class="mt-2 text-2xl font-bold text-error-600 dark:text-error-400">{{ $formatRupiah($totalOperatingExpense) }}</div>
            <div class="mt-2 text-xs text-gray-500 dark:text-gray-400">Expense + Gaji</div>
        </div>

        <div class="rounded-2xl border border-gray-200 bg-white p-5 dark:border-gray-800 dark:bg-white/[0.03]">
            <div class="text-sm font-medium text-gray-500 dark:text-gray-400">Laba Bersih</div>
            <div class="mt-2 text-2xl font-bold {{ $netProfit < 0 ? 'text-error-600 dark:text-error-400' : 'text-success-600 dark:text-success-400' }}">
                {{ $formatRupiah($netProfit) }}
            </div>
            <div class="mt-2 text-xs text-gray-500 dark:text-gray-400">Laba kotor - Beban operasional</div>
        </div>
    </div>

    <div class="overflow-hidden rounded-2xl border border-gray-200 bg-white dark:border-gray-800 dark:bg-white/[0.03]">
        <div class="border-b border-gray-200 px-5 py-4 dark:border-gray-800">
            <h3 class="text-base font-semibold text-gray-800 dark:text-white/90">Detail Perhitungan</h3>
        </div>

        <div class="overflow-x-auto">
            <table class="w-full">
                <tbody class="divide-y divide-gray-200 dark:divide-gray-800">
                    <tr class="bg-gray-50 dark:bg-gray-900/30">
                        <td colspan="2" class="px-5 py-3 text-sm font-semibold text-gray-800 dark:text-white/90">
                            Pendapatan
                        </td>
                    </tr>
                    <tr>
                        <td class="px-5 py-4 text-sm text-gray-700 dark:text-gray-300">Penjualan</td>
                        <td class="px-5 py-4 text-right text-sm font-semibold text-success-600 dark:text-success-400">
                            {{ $formatRupiah($revenue) }}
                        </td>
                    </tr>

                    <tr class="bg-gray-50 dark:bg-gray-900/30">
                        <td colspan="2" class="px-5 py-3 text-sm font-semibold text-gray-800 dark:text-white/90">
                            Beban Pokok / Pembelian
                        </td>
                    </tr>
                    <tr>
                        <td class="px-5 py-4 text-sm text-gray-700 dark:text-gray-300">Pembelian / HPP</td>
                        <td class="px-5 py-4 text-right text-sm font-semibold text-error-600 dark:text-error-400">
                            ({{ $formatRupiah($purchaseCost) }})
                        </td>
                    </tr>

                    <tr class="bg-gray-100 dark:bg-gray-800/50">
                        <td class="px-5 py-4 text-sm font-bold text-gray-800 dark:text-white/90">Laba Kotor</td>
                        <td class="px-5 py-4 text-right text-sm font-bold {{ $grossProfit < 0 ? 'text-error-600 dark:text-error-400' : 'text-success-600 dark:text-success-400' }}">
                            {{ $formatRupiah($grossProfit) }}
                        </td>
                    </tr>

                    <tr class="bg-gray-50 dark:bg-gray-900/30">
                        <td colspan="2" class="px-5 py-3 text-sm font-semibold text-gray-800 dark:text-white/90">
                            Beban Operasional
                        </td>
                    </tr>
                    <tr>
                        <td class="px-5 py-4 text-sm text-gray-700 dark:text-gray-300">Expense Operasional</td>
                        <td class="px-5 py-4 text-right text-sm font-semibold text-error-600 dark:text-error-400">
                            ({{ $formatRupiah($operationalExpense) }})
                        </td>
                    </tr>
                    <tr>
                        <td class="px-5 py-4 text-sm text-gray-700 dark:text-gray-300">Gaji</td>
                        <td class="px-5 py-4 text-right text-sm font-semibold text-error-600 dark:text-error-400">
                            ({{ $formatRupiah($salaryExpense) }})
                        </td>
                    </tr>

                    <tr class="bg-gray-100 dark:bg-gray-800/50">
                        <td class="px-5 py-4 text-base font-bold text-gray-900 dark:text-white">Laba Bersih</td>
                        <td class="px-5 py-4 text-right text-base font-bold {{ $netProfit < 0 ? 'text-error-600 dark:text-error-400' : 'text-success-600 dark:text-success-400' }}">
                            {{ $formatRupiah($netProfit) }}
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>

    <div class="mt-12 border-t border-gray-200 pt-6 text-center text-xs text-gray-500 dark:border-gray-800 dark:text-gray-400">
        <p>© {{ date('Y') }} <span class="font-semibold text-gray-700 dark:text-gray-300">aplikasipercetakan.com</span>. All rights reserved.</p>
    </div>
@endsection