<?php if (isset($component)) { $__componentOriginal9ac128a9029c0e4701924bd2d73d7f54 = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54 = $attributes; } ?>
<?php $component = App\View\Components\AppLayout::resolve([] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
<?php $component->withName('app-layout'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php $attributes = $attributes->except(\App\View\Components\AppLayout::ignoredParameterNames()); ?>
<?php endif; ?>
<?php $component->withAttributes([]); ?>
     <?php $__env->slot('header', null, []); ?> 
        <div class="d-flex align-items-center justify-content-between">
            <div>
                <h2 class="h4 mb-0">SMS Messages</h2>
            </div>
            <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('sms.create')): ?>
                <button class="btn btn-primary" id="sendSmsButton">Send SMS</button>
            <?php endif; ?>
        </div>
     <?php $__env->endSlot(); ?>

    <div class="card shadow-sm border-0">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-bordered table-hover" id="smsMessagesTable" style="width:100%">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Recipient</th>
                            <th>Gateway</th>
                            <th>Message</th>
                            <th>Status</th>
                            <th>Sent At</th>
                            <th class="text-end">Actions</th>
                        </tr>
                    </thead>
                </table>
            </div>
        </div>
    </div>

    <?php echo $__env->make('admin.sms-messages.partials.modal', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>

    <?php $__env->startPush('scripts'); ?>
        <script>
            document.addEventListener('DOMContentLoaded', function () {
                const messageModal = new bootstrap.Modal(document.getElementById('smsMessageModal'));
                const table = $('#smsMessagesTable').DataTable({
                    processing: true,
                    serverSide: true,
                    responsive: true,
                    ajax: '<?php echo route('admin.sms-messages.data'); ?>',
                    columns: [
                        { data: 'id', name: 'id' },
                        { data: 'recipient', name: 'recipient' },
                        { data: 'gateway_name', name: 'gateway.name', orderable: false, searchable: false },
                        { data: 'message', name: 'message', orderable: false, searchable: false },
                        { data: 'status_label', name: 'status', orderable: false, searchable: false },
                        { data: 'sent_at', name: 'sent_at', orderable: false, searchable: false },
                        { data: 'actions', name: 'actions', orderable: false, searchable: false, className: 'text-end' },
                    ]
                });

                $('#sendSmsButton').on('click', function () {
                    $('#smsMessageForm')[0].reset();
                    messageModal.show();
                });

                $('#smsAudience').on('change', function () {
                    const isSingle = this.value === 'single';
                    const isZone = this.value === 'zone';
                    $('#smsRecipientField').toggleClass('d-none', !isSingle);
                    $('#smsZoneField').toggleClass('d-none', !isZone);
                    $('#smsRecipientField input').prop('required', isSingle);
                    $('#smsZoneField select').prop('required', isZone);
                }).trigger('change');

                $(document).on('submit', '#smsMessageForm', function (e) {
                    e.preventDefault();
                    const form = $(this);
                    const action = form.attr('action');
                    const data = form.serialize();

                    $.ajax({
                        url: action,
                        method: 'POST',
                        data: data,
                        success: function () {
                            messageModal.hide();
                            table.ajax.reload(null, false);
                            showToast('SMS queued successfully.', 'success');
                        },
                        error: function (xhr) {
                            handleFormErrors(xhr, form);
                        }
                    });
                });
                $('#smsTemplate').on('change', function () {
                    const option = this.options[this.selectedIndex];
                    const body = option.dataset.body || '';
                    $('#smsMessageBody').val(body).prop('required', !this.value).prop('readonly', !!this.value);
                });
            });
        </script>
    <?php $__env->stopPush(); ?>
 <?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54)): ?>
<?php $attributes = $__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54; ?>
<?php unset($__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54); ?>
<?php endif; ?>
<?php if (isset($__componentOriginal9ac128a9029c0e4701924bd2d73d7f54)): ?>
<?php $component = $__componentOriginal9ac128a9029c0e4701924bd2d73d7f54; ?>
<?php unset($__componentOriginal9ac128a9029c0e4701924bd2d73d7f54); ?>
<?php endif; ?>
<?php /**PATH D:\herd\billing\laravel-app\resources\views/admin/sms-messages/index.blade.php ENDPATH**/ ?>