@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.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">
                        <i class="fas fa-plus ml-2"></i> إضافة مستهدف جديد
                    </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> العودة للمناديب
                    </a>
                </div>
            </div>

            @if(session('success'))
            <div class="bg-green-50 border-l-4 border-green-500 p-4 mb-6 rounded-r-lg">
                <p class="text-green-700 font-medium">{{ session('success') }}</p>
            </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">المندوب</th>
                            <th class="px-6 py-4 text-right">المبلغ المستهدف</th>
                            <th class="px-6 py-4 text-right">الفترة</th>
                            <th class="px-6 py-4 text-right">تاريخ البداية</th>
                            <th class="px-6 py-4 text-right">تاريخ النهاية</th>
                            <th class="px-6 py-4 text-right">الحالة</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($targets as $target)
                        <tr class="hover:bg-blue-50">
                            <td class="px-6 py-4 text-gray-900 font-medium">
                                {{ $target->salesRepresentative->employee->user->name ?? '---' }}
                            </td>
                            <td class="px-6 py-4 text-gray-900 font-bold">
                                {{ number_format($target->target_amount, 2) }}
                            </td>
                            <td class="px-6 py-4 text-gray-600">
                                @php
                                    $periodLabels = [
                                        'monthly' => 'شهري',
                                        'quarterly' => 'ربع سنوي',
                                        'yearly' => 'سنوي',
                                    ];
                                @endphp
                                {{ $periodLabels[$target->period_type] ?? $target->period_type }}
                            </td>
                            <td class="px-6 py-4 text-gray-600 text-sm">{{ $target->start_date }}</td>
                            <td class="px-6 py-4 text-gray-600 text-sm">{{ $target->end_date }}</td>
                            <td class="px-6 py-4">
                                <span class="px-3 py-1 rounded-full text-xs font-medium {{ $target->status == 'active' ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700' }}">
                                    {{ $target->status == 'active' ? 'نشط' : 'مغلق' }}
                                </span>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="6" class="px-6 py-12 text-center text-gray-500">لا يوجد مستهدفات مسجلة حالياً</td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
            @if($targets->hasPages())
            <div class="px-6 py-4 border-t">
                {{ $targets->links() }}
            </div>
            @endif
        </div>
    </div>
</div>
@endsection
