@extends('layouts.app')

@section('title', 'Fee Payments | 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">Payments</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-cash-register me-2"></i>Fee Payments</h3>
        <a href="{{ route('schooladmin.finance.payments.create') }}" class="btn btn-primary rounded-pill px-4">
            <i class="fas fa-plus me-2"></i>Record New Payment
        </a>
    </div>

    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            {{ session('success') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    @endif

    <div class="card shadow-sm border-0 rounded-4">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="bg-light">
                        <tr>
                            <th class="ps-4">Date</th>
                            <th>Student</th>
                            <th>Invoice #</th>
                            <th>Method</th>
                            <th>Reference</th>
                            <th>Amount (KES)</th>
                            <th class="text-end pe-4">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($payments as $payment)
                        <tr>
                            <td class="ps-4">{{ date('M d, Y', strtotime($payment->payment_date)) }}</td>
                            <td>
                                <div class="d-flex align-items-center">
                                    <div class="avatar-xs rounded-circle bg-light text-primary d-flex align-items-center justify-content-center me-2 fw-bold">
                                        {{ substr($payment->student->first_name, 0, 1) }}{{ substr($payment->student->last_name, 0, 1) }}
                                    </div>
                                    <span>{{ $payment->student->fullName() }}</span>
                                </div>
                            </td>
                            <td><a href="{{ route('schooladmin.finance.invoices.show', $payment->invoice) }}" class="text-decoration-none">{{ $payment->invoice->invoice_number }}</a></td>
                            <td class="text-capitalize small">{{ str_replace('_', ' ', $payment->payment_method) }}</td>
                            <td class="text-muted small">{{ $payment->reference_number ?? '-' }}</td>
                            <td class="fw-bold text-success">{{ number_format($payment->amount, 2) }}</td>
                            <td class="text-end pe-4">
                                <a href="{{ route('schooladmin.finance.payments.show', $payment) }}" class="btn btn-sm btn-outline-secondary" title="View Receipt">
                                    <i class="fas fa-receipt me-1"></i> Receipt
                                </a>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="7" class="text-center py-5 text-muted">No payment records found.</td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
        @if($payments->hasPages())
        <div class="card-footer bg-white border-0">
            {{ $payments->links() }}
        </div>
        @endif
    </div>
</div>
@endsection
