@extends('layouts.user')

@section('title', __('general_ledger.title'))

@section('content')
@php
    $m = fn($v)=> number_format((float)$v,2);
@endphp

<div class="space-y-2">

    {{-- Header (ULTRA THIN) --}}
    <div class="bg-white border border-slate-200 px-3 py-2 shadow-sm">
        <div class="text-[9px] font-medium uppercase tracking-wide text-slate-500">
            {{ __('general_ledger.header.section') }}
        </div>
        <div class="mt-0.5 text-base font-semibold text-slate-900 leading-tight">
            {{ __('general_ledger.header.title') }}
        </div>
    </div>

    {{-- Summary Cards (ULTRA COMPACT) --}}
    <div class="grid grid-cols-3 gap-2">

        <div class="border border-emerald-200 bg-emerald-50 px-3 py-2 shadow-sm">
            <div class="text-[9px] font-semibold uppercase text-emerald-700">
                {{ __('general_ledger.cards.debit') }}
            </div>
            <div class="text-base font-bold text-emerald-700 leading-tight">
                {{ $m($totalDebit) }}
            </div>
        </div>

        <div class="border border-rose-200 bg-rose-50 px-3 py-2 shadow-sm">
            <div class="text-[9px] font-semibold uppercase text-rose-700">
                {{ __('general_ledger.cards.credit') }}
            </div>
            <div class="text-base font-bold text-rose-700 leading-tight">
                {{ $m($totalCredit) }}
            </div>
        </div>

        <div class="border border-blue-200 bg-blue-50 px-3 py-2 shadow-sm">
            <div class="text-[9px] font-semibold uppercase text-blue-700">
                {{ __('general_ledger.cards.balance') }}
            </div>
            <div class="text-base font-bold text-blue-700 leading-tight">
                {{ $m($totalDebit - $totalCredit) }}
            </div>
        </div>

    </div>

    {{-- Filter --}}
    <div class="bg-white border border-slate-200 px-3 py-2 shadow-sm">
        <form method="GET" class="grid grid-cols-1 gap-2 text-[11px] md:grid-cols-6">

            <input type="date" name="date" value="{{ request('date') }}"
                class="border border-slate-300 px-2 py-1.5 text-[11px] outline-none focus:border-slate-500">

            <input type="number" name="month" value="{{ request('month') }}" placeholder="{{ __('general_ledger.filters.month') }}"
                class="border border-slate-300 px-2 py-1.5 text-[11px] outline-none focus:border-slate-500">

            <input type="number" name="year" value="{{ request('year') }}" placeholder="{{ __('general_ledger.filters.year') }}"
                class="border border-slate-300 px-2 py-1.5 text-[11px] outline-none focus:border-slate-500">

            <select name="user_account_id"
                class="border border-slate-300 px-2 py-1.5 text-[11px] outline-none focus:border-slate-500">
                <option value="">{{ __('general_ledger.filters.all_accounts') }}</option>
                @foreach($accounts as $a)
                    <option value="{{ $a->id }}" @selected((string)request('user_account_id')===(string)$a->id)>
                        {{ $a->name }}
                    </option>
                @endforeach
            </select>

            <input name="search" value="{{ request('search') }}" placeholder="{{ __('general_ledger.filters.search') }}"
                class="border border-slate-300 px-2 py-1.5 text-[11px] outline-none focus:border-slate-500">

            <button type="submit"
                class="bg-slate-900 px-2 py-1.5 text-[11px] font-semibold text-white hover:bg-slate-800">
                {{ __('general_ledger.filters.filter') }}
            </button>

        </form>
    </div>

    {{-- Table --}}
    <div class="overflow-hidden border border-slate-200 bg-white shadow-sm">
        <div class="overflow-x-auto">
            <table class="min-w-full text-[11px]">
                <thead class="bg-slate-100 text-slate-600">
                    <tr>
                        <th class="px-2 py-1.5 text-left">{{ __('general_ledger.table.date') }}</th>
                        <th class="px-2 py-1.5 text-left">{{ __('general_ledger.table.account') }}</th>
                        <th class="px-2 py-1.5 text-left">{{ __('general_ledger.table.type') }}</th>
                        <th class="px-2 py-1.5 text-left">{{ __('general_ledger.table.description') }}</th>
                        <th class="px-2 py-1.5 text-right">{{ __('general_ledger.table.debit') }}</th>
                        <th class="px-2 py-1.5 text-right">{{ __('general_ledger.table.credit') }}</th>
                    </tr>
                </thead>

                <tbody class="text-slate-700">
                    @forelse($ledgers as $row)
                        <tr class="border-t border-slate-200 hover:bg-slate-50">
                            <td class="px-2 py-1.5 whitespace-nowrap">
                                {{ optional($row->posted_at)->format('d M Y') }}
                            </td>

                            <td class="px-2 py-1.5 whitespace-nowrap">
                                {{ $row->account->name ?? '-' }}
                            </td>

                            <td class="px-2 py-1.5 whitespace-nowrap">
                                {{ \Illuminate\Support\Str::headline(str_replace('_',' ', $row->type)) }}
                            </td>

                            <td class="px-2 py-1.5 text-slate-500">
                                {{ $row->description }}
                            </td>

                            <td class="px-2 py-1.5 text-right font-semibold text-emerald-600">
                                @if($row->debit > 0)
                                    {{ $m($row->debit) }}
                                @endif
                            </td>

                            <td class="px-2 py-1.5 text-right font-semibold text-rose-600">
                                @if($row->credit > 0)
                                    {{ $m($row->credit) }}
                                @endif
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="6" class="px-2 py-6 text-center text-slate-500">
                                {{ __('general_ledger.table.no_data') }}
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        <div class="border-t border-slate-200 px-2 py-2 text-[11px]">
            {{ $ledgers->links() }}
        </div>
    </div>

</div>
@endsection