@extends('layouts.app')
@section('title', 'Fee Structure')
@section('page_title', 'Fee Structure')
@section('breadcrumbs')
    <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Home</a></li><li class="breadcrumb-item active">Fee Structure</li>
@endsection

@section('content')
    <div class="card mb-3"><div class="card-body">
        <form method="GET" class="row g-2 align-items-end">
            <div class="col-md-4"><label class="form-label small">Session</label>
                <select name="session_id" class="form-select form-select-sm">
                    @foreach($sessions as $s)<option value="{{ $s->id }}" @selected($sessionId==$s->id)>{{ $s->name }}</option>@endforeach
                </select></div>
            <div class="col-md-4"><label class="form-label small">Class</label>
                <select name="class_id" class="form-select form-select-sm">
                    <option value="">— Select class —</option>
                    @foreach($classes as $c)<option value="{{ $c->id }}" @selected($classId==$c->id)>{{ $c->name }}</option>@endforeach
                </select></div>
            <div class="col-md-4"><button class="btn btn-sm btn-primary"><i class="bi bi-search me-1"></i>Load</button></div>
        </form>
    </div></div>

    @if($classId)
        <form method="POST" action="{{ route('fee-structure.store') }}">
            @csrf
            <input type="hidden" name="session_id" value="{{ $sessionId }}">
            <input type="hidden" name="class_id" value="{{ $classId }}">
            <div class="card">
                <div class="card-header"><i class="bi bi-table me-1"></i>Amounts for selected class</div>
                <div class="card-body">
                    <table class="table align-middle">
                        <thead><tr><th>Fee Head</th><th>Frequency</th><th style="width:200px">Amount (Rs)</th></tr></thead>
                        <tbody>
                            @foreach($feeTypes as $type)
                                <tr>
                                    <td class="fw-semibold">{{ $type->name }}</td>
                                    <td><span class="badge bg-soft-info text-capitalize">{{ str_replace('_',' ',$type->frequency) }}</span></td>
                                    <td><input type="number" step="0.01" name="amounts[{{ $type->id }}]" value="{{ $amounts[$type->id] ?? $type->default_amount }}" class="form-control form-control-sm"></td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
                @can('fees.update')<div class="card-footer text-end"><button class="btn btn-primary"><i class="bi bi-save me-1"></i>Save Structure</button></div>@endcan
            </div>
        </form>
    @else
        <div class="card"><div class="card-body text-center text-muted py-4">Select a session and class to edit the fee structure.</div></div>
    @endif
@endsection
