

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

<?php $__env->startSection('content'); ?>
<div class="d-flex justify-content-between align-items-center mb-4">
    <h4 class="mb-0">Edit Template: <?php echo e($template->name); ?></h4>
    <a href="<?php echo e(route('dashboard.templates')); ?>" class="btn btn-secondary">
        <i class="bi bi-arrow-left"></i> Back to Templates
    </a>
</div>

<div class="row">
    <div class="col-lg-8">
        <div class="card">
            <div class="card-body">
                <form action="<?php echo e(route('dashboard.templates.update', $template->id)); ?>" method="POST">
                    <?php echo csrf_field(); ?>
                    <?php echo method_field('PUT'); ?>

                    <?php if($errors->any()): ?>
                    <div class="alert alert-danger">
                        <ul class="mb-0">
                            <?php $__currentLoopData = $errors->all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $error): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <li><?php echo e($error); ?></li>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </ul>
                    </div>
                    <?php endif; ?>

                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label for="client_id" class="form-label">Assign to Organization</label>
                            <select class="form-select <?php $__errorArgs = ['client_id'];
$__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); ?>" id="client_id" name="client_id">
                                <option value="">Global (All Organizations)</option>
                                <?php $__currentLoopData = $clients; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $client): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($client->id); ?>" <?php echo e(old('client_id', $template->client_id) == $client->id ? 'selected' : ''); ?>>
                                    <?php echo e($client->name); ?>

                                </option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                            <?php $__errorArgs = ['client_id'];
$__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>

                        <div class="col-md-6 mb-3">
                            <label for="name" class="form-label">Template Name <span class="text-danger">*</span></label>
                            <input type="text" class="form-control <?php $__errorArgs = ['name'];
$__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); ?>"
                                id="name" name="name" value="<?php echo e(old('name', $template->name)); ?>" required>
                            <?php $__errorArgs = ['name'];
$__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>
                    </div>

                    <div class="row">
                        <div class="col-md-4 mb-3">
                            <label for="type" class="form-label">Template Type <span class="text-danger">*</span></label>
                            <select class="form-select <?php $__errorArgs = ['type'];
$__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); ?>" id="type" name="type" required>
                                <option value="auto_reply" <?php echo e(old('type', $template->type) == 'auto_reply' ? 'selected' : ''); ?>>Auto Reply</option>
                                <option value="promotion" <?php echo e(old('type', $template->type) == 'promotion' ? 'selected' : ''); ?>>Promotion</option>
                            </select>
                            <?php $__errorArgs = ['type'];
$__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>

                        <div class="col-md-4 mb-3">
                            <label for="channel" class="form-label">Channel <span class="text-danger">*</span></label>
                            <select class="form-select <?php $__errorArgs = ['channel'];
$__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); ?>" id="channel" name="channel" required>
                                <option value="sms" <?php echo e(old('channel', $template->channel ?? 'sms') == 'sms' ? 'selected' : ''); ?>>SMS</option>
                                <option value="whatsapp" <?php echo e(old('channel', $template->channel ?? 'sms') == 'whatsapp' ? 'selected' : ''); ?>>WhatsApp</option>
                                <option value="both" <?php echo e(old('channel', $template->channel ?? 'sms') == 'both' ? 'selected' : ''); ?>>Both (SMS + WhatsApp)</option>
                            </select>
                            <?php $__errorArgs = ['channel'];
$__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>

                        <div class="col-md-4 mb-3">
                            <label for="call_type" class="form-label">Call Type <span class="text-danger">*</span></label>
                            <select class="form-select <?php $__errorArgs = ['call_type'];
$__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); ?>" id="call_type" name="call_type" required>
                                <option value="all" <?php echo e(old('call_type', $template->call_type) == 'all' ? 'selected' : ''); ?>>All Calls</option>
                                <option value="incoming" <?php echo e(old('call_type', $template->call_type) == 'incoming' ? 'selected' : ''); ?>>Incoming Only</option>
                                <option value="outgoing" <?php echo e(old('call_type', $template->call_type) == 'outgoing' ? 'selected' : ''); ?>>Outgoing Only</option>
                                <option value="missed" <?php echo e(old('call_type', $template->call_type) == 'missed' ? 'selected' : ''); ?>>Missed Only</option>
                            </select>
                            <?php $__errorArgs = ['call_type'];
$__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>
                    </div>

                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label for="priority" class="form-label">Priority</label>
                            <input type="number" class="form-control <?php $__errorArgs = ['priority'];
$__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); ?>"
                                id="priority" name="priority" value="<?php echo e(old('priority', $template->priority)); ?>" min="0">
                            <small class="text-muted">Lower = Higher priority</small>
                            <?php $__errorArgs = ['priority'];
$__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>
                    </div>

                    <!-- WhatsApp Media Attachment (shown when channel is whatsapp or both) -->
                    <div id="media-section" class="card bg-light mb-3" style="display: none;">
                        <div class="card-body">
                            <h6 class="mb-3"><i class="bi bi-image"></i> Media Attachment <small class="text-muted">(WhatsApp only)</small></h6>
                            <div class="row">
                                <div class="col-md-4 mb-3">
                                    <label for="media_type" class="form-label">Media Type</label>
                                    <select class="form-select <?php $__errorArgs = ['media_type'];
$__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); ?>" id="media_type" name="media_type">
                                        <option value="">None</option>
                                        <option value="image" <?php echo e(old('media_type', $template->media_type) == 'image' ? 'selected' : ''); ?>>Image</option>
                                        <option value="video" <?php echo e(old('media_type', $template->media_type) == 'video' ? 'selected' : ''); ?>>Video</option>
                                    </select>
                                    <?php $__errorArgs = ['media_type'];
