@extends('layouts.app')
@section('title','Accounting Ledger')

@push('styles')
<style>
@media (min-width: 900px) {
    .mf-table-toolbar--ledger-row {
        display: grid !important;
        grid-template-columns: minmax(240px, 1fr) minmax(145px, 190px) minmax(145px, 190px) auto auto auto;
        align-items: center;
        gap: 8px 12px;
    }

    .mf-table-toolbar--ledger-row > .mf-table-toolbar-left,
    .mf-table-toolbar--ledger-row > .mf-table-toolbar-actions,
    .mf-table-toolbar--ledger-row .mf-table-quick-filters {
        display: contents !important;
    }

    .mf-table-toolbar--ledger-row .mf-table-search {
        width: 100% !important;
        max-width: none !important;
        min-width: 0 !important;
    }

    .mf-table-toolbar--ledger-row .mf-quick-filter {
        width: 100% !important;
        min-width: 0 !important;
        max-width: none !important;
    }

    .mf-table-toolbar--ledger-row .mf-btn,
    .mf-table-toolbar--ledger-row .mf-columns-menu {
        white-space: nowrap;
        justify-self: start;
    }
}
</style>
@endpush

@section('content')
@php
$filters=[];
if(request()->filled('search')) $filters[]=['label'=>'Search','value'=>request('search'),'removeUrl'=>route('accounting-ledger.index',request()->except(['search','page']))];
if(request()->filled('category')) $filters[]=['label'=>'Category','value'=>str(request('category'))->replace('_',' ')->title(),'removeUrl'=>route('accounting-ledger.index',request()->except(['category','page']))];
if(request()->filled('branch') && ($b=$branches->firstWhere('id',(int)request('branch')))) $filters[]=['label'=>'Branch','value'=>$b->name,'removeUrl'=>route('accounting-ledger.index',request()->except(['branch','page']))];
@endphp
<x-ui.page-toolbar title="Accounting Ledger" description="Trace accounting entries by category, branch, source event and journal reference." :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Accounting Ledger']]" />
<x-ui.table-toolbar class="mf-table-toolbar--ledger-row" table-id="ledger-table" search-placeholder="Search journal, reference or description…" :filter-names="['category','branch']" :columns="['date'=>'Date','journal'=>'Journal','branch'=>'Branch','category'=>'Category','event'=>'Event','description'=>'Description','debit'=>'Debit','credit'=>'Credit','user'=>'User']" export-filename="accounting-ledger.csv">
<x-slot:filters><select class="mf-input mf-quick-filter" name="category"><option value="">All categories</option>@foreach($categories as $category)<option value="{{ $category }}" @selected(request('category')===$category)>{{ str($category)->replace('_',' ')->title() }}</option>@endforeach</select><select class="mf-input mf-quick-filter" name="branch"><option value="">All branches</option>@foreach($branches as $branch)<option value="{{ $branch->id }}" @selected((int)request('branch')===$branch->id)>{{ $branch->name }}</option>@endforeach</select></x-slot:filters>
</x-ui.table-toolbar>
<x-ui.applied-filter-chips :filters="$filters" :clear-url="route('accounting-ledger.index')" />
<div class="mf-table-frame"><table id="ledger-table" class="mf-table" data-standard-table><thead><tr><th data-table-column="date">Date</th><th data-table-column="journal">Journal</th><th data-table-column="branch">Branch</th><th data-table-column="category">Category</th><th data-table-column="event">Event</th><th data-table-column="description">Description</th><th data-table-column="debit" class="text-right">Debit</th><th data-table-column="credit" class="text-right">Credit</th><th data-table-column="user">User</th></tr></thead><tbody>
@forelse($entries as $entry)<tr><td data-table-column="date">{{ $entry->entry_date->format('d M Y H:i') }}</td><td data-table-column="journal"><span class="font-medium">{{ $entry->journal_number }}</span><div class="text-xs text-muted-foreground">{{ $entry->reference ?: '—' }}</div></td><td data-table-column="branch">{{ $entry->branch->name }}</td><td data-table-column="category">{{ str($entry->category)->replace('_',' ')->title() }}</td><td data-table-column="event">{{ str($entry->event_type)->replace('_',' ')->title() }}</td><td data-table-column="description">{{ $entry->description }}@if($entry->financialAccount)<div class="text-xs text-muted-foreground">{{ $entry->financialAccount->name }}</div>@endif</td><td data-table-column="debit" class="text-right">{{ (float)$entry->debit>0?number_format((float)$entry->debit,2):'—' }}</td><td data-table-column="credit" class="text-right">{{ (float)$entry->credit>0?number_format((float)$entry->credit,2):'—' }}</td><td data-table-column="user">{{ $entry->user?->name ?? 'System' }}</td></tr>@empty<tr><td colspan="9" class="text-center text-muted-foreground">No accounting entries found.</td></tr>@endforelse
</tbody></table></div><div class="mt-4">{{ $entries->links() }}</div>
@endsection
