@extends('layouts.app')

@section('title', 'Assign Fee')

@section('content')
    <x-page-head title="Assign Fee" subtitle="Create and issue fee invoice to student" />

    <x-panel>
        <form method="POST" action="{{ route('finance.billing.store') }}" id="feeForm">
            @csrf
            <div style="display:grid; grid-template-columns: repeat(2, 1fr); gap:16px; margin-bottom:20px;">
                <div class="form-group">
                    <label class="form-label" for="student_id">Student</label>
                    <select class="input" id="student_id" name="student_id" required>
                        <option value="">Select student</option>
                        @foreach ($students as $student)
                            <option value="{{ $student->id }}" @selected(old('student_id') == $student->id)>
                                {{ $student->admission_number }} — {{ $student->full_name }}
                            </option>
                        @endforeach
                    </select>
                </div>
                <div class="form-group">
                    <label class="form-label" for="issue_date">Issue Date</label>
                    <input class="input" type="date" id="issue_date" name="issue_date" value="{{ old('issue_date', now()->toDateString()) }}" required>
                </div>
                <div class="form-group">
                    <label class="form-label" for="due_date">Due Date</label>
                    <input class="input" type="date" id="due_date" name="due_date" value="{{ old('due_date') }}">
                </div>
                <div class="form-group">
                    <label class="form-label" for="tax_amount">Tax Amount</label>
                    <input class="input" type="number" step="0.01" min="0" id="tax_amount" name="tax_amount" value="{{ old('tax_amount', '0.00') }}">
                </div>
            </div>

            <h4 style="margin-bottom:12px;">Fee Line Items</h4>
            <table id="linesTable">
                <thead>
                    <tr><th>Description</th><th>Qty</th><th>Unit Price</th><th>Income Account</th></tr>
                </thead>
                <tbody>
                    @for ($i = 0; $i < max(1, count(old('lines', []))); $i++)
                        @php($line = old('lines.'.$i, []))
                        <tr>
                            <td><input class="input" type="text" name="lines[{{ $i }}][description]" value="{{ $line['description'] ?? 'Tuition Fee' }}" required></td>
                            <td><input class="input" type="number" step="0.01" min="0.01" name="lines[{{ $i }}][quantity]" value="{{ $line['quantity'] ?? '1' }}" required style="width:80px;"></td>
                            <td><input class="input" type="number" step="0.01" min="0" name="lines[{{ $i }}][unit_price]" value="{{ $line['unit_price'] ?? '' }}" required style="width:120px;"></td>
                            <td>
                                <select class="input" name="lines[{{ $i }}][account_id]">
                                    <option value="">Default revenue</option>
                                    @foreach ($revenueAccounts as $account)
                                        <option value="{{ $account->id }}" @selected(($line['account_id'] ?? '') == $account->id)>
                                            {{ $account->code }} — {{ $account->name }}
                                        </option>
                                    @endforeach
                                </select>
                            </td>
                        </tr>
                    @endfor
                </tbody>
            </table>

            <div style="display:flex; gap:10px; margin-top:16px;">
                <button type="submit" class="btn btn-primary">Assign & Issue Fee</button>
                <a href="{{ route('finance.billing.index') }}" class="btn btn-ghost">Cancel</a>
            </div>
        </form>
    </x-panel>
@endsection
