@extends('layouts.app')

@section('title', 'Demandes')
@section('page-title', 'Demandes')

@section('content')
<div class="space-y-6">
    <!-- En-tête -->
    <div class="flex justify-between items-center flex-wrap gap-3">
        <div>
            <h1 class="text-2xl font-bold text-gray-900">Demandes</h1>
            <p class="mt-1 text-sm text-gray-600">Gérez les demandes d'approvisionnement et de transfert</p>
        </div>
        <div class="flex items-center gap-3">

            <button type="button" data-modal-toggle="modal-new-request" class="inline-flex items-center px-5 py-2.5 bg-blue-600 hover:bg-blue-700 text-white font-semibold rounded-lg shadow-sm hover:shadow-md transition-all">
                <svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4" />
                </svg>
                Nouvelle demande
            </button>

            @if(auth()->user()->hasPermissionTo('requests.view'))
            <a href="{{ route('requests.export', request()->query()) }}" class="inline-flex items-center px-4 py-2.5 bg-green-600 hover:bg-green-700 text-white font-medium rounded-lg shadow-sm transition-all">
                <svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4" />
                </svg>
                Exporter
            </a>
            @endif
        </div>
    </div>

    <x-modal-form id="modal-new-request" title="Nouvelle demande">
        @include('components.modals.request', ['modalId' => 'modal-new-request', 'departments' => $departments ?? [], 'products' => $products ?? []])
    </x-modal-form>

    <!-- Onglets de statut -->
    @php
    $currentStatus = request('status');
    $tabUrl = fn($status) => route('requests.index', array_filter(array_merge(request()->query(), ['status' => $status ?: null]), fn($v) => $v !== null && $v !== ''));
    @endphp
    <div class="border-b border-gray-200">
        <nav class="-mb-px flex space-x-12">
            <a href="{{ $tabUrl(null) }}" class="border-b-2 py-4 px-3 text-sm font-medium transition-colors {{ !$currentStatus ? 'border-blue-600 text-blue-600' : 'border-transparent text-gray-600 hover:text-gray-900 hover:border-gray-300' }}">
                Toutes
            </a>
            <a href="{{ $tabUrl('en_attente') }}" class="border-b-2 py-4 px-3 text-sm font-medium transition-colors {{ $currentStatus === 'en_attente' ? 'border-blue-600 text-blue-600' : 'border-transparent text-gray-600 hover:text-gray-900 hover:border-gray-300' }}">
                En attente
            </a>
            <a href="{{ $tabUrl('validée') }}" class="border-b-2 py-4 px-3 text-sm font-medium transition-colors {{ $currentStatus === 'validée' ? 'border-blue-600 text-blue-600' : 'border-transparent text-gray-600 hover:text-gray-900 hover:border-gray-300' }}">
                Validées
            </a>
            <a href="{{ $tabUrl('rejetée') }}" class="border-b-2 py-4 px-3 text-sm font-medium transition-colors {{ $currentStatus === 'rejetée' ? 'border-blue-600 text-blue-600' : 'border-transparent text-gray-600 hover:text-gray-900 hover:border-gray-300' }}">
                Rejetées
            </a>
        </nav>
    </div>

    <!-- Filtres -->
    <div class="bg-white shadow-sm border border-gray-200 rounded-xl p-4">
        <form method="GET" action="{{ route('requests.index') }}" class="flex flex-wrap items-end gap-4">
            @if(request('status'))<input type="hidden" name="status" value="{{ request('status') }}">@endif
            <div class="flex-1 min-w-[200px]">
                <input type="text" name="search" value="{{ request('search') }}" placeholder="Rechercher (référence, produit)..." class="w-full px-4 py-2.5 border border-gray-200 rounded-lg bg-white text-gray-900 focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-all">
            </div>
            @unless(auth()->user()->hasRole('Responsable du département'))
            <div class="flex-1 min-w-[180px]">
                @php
                $departmentFilterOptions = collect([['id' => '', 'name' => 'Tous les départements']])->merge($departments->map(fn($d) => ['id' => $d->id, 'name' => $d->name]));
                @endphp
                <x-searchable-select name="department_id" :options="$departmentFilterOptions" :selected="request('department_id')" placeholder="Tous les départements" searchPlaceholder="Rechercher un département..." :required="false" :inputClass="'w-full px-4 py-2.5 border border-gray-200 rounded-lg bg-white text-gray-900 focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-all'" />
            </div>
            @endunless
            <div class="w-[180px]">
                <input type="date" name="date" value="{{ request('date') }}" class="w-full px-4 py-2.5 border border-gray-200 rounded-lg bg-white text-gray-900 focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-all">
            </div>
            <div class="flex gap-2 shrink-0">
                <button type="submit" class="px-5 py-2.5 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition-colors whitespace-nowrap">
                    Filtrer
                </button>
                @if(request()->hasAny(['search', 'department_id', 'date']))
                <a href="{{ route('requests.index', ['status' => request('status')]) }}" class="px-5 py-2.5 border border-gray-200 rounded-lg text-gray-700 hover:bg-gray-50 transition-colors whitespace-nowrap">
                    Effacer
                </a>
                @endif
            </div>
        </form>
    </div>

    <!-- Tableau des demandes -->
    <div class="bg-white shadow-sm border border-gray-200 rounded-xl overflow-hidden">
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-gray-200">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-6 py-4 text-left text-xs font-semibold text-gray-700 uppercase tracking-wider">
                            N° Demande
                        </th>
                        @unless(auth()->user()->hasRole('Responsable du département'))
                        <th class="px-6 py-4 text-left text-xs font-semibold text-gray-700 uppercase tracking-wider">
                            Département
                        </th>
                        @endunless
                        <th class="px-6 py-4 text-left text-xs font-semibold text-gray-700 uppercase tracking-wider">
                            Date
                        </th>
                        <th class="px-6 py-4 text-left text-xs font-semibold text-gray-700 uppercase tracking-wider">
                            Produits
                        </th>
                        <th class="px-6 py-4 text-left text-xs font-semibold text-gray-700 uppercase tracking-wider">
                            Quantité
                        </th>
                        <th class="px-6 py-4 text-left text-xs font-semibold text-gray-700 uppercase tracking-wider">
                            Statut
                        </th>
                        <th class="px-6 py-4 text-right text-xs font-semibold text-gray-700 uppercase tracking-wider">
                            Actions
                        </th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    @forelse($demandes as $demande)
                    <tr class="hover:bg-blue-50/50 transition-colors">
                        <td class="px-6 py-4 whitespace-nowrap">
                            <span class="text-sm font-medium text-gray-900">{{ $demande->reference ?? '-' }}</span>
                        </td>
                        @unless(auth()->user()->hasRole('Responsable du département'))
                        <td class="px-6 py-4 whitespace-nowrap">
                            <span class="text-sm text-gray-700">{{ $demande->department?->name ?? '-' }}</span>
                        </td>
                        @endunless
                        <td class="px-6 py-4 whitespace-nowrap">
                            <span class="text-sm text-gray-600">{{ $demande->created_at?->format('d/m/Y') ?? '-' }}</span>
                        </td>
                        <td class="px-6 py-4">
                            <span class="text-sm text-gray-700">
                                @foreach($demande->requestDetails as $detail)
                                {{ $detail->product?->name ?? '-' }}
                                @if(!$loop->last) · @endif
                                @endforeach
                                @if($demande->requestDetails->isEmpty())
                                -
                                @endif
                            </span>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap">
                            <span class="text-sm text-gray-700">
                                {{ $demande->requestDetails->sum('quantity') }}
                            </span>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap">
                            @php
                            $statusLabels = [
                            'en_attente' => 'En attente',
                            'validée' => 'Validée',
                            'rejetée' => 'Rejetée',
                            ];
                            $statusClasses = [
                            'en_attente' => 'bg-amber-100 text-amber-800',
                            'validée' => 'bg-green-100 text-green-800',
                            'rejetée' => 'bg-red-100 text-red-800',
                            ];
                            @endphp
                            <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium {{ $statusClasses[$demande->status] ?? 'bg-gray-100 text-gray-800' }}">
                                {{ $statusLabels[$demande->status] ?? $demande->status }}
                            </span>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                            <div class="flex items-center justify-end gap-2">
                                {{-- Icône Voir les détails (toujours visible) --}}
                                <button type="button" data-reference="{{ e($demande->reference ?? '') }}" data-department="{{ e($demande->department?->name ?? '-') }}" data-date="{{ $demande->created_at?->format('d/m/Y') ?? '-' }}" data-products="{{ e(collect($demande->requestDetails)->map(fn($d) => ($d->product?->name ?? '-') . ' (' . $d->quantity . ')')->join(', ')) }}" data-comments="{{ e($demande->comments ?? '-') }}" data-status="{{ $demande->status }}" data-rejection-reason="{{ e($demande->rejection_reason ?? '') }}" data-rejected-at="{{ $demande->rejected_at?->format('d/m/Y H:i') ?? '-' }}" onclick="openDetailsModal(this)" class="inline-flex items-center justify-center w-9 h-9 rounded-lg bg-gradient-to-br from-gray-50 to-gray-100 text-gray-600 hover:from-gray-500 hover:to-gray-600 hover:text-white transform hover:scale-110 transition-all duration-200 shadow-sm hover:shadow-md" title="Voir les détails">
                                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z" />
                                    </svg>
                                </button>
                                @if($demande->status === 'en_attente')
                                @can('update', $demande)
                                <button type="button" data-id="{{ $demande->id }}" data-department-id="{{ $demande->department_id }}" data-department-name="{{ e($demande->department?->name ?? '') }}" data-product-id="{{ $demande->requestDetails->first()?->product_id }}" data-product-name="{{ e($demande->requestDetails->first()?->product?->name ?? '') }}" data-quantity="{{ $demande->requestDetails->first()?->quantity }}" data-comments="{{ e($demande->comments ?? '') }}" onclick="openEditRequestModal(this)" class="inline-flex items-center justify-center w-9 h-9 rounded-lg bg-gradient-to-br from-blue-50 to-blue-100 text-blue-600 hover:from-blue-500 hover:to-blue-600 hover:text-white transform hover:scale-110 transition-all duration-200 shadow-sm hover:shadow-md" title="Modifier">
                                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z" />
                                    </svg>
                                </button>
                                @endcan
                                @can('approve', $demande)
                                <form method="POST" action="{{ route('requests.approve', $demande) }}" class="inline-block">
                                    @csrf
                                    <button type="submit" class="inline-flex items-center justify-center w-9 h-9 rounded-lg bg-gradient-to-br from-green-50 to-green-100 text-green-600 hover:from-green-500 hover:to-green-600 hover:text-white transform hover:scale-110 transition-all duration-200 shadow-sm hover:shadow-md" title="Approuver">
                                        <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" />
                                        </svg>
                                    </button>
                                </form>
                                @endcan
                                @can('reject', $demande)
                                <button type="button" data-id="{{ $demande->id }}" data-reference="{{ e($demande->reference ?? '') }}" onclick="openRejectModal(this)" class="inline-flex items-center justify-center w-9 h-9 rounded-lg bg-gradient-to-br from-red-50 to-red-100 text-red-600 hover:from-red-500 hover:to-red-600 hover:text-white transform hover:scale-110 transition-all duration-200 shadow-sm hover:shadow-md" title="Rejeter">
                                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
                                    </svg>
                                </button>
                                @endcan
                                @endif
                            </div>
                        </td>
                    </tr>
                    @empty
                    <tr class="hover:bg-blue-50/50 transition-colors">
                        <td colspan="{{ auth()->user()->hasRole('Responsable du département') ? 7 : 8 }}" class="px-6 py-12 text-center">
                            <div class="flex flex-col items-center">
                                <svg class="w-16 h-16 text-gray-300 mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
                                </svg>
                                <p class="text-sm font-medium text-gray-600 mb-1">Aucune demande enregistrée</p>
                            </div>
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        @if($demandes->hasPages())
        <div class="px-6 py-4 border-t border-gray-200">
            {{ $demandes->links() }}
        </div>
        @endif
    </div>
