@extends('layouts.admin')

@section('title', 'Monthly Ledger Summary')

@section('content')
<x-admin.report-page title="Monthly Ledger Summary" subtitle="Day-wise totals for {{ $month }}">
    <x-slot:actions>
        <a href="{{ route('admin.reports.day-ledger', ['date' => \Carbon\Carbon::parse($month . '-01')->toDateString()]) }}" class="ci-btn ci-btn--ghost">
            <i class="bi bi-arrow-left"></i> Back to Day Ledger
        </a>
    </x-slot:actions>

    <form method="GET" class="ci-filters ci-filters--report">
        <div class="ci-filters__field">
            <label for="month">Month</label>
            <input type="month" name="month" id="month" value="{{ $month }}" required>
        </div>
        <div class="ci-filters__actions">
            <button type="submit" class="ci-btn ci-btn--primary">Load</button>
        </div>
    </form>

    <div class="ci-table-card">
        <div class="ci-table-wrap">
            <table class="ci-table">
                <thead>
                    <tr>
                        <th>Date</th>
                        <th class="ci-end">Opening (Cash)</th>
                        <th class="ci-end">Sales</th>
                        <th class="ci-end">Collection</th>
                        <th class="ci-end">Expense</th>
                        <th class="ci-end">Refund</th>
                        <th class="ci-end">Closing (Cash)</th>
                        <th>Status</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($rows as $row)
                        <tr>
                            <td>{{ \Carbon\Carbon::parse($row->date)->toDateString() }}</td>
                            <td class="ci-end"><span class="ci-money">Rs {{ number_format((float) $row->opening_balance, 2) }}</span></td>
                            <td class="ci-end"><span class="ci-money">Rs {{ number_format((float) $row->total_sales, 2) }}</span></td>
                            <td class="ci-end"><span class="ci-money">Rs {{ number_format((float) $row->total_collection, 2) }}</span></td>
                            <td class="ci-end"><span class="ci-money">Rs {{ number_format((float) $row->total_expense, 2) }}</span></td>
                            <td class="ci-end"><span class="ci-money">Rs {{ number_format((float) $row->total_refund, 2) }}</span></td>
                            <td class="ci-end"><span class="ci-money">Rs {{ number_format((float) $row->closing_balance, 2) }}</span></td>
                            <td>
                                @php $status = strtolower((string) $row->status); @endphp
                                <span class="ci-pill {{ $status === 'closed' ? 'ci-pill--danger' : 'ci-pill--success' }}">
                                    {{ strtoupper($row->status) }}
                                </span>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="8">
                                <div class="ci-empty">
                                    <i class="bi bi-journal-text"></i>
                                    <h3>No Data Found</h3>
                                    <p>No ledger rows for this month.</p>
                                </div>
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</x-admin.report-page>
@endsection
