@extends('admin.layout.main')

{{-- @dd($project); --}}

@section('admin')
<div class="flex flex-col h-full">
    <!-- Header -->
    <div class="mb-8">
        <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4">
            <div>
                <h1 class="text-2xl md:text-3xl font-bold text-gray-900 dark:text-white">Upload Service Documents</h1>
                <p class="text-gray-500 dark:text-gray-400 mt-1">Manage documents for project services</p>
                <div class="flex items-center gap-2 mt-2">
                    <span class="text-sm text-gray-600 dark:text-gray-300">Project:</span>
                    <span class="text-sm font-medium text-blue-600 dark:text-blue-400">{{ $project->name }}</span>
                    <span class="text-sm text-gray-500">•</span>
                    <span class="text-sm text-gray-600 dark:text-gray-300">ID: {{ $project->id }}</span>
                </div>
            </div>
            <div class="flex items-center gap-3">
                <a href="{{ route('admin.project.show.index', $project->id) }}" 
                   class="inline-flex items-center gap-2 px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-300 bg-gray-100 dark:bg-gray-700 rounded-lg hover:bg-gray-200 dark:hover:bg-gray-600">
                    <i class="fas fa-arrow-left"></i>
                    Back to Project
                </a>
            </div>
        </div>
    </div>

    @if(session('success'))
        <div class="mb-6 p-4 bg-green-50 dark:bg-green-900/20 border border-green-200 dark:border-green-800 rounded-lg">
            <div class="flex items-center gap-3">
                <i class="fas fa-check-circle text-green-500"></i>
                <p class="text-green-700 dark:text-green-400">{{ session('success') }}</p>
            </div>
        </div>
    @endif

    @if(session('error'))
        <div class="mb-6 p-4 bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-lg">
            <div class="flex items-center gap-3">
                <i class="fas fa-exclamation-circle text-red-500"></i>
                <p class="text-red-700 dark:text-red-400">{{ session('error') }}</p>
            </div>
        </div>
    @endif

    <!-- Services List -->
    <div class="space-y-6">
        @php
            $services = is_array($project->services)
                ? $project->services
                : json_decode($project->services, true) ?? [];
        @endphp

        @foreach($services as $key => $service)
            @php
                $serviceDocuments = $service['documents'] ?? [];
                $totalDocuments = count($serviceDocuments);
                $serviceStatus = $service['status'] ?? 'Not Started';
            @endphp

            <div class="bg-white dark:bg-gray-800 rounded-xl border border-gray-200 dark:border-gray-700 shadow-sm overflow-hidden">
                <!-- Service Header -->
                <div class="p-6 border-b border-gray-100 dark:border-gray-700 bg-gray-50 dark:bg-gray-900/50">
                    <div class="flex flex-col md:flex-row md:items-center md:justify-between gap-4">
                        <div class="flex items-start gap-4">
                            <div class="w-12 h-12 rounded-lg bg-blue-50 dark:bg-blue-900/20 flex items-center justify-center flex-shrink-0">
                                <i class="fas fa-cogs text-blue-500 dark:text-blue-400 text-lg"></i>
                            </div>
                            <div>
                                <h3 class="text-lg font-semibold text-gray-900 dark:text-white">{{ $service['name'] ?? 'Unknown Service' }}</h3>
                                <div class="flex items-center gap-4 mt-1">
                                    <span class="text-sm text-gray-500 dark:text-gray-400">Service ID: {{ $service['id'] ?? 'N/A' }}</span>
                                    <form class="service-status-form" action="{{ route('admin.projects.update-service-status', $project->id) }}" method="POST">
                                        @csrf
                                        @method('PATCH')
                                        <input type="hidden" name="service_key" value="{{ $key }}">
                                        <select name="service_status" 
                                                class="text-xs px-3 py-1.5 rounded-lg border focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent
                                                @if($serviceStatus === 'Complete') bg-green-50 text-green-700 border-green-200
                                                @elseif($serviceStatus === 'In Progress') bg-blue-50 text-blue-700 border-blue-200
                                                @else bg-yellow-50 text-yellow-700 border-yellow-200 @endif">
                                            <option value="Not Started" @if($serviceStatus === 'Not Started') selected @endif>Not Started</option>
                                            <option value="In Progress" @if($serviceStatus === 'In Progress') selected @endif>In Progress</option>
                                            <option value="Complete" @if($serviceStatus === 'Complete') selected @endif>Complete</option>
                                        </select>
                                    </form>
                                </div>
                            </div>
                        </div>
                        <div class="flex items-center gap-4">
                            <div class="text-sm text-gray-600 dark:text-gray-400">
                                <i class="fas fa-file mr-1"></i>
                                {{ $totalDocuments }} document(s)
                            </div>
                            <div class="text-sm px-3 py-1 rounded-full font-medium
                                @if($serviceStatus === 'Complete')
                                    bg-green-100 text-green-800 dark:bg-green-900/30 dark:text-green-400
                                @elseif($serviceStatus === 'In Progress')
                                    bg-blue-100 text-blue-800 dark:bg-blue-900/30 dark:text-blue-400
                                @else
                                    bg-yellow-100 text-yellow-800 dark:bg-yellow-900/30 dark:text-yellow-400
                                @endif">
                                {{ $serviceStatus }}
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Uploaded Documents Section -->
                <div class="p-6">
                    <div class="flex items-center justify-between mb-4">
                        <h4 class="text-md font-medium text-gray-700 dark:text-gray-300 flex items-center gap-2">
                            <i class="fas fa-file-alt text-gray-400"></i>
                            Uploaded Documents
                        </h4>
                        @if($totalDocuments > 0)
                            <span class="text-sm text-gray-500 dark:text-gray-400">
                                Total: {{ $totalDocuments }}
                            </span>
                        @endif
                    </div>
                    
                    @if($totalDocuments > 0)
                        <div class="space-y-3 mb-6">
                            @foreach($serviceDocuments as $index => $document)
                                <div class="flex items-center justify-between p-4 bg-gray-50 dark:bg-gray-900/30 rounded-lg hover:bg-gray-100 dark:hover:bg-gray-900/50 transition-colors">
                                    <div class="flex items-center gap-4">
                                        <div class="w-10 h-10 rounded-lg flex items-center justify-center 
                                            @if(in_array($document['file_type'] ?? '', ['pdf']))
                                                bg-red-50 dark:bg-red-900/20 text-red-500
                                            @elseif(in_array($document['file_type'] ?? '', ['jpg', 'jpeg', 'png']))
                                                bg-green-50 dark:bg-green-900/20 text-green-500
                                            @elseif(in_array($document['file_type'] ?? '', ['doc', 'docx']))
                                                bg-blue-50 dark:bg-blue-900/20 text-blue-500
                                            @elseif(in_array($document['file_type'] ?? '', ['xls', 'xlsx']))
                                                bg-emerald-50 dark:bg-emerald-900/20 text-emerald-500
                                            @else
                                                bg-gray-50 dark:bg-gray-900/20 text-gray-500
                                            @endif">
                                            @if(in_array($document['file_type'] ?? '', ['pdf']))
                                                <i class="fas fa-file-pdf"></i>
                                            @elseif(in_array($document['file_type'] ?? '', ['jpg', 'jpeg', 'png']))
                                                <i class="fas fa-file-image"></i>
                                            @elseif(in_array($document['file_type'] ?? '', ['doc', 'docx']))
                                                <i class="fas fa-file-word"></i>
                                            @elseif(in_array($document['file_type'] ?? '', ['xls', 'xlsx']))
                                                <i class="fas fa-file-excel"></i>
                                            @else
                                                <i class="fas fa-file"></i>
                                            @endif
                                        </div>
                                        <div class="flex-1">
                                            <div class="flex items-start justify-between">
                                                <div>
                                                    <p class="font-medium text-gray-900 dark:text-white">{{ $document['name'] }}</p>
                                                    <div class="flex items-center gap-3 mt-1 text-xs text-gray-500 dark:text-gray-400">
                                                        <span>{{ $document['original_name'] }}</span>
                                                        <span>•</span>
                                                        <span>{{ round(($document['file_size'] ?? 0) / 1024) }} KB</span>
                                                        <span>•</span>
                                                        <span>{{ strtoupper($document['file_type'] ?? '') }}</span>
                                                        @if($document['notes'])
                                                            <span>•</span>
                                                            <span title="{{ $document['notes'] }}">
                                                                <i class="fas fa-sticky-note"></i>
                                                            </span>
                                                        @endif
                                                    </div>
                                                    <p class="text-xs text-gray-400 dark:text-gray-500 mt-1">
                                                        Uploaded: {{ \Carbon\Carbon::parse($document['uploaded_at'])->format('d M Y, h:i A') }}
                                                    </p>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="flex items-center gap-2">
                                        <a href="{{ $document['file_path'] }}" 
                                           target="_blank"
                                           class="p-2 text-gray-500 hover:text-blue-500 dark:text-gray-400 dark:hover:text-blue-400 rounded-lg hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors"
                                           title="View">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                        <a href="{{ $document['file_path'] }}" 
                                           download="{{ $document['original_name'] }}"
                                           class="p-2 text-gray-500 hover:text-green-500 dark:text-gray-400 dark:hover:text-green-400 rounded-lg hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors"
                                           title="Download">
                                            <i class="fas fa-download"></i>
                                        </a>
                                        <form action="{{ route('admin.projects.delete-service-document', $project->id) }}" 
                                              method="POST" 
                                              class="inline"
                                              onsubmit="return confirm('Are you sure you want to delete this document?')">
                                            @csrf
                                            @method('DELETE')
                                            <input type="hidden" name="service_id" value="{{ $key }}">
                                            <input type="hidden" name="document_index" value="{{ $index }}">
                                            <button type="submit" 
                                                    class="p-2 text-gray-500 hover:text-red-500 dark:text-gray-400 dark:hover:text-red-400 rounded-lg hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors"
                                                    title="Delete">
                                                <i class="fas fa-trash"></i>
                                            </button>
                                        </form>
                                    </div>
                                </div>
                            @endforeach
                        </div>
                    @else
                        <div class="text-center py-8 mb-6 border-2 border-dashed border-gray-200 dark:border-gray-700 rounded-lg">
                            <i class="fas fa-folder-open text-4xl text-gray-300 dark:text-gray-600 mb-3"></i>
                            <p class="text-gray-500 dark:text-gray-400">No documents uploaded for this service</p>
                            <p class="text-sm text-gray-400 dark:text-gray-500 mt-1">Upload your first document below</p>
                        </div>
                    @endif

                    <!-- Upload New Document Form -->
                    <div class="border-t border-gray-100 dark:border-gray-700 pt-6">
                        <h4 class="text-md font-medium text-gray-700 dark:text-gray-300 mb-4 flex items-center gap-2">
                            <i class="fas fa-upload text-gray-400"></i>
                            Upload New Document
                        </h4>
                        
                        <form action="{{ route('admin.projects.store-service-document', $project->id) }}" 
                              method="POST" 
                              enctype="multipart/form-data"
                              class="space-y-4">
                            @csrf
                            
                            <input type="hidden" name="service_id" value="{{ $key }}">
                            
                            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                                <div>
                                    <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                        Document Name <span class="text-red-500">*</span>
                                    </label>
                                    <input type="text" 
                                           name="document_name" 
                                           required
                                           placeholder="e.g., Trade License, NID Copy"
                                           class="w-full px-4 py-2.5 border border-gray-300 dark:border-gray-600 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:text-white"
                                           value="{{ old('document_name') }}">
                                    @error('document_name')
                                        <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                                    @enderror
                                </div>
                                
                                <div>
                                    <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                        Document File <span class="text-red-500">*</span>
                                    </label>
                                    <div class="relative">
                                        <input type="file" 
                                               name="document_file" 
                                               required
                                               accept=".pdf,.jpg,.jpeg,.png,.doc,.docx,.xls,.xlsx"
                                               class="w-full px-4 py-2.5 border border-gray-300 dark:border-gray-600 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:text-white
                                                      file:mr-4 file:py-2 file:px-4 file:rounded-lg file:border-0 
                                                      file:text-sm file:font-medium file:bg-blue-50 file:text-blue-700 
                                                      hover:file:bg-blue-100 dark:file:bg-blue-900/30 dark:file:text-blue-400"
                                               onchange="previewFileName(this)">
                                        <div class="text-xs text-gray-500 dark:text-gray-400 mt-1">
                                            Max: 10MB • PDF, Images, DOC, Excel
                                        </div>
                                    </div>
                                    @error('document_file')
                                        <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                                    @enderror
                                </div>
                            </div>
                            
                            <div>
                                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                    Notes (Optional)
                                </label>
                                <textarea name="notes" 
                                          rows="2"
                                          placeholder="Add any notes or instructions..."
                                          class="w-full px-4 py-2.5 border border-gray-300 dark:border-gray-600 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:text-white">{{ old('notes') }}</textarea>
                                @error('notes')
                                    <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                                @enderror
                            </div>
                            
                            <div class="flex justify-end">
                                <button type="submit" 
                                        class="inline-flex items-center gap-2 bg-gradient-to-r from-blue-500 to-blue-600 text-white px-5 py-2.5 rounded-lg hover:from-blue-600 hover:to-blue-700 active:scale-[0.98] transition-all shadow-sm hover:shadow">
                                    <i class="fas fa-upload"></i>
                                    Upload Document
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        @endforeach

        @if(empty($services))
            <div class="text-center py-12 bg-white dark:bg-gray-800 rounded-xl border border-gray-200 dark:border-gray-700">
                <i class="fas fa-cogs text-4xl text-gray-300 dark:text-gray-600 mb-4"></i>
                <h3 class="text-lg font-medium text-gray-700 dark:text-gray-300 mb-2">No Services Found</h3>
                <p class="text-gray-500 dark:text-gray-400">This project doesn't have any services assigned yet.</p>
            </div>
        @endif
    </div>
</div>

<script>
function previewFileName(input) {
    const fileName = input.files[0]?.name;
    if (fileName) {
        console.log('Selected file:', fileName);
    }
}

// Auto-submit service status form
document.querySelectorAll('.service-status-form select').forEach(select => {
    select.addEventListener('change', function() {
        this.closest('form').submit();
    });
});
</script>
@endsection