<?php $__env->startSection('title', 'Edit Template'); ?>

<?php $__env->startSection('content'); ?>
<div class="mb-4">
    <a href="<?php echo e(route('client.templates')); ?>" class="text-decoration-none">
        <i class="bi bi-arrow-left me-2"></i>Back to Templates
    </a>
</div>

<div class="row justify-content-center">
    <div class="col-lg-8">
        <div class="table-card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h5 class="mb-0"><i class="bi bi-pencil me-2"></i>Edit Template</h5>
                <span class="badge bg-<?php echo e($template->type === 'sms' ? 'primary' : ($template->type === 'call' ? 'success' : 'success')); ?>">
                    <?php echo e(ucfirst($template->type)); ?>

                </span>
            </div>
            <div class="card-body p-4">
                <?php if($template->isDerived()): ?>
                <div class="d-flex align-items-center gap-2 mb-4">
                    <button type="button" class="info-hint" data-bs-toggle="popover" data-bs-content="This template was derived from a SuperAdmin template. You can customize the content for your organization.">
                        <i class="bi bi-info-circle"></i>
                    </button>
                    <small class="text-muted">Derived from SuperAdmin template</small>
                </div>
                <?php endif; ?>

                <form method="POST" action="<?php echo e(route('client.templates.update', $template->id)); ?>">
                    <?php echo csrf_field(); ?>
                    <?php echo method_field('PUT'); ?>

                    <div class="mb-3">
                        <label class="form-label">Template Name</label>
                        <input type="text" class="form-control" value="<?php echo e($template->name); ?>" readonly disabled>
                        <div class="form-text">Template name is managed by SuperAdmin</div>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Message Content <span class="text-danger">*</span></label>
                        <textarea class="form-control <?php $__errorArgs = ['content'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>" name="content" rows="6" required><?php echo e(old('content', $template->content)); ?></textarea>
                        <?php $__errorArgs = ['content'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                        <div class="invalid-feedback"><?php echo e($message); ?></div>
                        <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        <div class="form-text">
                            Available placeholders: <code>{name}</code>, <code>{phone}</code>, <code>{date}</code>, <code>{time}</code>
                        </div>
                    </div>

                    <div class="mt-4">
                        <div class="form-check form-switch">
                            <input class="form-check-input" type="checkbox" name="is_active" id="is_active" <?php echo e(old('is_active', $template->is_active) ? 'checked' : ''); ?>>
                            <label class="form-check-label" for="is_active">Template Active</label>
                        </div>
                        <div class="form-text">Inactive templates won't be used for auto-replies</div>
                    </div>

                    <div class="mt-4 pt-3 border-top d-flex gap-2">
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-check-lg me-2"></i>Save Changes
                        </button>
                        <a href="<?php echo e(route('client.templates')); ?>" class="btn btn-outline-secondary">Cancel</a>
                    </div>
                </form>
            </div>
        </div>

        <!-- Preview Card -->
        <div class="table-card mt-4">
            <div class="card-header">
                <h6 class="mb-0"><i class="bi bi-eye me-2"></i>Live Preview</h6>
            </div>
            <div class="card-body">
                <div class="bg-light rounded p-3">
                    <div class="small text-muted mb-2">Message Preview (with sample data):</div>
                    <div id="messagePreview" class="border-start border-3 border-primary ps-3">
                        <?php echo e($template->content); ?>

                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('scripts'); ?>
<script>
    // Live preview
    const contentTextarea = document.querySelector('textarea[name="content"]');
    const previewDiv = document.getElementById('messagePreview');

    const sampleData = {
        '{name}': 'John Doe',
        '{phone}': '+1234567890',
        '{date}': new Date().toLocaleDateString(),
        '{time}': new Date().toLocaleTimeString()
    };

    function updatePreview() {
        let content = contentTextarea.value;
        for (const [placeholder, value] of Object.entries(sampleData)) {
            content = content.replaceAll(placeholder, `<strong class="text-primary">${value}</strong>`);
        }
        previewDiv.innerHTML = content.replace(/\n/g, '<br>');
    }

    contentTextarea.addEventListener('input', updatePreview);
    updatePreview();
</script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('client.layouts.dashboard', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/zwfgftjb/autopromix.byteengine.in/resources/views/client/templates/edit.blade.php ENDPATH**/ ?>