@extends('layouts.app')

@section('title', 'Record Fee Payment | 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><a href="{{ route('schooladmin.finance.payments.index') }}">Payments</a></span> / <span class="active-page">Record</span>
@endsection

@section('content')
<div class="container-fluid">
    <div class="row justify-content-center">
        <div class="col-lg-8">
            <div class="card shadow border-0 rounded-4">
                <div class="card-header bg-white py-3">
                    <h5 class="fw-bold mb-0">Record Student Payment</h5>
                </div>
                <div class="card-body p-4">
                    @if(session('error'))
                        <div class="alert alert-danger border-0 rounded-4">{{ session('error') }}</div>
                    @endif

                    <form action="{{ route('schooladmin.finance.payments.store') }}" method="POST">
                        @csrf
                        <div class="row g-4">
                            <div class="col-md-12">
                                <label class="form-label fw-semibold">Select Invoice</label>
                                <select name="invoice_id" id="invoice_id" class="form-select rounded-pill @error('invoice_id') is-invalid @enderror" required>
                                    <option value="">Search Student or Invoice Number...</option>
                                    @foreach($invoices as $inv)
                                        <option value="{{ $inv->id }}" 
                                            data-balance="{{ $inv->balance() }}" 
                                            {{ (isset($invoice) && $invoice->id == $inv->id) ? 'selected' : '' }}>
                                            {{ $inv->invoice_number }} - {{ $inv->student->fullName() }} (Bal: KES {{ number_format($inv->balance(), 2) }})
                                        </option>
                                    @endforeach
                                </select>
                                @error('invoice_id') <div class="invalid-feedback">{{ $message }}</div> @enderror
                            </div>

                            <div class="col-md-6">
                                <label class="form-label fw-semibold">Payment Date</label>
                                <input type="date" name="payment_date" class="form-control rounded-pill @error('payment_date') is-invalid @enderror" value="{{ date('Y-m-d') }}" required>
                                @error('payment_date') <div class="invalid-feedback">{{ $message }}</div> @enderror
                            </div>

                            <div class="col-md-6">
                                <label class="form-label fw-semibold">Amount to Pay (KES)</label>
                                <input type="number" name="amount" id="amount" class="form-control rounded-pill @error('amount') is-invalid @enderror" step="0.01" value="{{ $invoice ? $invoice->balance() : '' }}" required>
                                <div id="balance-info" class="form-text mt-2 text-primary fw-semibold">
                                    @if($invoice) Max allowed: KES {{ number_format($invoice->balance(), 2) }} @endif
                                </div>
                                @error('amount') <div class="invalid-feedback">{{ $message }}</div> @enderror
                            </div>

                            <div class="col-md-6">
                                <label class="form-label fw-semibold">Payment Method</label>
                                <select name="payment_method" class="form-select rounded-pill @error('payment_method') is-invalid @enderror" required>
                                    <option value="cash">Cash</option>
                                    <option value="bank">Bank Transfer / Deposit</option>
                                    <option value="mobile_money">Mobile Money (M-Pesa etc.)</option>
                                </select>
                                @error('payment_method') <div class="invalid-feedback">{{ $message }}</div> @enderror
                            </div>

                            <div class="col-md-6">
                                <label class="form-label fw-semibold">Reference Number</label>
                                <input type="text" name="reference_number" class="form-control rounded-pill" placeholder="Bank Ref / Transaction ID">
                            </div>
                        </div>

                        <div class="mt-5 text-end">
                            <a href="{{ route('schooladmin.finance.payments.index') }}" class="btn btn-light px-4 me-2">Cancel</a>
                            <button type="submit" class="btn btn-primary px-5 rounded-pill shadow-sm">
                                <i class="fas fa-save me-2"></i>Record Payment
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

@push('scripts')
<script>
    document.getElementById('invoice_id').addEventListener('change', function() {
        const selected = this.options[this.selectedIndex];
        const balance = selected.getAttribute('data-balance');
        const amountInput = document.getElementById('amount');
        const balanceInfo = document.getElementById('balance-info');
        
        if (balance) {
            amountInput.value = balance;
            balanceInfo.innerText = 'Max allowed: KES ' + parseFloat(balance).toLocaleString('en-US', {minimumFractionDigits: 2});
        } else {
            amountInput.value = '';
            balanceInfo.innerText = '';
        }
    });
</script>
@endpush
@endsection
