@extends('layouts.app')
@section('title', 'Trial Balance')
@section('page_title', 'Trial Balance')
@section('breadcrumbs')
    <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Home</a></li><li class="breadcrumb-item active">Trial Balance</li>
@endsection

@section('content')
    <div class="card mb-3"><div class="card-body py-2">
        <form method="GET" class="row g-2 align-items-end">
            <div class="col-md-3"><label class="form-label small">As of</label><input type="date" name="as_of" value="{{ $asOf }}" class="form-control form-control-sm"></div>
            <div class="col-md-3"><button class="btn btn-sm btn-primary"><i class="bi bi-search me-1"></i>Generate</button></div>
        </form>
    </div></div>

    <div class="card"><div class="card-body">
        <table class="table table-sm align-middle">
            <thead><tr><th>Code</th><th>Account</th><th>Type</th><th class="text-end">Debit</th><th class="text-end">Credit</th></tr></thead>
            <tbody>
                @foreach($rows as $r)
                    <tr>
                        <td class="small">{{ $r['account']->code }}</td>
                        <td>{{ $r['account']->name }}</td>
                        <td><span class="badge bg-soft-{{ $r['account']->type_color }} text-capitalize">{{ $r['account']->type }}</span></td>
                        <td class="text-end">{{ $r['debit'] > 0 ? number_format($r['debit'], 2) : '' }}</td>
                        <td class="text-end">{{ $r['credit'] > 0 ? number_format($r['credit'], 2) : '' }}</td>
                    </tr>
                @endforeach
            </tbody>
            <tfoot class="table-light">
                <tr class="fw-bold"><td colspan="3">Total</td><td class="text-end">{{ number_format($totalDebit, 2) }}</td><td class="text-end">{{ number_format($totalCredit, 2) }}</td></tr>
                <tr><td colspan="5" class="text-center small {{ abs($totalDebit-$totalCredit) < 0.01 ? 'text-success' : 'text-danger' }}">
                    {{ abs($totalDebit-$totalCredit) < 0.01 ? '✓ In balance' : 'Out of balance by '.number_format(abs($totalDebit-$totalCredit),2) }}
                </td></tr>
            </tfoot>
        </table>
    </div></div>
@endsection
