<?php
/**
 * @var array $templates
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-envelope me-2 text-gold"></i>Email 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 Email Templates</h6>
        </div>
        <div class="card-header-right">
            <button type="button" class="btn btn-sm btn-gold" data-bs-toggle="modal" data-bs-target="#templateModal">
                <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>Subject</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-envelope fa-2x mb-2"></i>
                                <p class="mb-0">No email 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->subject, 60)) ?></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-template"
                                            data-id="<?= $tpl->id ?>"
                                            data-name="<?= e($tpl->name) ?>"
                                            data-subject="<?= e($tpl->subject) ?>"
                                            data-body="<?= e($tpl->body) ?>"
                                            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="templateModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <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="templateModalTitle">Add</span> Email Template</h6>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST" action="<?= url('marketing/email-templates/store') ?>">
                <?= csrf_field() ?>
                <input type="hidden" name="id" id="templateId">
                <div class="modal-body">
                    <div class="row g-3">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">Template Name</label>
                                <input type="text" name="name" id="templateName" class="form-control bg-transparent" required maxlength="100">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">Status</label>
                                <div class="form-check form-switch pt-2">
                                    <input type="hidden" name="is_active" value="0">
                                    <input type="checkbox" name="is_active" id="templateIsActive" class="form-check-input" value="1" checked>
                                    <label class="form-check-label" for="templateIsActive">Active</label>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Subject</label>
                        <input type="text" name="subject" id="templateSubject" class="form-control bg-transparent" required maxlength="255">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Body (HTML)</label>
                        <textarea name="body" id="templateBody" class="form-control bg-transparent" rows="8" required></textarea>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Available Variables</label>
                        <input type="text" name="variables" id="templateVariables" class="form-control bg-transparent" placeholder="e.g. {name}, {email}, {phone}" maxlength="1000">
                        <small class="text-muted">Comma separated list of variable names</small>
                    </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-template').click(function() {
    $('#templateModalTitle').text('Edit');
    $('#templateId').val($(this).data('id'));
    $('#templateName').val($(this).data('name'));
    $('#templateSubject').val($(this).data('subject'));
    $('#templateBody').val($(this).data('body'));
    $('#templateVariables').val($(this).data('variables'));
    $('#templateIsActive').prop('checked', $(this).data('is_active') == 1);
    $('#templateModal').modal('show');
});

$('#templateModal').on('hidden.bs.modal', function() {
    $('#templateModalTitle').text('Add');
    $('#templateId').val('');
    $('#templateName').val('');
    $('#templateSubject').val('');
    $('#templateBody').val('');
    $('#templateVariables').val('');
    $('#templateIsActive').prop('checked', true);
});
EOS;
?>
