@extends('layouts.app')

@section('title', \App\Helpers\TranslationHelper::transFromDatabase('Edit Leave Request', __('Edit Leave Request')))

@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="flex items-center justify-between mb-8">
            <div>
                <h1 class="text-3xl font-bold text-gray-900 mb-2">{{ \App\Helpers\TranslationHelper::transFromDatabase('Edit Leave Request', __('Edit Leave Request')) }}</h1>
                <p class="text-gray-600">{{ \App\Helpers\TranslationHelper::transFromDatabase('Update the details of the leave request', __('Update the details of the leave request')) }}</p>
            </div>
            <a href="{{ route('employee-leaves.index') }}" class="inline-flex items-center px-4 py-2 bg-white border border-gray-300 rounded-lg text-gray-700 font-medium hover:bg-gray-50 transition-all duration-200 shadow-sm">
                <i class="fas fa-arrow-right ml-2"></i>
                <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('Back to List', __('Back to List')) }}</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"></i>
                </div>
                <div class="mr-3">
                    <ul class="list-disc list-inside text-red-700">
                        @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            </div>
        </div>
        @endif

        <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200">
            <form action="{{ route('employee-leaves.update', $employeeLeave) }}" method="POST" enctype="multipart/form-data" class="p-8">
                @csrf
                @method('PUT')
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
                    <div>
                        <label for="employee_id" class="block text-sm font-semibold text-gray-700 mb-2">{{ \App\Helpers\TranslationHelper::transFromDatabase('Employee', __('Employee')) }} <span class="text-red-500">*</span></label>
                        <select name="employee_id" id="employee_id" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-all @error('employee_id') border-red-500 @enderror" required>
                            <option value="">-- {{ \App\Helpers\TranslationHelper::transFromDatabase('Select Employee', __('Select Employee')) }} --</option>
                            @foreach($employees as $employee)
                            <option value="{{ $employee->id }}" {{ old('employee_id', $employeeLeave->employee_id) == $employee->id ? 'selected' : '' }}>
                                {{ $employee->user->name }} ({{ $employee->employee_number }})
                            </option>
                            @endforeach
                        </select>
                        @error('employee_id')
                        <p class="mt-1 text-sm text-red-500">{{ $message }}</p>
                        @enderror
                    </div>

                    <div>
                        <label for="leave_type" class="block text-sm font-semibold text-gray-700 mb-2">{{ \App\Helpers\TranslationHelper::transFromDatabase('Leave Type', __('Leave Type')) }} <span class="text-red-500">*</span></label>
                        <select name="leave_type" id="leave_type" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-all @error('leave_type') border-red-500 @enderror" required>
                            <option value="">-- {{ \App\Helpers\TranslationHelper::transFromDatabase('Select Leave Type', __('Select Leave Type')) }} --</option>
                            @foreach($leaveTypes as $value => $label)
                            <option value="{{ $value }}" {{ old('leave_type', $employeeLeave->leave_type) == $value ? 'selected' : '' }}>
                                {{ $label }}
                            </option>
                            @endforeach
                        </select>
                        @error('leave_type')
                        <p class="mt-1 text-sm text-red-500">{{ $message }}</p>
                        @enderror
                    </div>
                </div>

                <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
                    <div>
                        <label for="start_date" class="block text-sm font-semibold text-gray-700 mb-2">{{ \App\Helpers\TranslationHelper::transFromDatabase('Start Date', __('Start Date')) }} <span class="text-red-500">*</span></label>
                        <input type="date" name="start_date" id="start_date" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-all @error('start_date') border-red-500 @enderror" value="{{ old('start_date', $employeeLeave->start_date) }}" required>
                        @error('start_date')
                        <p class="mt-1 text-sm text-red-500">{{ $message }}</p>
                        @enderror
                    </div>

                    <div>
                        <label for="end_date" class="block text-sm font-semibold text-gray-700 mb-2">{{ \App\Helpers\TranslationHelper::transFromDatabase('End Date', __('End Date')) }} <span class="text-red-500">*</span></label>
                        <input type="date" name="end_date" id="end_date" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-all @error('end_date') border-red-500 @enderror" value="{{ old('end_date', $employeeLeave->end_date) }}" required>
                        @error('end_date')
                        <p class="mt-1 text-sm text-red-500">{{ $message }}</p>
                        @enderror
                    </div>
                </div>

                <div class="mb-6">
                    <label for="reason" class="block text-sm font-semibold text-gray-700 mb-2">{{ \App\Helpers\TranslationHelper::transFromDatabase('Reason', __('Reason')) }} <span class="text-red-500">*</span></label>
                    <textarea name="reason" id="reason" rows="4" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-all @error('reason') border-red-500 @enderror" required>{{ old('reason', $employeeLeave->reason) }}</textarea>
                    @error('reason')
                    <p class="mt-1 text-sm text-red-500">{{ $message }}</p>
                    @enderror
                </div>

                <div class="mb-8">
                    <label class="block text-sm font-semibold text-gray-700 mb-2">{{ \App\Helpers\TranslationHelper::transFromDatabase('Attachments', __('Attachments')) }}</label>
                    @if($employeeLeave->attachment)
                    <div class="mb-4 p-4 bg-gray-50 rounded-lg border border-gray-200 flex items-center justify-between">
                        <div class="flex items-center">
                            <i class="fas fa-paperclip text-gray-400 ml-2"></i>
                            <span class="text-sm text-gray-600">{{ \App\Helpers\TranslationHelper::transFromDatabase('Current Attachment', __('Current Attachment')) }}</span>
                        </div>
                        <a href="{{ Storage::url($employeeLeave->attachment) }}" target="_blank" class="text-blue-600 hover:text-blue-500 text-sm font-medium">
                            <i class="fas fa-download ml-1"></i>
                            {{ \App\Helpers\TranslationHelper::transFromDatabase('View File', __('View File')) }}
                        </a>
                    </div>
                    @endif
                    
                    <div class="mt-1 flex justify-center px-6 pt-5 pb-6 border-2 border-gray-300 border-dashed rounded-lg hover:border-blue-400 transition-colors">
                        <div class="space-y-1 text-center">
                            <i class="fas fa-cloud-upload-alt text-gray-400 text-3xl mb-3"></i>
                            <div class="flex text-sm text-gray-600">
                                <label for="attachment" class="relative cursor-pointer bg-white rounded-md font-medium text-blue-600 hover:text-blue-500 focus-within:outline-none focus-within:ring-2 focus-within:ring-offset-2 focus-within:ring-blue-500">
                                    <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('Upload a new file', __('Upload a new file')) }}</span>
                                    <input id="attachment" name="attachment" type="file" class="sr-only">
                                </label>
                                <p class="pr-1">{{ \App\Helpers\TranslationHelper::transFromDatabase('or drag and drop', __('or drag and drop')) }}</p>
                            </div>
                            <p class="text-xs text-gray-500">PDF, DOC, JPG, PNG up to 10MB</p>
                        </div>
                    </div>
                    <div id="file-name" class="mt-2 text-sm text-gray-600 hidden">
                        <i class="fas fa-file ml-1"></i>
                        <span id="file-name-text"></span>
                    </div>
                    @error('attachment')
                    <p class="mt-1 text-sm text-red-500">{{ $message }}</p>
                    @enderror
                </div>

                <div class="flex items-center justify-end gap-4">
                    <a href="{{ route('employee-leaves.index') }}" class="px-6 py-2 border border-gray-300 rounded-lg text-gray-700 font-semibold hover:bg-gray-50 transition-all">
                        {{ \App\Helpers\TranslationHelper::transFromDatabase('Cancel', __('Cancel')) }}
                    </a>
                    <button type="submit" class="px-10 py-2 bg-gradient-to-r from-blue-600 to-blue-700 text-white font-bold rounded-lg hover:from-blue-700 hover:to-blue-800 transition-all shadow-lg hover:shadow-xl transform hover:scale-105">
                        <i class="fas fa-save ml-2"></i>
                        {{ \App\Helpers\TranslationHelper::transFromDatabase('Update Leave Request', __('Update Leave Request')) }}
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script>
    document.getElementById('attachment').addEventListener('change', function(e) {
        const fileName = e.target.files[0] ? e.target.files[0].name : '';
        const fileNameContainer = document.getElementById('file-name');
        const fileNameText = document.getElementById('file-name-text');
        
        if (fileName) {
            fileNameText.textContent = fileName;
            fileNameContainer.classList.remove('hidden');
        } else {
            fileNameContainer.classList.add('hidden');
        }
    });
</script>
@endsection