</div>

{{-- Modal Détails de la demande (avec motif de rejet si rejetée) --}}
<x-modal-form id="modal-details-request" title="Détails de la demande" size="lg">
    <div class="space-y-5">
        <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
            <div>
                <p class="text-xs font-medium text-gray-500 uppercase">Référence</p>
                <p id="details-reference" class="text-sm font-semibold text-gray-900 mt-1">-</p>
            </div>
            <div>
                <p class="text-xs font-medium text-gray-500 uppercase">Département</p>
                <p id="details-department" class="text-sm text-gray-900 mt-1">-</p>
            </div>
            <div>
                <p class="text-xs font-medium text-gray-500 uppercase">Date</p>
                <p id="details-date" class="text-sm text-gray-900 mt-1">-</p>
            </div>
            <div>
                <p class="text-xs font-medium text-gray-500 uppercase">Statut</p>
                <p id="details-status" class="text-sm mt-1">-</p>
            </div>
        </div>
        <div>
            <p class="text-xs font-medium text-gray-500 uppercase">Produits demandés</p>
            <p id="details-products" class="text-sm text-gray-900 mt-1">-</p>
        </div>
        <div>
            <p class="text-xs font-medium text-gray-500 uppercase">Commentaire</p>
            <p id="details-comments" class="text-sm text-gray-900 mt-1">-</p>
        </div>
        {{-- Bloc motif de rejet (visible uniquement pour les demandes rejetées) --}}
        <div id="details-rejection-block" class="hidden rounded-lg border-l-4 border-red-500 bg-red-50 p-4">
            <p class="text-xs font-semibold text-red-800 uppercase">Motif du rejet</p>
            <p id="details-rejection-reason" class="text-sm text-red-900 mt-2">-</p>
            <p id="details-rejected-at" class="text-xs text-red-600 mt-2">-</p>
        </div>
        <div class="flex justify-end pt-4 border-t border-gray-200">
            <button type="button" data-modal-close="modal-details-request" class="px-4 py-2 text-sm font-medium border border-gray-300 rounded-lg text-gray-700 bg-white hover:bg-gray-50 transition-colors">Fermer</button>
        </div>
    </div>
