

<?php $__env->startSection('title', 'Bulk SMS'); ?>
<?php $__env->startSection('page-title', 'Bulk SMS'); ?>
<?php $__env->startSection('page-subtitle', 'Send SMS campaigns to members or custom phone numbers'); ?>

<?php $__env->startSection('content'); ?>
<?php
    $campaignPage = $campaigns->getCollection();
    $recipientsOnPage = $campaignPage->sum('total_recipients');
    $failedOnPage = $campaignPage->sum('failed_count');
?>

<section class="section-hero mb-4">
    <div class="section-hero-copy">
        <span>Account center</span>
        <h2>Bulk SMS campaigns</h2>
        <p>Send reminders, announcements, thrift notices, payment alerts, and custom SMS campaigns to members or external numbers.</p>
    </div>
    <div class="section-hero-panel">
        <span>SMS status</span>
        <strong><?php echo e($smsConfigured ? 'Ready' : 'Log mode'); ?></strong>
        <small><?php echo e(number_format($customers->count())); ?> members with phone numbers</small>
    </div>
</section>

<div class="section-stat-grid mb-4">
    <div class="section-stat-card">
        <i class="bi bi-phone"></i>
        <span>Phone contacts</span>
        <strong><?php echo e(number_format($customers->count())); ?></strong>
        <small>Members available for SMS</small>
    </div>
    <div class="section-stat-card">
        <i class="bi bi-send-check"></i>
        <span>Campaigns shown</span>
        <strong><?php echo e(number_format($campaignPage->count())); ?></strong>
        <small>Current page</small>
    </div>
    <div class="section-stat-card">
        <i class="bi bi-people"></i>
        <span>Recipients</span>
        <strong><?php echo e(number_format($recipientsOnPage)); ?></strong>
        <small>From listed campaigns</small>
    </div>
    <div class="section-stat-card">
        <i class="bi bi-exclamation-triangle"></i>
        <span>Failed</span>
        <strong><?php echo e(number_format($failedOnPage)); ?></strong>
        <small>Delivery issues</small>
    </div>
</div>

