@extends('layouts.app')

@section('title', 'Cash Flow Statement')

@section('content')
    <x-page-head title="Cash Flow Statement" subtitle="{{ $from }} to {{ $to }}">
        <x-slot:actions>
            <x-export-menu module="cashflow" :filters="['from' => $from, 'to' => $to]" :import="false" />
        </x-slot:actions>
    </x-page-head>

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

        <div class="stat-grid" style="margin-bottom:20px;">
            <x-stat-card label="Opening Cash" :value="number_format($report['opening_cash'], 2)" />
            <x-stat-card label="Closing Cash" :value="number_format($report['closing_cash'], 2)" color="teal" />
            <x-stat-card label="Net Change" :value="number_format($report['net_change'], 2)" color="violet" />
        </div>

        <h4>Cash Inflows</h4>
        <div class="table-wrap" style="margin-bottom:20px;">
            <table>
                <thead><tr><th>Account</th><th style="text-align:right;">Amount</th></tr></thead>
                <tbody>
                    @forelse ($report['inflows'] as $row)
                        <tr><td>{{ $row['code'] }} — {{ $row['name'] }}</td><td style="text-align:right;">{{ number_format($row['amount'], 2) }}</td></tr>
                    @empty
                        <tr><td colspan="2">No inflows in period</td></tr>
                    @endforelse
                    <tr style="font-weight:bold;"><td>Total Inflows</td><td style="text-align:right;">{{ number_format($report['total_inflows'], 2) }}</td></tr>
                </tbody>
            </table>
        </div>

        <h4>Cash Outflows</h4>
        <div class="table-wrap" style="margin-bottom:20px;">
            <table>
                <thead><tr><th>Account</th><th style="text-align:right;">Amount</th></tr></thead>
                <tbody>
                    @forelse ($report['outflows'] as $row)
                        <tr><td>{{ $row['code'] }} — {{ $row['name'] }}</td><td style="text-align:right;">{{ number_format($row['amount'], 2) }}</td></tr>
                    @empty
                        <tr><td colspan="2">No outflows in period</td></tr>
                    @endforelse
                    <tr style="font-weight:bold;"><td>Total Outflows</td><td style="text-align:right;">{{ number_format($report['total_outflows'], 2) }}</td></tr>
                </tbody>
            </table>
        </div>

        <p style="font-size:1.1em; font-weight:bold;">
            Net Cash Change: {{ number_format($report['net_change'], 2) }}
        </p>
    </x-panel>

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