@extends('layouts.app')
@section('title','Financial Accounts')

@push('styles')
<style>
@media (min-width: 900px) {
    .mf-table-toolbar--accounts-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--accounts-row > .mf-table-toolbar-left,
    .mf-table-toolbar--accounts-row > .mf-table-toolbar-actions,
    .mf-table-toolbar--accounts-row .mf-table-quick-filters {
        display: contents !important;
    }

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

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

    .mf-table-toolbar--accounts-row .mf-btn,
    .mf-table-toolbar--accounts-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('accounts.index',request()->except(['search','page']))];
if(request()->filled('type')) $filters[]=['label'=>'Type','value'=>str(request('type'))->replace('_',' ')->title(),'removeUrl'=>route('accounts.index',request()->except(['type','page']))];
if(request()->filled('status')) $filters[]=['label'=>'Status','value'=>str(request('status'))->title(),'removeUrl'=>route('accounts.index',request()->except(['status','page']))];
if(request()->filled('branch') && ($b=$branches->firstWhere('id',(int)request('branch')))) $filters[]=['label'=>'Branch','value'=>$b->name,'removeUrl'=>route('accounts.index',request()->except(['branch','page']))];
@endphp
<x-ui.page-toolbar title="Financial Accounts" description="Manage branch cash, mobile money, bank and other business funds with traceable balances." :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Financial Accounts']]">
<x-slot:actions><div class="flex gap-2">@if(auth()->user()->can('accounts.transfer'))<a class="mf-btn" href="{{ route('account-transfers.create') }}">Transfer Funds</a>@endif @if(auth()->user()->can('accounts.capital'))<a class="mf-btn" href="{{ route('capital-injections.create') }}">Add Capital</a>@endif @if(auth()->user()->can('accounts.manage'))<a class="mf-btn mf-btn-primary" href="{{ route('accounts.create') }}">New Account</a>@endif</div></x-slot:actions>
</x-ui.page-toolbar>
<x-ui.quick-status-tabs :tabs="[['label'=>'All','value'=>''],['label'=>'Active','value'=>'active'],['label'=>'Inactive','value'=>'inactive']]" />
<x-ui.table-toolbar class="mf-table-toolbar--accounts-row" table-id="accounts-table" search-placeholder="Search account or reference…" :filter-names="['type','branch']" :columns="['name'=>'Account','type'=>'Type','branch'=>'Branch','opening'=>'Opening Balance','current'=>'Current Balance','status'=>'Status']" export-filename="financial-accounts.csv">
<x-slot:filters><select class="mf-input mf-quick-filter" name="type"><option value="">All types</option>@foreach($types as $type)<option value="{{ $type }}" @selected(request('type')===$type)>{{ str($type)->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('accounts.index')" />
<div class="mf-table-frame"><table id="accounts-table" class="mf-table" data-standard-table><thead><tr><th data-table-column="name">Account</th><th data-table-column="type">Type</th><th data-table-column="branch">Branch</th><th data-table-column="opening">Opening Balance</th><th data-table-column="current">Current Balance</th><th data-table-column="status">Status</th><th data-no-export></th></tr></thead><tbody>
@forelse($accounts as $account)<tr><td data-table-column="name"><a class="font-medium text-primary hover:underline" href="{{ route('accounts.show',$account) }}">{{ $account->name }}</a><div class="text-xs text-muted-foreground">{{ $account->reference_details ?: 'No reference details' }}</div></td><td data-table-column="type">{{ str($account->account_type)->replace('_',' ')->title() }}</td><td data-table-column="branch">{{ $account->branch->name }}</td><td data-table-column="opening">{{ number_format((float)$account->opening_balance,2) }}</td><td data-table-column="current" class="font-medium">{{ number_format((float)$account->current_balance,2) }}</td><td data-table-column="status"><span class="mf-badge {{ $account->status==='active'?'mf-badge-success':'mf-badge-muted' }}">{{ ucfirst($account->status) }}</span></td><td data-no-export class="text-right"><a class="text-primary hover:underline" href="{{ route('accounts.show',$account) }}">View</a></td></tr>@empty<tr><td colspan="7" class="text-center text-muted-foreground">No financial accounts found.</td></tr>@endforelse
</tbody></table></div><div class="mt-4">{{ $accounts->links() }}</div>
@endsection
