@extends('layouts.app')
@section('title', 'Fee Structure')
@section('page-title', 'Fee Management')
@section('sidebar-nav')
<div class="nav-item"><a href="{{ route('admin.dashboard') }}"><i class="bi bi-grid"></i> Dashboard</a></div>
<div class="nav-item"><a href="{{ route('admin.students.index') }}"><i class="bi bi-people"></i> Students</a></div>
<div class="nav-item"><a href="{{ route('admin.fees.structure') }}"><i class="bi bi-cash-stack"></i> Fee Structure</a></div>
<div class="nav-item"><a href="{{ route('admin.fees.invoices') }}"><i class="bi bi-receipt"></i> Invoices</a></div>
@endsection

@section('content')
<div class="page-header">
    <div><h1>Fee Structure</h1></div>
    <div style="display:flex;gap:8px;">
        <form method="POST" action="{{ route('admin.fees.generate') }}" style="display:inline;">
            @csrf
            <select name="term_id" required style="width:180px;padding:10px 14px;">
                @foreach(\App\Models\Term::whereHas('academicYear', fn($q) => $q->where('school_id', auth()->user()->school_id))->get() as $term)
                    <option value="{{ $term->id }}">{{ $term->academicYear->name }} {{ $term->name }}</option>
                @endforeach
            </select>
            <button type="submit" class="btn btn-secondary"><i class="bi bi-lightning"></i> Generate Invoices</button>
        </form>
        <button onclick="document.getElementById('addModal').style.display='flex'" class="btn btn-primary">
            <i class="bi bi-plus-lg"></i> Add Fee Item
        </button>
    </div>
</div>

<div class="card">
    <div class="table-wrap">
        <table>
            <thead>
                <tr><th>Name</th><th>Term</th><th>Class</th><th>Amount</th><th>Mandatory</th></tr>
            </thead>
            <tbody>
                @forelse($structures as $item)
                <tr>
                    <td><strong>{{ $item->name }}</strong></td>
                    <td>{{ $item->term->name }}</td>
                    <td>{{ $item->schoolClass?->name ?? 'All Classes' }}</td>
                    <td>KES {{ number_format($item->amount, 2) }}</td>
                    <td>
                        <span class="badge {{ $item->is_mandatory ? 'badge-success' : 'badge-gray' }}">
                            {{ $item->is_mandatory ? 'Yes' : 'No' }}
                        </span>
                    </td>
                </tr>
                @empty
                <tr><td colspan="5" style="text-align:center;padding:40px;color:var(--gray);">No fee items yet</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>
</div>

<!-- Add Fee Modal -->
<div id="addModal" style="display:none;position:fixed;inset:0;background:rgba(0,0,0,.5);z-index:2000;align-items:center;justify-content:center;">
    <div class="card" style="width:100%;max-width:480px;margin:20px;">
        <div class="card-header">
            <h5>Add Fee Item</h5>
            <button onclick="document.getElementById('addModal').style.display='none'" style="background:none;border:none;cursor:pointer;font-size:20px;">&times;</button>
        </div>
        <div class="card-body">
            <form method="POST" action="{{ route('admin.fees.structure.store') }}">
                @csrf
                <div class="form-group">
                    <label>Fee Name *</label>
                    <input type="text" name="name" placeholder="e.g. Tuition Fee" required>
                </div>
                <div class="grid grid-2">
                    <div class="form-group">
                        <label>Amount (KES) *</label>
                        <input type="number" name="amount" min="0" step="0.01" required>
                    </div>
                    <div class="form-group">
                        <label>Term *</label>
                        <select name="term_id" required>
                            @foreach(\App\Models\Term::whereHas('academicYear', fn($q) => $q->where('school_id', auth()->user()->school_id))->get() as $term)
                                <option value="{{ $term->id }}">{{ $term->academicYear->name }} {{ $term->name }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="form-group">
                        <label>Class (optional)</label>
                        <select name="class_id">
                            <option value="">All Classes</option>
                            @foreach(\App\Models\SchoolClass::where('school_id', auth()->user()->school_id)->get() as $class)
                                <option value="{{ $class->id }}">{{ $class->name }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="form-group">
                        <label>Mandatory?</label>
                        <select name="is_mandatory">
                            <option value="1">Yes</option>
                            <option value="0">No</option>
                        </select>
                    </div>
                </div>
                <button type="submit" class="btn btn-primary">Add Fee Item</button>
            </form>
        </div>
    </div>
</div>
@endsection