$__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>
                                <div class="col-md-8 mb-3">
                                    <label for="media_url" class="form-label">Media URL</label>
                                    <input type="url" class="form-control <?php $__errorArgs = ['media_url'];
$__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); ?>"
                                        id="media_url" name="media_url" value="<?php echo e(old('media_url', $template->media_url)); ?>"
                                        placeholder="https://example.com/image.jpg">
                                    <small class="text-muted">Direct link to the image or video file. Must be a publicly accessible URL.</small>
                                    <?php $__errorArgs = ['media_url'];
$__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>
                            </div>
                            <div id="media-preview" class="mt-2" style="display: none;">
                                <img id="media-preview-img" src="" alt="Preview" class="img-thumbnail" style="max-height: 150px; display: none;">
                                <div id="media-preview-video" class="badge bg-info" style="display: none;"><i class="bi bi-camera-video"></i> Video file</div>
                            </div>
                        </div>
                    </div>

                    <div class="mb-3">
                        <label for="content" 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); ?>"
                            id="content" name="content" rows="5" required maxlength="1000"><?php echo e(old('content', $template->content)); ?></textarea>
                        <small class="text-muted">
                            Available variables: {name}, {phone}, {time}, {date}
                        </small>
                        <?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>

                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" id="is_active"
                                    name="is_active" value="1" <?php echo e(old('is_active', $template->is_active) ? 'checked' : ''); ?>>
                                <label class="form-check-label" for="is_active">
                                    Active
                                </label>
                            </div>
                        </div>

                        <div class="col-md-6 mb-3">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" id="is_default"
                                    name="is_default" value="1" <?php echo e(old('is_default', $template->is_default) ? 'checked' : ''); ?>>
                                <label class="form-check-label" for="is_default">
                                    Set as Default
                                </label>
                            </div>
                        </div>
                    </div>

                    <div class="d-flex gap-2 mt-4">
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-check-lg"></i> Update Template
                        </button>
                        <a href="<?php echo e(route('dashboard.templates')); ?>" class="btn btn-secondary">Cancel</a>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <div class="col-lg-4">
        <div class="card mb-3">
            <div class="card-header">
                <h6 class="mb-0"><i class="bi bi-info-circle"></i> Template Info</h6>
            </div>
            <div class="card-body">
                <p class="mb-1"><strong>Template ID:</strong> <?php echo e($template->id); ?></p>
                <p class="mb-1"><strong>Created:</strong> <?php echo e($template->created_at->format('M d, Y H:i')); ?></p>
                <p class="mb-0"><strong>Last Updated:</strong> <?php echo e($template->updated_at->format('M d, Y H:i')); ?></p>
            </div>
        </div>

        <div class="card bg-light mb-3">
            <div class="card-body">
                <h6><i class="bi bi-code-slash"></i> Template Variables</h6>
                <ul class="small text-muted mb-0">
                    <li><code>{name}</code> - Caller's name (if available)</li>
                    <li><code>{phone}</code> - Caller's phone number</li>
                    <li><code>{time}</code> - Current time</li>
                    <li><code>{date}</code> - Current date</li>
                </ul>
            </div>
        </div>

        <div class="card">
            <div class="card-header">
                <h6 class="mb-0"><i class="bi bi-shield-exclamation"></i> Danger Zone</h6>
            </div>
            <div class="card-body">
                <form action="<?php echo e(route('dashboard.templates.delete', $template->id)); ?>" method="POST">
                    <?php echo csrf_field(); ?>
                    <?php echo method_field('DELETE'); ?>
                    <button type="submit" class="btn btn-danger btn-sm w-100"
                        data-confirm="Are you sure you want to delete this template? This action cannot be undone."
                        data-confirm-title="Delete Template"
                        data-confirm-btn="Delete"
                        data-confirm-class="btn-danger">
                        <i class="bi bi-trash"></i> Delete Template
                    </button>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const channelSelect = document.getElementById('channel');
    const mediaSection = document.getElementById('media-section');
    const mediaType = document.getElementById('media_type');
    const mediaUrl = document.getElementById('media_url');
    const previewContainer = document.getElementById('media-preview');
    const previewImg = document.getElementById('media-preview-img');
    const previewVideo = document.getElementById('media-preview-video');

    function toggleMediaSection() {
        const val = channelSelect.value;
        mediaSection.style.display = (val === 'whatsapp' || val === 'both') ? 'block' : 'none';
        if (val === 'sms') {
            mediaType.value = '';
            mediaUrl.value = '';
            previewContainer.style.display = 'none';
        }
    }

    function updatePreview() {
        const type = mediaType.value;
        const url = mediaUrl.value;
        previewImg.style.display = 'none';
        previewVideo.style.display = 'none';
        previewContainer.style.display = 'none';
        if (url && type === 'image') {
            previewImg.src = url;
            previewImg.style.display = 'block';
            previewContainer.style.display = 'block';
        } else if (url && type === 'video') {
            previewVideo.style.display = 'inline-block';
            previewContainer.style.display = 'block';
        }
    }

    channelSelect.addEventListener('change', toggleMediaSection);
    mediaType.addEventListener('change', updatePreview);
    mediaUrl.addEventListener('input', updatePreview);
    toggleMediaSection();
    updatePreview();
});
</script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.dashboard', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH D:\xampp82\htdocs\AutoPromix\backend\resources\views/dashboard/templates-edit.blade.php ENDPATH**/ ?>