<?php
/**
 * @var array $templates
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-sms me-2 text-gold"></i>SMS Templates</h5>
</div>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-list card-icon"></i>
            <h6 class="card-title">Manage SMS Templates</h6>
        </div>
        <div class="card-header-right">
            <button type="button" class="btn btn-sm btn-gold" data-bs-toggle="modal" data-bs-target="#smsTemplateModal">
                <i class="fas fa-plus me-1"></i>Add Template
            </button>
        </div>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-dark table-hover mb-0">
                <thead>
                    <tr>
                        <th width="50">#</th>
                        <th>Name</th>
                        <th>Message</th>
                        <th>Status</th>
                        <th width="80" class="text-center">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($templates)): ?>
                        <tr>
                            <td colspan="5" class="text-center text-muted py-4">
                                <i class="fas fa-sms fa-2x mb-2"></i>
                                <p class="mb-0">No SMS templates found</p>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php $i = 1; ?>
                        <?php foreach ($templates as $tpl): ?>
                            <tr>
                                <td><?= $i++ ?></td>
                                <td><strong><?= e($tpl->name) ?></strong></td>
                                <td><?= e(truncate($tpl->message, 80)) ?></td>
                                <td>
                                    <span class="badge bg-<?= $tpl->is_active ? 'success' : 'secondary' ?>">
                                        <?= $tpl->is_active ? 'Active' : 'Inactive' ?>
                                    </span>
                                </td>
                                <td class="text-center">
                                    <button type="button" class="btn btn-sm btn-outline-gold btn-edit-sms"
                                            data-id="<?= $tpl->id ?>"
                                            data-name="<?= e($tpl->name) ?>"
                                            data-message="<?= e($tpl->message) ?>"
                                            data-variables="<?= e($tpl->variables ?? '') ?>"
                                            data-is_active="<?= $tpl->is_active ?>"
                                            title="Edit">
                                        <i class="fas fa-edit"></i>
                                    </button>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<div class="modal fade" id="smsTemplateModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content bg-dark">
            <div class="modal-header border-secondary">
                <h6 class="modal-title"><i class="fas fa-plus-circle text-gold me-2"></i><span id="smsTemplateModalTitle">Add</span> SMS Template</h6>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST" action="<?= url('marketing/sms-templates/store') ?>">
                <?= csrf_field() ?>
                <input type="hidden" name="id" id="smsTemplateId">
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">Template Name</label>
                        <input type="text" name="name" id="smsTemplateName" class="form-control bg-transparent" required maxlength="100">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Message</label>
                        <textarea name="message" id="smsTemplateMessage" class="form-control bg-transparent" rows="4" required maxlength="1600"></textarea>
                        <small class="text-muted">Max 1600 characters</small>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Available Variables</label>
                        <input type="text" name="variables" id="smsTemplateVariables" class="form-control bg-transparent" placeholder="e.g. {name}, {phone}" maxlength="1000">
                    </div>
                    <div class="mb-3">
                        <div class="form-check form-switch">
                            <input type="hidden" name="is_active" value="0">
                            <input type="checkbox" name="is_active" id="smsTemplateIsActive" class="form-check-input" value="1" checked>
                            <label class="form-check-label" for="smsTemplateIsActive">Active</label>
                        </div>
                    </div>
                </div>
                <div class="modal-footer border-secondary">
                    <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-gold">Save Template</button>
                </div>
            </form>
        </div>
    </div>
</div>

<?php $inlineScripts = <<<'EOS'
$('.btn-edit-sms').click(function() {
    $('#smsTemplateModalTitle').text('Edit');
    $('#smsTemplateId').val($(this).data('id'));
    $('#smsTemplateName').val($(this).data('name'));
    $('#smsTemplateMessage').val($(this).data('message'));
    $('#smsTemplateVariables').val($(this).data('variables'));
    $('#smsTemplateIsActive').prop('checked', $(this).data('is_active') == 1);
    $('#smsTemplateModal').modal('show');
});

$('#smsTemplateModal').on('hidden.bs.modal', function() {
    $('#smsTemplateModalTitle').text('Add');
    $('#smsTemplateId').val('');
    $('#smsTemplateName').val('');
    $('#smsTemplateMessage').val('');
    $('#smsTemplateVariables').val('');
    $('#smsTemplateIsActive').prop('checked', true);
});
EOS;
?>
