@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('sales.representatives.index') }}" class="inline-flex items-center px-4 py-2 bg-gray-600 text-white font-semibold rounded-lg hover:bg-gray-700 transition-all duration-200">
                    <i class="fas fa-arrow-right ml-2"></i>
                    <span>العودة للمناديب</span>
                </a>
            </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
        </div>

        <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-center font-semibold text-sm">الإجراءات</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($commissions as $commission)
                        <tr class="hover:bg-blue-50 transition-colors duration-150">
                            <td class="px-6 py-4 text-gray-900 font-medium">
                                {{ $commission->salesRepresentative->employee->user->name ?? '---' }}
                            </td>
                            <td class="px-6 py-4 text-blue-600">
                                <a href="{{ route('customer-invoices.show', $commission->customerInvoice) }}">
                                    {{ $commission->customerInvoice->invoice_number }}
                                </a>
                            </td>
                            <td class="px-6 py-4 text-gray-900 font-bold">
                                {{ number_format($commission->commission_amount, 2) }}
                            </td>
                            <td class="px-6 py-4">
                                @php
                                    $statusColors = [
                                        'pending' => 'bg-amber-100 text-amber-700',
                                        'earned' => 'bg-blue-100 text-blue-700',
                                        'paid' => 'bg-green-100 text-green-700',
                                        'cancelled' => 'bg-red-100 text-red-700',
                                    ];
                                    $statusLabels = [
                                        'pending' => 'معلق',
                                        'earned' => 'مكتسب',
                                        'paid' => 'مدفوع',
                                        'cancelled' => 'ملغي',
                                    ];
                                @endphp
                                <span class="px-3 py-1 rounded-full text-xs font-medium {{ $statusColors[$commission->status] ?? 'bg-gray-100' }}">
                                    {{ $statusLabels[$commission->status] ?? $commission->status }}
                                </span>
                            </td>
                            <td class="px-6 py-4 text-gray-600 text-sm">
                                {{ $commission->created_at->format('Y-m-d') }}
                            </td>
                            <td class="px-6 py-4 text-center">
                                <a href="{{ route('sales.commissions.show', $commission) }}" class="text-blue-600 hover:text-blue-800">
                                    <i class="fas fa-eye"></i>
                                </a>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="6" class="px-6 py-12 text-center text-gray-500">لا يوجد عمولات مسجلة حالياً</td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
            @if($commissions->hasPages())
            <div class="px-6 py-4 bg-gray-50 border-t border-gray-200">
                {{ $commissions->links() }}
            </div>
            @endif
        </div>
    </div>
</div>
@endsection
