<x-app-layout>
    <x-slot name="title">Send Notification</x-slot>
    <div class="mb-6 flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4">
        <div>
            <h1 class="text-2xl font-bold text-gray-900">Send Notification</h1>
            <p class="text-sm text-gray-500 mt-1">Broadcast custom messages to users and customers.</p>
        </div>
    </div>

    @if(session('success'))
        <div class="mb-4 bg-green-50 p-4 rounded-md border border-green-200">
            <p class="text-green-700 text-sm font-medium">{{ session('success') }}</p>
        </div>
    @endif

    <div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden max-w-3xl">
        <form action="{{ route('notifications.store') }}" method="POST" class="p-6 sm:p-8" x-data="{ targetGroup: 'all' }">
            @csrf
            
            <div class="grid grid-cols-1 gap-y-6 gap-x-4 sm:grid-cols-6">
                <div class="sm:col-span-6">
                    <label class="block text-sm font-medium text-gray-700">Notification Title</label>
                    <input type="text" name="title" required placeholder="e.g. System Maintenance or Payment Reminder" class="mt-1 block w-full border-gray-300 rounded-md shadow-sm focus:ring-primary-500 focus:border-primary-500 sm:text-sm">
                </div>

                <div class="sm:col-span-6">
                    <label class="block text-sm font-medium text-gray-700">Message Body</label>
                    <textarea name="message" rows="4" required placeholder="Enter the notification message here..." class="mt-1 block w-full border-gray-300 rounded-md shadow-sm focus:ring-primary-500 focus:border-primary-500 sm:text-sm"></textarea>
                </div>

                <div class="sm:col-span-6 border-t border-gray-200 pt-6">
                    <label class="block text-sm font-medium text-gray-700 mb-4">Who should receive this notification?</label>
                    
                    <div class="space-y-4 sm:flex sm:items-center sm:space-y-0 sm:space-x-6">
                        <div class="flex items-center">
                            <input id="target_all" name="target_group" type="radio" value="all" x-model="targetGroup" class="focus:ring-primary-500 h-4 w-4 text-primary-600 border-gray-300">
                            <label for="target_all" class="ml-3 block text-sm font-medium text-gray-700">
                                All Users & Customers
                            </label>
                        </div>
                        <div class="flex items-center">
                            <input id="target_staff" name="target_group" type="radio" value="staff" x-model="targetGroup" class="focus:ring-primary-500 h-4 w-4 text-primary-600 border-gray-300">
                            <label for="target_staff" class="ml-3 block text-sm font-medium text-gray-700">
                                Staff Only (Admin/Agents)
                            </label>
                        </div>
                        <div class="flex items-center">
                            <input id="target_customers" name="target_group" type="radio" value="customers" x-model="targetGroup" class="focus:ring-primary-500 h-4 w-4 text-primary-600 border-gray-300">
                            <label for="target_customers" class="ml-3 block text-sm font-medium text-gray-700">
                                Customers Only
                            </label>
                        </div>
                        <div class="flex items-center">
                            <input id="target_specific" name="target_group" type="radio" value="specific" x-model="targetGroup" class="focus:ring-primary-500 h-4 w-4 text-primary-600 border-gray-300">
                            <label for="target_specific" class="ml-3 block text-sm font-medium text-gray-700">
                                Specific Users
                            </label>
                        </div>
                    </div>
                </div>

                <!-- Specific Users Select Box -->
                <div class="sm:col-span-6 bg-gray-50 p-4 rounded-lg border border-gray-200" x-show="targetGroup === 'specific'" x-cloak>
                    <label class="block text-sm font-medium text-gray-700 mb-2">Select Specific Recipients</label>
                    <select name="user_ids[]" multiple class="mt-1 block w-full border-gray-300 rounded-md shadow-sm focus:ring-primary-500 focus:border-primary-500 sm:text-sm h-48 custom-scrollbar">
                        <optgroup label="Staff (Admins & Agents)">
                            @foreach($staff as $user)
                                <option value="{{ $user->id }}">{{ $user->name }} ({{ $user->email }}) - {{ $user->roles->pluck('name')->first() ?? 'Staff' }}</option>
                            @endforeach
                        </optgroup>
                        <optgroup label="Customers">
                            @foreach($customers as $user)
                                <option value="{{ $user->id }}">{{ $user->name }} ({{ $user->email }})</option>
                            @endforeach
                        </optgroup>
                    </select>
                    <p class="text-xs text-gray-500 mt-2">Hold Ctrl (Windows) or Cmd (Mac) to select multiple specific users.</p>
                </div>
            </div>

            <div class="pt-5 mt-6 border-t border-gray-200 flex justify-end">
                <button type="submit" class="inline-flex justify-center items-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-primary-600 hover:bg-primary-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary-500 transition-colors">
                    <svg class="h-4 w-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 19l9 2-9-18-9 18 9-2zm0 0v-8"></path></svg>
                    Send Notification
                </button>
            </div>
        </form>
    </div>
</x-app-layout>
