@extends('layouts.app')

@section('title', 'Bulk Invoicing')

@section('content')
    <x-page-head title="Bulk Invoice Generation" subtitle="Batch invoices with transaction rollback on error" />

    <x-panel>
        <form method="POST" action="{{ route('finance.bulk-invoices.store') }}">
            @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="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="description">Fee Description</label>
                    <input class="input" type="text" id="description" name="description" value="{{ old('description', 'Term Fee') }}" required>
                </div>
                <div class="form-group">
                    <label class="form-label" for="account_id">Income Account</label>
                    <select class="input" id="account_id" name="account_id" required>
                        @foreach ($revenueAccounts as $account)
                            <option value="{{ $account->id }}" @selected(old('account_id') == $account->id)>
                                {{ $account->code }} — {{ $account->name }}
                            </option>
                        @endforeach
                    </select>
                </div>
                <div class="form-group">
                    <label class="form-label" for="unit_price">Unit Price</label>
                    <input class="input" type="number" step="0.01" min="0.01" id="unit_price" name="unit_price" value="{{ old('unit_price') }}" required>
                </div>
                <div class="form-group">
                    <label class="form-label" for="tax_amount">Tax Amount (per invoice)</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>

            <div class="form-group">
                <label class="form-label">Select Students</label>
                <div style="max-height:300px; overflow-y:auto; border:1px solid var(--border); padding:12px; border-radius:8px;">
                    <label style="display:block; margin-bottom:8px;">
                        <input type="checkbox" onclick="toggleAll(this)"> Select All
                    </label>
                    @foreach ($students as $student)
                        <label style="display:block; margin-bottom:4px;">
                            <input type="checkbox" name="student_ids[]" value="{{ $student->id }}"
                                @checked(is_array(old('student_ids')) && in_array($student->id, old('student_ids')))>
                            {{ $student->admission_number }} — {{ $student->full_name }}
                        </label>
                    @endforeach
                </div>
            </div>

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

@push('scripts')
<script>
function toggleAll(el) {
    document.querySelectorAll('input[name="student_ids[]"]').forEach(cb => cb.checked = el.checked);
}
</script>
@endpush
