@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>
                @can('hr.deductions.create')
                <a href="{{ route('deductions.create') }}" class="inline-flex items-center px-6 py-3 bg-gradient-to-r from-red-600 to-red-700 text-white font-semibold rounded-lg hover:from-red-700 hover:to-red-800 transition-all duration-200 shadow-lg hover:shadow-xl transform hover:scale-105">
                    <i class="fas fa-minus ml-2"></i>
                    <span>إضافة خصم/استقطاع</span>
                </a>
                @endcan
            </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

            @if(session('error'))
            <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">{{ session('error') }}</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-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($deductions as $deduction)
                        <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">{{ $deduction->employee->user->name }}</div>
                                <div class="text-xs text-gray-500">{{ $deduction->employee->employee_number }}</div>
                            </td>
                            <td class="px-6 py-4">
                                <span class="font-bold text-red-600">{{ number_format($deduction->amount, 2) }}</span>
                                <span class="text-xs text-gray-500">ر.س</span>
                            </td>
                            <td class="px-6 py-4 text-gray-700">{{ $deduction->deduction_date->format('Y-m-d') }}</td>
                            <td class="px-6 py-4 text-gray-700">
                                @php
                                    $types = [
                                        'absence' => 'غياب',
                                        'late' => 'تأخير',
                                        'loan' => 'قسط قرض',
                                        'penalty' => 'جزاء إداري',
                                        'other' => 'أخرى',
                                    ];
                                    echo $types[$deduction->deduction_type] ?? $deduction->deduction_type;
                                @endphp
                            </td>
                            <td class="px-6 py-4">
                                @php
                                    $statusClasses = [
                                        'pending' => 'bg-yellow-100 text-yellow-700',
                                        'approved' => 'bg-green-100 text-green-700',
                                        'rejected' => 'bg-red-100 text-red-700',
                                        'paid' => 'bg-blue-100 text-blue-700',
                                    ];
                                    $class = $statusClasses[$deduction->status] ?? 'bg-gray-100 text-gray-700';
                                    
                                    $statusTexts = [
                                        'pending' => 'قيد الانتظار',
                                        'approved' => 'معتمدة',
                                        'rejected' => 'مرفوضة',
                                        'paid' => 'مخصومة',
                                    ];
                                    $statusText = $statusTexts[$deduction->status] ?? $deduction->status;
                                @endphp
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium {{ $class }}">
                                    {{ $statusText }}
                                </span>
                            </td>
                            <td class="px-6 py-4">
                                <div class="flex items-center justify-center gap-1">
                                    @if($deduction->status === 'pending')
                                        @can('hr.deductions.edit')
                                        <a href="{{ route('deductions.edit', $deduction->id) }}" 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 whitespace-nowrap" title="تعديل">
                                            <i class="fas fa-edit mr-1"></i>
                                            <span>تعديل</span>
                                        </a>
                                        @endcan

                                        @can('hr.deductions.delete')
                                        <form action="{{ route('deductions.delete', $deduction->id) }}" 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 whitespace-nowrap" title="حذف" onclick="return confirm('هل أنت متأكد من حذف هذا الخصم؟')">
                                                <i class="fas fa-trash mr-1"></i>
                                                <span>حذف</span>
                                            </button>
                                        </form>
                                        @endcan
                                    @endif
                                </div>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="7" class="px-6 py-12 text-center">
                                <div class="flex flex-col items-center justify-center">
                                    <i class="fas fa-user-minus text-gray-400 text-4xl mb-3"></i>
                                    <p class="text-gray-600 font-medium">لا توجد خصومات حالياً</p>
                                    <p class="text-gray-500 text-sm mt-1">ابدأ بإضافة خصم جديد</p>
                                </div>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            @if($deductions->hasPages())
            <div class="px-6 py-4 bg-gray-50 border-t border-gray-200">
                <div class="flex justify-center">
                    {{ $deductions->links() }}
                </div>
            </div>
            @endif
        </div>
    </div>
</div>
@endsection
