@extends('layouts.app')
@section('title', 'Balance Sheet')
@section('page_title', 'Balance Sheet')
@section('breadcrumbs')
    <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Home</a></li><li class="breadcrumb-item active">Balance Sheet</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="row g-3">
        <div class="col-lg-6">
            <div class="card h-100"><div class="card-header text-primary"><i class="bi bi-bank me-1"></i>Assets</div><div class="card-body">
                <table class="table table-sm mb-0">
                    <tbody>
                        @foreach($assets as $r)<tr><td>{{ $r['account']->name }}</td><td class="text-end">{{ number_format($r['amount'], 2) }}</td></tr>@endforeach
                    </tbody>
                    <tfoot class="table-light"><tr class="fw-bold"><td>Total Assets</td><td class="text-end">{{ number_format($totalAssets, 2) }}</td></tr></tfoot>
                </table>
            </div></div>
        </div>
        <div class="col-lg-6">
            <div class="card h-100"><div class="card-header text-warning"><i class="bi bi-cash-stack me-1"></i>Liabilities & Equity</div><div class="card-body">
                <table class="table table-sm mb-0">
                    <tbody>
                        @foreach($liabilities as $r)<tr><td>{{ $r['account']->name }}</td><td class="text-end">{{ number_format($r['amount'], 2) }}</td></tr>@endforeach
                        @foreach($equity as $r)<tr><td>{{ $r['account']->name }}</td><td class="text-end">{{ number_format($r['amount'], 2) }}</td></tr>@endforeach
                        <tr><td>Net Profit / (Loss) to date</td><td class="text-end">{{ number_format($netProfit, 2) }}</td></tr>
                    </tbody>
                    <tfoot class="table-light"><tr class="fw-bold"><td>Total Liabilities & Equity</td><td class="text-end">{{ number_format($totalLiabEquity, 2) }}</td></tr></tfoot>
                </table>
            </div></div>
        </div>
        <div class="col-12">
            <div class="alert {{ abs($totalAssets - $totalLiabEquity) < 0.01 ? 'alert-success' : 'alert-warning' }} small mb-0">
                {{ abs($totalAssets - $totalLiabEquity) < 0.01 ? '✓ Balance sheet balances.' : 'Difference: '.number_format($totalAssets - $totalLiabEquity, 2) }}
            </div>
        </div>
    </div>
@endsection