<div class="row g-4">
    <div class="col-xl-5">
        <div class="section-form-card sticky-lg-top">
            <div class="section-card-title">
                <span><i class="bi bi-chat-dots"></i></span>
                <div>
                    <strong>New SMS campaign</strong>
                    <small>Uses the SMS gateway configured in tenant settings.</small>
                </div>
            </div>

            <form method="POST" action="<?php echo e(route('tenant.bulk-sms.store')); ?>" class="row g-3">
                <?php echo csrf_field(); ?>
                <div class="col-12">
                    <label class="form-label">Campaign name</label>
                    <input name="subject" value="<?php echo e(old('subject')); ?>" class="form-control" placeholder="Loan repayment reminder" required>
                </div>

                <div class="col-12">
                    <label class="form-label">Audience</label>
                    <select name="audience_type" id="bulkSmsAudience" class="form-select" required>
                        <option value="all" <?php if(old('audience_type', 'all') === 'all'): echo 'selected'; endif; ?>>All members</option>
                        <option value="status" <?php if(old('audience_type') === 'status'): echo 'selected'; endif; ?>>Member status</option>
                        <option value="kyc" <?php if(old('audience_type') === 'kyc'): echo 'selected'; endif; ?>>KYC status</option>
                        <option value="individual" <?php if(old('audience_type') === 'individual'): echo 'selected'; endif; ?>>Selected members</option>
                        <option value="numbers" <?php if(old('audience_type') === 'numbers'): echo 'selected'; endif; ?>>Custom phone numbers</option>
                    </select>
                </div>

                <div class="col-12 audience-box" data-bulk-sms-section="status">
                    <label class="form-label">Member group</label>
                    <select name="customer_status" class="form-select">
                        <option value="active" <?php if(old('customer_status', 'active') === 'active'): echo 'selected'; endif; ?>>Active members</option>
                        <option value="inactive" <?php if(old('customer_status') === 'inactive'): echo 'selected'; endif; ?>>Inactive members</option>
                        <option value="suspended" <?php if(old('customer_status') === 'suspended'): echo 'selected'; endif; ?>>Suspended members</option>
                    </select>
                </div>

                <div class="col-12 audience-box" data-bulk-sms-section="kyc">
                    <label class="form-label">KYC group</label>
                    <select name="kyc_status" class="form-select">
                        <option value="pending" <?php if(old('kyc_status', 'pending') === 'pending'): echo 'selected'; endif; ?>>Pending KYC</option>
                        <option value="verified" <?php if(old('kyc_status') === 'verified'): echo 'selected'; endif; ?>>Verified KYC</option>
                        <option value="rejected" <?php if(old('kyc_status') === 'rejected'): echo 'selected'; endif; ?>>Rejected KYC</option>
                    </select>
                </div>

                <div class="col-12 audience-box" data-bulk-sms-section="individual">
                    <label class="form-label">Members</label>
                    <select name="customer_ids[]" class="form-select" multiple size="8">
                        <?php $__currentLoopData = $customers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $customer): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <option value="<?php echo e($customer->id); ?>" <?php if(in_array($customer->id, old('customer_ids', []))): echo 'selected'; endif; ?>><?php echo e($customer->fullName()); ?> - <?php echo e($customer->phone); ?></option>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </select>
                    <div class="form-text">Hold Ctrl to select more than one member.</div>
                </div>

                <div class="col-12 audience-box" data-bulk-sms-section="numbers">
                    <label class="form-label">Phone numbers</label>
                    <textarea name="phone_numbers" class="form-control" rows="5" placeholder="Enter one number per line, or separate with commas"><?php echo e(old('phone_numbers')); ?></textarea>
                </div>

                <div class="col-12">
                    <label class="form-label">Message</label>
                    <textarea name="message" id="bulkSmsMessage" class="form-control" rows="5" maxlength="918" placeholder="Type your SMS message" required><?php echo e(old('message')); ?></textarea>
                    <div class="sms-counter">
                        <span id="bulkSmsCount">0</span>/918 characters
                        <span class="mx-1">-</span>
                        <span id="bulkSmsUnits">1</span> SMS unit<span id="bulkSmsUnitsPlural"></span>
                    </div>
                    <div class="form-text">1 SMS is up to 160 characters. Longer messages use 153 characters per SMS page, so 2 pages = 306 and 3 pages = 459.</div>
                </div>

                <div class="col-12 d-grid">
                    <button class="btn btn-primary btn-lg"><i class="bi bi-send me-1"></i> Send bulk SMS</button>
                </div>
            </form>
        </div>
    </div>

    <div class="col-xl-7">
        <div class="section-data-card">
            <div class="section-card-title">
                <span><i class="bi bi-clock-history"></i></span>
                <div>
                    <strong>SMS campaign history</strong>
                    <small>Recent bulk messages sent or logged by the system.</small>
                </div>
            </div>
            <div class="table-responsive">
                <table class="table section-table align-middle mb-0">
                    <thead><tr><th>Date</th><th>Campaign</th><th>Audience</th><th>Recipients</th><th>Status</th><th>Action</th></tr></thead>
                    <tbody>
                    <?php $__empty_1 = true; $__currentLoopData = $campaigns; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $campaign): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                        <?php
                            $lastSmsError = $campaign->recipients
                                ->pluck('sms_error')
                                ->filter()
                                ->first();
                        ?>
                        <tr>
                            <td><?php echo e(app_datetime($campaign->created_at)); ?></td>
                            <td>
                                <div class="fw-semibold"><?php echo e($campaign->subject); ?></div>
                                <small class="text-muted"><?php echo e(str($campaign->message)->limit(80)); ?></small>
                                <?php if($lastSmsError): ?>
                                    <div class="small text-danger mt-1"><i class="bi bi-exclamation-circle me-1"></i><?php echo e(str($lastSmsError)->limit(90)); ?></div>
                                <?php endif; ?>
                            </td>
                            <td><?php echo e(ucfirst(str_replace('_', ' ', $campaign->audience_type))); ?></td>
                            <td>
                                <div><?php echo e(number_format($campaign->total_recipients)); ?> total</div>
                                <small class="text-muted"><?php echo e(number_format($campaign->sms_sent_count)); ?> sent/logged, <?php echo e(number_format($campaign->failed_count)); ?> failed</small>
                            </td>
                            <td><span class="badge text-bg-<?php echo e($campaign->status === 'completed' ? 'success' : ($campaign->status === 'completed_with_errors' ? 'warning' : 'secondary')); ?>"><?php echo e(str_replace('_', ' ', ucfirst($campaign->status))); ?></span></td>
                            <td>
                                <button type="button" class="btn btn-sm btn-outline-primary" data-bs-toggle="modal" data-bs-target="#bulkSmsCampaign<?php echo e($campaign->id); ?>">
                                    <i class="bi bi-eye me-1"></i> View
                                </button>
                            </td>
                        </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                        <tr><td colspan="6" class="text-center text-muted py-5">No bulk SMS campaigns yet.</td></tr>
                    <?php endif; ?>
                    </tbody>
                </table>
            </div>
            <?php $__currentLoopData = $campaigns; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $campaign): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <?php
                    $messageLength = function_exists('mb_strlen') ? mb_strlen($campaign->message ?? '') : strlen($campaign->message ?? '');
                    $messageUnits = $messageLength <= 160 ? 1 : (int) ceil($messageLength / 153);
                ?>
                <div class="modal fade" id="bulkSmsCampaign<?php echo e($campaign->id); ?>" tabindex="-1" aria-labelledby="bulkSmsCampaign<?php echo e($campaign->id); ?>Label" aria-hidden="true">
                    <div class="modal-dialog modal-xl modal-dialog-centered modal-dialog-scrollable">
                        <div class="modal-content">
                            <div class="modal-header">
                                <div>
                                    <h5 class="modal-title" id="bulkSmsCampaign<?php echo e($campaign->id); ?>Label"><?php echo e($campaign->subject); ?></h5>
                                    <div class="text-muted small"><?php echo e(app_datetime($campaign->created_at)); ?> - <?php echo e(ucfirst(str_replace('_', ' ', $campaign->audience_type))); ?></div>
                                </div>
                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                            </div>
                            <div class="modal-body">
                                <div class="row g-3 mb-3">
                                    <div class="col-md-3"><div class="border rounded-3 p-3 h-100"><span class="text-muted small">Recipients</span><div class="fw-semibold"><?php echo e(number_format($campaign->total_recipients)); ?></div></div></div>
                                    <div class="col-md-3"><div class="border rounded-3 p-3 h-100"><span class="text-muted small">Sent/logged</span><div class="fw-semibold text-success"><?php echo e(number_format($campaign->sms_sent_count)); ?></div></div></div>
                                    <div class="col-md-3"><div class="border rounded-3 p-3 h-100"><span class="text-muted small">Failed</span><div class="fw-semibold text-danger"><?php echo e(number_format($campaign->failed_count)); ?></div></div></div>
                                    <div class="col-md-3"><div class="border rounded-3 p-3 h-100"><span class="text-muted small">SMS units per recipient</span><div class="fw-semibold"><?php echo e(number_format($messageUnits)); ?></div></div></div>
                                </div>
                                <div class="border rounded-3 p-3 bg-light mb-3">
                                    <div class="fw-semibold mb-1">Message</div>
                                    <div><?php echo e($campaign->message); ?></div>
                                    <div class="text-muted small mt-2"><?php echo e(number_format($messageLength)); ?> characters - <?php echo e(number_format($messageUnits)); ?> SMS unit<?php echo e($messageUnits === 1 ? '' : 's'); ?> per recipient</div>
                                </div>
                                <div class="table-responsive">
                                    <table class="table section-table align-middle mb-0">
                                        <thead><tr><th>Recipient</th><th>Phone</th><th>Status</th><th>Error / Delivery note</th></tr></thead>
                                        <tbody>
                                        <?php $__currentLoopData = $campaign->recipients; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $recipient): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <tr>
                                                <td><?php echo e($recipient->customer_name ?: 'Manual number'); ?></td>
                                                <td><?php echo e($recipient->phone ?: '-'); ?></td>
                                                <td>
                                                    <span class="badge text-bg-<?php echo e(in_array($recipient->sms_status, ['sent', 'logged'], true) ? 'success' : ($recipient->sms_status === 'failed' ? 'danger' : 'secondary')); ?>">
                                                        <?php echo e(ucfirst($recipient->sms_status ?: 'pending')); ?>

                                                    </span>
                                                </td>
                                                <td>
                                                    <?php if($recipient->sms_error): ?>
                                                        <span class="text-danger"><?php echo e($recipient->sms_error); ?></span>
                                                    <?php else: ?>
                                                        <span class="text-muted">No error reported.</span>
                                                    <?php endif; ?>
                                                </td>
                                            </tr>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Close</button>
                            </div>
                        </div>
                    </div>
                </div>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            <div class="p-3"><?php echo e($campaigns->links()); ?></div>
        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', () => {
    const audience = document.getElementById('bulkSmsAudience');
    const sections = document.querySelectorAll('[data-bulk-sms-section]');
    const message = document.getElementById('bulkSmsMessage');
    const count = document.getElementById('bulkSmsCount');
    const units = document.getElementById('bulkSmsUnits');
    const unitsPlural = document.getElementById('bulkSmsUnitsPlural');

    const toggleSections = () => {
        sections.forEach((section) => {
            section.classList.toggle('d-none', section.dataset.bulkSmsSection !== audience.value);
        });
    };

    const updateCount = () => {
        const length = message.value.length;
        const smsUnits = length <= 160 ? 1 : Math.ceil(length / 153);

        count.textContent = length;
        units.textContent = smsUnits;
        unitsPlural.textContent = smsUnits === 1 ? '' : 's';
    };

    audience.addEventListener('change', toggleSections);
    message.addEventListener('input', updateCount);
    toggleSections();
    updateCount();
});
</script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\Users\Polite hp\Documents\Codex\2026-05-08\build-a-complete-production-ready-saas\microfinance-saas\resources\views\tenant\bulk-sms\index.blade.php ENDPATH**/ ?>