@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-4xl 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>
                <a href="{{ route('sales.representatives.index') }}" class="inline-flex items-center px-6 py-3 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 ($errors->any())
            <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">حدث خطأ في البيانات المدخلة:</p>
                        <ul class="mt-2 list-disc list-inside text-red-600 text-sm">
                            @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
                            @endforeach
                        </ul>
                    </div>
                </div>
            </div>
            @endif
        </div>

        <form action="{{ route('sales.representatives.update', $representative) }}" method="POST" class="space-y-6">
            @csrf
            @method('PUT')

            <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 space-y-4">
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div class="md:col-span-2">
                            <label class="block text-sm font-medium text-gray-700 mb-2">الموظف</label>
                            <input type="text" class="w-full px-4 py-2 bg-gray-100 border border-gray-300 rounded-lg text-gray-600 cursor-not-allowed" value="{{ $representative->employee->user->name ?? $representative->employee->full_name }}" readonly>
                        </div>

                        <div>
                            <label for="commission_type" class="block text-sm font-medium text-gray-700 mb-2">نوع العمولة *</label>
                            <select id="commission_type" name="commission_type" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent @error('commission_type') border-red-500 @enderror" required>
                                <option value="percentage" {{ old('commission_type', $representative->commission_type) == 'percentage' ? 'selected' : '' }}>نسبة مئوية (%)</option>
                                <option value="fixed" {{ old('commission_type', $representative->commission_type) == 'fixed' ? 'selected' : '' }}>مبلغ ثابت</option>
                            </select>
                            @error('commission_type')
                            <p class="text-red-500 text-sm mt-1">{{ $message }}</p>
                            @enderror
                        </div>

                        <div>
                            <label for="commission_rate" class="block text-sm font-medium text-gray-700 mb-2">قيمة/نسبة العمولة *</label>
                            <input type="number" step="0.01" id="commission_rate" name="commission_rate" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent @error('commission_rate') border-red-500 @enderror" value="{{ old('commission_rate', $representative->commission_rate) }}" required>
                            @error('commission_rate')
                            <p class="text-red-500 text-sm mt-1">{{ $message }}</p>
                            @enderror
                        </div>

                        <div>
                            <label for="target_amount" class="block text-sm font-medium text-gray-700 mb-2">المبلغ المستهدف *</label>
                            <input type="number" step="0.01" id="target_amount" name="target_amount" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent @error('target_amount') border-red-500 @enderror" value="{{ old('target_amount', $representative->target_amount) }}" required>
                            @error('target_amount')
                            <p class="text-red-500 text-sm mt-1">{{ $message }}</p>
                            @enderror
                        </div>

                        <div>
                            <label for="target_period" class="block text-sm font-medium text-gray-700 mb-2">فترة المستهدف *</label>
                            <select id="target_period" name="target_period" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent @error('target_period') border-red-500 @enderror" required>
                                <option value="monthly" {{ old('target_period', $representative->target_period) == 'monthly' ? 'selected' : '' }}>شهرياً</option>
                                <option value="quarterly" {{ old('target_period', $representative->target_period) == 'quarterly' ? 'selected' : '' }}>ربع سنوي</option>
                                <option value="yearly" {{ old('target_period', $representative->target_period) == 'yearly' ? 'selected' : '' }}>سنوياً</option>
                            </select>
                            @error('target_period')
                            <p class="text-red-500 text-sm mt-1">{{ $message }}</p>
                            @enderror
                        </div>

                        <div>
                            <label for="status" class="block text-sm font-medium text-gray-700 mb-2">الحالة *</label>
                            <select id="status" name="status" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent @error('status') border-red-500 @enderror" required>
                                <option value="active" {{ old('status', $representative->status) == 'active' ? 'selected' : '' }}>نشط</option>
                                <option value="inactive" {{ old('status', $representative->status) == 'inactive' ? 'selected' : '' }}>غير نشط</option>
                            </select>
                            @error('status')
                            <p class="text-red-500 text-sm mt-1">{{ $message }}</p>
                            @enderror
                        </div>
                    </div>
                </div>
            </div>

            <div class="flex justify-end gap-3">
                <button type="submit" class="inline-flex items-center px-8 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-save ml-2"></i>
                    <span>تحديث البيانات</span>
                </button>
            </div>
        </form>
    </div>
</div>
@endsection