</x-modal-form>

{{-- Modal de rejet avec motif (visible pour les utilisateurs pouvant rejeter) --}}
@if(auth()->user()->hasPermissionTo('requests.reject'))
<x-modal-form id="modal-reject-request" title="Rejeter la demande">
    <form method="POST" id="reject-request-form" class="space-y-6">
        @csrf
        <p class="text-sm text-gray-600" id="reject-modal-reference">Demande <strong id="reject-ref-display"></strong></p>
        <div>
            <label class="block text-sm font-medium text-gray-700 mb-2">Motif du rejet <span class="text-red-500">*</span></label>
            <textarea name="rejection_reason" id="reject_reason" rows="4" maxlength="500" required placeholder="Indiquez le motif du rejet..." class="w-full px-3 py-2.5 text-sm border border-gray-200 rounded-lg bg-white focus:ring-2 focus:ring-red-500 focus:border-red-500 transition-colors resize-none"></textarea>
            <p class="mt-1 text-xs text-gray-500">Ce motif sera communiqué à l'utilisateur ayant effectué la demande.</p>
        </div>
        <div class="flex justify-end gap-3 pt-6 mt-6 border-t border-gray-200">
            <button type="button" data-modal-close="modal-reject-request" class="px-4 py-2 text-sm font-medium border border-gray-300 rounded-lg text-gray-700 bg-white hover:bg-gray-50 transition-colors">Annuler</button>
            <button type="submit" class="px-4 py-2 text-sm font-medium rounded-lg bg-red-600 hover:bg-red-700 text-white transition-all">Confirmer le rejet</button>
        </div>
    </form>
