<x-app-layout>
<x-module-hero :title="'Edit Ticket #' . $ticket->unique_identifier" description="Modify ticket details" icon="fa-edit">
    <x-slot name="backButton">
        <a href="{{ route('tickets.show', $ticket) }}" class="inline-flex items-center gap-1 text-sm text-[#475569] hover:text-[#1e293b] transition-colors">
            <i class="fas fa-arrow-left text-xs"></i> Back to Ticket
        </a>
    </x-slot>
    <x-slot name="tabs">
        @can('view tickets')
        <a href="{{ route('tickets.index') }}"
           class="module-hero-tab {{ request()->routeIs('tickets.index') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }} inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
            <i class="fas fa-list text-sm"></i> All Tickets
        </a>
        @endcan
        @can('create tickets')
        <a href="{{ route('tickets.create') }}"
           class="module-hero-tab {{ request()->routeIs('tickets.create') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }} inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
            <i class="fas fa-plus text-sm"></i> New Ticket
        </a>
        @endcan
    </x-slot>
</x-module-hero>

<div class="p-6">
    @if(session('error'))
        <div class="mb-4 p-4 bg-red-100 border border-red-400 text-red-700 rounded-lg flex items-center gap-2">
            <i class="fas fa-exclamation-circle"></i> {{ session('error') }}
        </div>
    @endif

    <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
        <div class="lg:col-span-2">
            <form action="{{ route('tickets.update', $ticket) }}" method="POST">
                @csrf
                @method('PUT')

                {{-- Section: Ticket Details --}}
                <div class="mb-6">
                    <h3 class="text-lg font-semibold text-gray-800 border-b pb-2 mb-4">Ticket Details</h3>
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                        <div class="md:col-span-2">
                            <label for="title" class="block text-sm font-medium text-gray-700 mb-1">
                                Subject <span class="text-red-500">*</span>
                            </label>
                            <input type="text" id="title" name="title"
                                value="{{ old('title', $ticket->title) }}"
                                required
                                class="w-full px-4 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500 @error('title') border-red-400 @enderror">
                            @error('title')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                        </div>

                        <div>
                            <label for="priority" class="block text-sm font-medium text-gray-700 mb-1">
                                Priority <span class="text-red-500">*</span>
                            </label>
                            <select id="priority" name="priority" required
                                class="w-full px-4 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500">
                                <option value="low" {{ old('priority', $ticket->priority) == 'low' ? 'selected' : '' }}>Low</option>
                                <option value="medium" {{ old('priority', $ticket->priority) == 'medium' ? 'selected' : '' }}>Medium</option>
                                <option value="high" {{ old('priority', $ticket->priority) == 'high' ? 'selected' : '' }}>High</option>
                                <option value="urgent" {{ old('priority', $ticket->priority) == 'urgent' ? 'selected' : '' }}>Urgent</option>
                            </select>
                            @error('priority')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                        </div>

                        <div>
                            <label for="category" class="block text-sm font-medium text-gray-700 mb-1">
                                Category <span class="text-gray-400 text-xs">(optional)</span>
                            </label>
                            <input type="text" id="category" name="category"
                                value="{{ old('category', $ticket->category) }}"
                                placeholder="e.g. Technical, Billing, General"
                                class="w-full px-4 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500">
                        </div>

                        @if(auth()->user()->hasAnyRole(['Super Manager', 'Manager']))
                        <div>
                            <label for="status" class="block text-sm font-medium text-gray-700 mb-1">Status</label>
                            <select id="status" name="status"
                                class="w-full px-4 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500">
                                <option value="pending" {{ old('status', $ticket->status) == 'pending' ? 'selected' : '' }}>Pending</option>
                                <option value="in_progress" {{ old('status', $ticket->status) == 'in_progress' ? 'selected' : '' }}>In Progress</option>
                                <option value="resolved" {{ old('status', $ticket->status) == 'resolved' ? 'selected' : '' }}>Resolved</option>
                                <option value="closed" {{ old('status', $ticket->status) == 'closed' ? 'selected' : '' }}>Closed</option>
                            </select>
                        </div>
                        @endif
                    </div>
                </div>

                {{-- Section: Description --}}
                <div class="mb-6">
                    <h3 class="text-lg font-semibold text-gray-800 border-b pb-2 mb-4">Description</h3>
                    <textarea id="description" name="description" rows="6" required
                        class="w-full px-4 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500 @error('description') border-red-400 @enderror">{{ old('description', $ticket->description) }}</textarea>
                    @error('description')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                </div>

                <div class="flex items-center justify-end gap-3 pt-4 border-t border-gray-200">
                    <a href="{{ route('tickets.show', $ticket) }}"
                       class="px-4 py-2 border border-gray-300 rounded-lg text-sm text-gray-700 hover:bg-gray-50 transition">
                        <i class="fas fa-times mr-1"></i> Cancel
                    </a>
                    <button type="submit"
                        class="px-6 py-2 bg-[#1e293b] text-white text-sm font-semibold rounded-lg hover:bg-[#334155] transition">
                        <i class="fas fa-save mr-1"></i> Save Changes
                    </button>
                </div>
            </form>
        </div>

        {{-- Ticket Info Sidebar --}}
        <div>
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
                <div class="px-5 py-4 border-b border-gray-100 bg-gray-50">
                    <h4 class="font-semibold text-gray-800">Ticket Information</h4>
                </div>
                <div class="divide-y divide-gray-100 text-sm">
                    <div class="px-5 py-3 flex justify-between">
                        <span class="text-gray-500">Ticket ID</span>
                        <span class="font-mono font-medium text-gray-800">{{ $ticket->unique_identifier }}</span>
                    </div>
                    <div class="px-5 py-3 flex justify-between">
                        <span class="text-gray-500">Created By</span>
                        <span class="font-medium text-gray-800">{{ $ticket->user->name }}</span>
                    </div>
                    <div class="px-5 py-3 flex justify-between">
                        <span class="text-gray-500">Created</span>
                        <span class="font-medium text-gray-800">{{ $ticket->created_at->format('M d, Y') }}</span>
                    </div>
                    <div class="px-5 py-3 flex justify-between">
                        <span class="text-gray-500">Last Updated</span>
                        <span class="font-medium text-gray-800">{{ $ticket->updated_at->diffForHumans() }}</span>
                    </div>
                    <div class="px-5 py-3 flex justify-between">
                        <span class="text-gray-500">Current Status</span>
                        <span>{!! $ticket->status_badge !!}</span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</x-app-layout>
