@extends('layouts.app')

@section('title', 'Financial Reports | Institution Axis')

@section('sidebar-menu')
    @include('schooladmin.partials.sidebar')
@endsection

@section('sidebar-footer')
    <p>{{ auth()->user()->school?->name ?? 'Institution Axis' }}</p>
@endsection

@section('breadcrumb')
    <span><a href="{{ route('schooladmin.dashboard') }}">Home</a></span> / <span>Finance</span> / <span class="active-page">Reports</span>
@endsection

@section('content')
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h3 class="fw-bold text-dark"><i class="fas fa-chart-pie me-2"></i>Financial Overview</h3>
        <div>
            <button class="btn btn-outline-secondary rounded-pill me-2"><i class="fas fa-download me-2"></i>Export PDF</button>
            <button class="btn btn-primary rounded-pill"><i class="fas fa-print me-2"></i>Print Summary</button>
        </div>
    </div>

    <!-- Summary Cards -->
    <div class="row g-4 mb-5">
        <div class="col-md-3">
            <div class="card border-0 shadow-sm rounded-4 h-100">
                <div class="card-body">
                    <div class="d-flex align-items-center mb-3">
                        <div class="bg-primary bg-opacity-10 text-primary p-3 rounded-circle me-3">
                            <i class="fas fa-file-invoice fa-lg"></i>
                        </div>
                        <h6 class="mb-0 text-muted">Total Invoiced</h6>
                    </div>
                    <h3 class="fw-bold mb-1">KES {{ number_format($totalInvoiced/1000000, 1) }}M</h3>
                    <p class="text-muted small mb-0">Total fees billed this year</p>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-0 shadow-sm rounded-4 h-100">
                <div class="card-body">
                    <div class="d-flex align-items-center mb-3">
                        <div class="bg-success bg-opacity-10 text-success p-3 rounded-circle me-3">
                            <i class="fas fa-cash-register fa-lg"></i>
                        </div>
                        <h6 class="mb-0 text-muted">Total Collections</h6>
                    </div>
                    <h3 class="fw-bold mb-1 text-success">KES {{ number_format($totalPaid/1000, 1) }}K</h3>
                    <p class="text-muted small mb-0">{{ $totalInvoiced > 0 ? number_format(($totalPaid/$totalInvoiced)*100, 1) : 0 }}% Collection rate</p>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-0 shadow-sm rounded-4 h-100">
                <div class="card-body">
                    <div class="d-flex align-items-center mb-3">
                        <div class="bg-danger bg-opacity-10 text-danger p-3 rounded-circle me-3">
                            <i class="fas fa-exclamation-triangle fa-lg"></i>
                        </div>
                        <h6 class="mb-0 text-muted">Outstanding</h6>
                    </div>
                    <h3 class="fw-bold mb-1 text-danger">KES {{ number_format($totalOutstanding/1000, 1) }}K</h3>
                    <p class="text-muted small mb-0">Unpaid school fees</p>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-0 shadow-sm rounded-4 h-100">
                <div class="card-body">
                    <div class="d-flex align-items-center mb-3">
                        <div class="bg-info bg-opacity-10 text-info p-3 rounded-circle me-3">
                            <i class="fas fa-money-bill-transfer fa-lg"></i>
                        </div>
                        <h6 class="mb-0 text-muted">Expenses</h6>
                    </div>
                    <h3 class="fw-bold mb-1 text-info">KES {{ number_format($totalExpenses/1000, 1) }}K</h3>
                    <p class="text-muted small mb-0">Running costs to date</p>
                </div>
            </div>
        </div>
    </div>

    <div class="row g-4">
        <!-- Collection Trend Chart -->
        <div class="col-lg-8">
            <div class="card border-0 shadow-sm rounded-4 h-100">
                <div class="card-header bg-white border-0 py-3">
                    <h6 class="fw-bold mb-0">Collection Trends (Last 6 Months)</h6>
                </div>
                <div class="card-body">
                    <canvas id="collectionTrendChart" height="300"></canvas>
                </div>
            </div>
        </div>

        <!-- Arrears per Class -->
        <div class="col-lg-4">
            <div class="card border-0 shadow-sm rounded-4 h-100">
                <div class="card-header bg-white border-0 py-3">
                    <h6 class="fw-bold mb-0">Arrears by Class</h6>
                </div>
                <div class="card-body p-0">
                    <div class="list-group list-group-flush">
                        @forelse($outstandingPerClass as $class)
                        <div class="list-group-item border-0 py-3 d-flex justify-content-between align-items-center">
                            <div>
                                <h6 class="mb-0 small fw-bold">{{ $class['name'] }}</h6>
                                <div class="progress mt-2" style="height: 4px; width: 100px;">
                                    <div class="progress-bar bg-danger" style="width: 70%;"></div>
                                </div>
                            </div>
                            <span class="text-danger fw-bold small">KES {{ number_format($class['amount'], 0) }}</span>
                        </div>
                        @empty
                        <div class="p-4 text-center text-muted small">No outstanding balances found.</div>
                        @endforelse
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@section('custom-scripts')
<script>
    const ctx = document.getElementById('collectionTrendChart').getContext('2d');
    new Chart(ctx, {
        type: 'bar',
        data: {
            labels: {!! json_encode($collectionTrend->pluck('month')) !!},
            datasets: [{
                label: 'Collections (KES)',
                data: {!! json_encode($collectionTrend->pluck('total')) !!},
                backgroundColor: '#e54027',
                borderRadius: 10,
                barThickness: 30
            }]
        },
        options: {
            responsive: true,
            maintainAspectRatio: false,
            plugins: {
                legend: { display: false }
            },
            scales: {
                y: { beginAtZero: true, grid: { display: false } },
                x: { grid: { display: false } }
            }
        }
    });
</script>
@endsection