</x-modal-form>
@endif

@if(auth()->user()->hasPermissionTo('requests.update'))
<x-modal-form id="modal-edit-request" title="Modifier la demande">
    <form method="POST" id="edit-request-form" class="space-y-6">
        @csrf
        @method('PUT')
        @php $inputClass = 'w-full px-3 py-2.5 text-sm border border-gray-200 rounded-lg bg-white focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-colors'; @endphp
        <div class="grid grid-cols-1 sm:grid-cols-2 gap-5">
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-2">Département <span class="text-red-500">*</span></label>
                @php $editDepts = $departments ?? collect(); @endphp
                @if($editDepts->count() === 1)
                @php $editDept = $editDepts->first(); @endphp
                <input type="hidden" name="department_id" id="edit_department_id" value="{{ $editDept->id }}">
                <div class="{{ $inputClass }} bg-gray-50 text-gray-700">{{ $editDept->name }}</div>
                @else
                <x-searchable-select name="department_id" :options="$departments ?? []" :selected="''" placeholder="Choisir un département" searchPlaceholder="Rechercher un département..." :required="true" :inputClass="$inputClass" />
                @endif
            </div>
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-2">Produit <span class="text-red-500">*</span></label>
                <x-searchable-select name="product_id" :options="$products ?? []" :selected="''" placeholder="Choisir un produit" searchPlaceholder="Rechercher un produit..." :required="true" :inputClass="$inputClass" />
            </div>
        </div>
        <div>
            <label class="block text-sm font-medium text-gray-700 mb-2">Quantité demandée <span class="text-red-500">*</span></label>
            <input type="number" name="quantity" min="1" id="edit_quantity" class="{{ $inputClass }}" placeholder="0">
        </div>
        <div>
            <label class="block text-sm font-medium text-gray-700 mb-2">Commentaire</label>
            <textarea rows="3" name="comments" id="edit_comments" class="{{ $inputClass }} resize-none" placeholder="Optionnel"></textarea>
        </div>
        @include('components.modals.modal-actions', ['modalId' => 'modal-edit-request', 'submitLabel' => 'Enregistrer'])
    </form>
