<x-app-layout>
<x-module-hero title="Compose SMS" description="Create and send SMS messages" icon="fa-comment-sms">
    <x-slot name="tabs">
        @can('view sms')
        <a href="{{ route('sms.index') }}"
           class="module-hero-tab 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
                  {{ request()->routeIs('sms.index') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}">
            <i class="fas fa-tachometer-alt text-sm"></i> Dashboard
        </a>
        @endcan
        @can('create sms')
        <a href="{{ route('sms.compose') }}"
           class="module-hero-tab 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
                  {{ request()->routeIs('sms.compose') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}">
            <i class="fas fa-pen text-sm"></i> Compose
        </a>
        @endcan
        @can('view sms')
        <a href="{{ route('sms.history') }}"
           class="module-hero-tab 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
                  {{ request()->routeIs('sms.history') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}">
            <i class="fas fa-history text-sm"></i> History
        </a>
        @endcan
        @can('manage sms templates')
        <a href="{{ route('sms.templates') }}"
           class="module-hero-tab 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
                  {{ request()->routeIs('sms.templates') || request()->routeIs('sms.*template*') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}">
            <i class="fas fa-file-lines text-sm"></i> Templates
        </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

    <form action="{{ route('sms.store') }}" method="POST" x-data="smsComposer()">
        @csrf

        {{-- Section: Recipients --}}
        <div class="mb-6">
            <h3 class="text-lg font-semibold text-gray-800 border-b pb-2 mb-4">Recipients</h3>
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                {{-- Recipient Type --}}
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">Recipient Type <span class="text-red-500">*</span></label>
                    <div class="flex gap-4">
                        <label class="flex items-center gap-2 cursor-pointer">
                            <input type="radio" name="recipient_type" value="manual" x-model="recipientType" class="text-green-600">
                            <span class="text-sm">Manual Entry</span>
                        </label>
                        <label class="flex items-center gap-2 cursor-pointer">
                            <input type="radio" name="recipient_type" value="role" x-model="recipientType" class="text-green-600">
                            <span class="text-sm">By Role</span>
                        </label>
                    </div>
                </div>

                {{-- Template Selection --}}
                @if(isset($templates) && $templates->count() > 0)
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">Use Template (Optional)</label>
                    <select name="template_id" class="w-full px-4 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500" @change="applyTemplate($event)">
                        <option value="">Start from scratch</option>
                        @foreach($templates as $template)
                            <option value="{{ $template->id }}"
                                    data-content="{{ $template->content }}"
                                    {{ request('template') == $template->id ? 'selected' : '' }}>
                                {{ $template->name }}
                            </option>
                        @endforeach
                    </select>
                </div>
                @endif
            </div>

            {{-- Manual Recipients --}}
            <div x-show="recipientType === 'manual'" class="mt-4">
                <label class="block text-sm font-medium text-gray-700 mb-2">Phone Numbers <span class="text-red-500">*</span></label>
                <textarea name="recipients" rows="3"
                    class="w-full px-4 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500"
                    placeholder="Enter phone numbers separated by commas (e.g. +254700000001, +254700000002)">{{ old('recipients') }}</textarea>
                @error('recipients')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
            </div>

            {{-- Role Selection --}}
            <div x-show="recipientType === 'role'" class="mt-4">
                <label class="block text-sm font-medium text-gray-700 mb-2">Select Role <span class="text-red-500">*</span></label>
                <select name="target_role" class="w-full px-4 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500">
                    <option value="">Choose a role...</option>
                    @if(isset($availableRoles))
                        @foreach($availableRoles as $role)
                            <option value="{{ $role }}" {{ old('target_role') == $role ? 'selected' : '' }}>{{ $role }}</option>
                        @endforeach
                    @endif
                </select>
                @error('target_role')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
            </div>
        </div>

        {{-- Section: Message --}}
        <div class="mb-6">
            <h3 class="text-lg font-semibold text-gray-800 border-b pb-2 mb-4">Message</h3>

            {{-- Sender ID --}}
            <div class="mb-4">
                <label class="block text-sm font-medium text-gray-700 mb-2">Sender ID (Optional)</label>
                <input type="text" name="sender_id_name" value="{{ old('sender_id_name') }}" maxlength="11"
                    class="w-full md:w-64 px-4 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500"
                    placeholder="e.g. SSHIA (max 11 chars)">
                <p class="mt-1 text-xs text-gray-500">Leave blank to use the default sender ID.</p>
            </div>

            {{-- Message Body --}}
            <div class="mb-4">
                <label class="block text-sm font-medium text-gray-700 mb-2">
                    Message <span class="text-red-500">*</span>
                    <span class="ml-2 text-xs text-gray-500 font-normal" x-text="charCount + ' characters / ' + segmentCount + ' segment(s)'"></span>
                </label>
                <textarea name="message" rows="5" x-model="messageBody" @input="updateCount"
                    class="w-full px-4 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500"
                    placeholder="Type your SMS message here...">{{ old('message') }}</textarea>
                @error('message')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
            </div>
        </div>

        {{-- Section: Scheduling --}}
        <div class="mb-6">
            <h3 class="text-lg font-semibold text-gray-800 border-b pb-2 mb-4">Scheduling</h3>
            <div class="flex items-center gap-3 mb-3">
                <input type="checkbox" id="schedule-toggle" class="rounded border-gray-300 text-green-600 focus:ring-green-500"
                       @change="showSchedule = $event.target.checked">
                <label for="schedule-toggle" class="text-sm font-medium text-gray-700">Schedule for later</label>
            </div>
            <div x-show="showSchedule" class="mt-3">
                <label class="block text-sm font-medium text-gray-700 mb-2">Send Date &amp; Time</label>
                <input type="datetime-local" name="scheduled_at" value="{{ old('scheduled_at') }}"
                    class="px-4 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500">
                @error('scheduled_at')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
            </div>
        </div>

        {{-- Submit --}}
        <div class="flex items-center justify-end gap-3 pt-4 border-t border-gray-200">
            <a href="{{ route('sms.index') }}" class="px-6 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-[#1a4731] text-white text-sm font-semibold rounded-lg hover:bg-[#276749] transition">
                <i class="fas fa-paper-plane mr-1"></i> Send SMS
            </button>
        </div>
    </form>
</div>

@push('scripts')
<script>
function smsComposer() {
    return {
        recipientType: '{{ old('recipient_type', 'manual') }}',
        messageBody: @json(old('message', '')),
        showSchedule: false,
        get charCount() {
            return this.messageBody.length;
        },
        get segmentCount() {
            return Math.max(1, Math.ceil(this.messageBody.length / 160));
        },
        updateCount() {},
        applyTemplate(e) {
            const opt = e.target.selectedOptions[0];
            if (opt && opt.value) {
                this.messageBody = opt.dataset.content || '';
            }
        }
    }
}
</script>
@endpush
</x-app-layout>
