@extends('layouts.app')

@section('content')
<div class="container mx-auto px-4 py-8">
    <div class="max-w-2xl mx-auto">
        <h1 class="text-3xl font-bold text-gray-900 mb-6">Edit Ticket Center</h1>

        <div class="bg-white shadow-md rounded-lg p-6">
            <form action="{{ route('admin.ticket-centers.update', $ticketCenter) }}" method="POST">
                @csrf
                @method('PUT')
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <label for="name" class="block text-sm font-medium text-gray-700 mb-2">Center Name</label>
                        <input type="text" 
                               id="name" 
                               name="name" 
                               value="{{ old('name', $ticketCenter->name) }}"
                               class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 @error('name') border-red-500 @enderror"
                               required>
                        @error('name')
                            <p class="text-red-500 text-sm mt-1">{{ $message }}</p>
                        @enderror
                    </div>

                    <div>
                        <label for="location" class="block text-sm font-medium text-gray-700 mb-2">Location</label>
                        <input type="text" 
                               id="location" 
                               name="location" 
                               value="{{ old('location', $ticketCenter->location) }}"
                               class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 @error('location') border-red-500 @enderror"
                               required>
                        @error('location')
                            <p class="text-red-500 text-sm mt-1">{{ $message }}</p>
                        @enderror
                    </div>

                    <div class="md:col-span-2">
                        <label for="staff_user_id" class="block text-sm font-medium text-gray-700 mb-2">Assign Staff Member</label>
                        <select id="staff_user_id" 
                                name="staff_user_id" 
                                class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 @error('staff_user_id') border-red-500 @enderror"
                                required>
                            <option value="">Select Staff Member</option>
                            @forelse($availableStaff as $staff)
                                <option value="{{ $staff->id }}" 
                                        {{ old('staff_user_id', $ticketCenter->staff_user_id) == $staff->id ? 'selected' : '' }}>
                                    {{ $staff->name }} ({{ $staff->email }})
                                    @if($ticketCenter->staff && $ticketCenter->staff->id == $staff->id)
                                        - Current Assignment
                                    @endif
                                </option>
                            @empty
                                <option value="" disabled>No available staff members</option>
                            @endforelse
                        </select>
                        @error('staff_user_id')
                            <p class="text-red-500 text-sm mt-1">{{ $message }}</p>
                        @enderror
                        @if($availableStaff->isEmpty())
                            <p class="text-yellow-600 text-sm mt-1">
                                No available staff members. All staff are already assigned to other ticket centers.
                            </p>
                        @endif
                    </div>
                </div>

                <div class="flex justify-end space-x-4 mt-6">
                    <a href="{{ route('admin.ticket-centers.index') }}" 
                       class="px-4 py-2 border border-gray-300 rounded-md text-gray-700 hover:bg-gray-50">
                        Cancel
                    </a>
                    <button type="submit" 
                            class="px-4 py-2 bg-blue-500 text-white rounded-md hover:bg-blue-600 focus:outline-none focus:ring-2 focus:ring-blue-500"
                            {{ $availableStaff->isEmpty() ? 'disabled' : '' }}>
                        Update Ticket Center
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection
