@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>
                <div class="flex gap-2">
                    <a href="{{ route('sales.targets.index') }}" class="inline-flex items-center px-4 py-2 bg-amber-600 text-white font-semibold rounded-lg hover:bg-amber-700 transition-all duration-200 shadow-md">
                        <i class="fas fa-bullseye ml-2"></i>
                        <span>المستهدفات</span>
                    </a>
                    <a href="{{ route('sales.commissions.index') }}" class="inline-flex items-center px-4 py-2 bg-green-600 text-white font-semibold rounded-lg hover:bg-green-700 transition-all duration-200 shadow-md">
                        <i class="fas fa-money-bill-wave ml-2"></i>
                        <span>العمولات</span>
                    </a>
                    <a href="{{ route('sales.representatives.create') }}" 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-plus 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
        </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($representatives as $representative)
                        <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">{{ $representative->name }}</div>
                            </td>
                            <td class="px-6 py-4 text-gray-700">
                                {{ $representative->commission_rate }} % 
                                ({{ $representative->commission_type == 'percentage' ? 'نسبة مئوية' : 'مبلغ ثابت' }})
                            </td>
                            <td class="px-6 py-4 text-gray-700">
                                {{ number_format($representative->target_amount, 2) }} 
                                / {{ $representative->target_period == 'monthly' ? 'شهرياً' : ($representative->target_period == 'quarterly' ? 'ربع سنوي' : 'سنوياً') }}
                            </td>
                            <td class="px-6 py-4">
                                @if($representative->status == 'active')
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-green-100 text-green-700">
                                    <span class="w-2 h-2 bg-green-500 rounded-full ml-2"></span>
                                    نشط
                                </span>
                                @else
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-red-100 text-red-700">
                                    غير نشط
                                </span>
                                @endif
                            </td>
                            <td class="px-6 py-4">
                                <div class="flex items-center justify-center gap-1">
                                    <a href="{{ route('sales.representatives.show', $representative) }}" 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" title="عرض التفاصيل">
                                        <i class="fas fa-eye mr-1"></i>
                                        <span>عرض</span>
                                    </a>
                                    <a href="{{ route('sales.representatives.edit', $representative) }}" class="inline-flex items-center px-3 py-2 rounded-lg bg-amber-100 text-amber-600 hover:bg-amber-200 transition-all duration-150 text-sm font-medium" title="تعديل">
                                        <i class="fas fa-edit mr-1"></i>
                                        <span>تعديل</span>
                                    </a>
                                    <form action="{{ route('sales.representatives.destroy', $representative) }}" method="POST" class="inline">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="inline-flex items-center px-3 py-2 rounded-lg bg-red-100 text-red-600 hover:bg-red-200 transition-all duration-150 text-sm font-medium" title="حذف" onclick="return confirm('هل أنت متأكد؟')">
                                            <i class="fas fa-trash mr-1"></i>
                                            <span>حذف</span>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="6" class="px-6 py-12 text-center text-gray-500">
                                لا يوجد مناديب مبيعات حالياً
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
            @if($representatives->hasPages())
            <div class="px-6 py-4 bg-gray-50 border-t border-gray-200">
                {{ $representatives->links() }}
            </div>
            @endif
        </div>
    </div>
</div>
@endsection
