@extends('layouts.app')

@section('content')
    <x-common.page-breadcrumb pageTitle="{{ $title ?? 'Jurnal Umum' }}" />

    @php
        $formatRupiah = fn ($value) => 'Rp ' . number_format((float) $value, 0, ',', '.');
        $entries = $entries ?? collect();
        $summary = $summary ?? [];
        $accounts = $accounts ?? collect();
    @endphp

    <div class="mb-6 grid grid-cols-1 gap-4 sm:grid-cols-2 xl:grid-cols-4">
        <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">Jumlah Baris</div>
            <div class="mt-2 text-2xl font-bold text-gray-800 dark:text-white/90">{{ number_format((int) ($summary['total_rows'] ?? 0), 0, ',', '.') }}</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">Jumlah Jurnal</div>
            <div class="mt-2 text-2xl font-bold text-gray-800 dark:text-white/90">{{ number_format((int) ($summary['total_journals'] ?? 0), 0, ',', '.') }}</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">Total Debit</div>
            <div class="mt-2 text-2xl font-bold text-success-600 dark:text-success-400">{{ $formatRupiah($summary['total_debit'] ?? 0) }}</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">Total Kredit</div>
            <div class="mt-2 text-2xl font-bold text-error-600 dark:text-error-400">{{ $formatRupiah($summary['total_kredit'] ?? 0) }}</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 gap-4 lg:grid-cols-5">
            <div>
                <label class="mb-1.5 block text-sm font-medium text-gray-700 dark:text-gray-300">Tanggal Awal</label>
                <input type="date" name="start_date" value="{{ $startDate }}"
                    class="h-11 w-full rounded-xl border border-gray-300 bg-transparent px-4 text-sm text-gray-800 focus:border-brand-300 focus:outline-none focus:ring-4 focus:ring-brand-500/10 dark:border-gray-700 dark:bg-gray-800 dark:text-white/90">
            </div>

            <div>
                <label class="mb-1.5 block text-sm font-medium text-gray-700 dark:text-gray-300">Tanggal Akhir</label>
                <input type="date" name="end_date" value="{{ $endDate }}"
                    class="h-11 w-full rounded-xl border border-gray-300 bg-transparent px-4 text-sm text-gray-800 focus:border-brand-300 focus:outline-none focus:ring-4 focus:ring-brand-500/10 dark:border-gray-700 dark:bg-gray-800 dark:text-white/90">
            </div>

            <div class="lg:col-span-2">
                <label class="mb-1.5 block text-sm font-medium text-gray-700 dark:text-gray-300">Filter Rekening</label>
                <select name="payment_account_id"
                    class="h-11 w-full rounded-xl border border-gray-300 bg-transparent px-4 text-sm text-gray-800 focus:border-brand-300 focus:outline-none focus:ring-4 focus:ring-brand-500/10 dark:border-gray-700 dark:bg-gray-800 dark:text-white/90">
                    <option value="">Semua Rekening</option>
                    @foreach($accounts as $account)
                        <option value="{{ $account->id }}" {{ (string) $selectedAccount === (string) $account->id ? 'selected' : '' }}>
                            {{ $account->name }}
                        </option>
                    @endforeach
                </select>
            </div>

            <div class="flex items-end gap-2">
                <button type="submit"
                    class="h-11 w-full rounded-xl bg-brand-500 px-4 text-sm font-medium text-white hover:bg-brand-600">
                    Filter
                </button>
            </div>
        </form>

        <div class="mt-4 flex flex-wrap gap-2">
            <a href="{{ route('reports.general-journal.print', request()->query()) }}" target="_blank"
                class="inline-flex items-center rounded-xl bg-gray-900 px-4 py-2.5 text-sm font-medium text-white hover:bg-black dark:bg-white dark:text-gray-900 dark:hover:bg-gray-200">
                Print
            </a>
            <a href="{{ route('reports.general-journal.export', request()->query()) }}"
                class="inline-flex items-center rounded-xl bg-success-500 px-4 py-2.5 text-sm font-medium text-white hover:bg-success-600">
                Export Excel
            </a>
        </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="overflow-x-auto">
            <table class="w-full min-w-[980px]">
                <thead>
                    <tr class="border-b border-gray-200 bg-gray-50 dark:border-gray-800 dark:bg-white/[0.02]">
                        <th class="px-5 py-3 text-left text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">Tanggal</th>
                        <th class="px-5 py-3 text-left text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">Ref</th>
                        <th class="px-5 py-3 text-left text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">Keterangan</th>
                        <th class="px-5 py-3 text-left text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">Akun</th>
                        <th class="px-5 py-3 text-right text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">Debit</th>
                        <th class="px-5 py-3 text-right text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">Kredit</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-200 dark:divide-gray-800">
                    @php $lastGroup = null; @endphp

                    @forelse($entries as $entry)
                        <tr class="{{ $lastGroup !== null && $lastGroup !== $entry['group_key'] ? 'border-t-4 border-t-gray-100 dark:border-t-gray-900' : '' }}">
                            <td class="px-5 py-3 text-sm text-gray-600 dark:text-gray-300">
                                {{ \Carbon\Carbon::parse($entry['tanggal'])->format('d/m/Y') }}
                            </td>
                            <td class="px-5 py-3 text-sm font-medium text-gray-700 dark:text-gray-200">
                                {{ $entry['reference'] }}
                            </td>
                            <td class="px-5 py-3 text-sm text-gray-600 dark:text-gray-300">
                                {{ $entry['description'] }}
                            </td>
                            <td class="px-5 py-3 text-sm text-gray-800 dark:text-white/90 {{ ($entry['debit'] ?? 0) == 0 ? 'pl-10' : '' }}">
                                {{ $entry['account'] }}
                            </td>
                            <td class="px-5 py-3 text-right text-sm font-medium text-success-600 dark:text-success-400">
                                {{ ($entry['debit'] ?? 0) > 0 ? $formatRupiah($entry['debit']) : '-' }}
                            </td>
                            <td class="px-5 py-3 text-right text-sm font-medium text-error-600 dark:text-error-400">
                                {{ ($entry['kredit'] ?? 0) > 0 ? $formatRupiah($entry['kredit']) : '-' }}
                            </td>
                        </tr>
                        @php $lastGroup = $entry['group_key']; @endphp
                    @empty
                        <tr>
                            <td colspan="6" class="px-5 py-8 text-center text-sm text-gray-500 dark:text-gray-400">
                                Tidak ada data jurnal pada periode ini.
                            </td>
                        </tr>
                    @endforelse
                </tbody>
                @if($entries->isNotEmpty())
                    <tfoot>
                        <tr class="border-t border-gray-200 bg-gray-50 dark:border-gray-800 dark:bg-white/[0.02]">
                            <td colspan="4" class="px-5 py-4 text-right text-sm font-semibold text-gray-800 dark:text-white/90">Total</td>
                            <td class="px-5 py-4 text-right text-sm font-bold text-success-600 dark:text-success-400">{{ $formatRupiah($summary['total_debit'] ?? 0) }}</td>
                            <td class="px-5 py-4 text-right text-sm font-bold text-error-600 dark:text-error-400">{{ $formatRupiah($summary['total_kredit'] ?? 0) }}</td>
                        </tr>
                    </tfoot>
                @endif
            </table>
        </div>
    </div>
@endsection