@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">عرض كافة البيانات المتعلقة بالمندوب: {{ $representative->employee->user->name ?? $representative->employee->full_name }}</p>
                </div>
                <div class="flex gap-2">
                    <a href="{{ route('sales.representatives.edit', $representative) }}" 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">
                        <i class="fas fa-edit ml-2"></i>
                        <span>تعديل</span>
                    </a>
                    <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>
            </div>
        </div>

        <div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
            <!-- Basic Info Card -->
            <div class="lg:col-span-1 space-y-6">
                <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200">
                    <div class="px-6 py-4 bg-gradient-to-r from-gray-800 to-gray-900 text-white">
                        <h2 class="text-xl font-semibold">المعلومات الأساسية</h2>
                    </div>
                    <div class="p-6">
                        <div class="space-y-4">
                            <div>
                                <label class="text-sm text-gray-500 block">اسم المندوب</label>
                                <p class="text-lg font-bold text-gray-900">{{ $representative->employee->user->name ?? $representative->employee->full_name }}</p>
                            </div>
                            <div>
                                <label class="text-sm text-gray-500 block">نوع العمولة</label>
                                <p class="text-gray-900 font-medium">{{ $representative->commission_type == 'percentage' ? 'نسبة مئوية' : 'مبلغ ثابت' }}</p>
                            </div>
                            <div>
                                <label class="text-sm text-gray-500 block">قيمة/نسبة العمولة</label>
                                <p class="text-gray-900 font-medium">{{ $representative->commission_rate }} {{ $representative->commission_type == 'percentage' ? '%' : '' }}</p>
                            </div>
                            <div>
                                <label class="text-sm text-gray-500 block">المبلغ المستهدف</label>
                                <p class="text-gray-900 font-medium">{{ number_format($representative->target_amount, 2) }}</p>
                            </div>
                            <div>
                                <label class="text-sm text-gray-500 block">فترة المستهدف</label>
                                <p class="text-gray-900 font-medium">
                                    {{ $representative->target_period == 'monthly' ? 'شهرياً' : ($representative->target_period == 'quarterly' ? 'ربع سنوي' : 'سنوياً') }}
                                </p>
                            </div>
                            <div>
                                <label class="text-sm text-gray-500 block">الحالة</label>
                                @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>
                                @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
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Stats/Tables Column -->
            <div class="lg:col-span-2 space-y-8">
                <!-- Customers Table -->
                <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200">
                    <div class="px-6 py-4 bg-gray-50 border-b border-gray-200 flex justify-between items-center">
                        <h2 class="text-xl font-semibold text-gray-800">عملاء المندوب</h2>
                        <span class="bg-blue-100 text-blue-700 px-3 py-1 rounded-full text-sm font-bold">{{ $representative->customers->count() }} عميل</span>
                    </div>
                    <div class="overflow-x-auto">
                        <table class="w-full">
                            <thead class="bg-gray-100">
                                <tr>
                                    <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">اسم العميل</th>
                                    <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">رقم الهاتف</th>
                                    <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">العنوان</th>
                                </tr>
                            </thead>
                            <tbody class="divide-y divide-gray-200">
                                @forelse($representative->customers as $customer)
                                <tr>
                                    <td class="px-6 py-4 text-sm text-gray-900">{{ $customer->name }}</td>
                                    <td class="px-6 py-4 text-sm text-gray-600">{{ $customer->phone ?? '---' }}</td>
                                    <td class="px-6 py-4 text-sm text-gray-600">{{ $customer->address ?? '---' }}</td>
                                </tr>
                                @empty
                                <tr>
                                    <td colspan="3" class="px-6 py-8 text-center text-gray-500">لا يوجد عملاء مرتبطين بهذا المندوب حالياً</td>
                                </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>

                <!-- Recent Sales Orders -->
                <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200">
                    <div class="px-6 py-4 bg-gray-50 border-b border-gray-200">
                        <h2 class="text-xl font-semibold text-gray-800">آخر أوامر البيع</h2>
                    </div>
                    <div class="overflow-x-auto">
                        <table class="w-full">
                            <thead class="bg-gray-100">
                                <tr>
                                    <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">رقم الأمر</th>
                                    <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">التاريخ</th>
                                    <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">المبلغ الإجمالي</th>
                                    <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">الحالة</th>
                                </tr>
                            </thead>
                            <tbody class="divide-y divide-gray-200">
                                @forelse($representative->salesOrders->take(5) as $order)
                                <tr>
                                    <td class="px-6 py-4 text-sm text-blue-600 font-medium">
                                        <a href="{{ route('sales-orders.show', $order) }}">{{ $order->so_number }}</a>
                                    </td>
                                    <td class="px-6 py-4 text-sm text-gray-600">{{ $order->so_date }}</td>
                                    <td class="px-6 py-4 text-sm text-gray-900 font-bold">{{ number_format($order->total_amount, 2) }}</td>
                                    <td class="px-6 py-4">
                                        <span class="px-2 py-1 text-xs rounded-full {{ $order->status == 'completed' ? 'bg-green-100 text-green-700' : 'bg-amber-100 text-amber-700' }}">
                                            {{ $order->status }}
                                        </span>
                                    </td>
                                </tr>
                                @empty
                                <tr>
                                    <td colspan="4" class="px-6 py-8 text-center text-gray-500">لا يوجد أوامر بيع حالياً</td>
                                </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
