@extends('layouts.app')

@section('title', \App\Helpers\TranslationHelper::transFromDatabase('Employee Leaves', __('Employee Leaves')))

@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">{{ \App\Helpers\TranslationHelper::transFromDatabase('Employee Leaves', __('Employee Leaves')) }}</h1>
                    <p class="text-gray-600">{{ \App\Helpers\TranslationHelper::transFromDatabase('Manage and track employee leave requests', __('Manage and track employee leave requests')) }}</p>
                </div>
                @can('hr.leaves.create')
                <a href="{{ route('employee-leaves.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>{{ \App\Helpers\TranslationHelper::transFromDatabase('Add New Leave', __('Add New Leave')) }}</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">{{ \App\Helpers\TranslationHelper::transFromDatabase('Employee', __('Employee')) }}</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Leave Type', __('Leave Type')) }}</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Start Date', __('Start Date')) }}</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('End Date', __('End Date')) }}</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Days', __('Days')) }}</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Status', __('Status')) }}</th>
                            <th class="px-6 py-4 text-center font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Actions', __('Actions')) }}</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($leaves as $leave)
                        <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 font-semibold text-gray-900">
                                {{ $leave->employee->user->name }}
                            </td>
                            <td class="px-6 py-4 text-gray-700">
                                @if(isset($leave::$leaveTypes[$leave->leave_type]))
                                    {{ $leave::$leaveTypes[$leave->leave_type] }}
                                @else
                                    {{ $leave->leave_type }}
                                @endif
                            </td>
                            <td class="px-6 py-4 text-gray-700">{{ $leave->start_date }}</td>
                            <td class="px-6 py-4 text-gray-700">{{ $leave->end_date }}</td>
                            <td class="px-6 py-4 text-gray-700">{{ $leave->days }}</td>
                            <td class="px-6 py-4">
                                @if($leave->status == 'pending')
                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-amber-100 text-amber-700">
                                        <span class="w-2 h-2 bg-amber-500 rounded-full ml-2"></span>
                                        {{ \App\Helpers\TranslationHelper::transFromDatabase('Pending', __('Pending')) }}
                                    </span>
                                @elseif($leave->status == 'approved')
                                    <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>
                                        {{ \App\Helpers\TranslationHelper::transFromDatabase('Approved', __('Approved')) }}
                                    </span>
                                @elseif($leave->status == 'rejected')
                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-red-100 text-red-700">
                                        <span class="w-2 h-2 bg-red-500 rounded-full ml-2"></span>
                                        {{ \App\Helpers\TranslationHelper::transFromDatabase('Rejected', __('Rejected')) }}
                                    </span>
                                @else
                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-gray-100 text-gray-700">
                                        {{ $leave->status }}
                                    </span>
                                @endif
                            </td>
                            <td class="px-6 py-4">
                                <div class="flex items-center justify-center gap-1">
                                    @can('hr.leaves.view')
                                    <a href="{{ route('employee-leaves.show', $leave) }}" class="inline-flex items-center px-2 py-1 rounded-lg bg-blue-100 text-blue-600 hover:bg-blue-200 transition-all duration-150 text-xs font-medium whitespace-nowrap" title="{{ \App\Helpers\TranslationHelper::transFromDatabase('View Details', __('View Details')) }}">
                                        <i class="fas fa-eye mr-1"></i>
                                        <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('View', __('View')) }}</span>
                                    </a>
                                    @endcan
                                    
                                    @if($leave->status == 'pending')
                                        @can('hr.leaves.edit')
                                        <a href="{{ route('employee-leaves.edit', $leave) }}" class="inline-flex items-center px-2 py-1 rounded-lg bg-amber-100 text-amber-600 hover:bg-amber-200 transition-all duration-150 text-xs font-medium whitespace-nowrap" title="{{ \App\Helpers\TranslationHelper::transFromDatabase('Edit', __('Edit')) }}">
                                            <i class="fas fa-edit mr-1"></i>
                                            <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('Edit', __('Edit')) }}</span>
                                        </a>
                                        @endcan
                                        
                                        @can('hr.leaves.delete')
                                        <form action="{{ route('employee-leaves.destroy', $leave) }}" method="POST" class="inline" onsubmit="return confirm('{{ \App\Helpers\TranslationHelper::transFromDatabase('Are you sure you want to delete this leave request?', __('Are you sure you want to delete this leave request?')) }}');">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="inline-flex items-center px-2 py-1 rounded-lg bg-red-100 text-red-600 hover:bg-red-200 transition-all duration-150 text-xs font-medium whitespace-nowrap" title="{{ \App\Helpers\TranslationHelper::transFromDatabase('Delete', __('Delete')) }}">
                                                <i class="fas fa-trash mr-1"></i>
                                                <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('Delete', __('Delete')) }}</span>
                                            </button>
                                        </form>
                                        @endcan
                                        
                                        @can('hr.leaves.approve')
                                        <form action="{{ route('employee-leaves.approve', $leave) }}" method="POST" class="inline" onsubmit="return confirm('{{ \App\Helpers\TranslationHelper::transFromDatabase('Are you sure you want to approve this leave request?', __('Are you sure you want to approve this leave request?')) }}');">
                                            @csrf
                                            @method('POST')
                                            <button type="submit" class="inline-flex items-center px-2 py-1 rounded-lg bg-green-100 text-green-600 hover:bg-green-200 transition-all duration-150 text-xs font-medium whitespace-nowrap" title="{{ \App\Helpers\TranslationHelper::transFromDatabase('Approve', __('Approve')) }}">
                                                <i class="fas fa-check mr-1"></i>
                                                <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('Approve', __('Approve')) }}</span>
                                            </button>
                                        </form>
                                        
                                        <button type="button" class="inline-flex items-center px-2 py-1 rounded-lg bg-red-100 text-red-600 hover:bg-red-200 transition-all duration-150 text-xs font-medium whitespace-nowrap" data-toggle="modal" data-target="#rejectModal{{ $leave->id }}" title="{{ \App\Helpers\TranslationHelper::transFromDatabase('Reject', __('Reject')) }}">
                                            <i class="fas fa-times mr-1"></i>
                                            <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('Reject', __('Reject')) }}</span>
                                        </button>
                                        @endcan
                                    @endif
                                </div>
                                
                                <!-- Modal for Rejection -->
                                @can('hr.leaves.reject')
                                <div class="modal fade" id="rejectModal{{ $leave->id }}" tabindex="-1" role="dialog" aria-labelledby="rejectModalLabel{{ $leave->id }}" aria-hidden="true">
                                    <div class="modal-dialog" role="document">
                                        <div class="modal-content">
                                            <form action="{{ route('employee-leaves.reject', $leave) }}" method="POST">
                                                @csrf
                                                @method('POST')
                                                <div class="modal-header">
                                                    <h5 class="modal-title" id="rejectModalLabel{{ $leave->id }}">{{ \App\Helpers\TranslationHelper::transFromDatabase('Reject Leave Request', __('Reject Leave Request')) }}</h5>
                                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                        <span aria-hidden="true">&times;</span>
                                                    </button>
                                                </div>
                                                <div class="modal-body">
                                                    <div class="form-group text-right">
                                                        <label for="rejection_reason">{{ \App\Helpers\TranslationHelper::transFromDatabase('Rejection Reason', __('Rejection Reason')) }} <span class="text-danger">*</span></label>
                                                        <textarea name="rejection_reason" id="rejection_reason" rows="3" class="form-control" required></textarea>
                                                    </div>
                                                </div>
                                                <div class="modal-footer">
                                                    <button type="button" class="btn btn-secondary" data-dismiss="modal">{{ \App\Helpers\TranslationHelper::transFromDatabase('Cancel', __('Cancel')) }}</button>
                                                    <button type="submit" class="btn btn-danger">{{ \App\Helpers\TranslationHelper::transFromDatabase('Reject Leave', __('Reject Leave')) }}</button>
                                                </div>
                                            </form>
                                        </div>
                                    </div>
                                </div>
                                @endcan
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="8" class="px-6 py-12 text-center">
                                <div class="flex flex-col items-center justify-center">
                                    <i class="fas fa-inbox text-gray-400 text-4xl mb-3"></i>
                                    <p class="text-gray-600 font-medium">{{ \App\Helpers\TranslationHelper::transFromDatabase('No leave requests currently', __('No leave requests currently')) }}</p>
                                </div>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>

        <div class="mt-6">
            {{ $leaves->links() }}
        </div>
    </div>
</div>
@endsection
