@extends('layouts.app')

@section('title', 'Balance Sheet')

@section('content')
    <x-page-head title="Balance Sheet" subtitle="As of {{ $asOf }}">
        <x-slot:actions>
            <x-export-menu module="balance-sheet" :filters="['as_of' => $asOf]" :import="false" />
        </x-slot:actions>
    </x-page-head>

    <x-panel>
        <form method="GET" action="{{ route('finance.reports.balance-sheet') }}" style="display:flex; gap:12px; margin-bottom:20px; align-items:end;">
            <div class="form-group">
                <label class="form-label" for="as_of">As Of</label>
                <input class="input" type="date" id="as_of" name="as_of" value="{{ $asOf }}">
            </div>
            <button type="submit" class="btn btn-outline">Update</button>
        </form>

        @if (!$report['is_balanced'])
            <div style="padding:12px; background:var(--warning-bg, #fef3cd); border-radius:8px; margin-bottom:16px;">
                Assets ({{ number_format($report['total_assets'], 2) }}) do not equal Liabilities + Equity ({{ number_format(bcadd($report['total_liabilities'], $report['total_equity'], 2), 2) }}).
            </div>
        @endif

        <div style="display:grid; grid-template-columns: repeat(3, 1fr); gap:20px;">
            <div>
                <h4>Assets</h4>
                <div class="table-wrap">
                    <table>
                        <tbody>
                            @foreach ($report['assets'] as $row)
                                <tr><td>{{ $row['code'] }} — {{ $row['name'] }}</td><td style="text-align:right;">{{ number_format(ltrim($row['balance'], '-'), 2) }}</td></tr>
                            @endforeach
                            <tr style="font-weight:bold;"><td>Total Assets</td><td style="text-align:right;">{{ number_format($report['total_assets'], 2) }}</td></tr>
                        </tbody>
                    </table>
                </div>
            </div>
            <div>
                <h4>Liabilities</h4>
                <div class="table-wrap">
                    <table>
                        <tbody>
                            @foreach ($report['liabilities'] as $row)
                                <tr><td>{{ $row['code'] }} — {{ $row['name'] }}</td><td style="text-align:right;">{{ number_format(ltrim($row['balance'], '-'), 2) }}</td></tr>
                            @endforeach
                            <tr style="font-weight:bold;"><td>Total Liabilities</td><td style="text-align:right;">{{ number_format($report['total_liabilities'], 2) }}</td></tr>
                        </tbody>
                    </table>
                </div>
            </div>
            <div>
                <h4>Equity</h4>
                <div class="table-wrap">
                    <table>
                        <tbody>
                            @foreach ($report['equity'] as $row)
                                <tr><td>{{ $row['code'] }} — {{ $row['name'] }}</td><td style="text-align:right;">{{ number_format(ltrim($row['balance'], '-'), 2) }}</td></tr>
                            @endforeach
                            <tr style="font-weight:bold;"><td>Total Equity</td><td style="text-align:right;">{{ number_format($report['total_equity'], 2) }}</td></tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </x-panel>

    <div style="margin-top:16px;">
        <a href="{{ route('finance.reports.index') }}" class="btn btn-outline">Back to Reports</a>
    </div>
@endsection
