@extends('layouts.app')
@section('title', 'مرتجعات فواتير الموردين')

@section('content')
<div class="min-h-screen bg-gradient-to-br from-slate-50 to-slate-100 py-8 px-4 md:px-8">
    <div class="max-w-7xl mx-auto">
        <div class="mb-8">
            <div class="flex items-center justify-between mb-6">
                <div>
                    <h1 class="text-3xl font-bold text-gray-900 mb-2">مرتجعات فواتير الموردين</h1>
                    <p class="text-gray-600">قائمة المرتجعات والإشعارات المدينة للموردين</p>
                </div>
                <a href="{{ route('supplier-invoices.index') }}" class="inline-flex items-center px-6 py-3 bg-gradient-to-r from-blue-600 to-blue-700 text-white font-semibold rounded-lg hover:from-blue-700 hover:to-blue-800 transition-all duration-200 shadow-lg hover:shadow-xl transform hover:scale-105">
                    <i class="fas fa-arrow-right ml-2"></i>
                    <span>العودة للفواتير</span>
                </a>
            </div>
        </div>

        @if(session('success'))
        <div class="bg-green-50 border-l-4 border-green-500 p-4 mb-6 rounded-r-lg">
            <div class="flex">
                <div class="flex-shrink-0">
                    <i class="fas fa-check-circle text-green-500 text-xl"></i>
                </div>
                <div class="mr-3">
                    <p class="text-green-700 font-medium">{{ session('success') }}</p>
                </div>
            </div>
        </div>
        @endif

        @if(session('error'))
        <div class="bg-red-50 border-l-4 border-red-500 p-4 mb-6 rounded-r-lg">
            <div class="flex">
                <div class="flex-shrink-0">
                    <i class="fas fa-exclamation-circle text-red-500 text-xl"></i>
                </div>
                <div class="mr-3">
                    <p class="text-red-700 font-medium">{{ session('error') }}</p>
                </div>
            </div>
        </div>
        @endif

        <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200">
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead class="bg-gradient-to-r from-gray-800 to-gray-900 text-white">
                        <tr>
                            <th class="px-6 py-4 text-right font-semibold text-sm">#</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">رقم المرتجع</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">الفاتورة الأصلية</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">المورد</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">النوع</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">المبلغ</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">التاريخ</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">الحالة</th>
                            <th class="px-6 py-4 text-center font-semibold text-sm">الإجراءات</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($returns as $return)
                        <tr class="hover:bg-blue-50 transition-colors duration-150 border-b border-gray-100">
                            <td class="px-6 py-4 text-gray-600 font-medium">{{ $loop->iteration }}</td>
                            <td class="px-6 py-4">
                                <div class="font-semibold text-gray-900">{{ $return->return_number }}</div>
                            </td>
                            <td class="px-6 py-4">
                                <a href="{{ route('supplier-invoices.show', $return->invoice->id) }}" class="text-blue-600 hover:text-blue-800">
                                    {{ $return->invoice->invoice_number }}
                                </a>
                            </td>
                            <td class="px-6 py-4">
                                <div class="text-gray-900">{{ $return->supplier->name }}</div>
                                <div class="text-sm text-gray-500">{{ $return->supplier->phone }}</div>
                            </td>
                            <td class="px-6 py-4">
                                @if($return->return_type === 'debit_note')
                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-blue-100 text-blue-700">
                                        <i class="fas fa-file-invoice-dollar ml-1"></i> إشعار مدين
                                    </span>
                                @else
                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-amber-100 text-amber-700">
                                        <i class="fas fa-undo-alt ml-1"></i> مرتجع مشتريات
                                    </span>
                                @endif
                            </td>
                            <td class="px-6 py-4 text-right font-semibold text-gray-900">
                                {{ number_format($return->total_amount, 2) }}
                            </td>
                            <td class="px-6 py-4 text-gray-600">
                                {{ $return->return_date->format('Y-m-d') }}
                            </td>
                            <td class="px-6 py-4">
                                @switch($return->status)
                                    @case('draft')
                                        <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-gray-100 text-gray-800">
                                            <span class="w-2 h-2 bg-gray-500 rounded-full mr-1"></span>
                                            مسودة
                                        </span>
                                        @break
                                    @case('sent')
                                        <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-blue-100 text-blue-800">
                                            <span class="w-2 h-2 bg-blue-500 rounded-full mr-1"></span>
                                            مرسلة
                                        </span>
                                        @break
                                    @case('approved')
                                        <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-800">
                                            <span class="w-2 h-2 bg-green-500 rounded-full mr-1"></span>
                                            معتمدة
                                        </span>
                                        @break
                                    @case('cancelled')
                                        <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-red-100 text-red-800">
                                            <span class="w-2 h-2 bg-red-500 rounded-full mr-1"></span>
                                            ملغاة
                                        </span>
                                        @break
                                @endswitch
                            </td>
                            <td class="px-6 py-4 text-center">
                                <a href="{{ route('supplier-invoice-returns.show', $return->id) }}" class="inline-flex items-center px-3 py-2 rounded-lg bg-blue-100 text-blue-600 hover:bg-blue-200 transition-all duration-150 text-sm font-medium whitespace-nowrap" title="عرض التفاصيل">
                                    <i class="fas fa-eye mr-1"></i>
                                    <span>عرض</span>
                                </a>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="9" class="px-6 py-12 text-center">
                                <div class="flex flex-col items-center justify-center">
                                    <i class="fas fa-inbox text-gray-400 text-4xl mb-3"></i>
                                    <p class="text-gray-600 font-medium">لا توجد مرتجعات حالياً</p>
                                    <p class="text-gray-500 text-sm mt-1">ابدأ بإضافة مرتجع من فواتير الموردين</p>
                                </div>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>

        @if($returns->hasPages())
        <div class="mt-4">
            {{ $returns->links() }}
        </div>
        @endif
    </div>
</div>
@endsection