</x-modal-form>

<script>
    function openEditRequestModal(btn) {
        var id = btn.dataset.id;
        var departmentId = btn.dataset.departmentId || '';
        var departmentName = btn.dataset.departmentName || 'Choisir un département';
        var productId = btn.dataset.productId || '';
        var productName = btn.dataset.productName || 'Choisir un produit';
        var quantity = btn.dataset.quantity || '';
        var comments = btn.dataset.comments || '';

        var form = document.getElementById('edit-request-form');
        form.action = '{{ url("requests") }}/' + id;

        document.getElementById('edit_quantity').value = quantity;
        document.getElementById('edit_comments').value = comments;

        var deptInput = form.querySelector('input[name="department_id"]');
        var productInput = form.querySelector('input[name="product_id"]');
        if (deptInput) deptInput.value = departmentId;
        if (productInput) productInput.value = productId;

        var deptContainer = form.querySelector('input[name="department_id"]') ? .closest('.searchable-select');
        var productContainer = form.querySelector('input[name="product_id"]') ? .closest('.searchable-select');
        if (deptContainer) {
            deptContainer.dataset.selectedId = departmentId;
            deptContainer.dataset.selectedName = departmentName;
            var deptLabel = deptContainer.querySelector('.searchable-select-label');
            if (deptLabel) {
                deptLabel.textContent = departmentName || 'Choisir un département';
                deptLabel.classList.toggle('text-gray-500', !departmentId);
                deptLabel.classList.toggle('text-gray-900', !!departmentId);
            }
        }
        if (productContainer) {
            productContainer.dataset.selectedId = productId;
            productContainer.dataset.selectedName = productName;
            var productLabel = productContainer.querySelector('.searchable-select-label');
            if (productLabel) {
                productLabel.textContent = productName || 'Choisir un produit';
                productLabel.classList.toggle('text-gray-500', !productId);
                productLabel.classList.toggle('text-gray-900', !!productId);
            }
        }

        if (typeof openModal === 'function') openModal('modal-edit-request');
    }

</script>
@endif

<script>
    function openDetailsModal(btn) {
        document.getElementById('details-reference').textContent = btn.dataset.reference || '-';
        document.getElementById('details-department').textContent = btn.dataset.department || '-';
        document.getElementById('details-date').textContent = btn.dataset.date || '-';
        document.getElementById('details-products').textContent = btn.dataset.products || '-';
        document.getElementById('details-comments').textContent = btn.dataset.comments || '-';
        var statusLabels = {
            'en_attente': 'En attente'
            , 'validée': 'Validée'
            , 'rejetée': 'Rejetée'
        };
        var statusClasses = {
            'en_attente': 'text-amber-700'
            , 'validée': 'text-green-700'
            , 'rejetée': 'text-red-700'
        };
        var status = btn.dataset.status || '';
        var statusEl = document.getElementById('details-status');
        statusEl.textContent = statusLabels[status] || status;
        statusEl.className = 'text-sm mt-1 font-medium ' + (statusClasses[status] || 'text-gray-900');
        var rejectionBlock = document.getElementById('details-rejection-block');
        if (status === 'rejetée' && btn.dataset.rejectionReason) {
            rejectionBlock.classList.remove('hidden');
            document.getElementById('details-rejection-reason').textContent = btn.dataset.rejectionReason || 'Non précisé';
            document.getElementById('details-rejected-at').textContent = 'Rejetée le ' + (btn.dataset.rejectedAt || '-');
        } else {
            rejectionBlock.classList.add('hidden');
        }
        if (typeof openModal === 'function') openModal('modal-details-request');
    }

</script>

@if(auth()->user()->hasPermissionTo('requests.reject'))
<script>
    function openRejectModal(btn) {
        var id = btn.dataset.id;
        var reference = btn.dataset.reference || '-';
        var form = document.getElementById('reject-request-form');
        if (form) {
            form.action = '{{ url("requests") }}/' + id + '/reject';
            document.getElementById('reject_reason').value = '';
            var refDisplay = document.getElementById('reject-ref-display');
            if (refDisplay) refDisplay.textContent = reference;
            if (typeof openModal === 'function') openModal('modal-reject-request');
        }
    }

</script>
@endif
@endsection
