

<?php $__env->startSection('content'); ?>
<div class="container">
    <div class="container-header">
        <h1><?php echo e($title); ?></h1>
    </div>

    <div class="card mb-25">
        <div class="card-body">
            <form action="<?php echo e(route('backend.tutorials.store')); ?>" method="post">
                <?php echo csrf_field(); ?>
                <div class="row">
                    <div class="form-group width-50">
                        <label class="form-label">Title<span class="required">*</span></label>
                        <input type="text" class="form-control" name="title" placeholder="e.g. How to Fund Your Wallet With Paystack" required>
                    </div>
                    <div class="form-group width-50">
                        <label class="form-label">URL</label>
                        <input type="text" class="form-control" name="url" placeholder="https://... or #">
                    </div>
                </div>
                <div class="row">
                    <div class="form-group width-25">
                        <label class="form-label">Sort order</label>
                        <input type="number" class="form-control" name="sort_order" value="0" min="0" placeholder="0">
                    </div>
                </div>
                <button type="submit" class="btn btn-primary">Add tutorial</button>
            </form>
        </div>
    </div>

    <div class="card table-card">
        <div class="table-header">
            <form method="GET" action="<?php echo e(route('backend.tutorials.index')); ?>" class="table-search-form">
                <div class="table-search">
                    <input type="text" name="search" class="search-input" placeholder="Search tutorials..." value="<?php echo e($search ?? ''); ?>">
                    <i class="las la-search"></i>
                </div>
                <button type="submit" class="btn btn-secondary search-btn">Search</button>
                <?php if(!empty($search)): ?>
                    <a href="<?php echo e(route('backend.tutorials.index')); ?>" class="btn btn-outline">Clear</a>
                <?php endif; ?>
            </form>
        </div>
        <div class="table-responsive">
            <table class="table">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Title</th>
                        <th>URL</th>
                        <th>Sort</th>
                        <th>Status</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php $__empty_1 = true; $__currentLoopData = $tutorials; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $t): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                    <tr>
                        <td><?php echo e($tutorials->firstItem() + $loop->index); ?></td>
                        <td><?php echo e($t->title); ?></td>
                        <td><a href="<?php echo e($t->url); ?>" target="_blank" rel="noopener"><?php echo e(Str::limit($t->url, 40)); ?></a></td>
                        <td><?php echo e($t->sort_order); ?></td>
                        <td>
                            <div class="toggle-switch">
                                <input type="checkbox" id="toggle-tutorial-<?php echo e($t->id); ?>" <?php echo e($t->status ? 'checked' : ''); ?>>
                                <label for="toggle-tutorial-<?php echo e($t->id); ?>" class="toggle-label">
                                    <span class="toggle-text"><?php echo e($t->status ? 'On' : 'Off'); ?></span>
                                </label>
                            </div>
                        </td>
                        <td>
                            <div class="table-actions">
                                <button type="button" class="table-btn edit-tutorial" data-id="<?php echo e($t->id); ?>" data-title="<?php echo e($t->title); ?>" data-url="<?php echo e($t->url); ?>" data-sort="<?php echo e($t->sort_order); ?>" title="Edit"><i class="las la-pen"></i></button>
                                <form action="<?php echo e(route('backend.tutorials.destroy', $t->id)); ?>" method="post" class="d-inline" onsubmit="return confirm('Delete this tutorial?');">
                                    <?php echo csrf_field(); ?>
                                    <?php echo method_field('DELETE'); ?>
                                    <button type="submit" class="table-btn" title="Delete"><i class="las la-trash"></i></button>
                                </form>
                            </div>
                        </td>
                    </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                    <tr><td colspan="6" class="text-center">No tutorials yet.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        <?php echo e($tutorials->links('vendor.pagination.default')); ?>

    </div>
</div>

<div class="modal" id="edit-tutorial-modal">
    <div class="modal-content">
        <div class="modal-header">
            <h3 class="modal-title">Edit tutorial</h3>
            <button class="close-modal"><i class="las la-times"></i></button>
        </div>
        <form action="<?php echo e(route('backend.tutorials.update', ['id' => '__ID__'])); ?>" method="post" id="edit-tutorial-form">
            <?php echo csrf_field(); ?>
            <?php echo method_field('PUT'); ?>
            <div class="modal-body">
                <div class="form-group mb-25">
                    <label class="form-label">Title<span class="required">*</span></label>
                    <input type="text" class="form-control" name="title" id="edit-tutorial-title" required>
                </div>
                <div class="form-group mb-25">
                    <label class="form-label">URL</label>
                    <input type="text" class="form-control" name="url" id="edit-tutorial-url" placeholder="https://... or #">
                </div>
                <div class="form-group mb-25">
                    <label class="form-label">Sort order</label>
                    <input type="number" class="form-control" name="sort_order" id="edit-tutorial-sort" value="0" min="0">
                </div>
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary" type="submit">Update tutorial</button>
            </div>
        </form>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('script'); ?>
<script>
document.addEventListener('DOMContentLoaded', function() {
    const toggleSwitches = document.querySelectorAll('input[type="checkbox"][id^="toggle-tutorial-"]');
    toggleSwitches.forEach(toggle => {
        toggle.addEventListener('change', function() {
            const id = this.id.replace('toggle-tutorial-', '');
            const toggleText = this.nextElementSibling.querySelector('.toggle-text');
            const checked = this.checked;
            toggleText.textContent = checked ? 'On' : 'Off';

            fetch('<?php echo e(route("backend.tutorials.toggle-status", ["id" => "__ID__"])); ?>'.replace('__ID__', id), {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
                    'Accept': 'application/json'
                },
                body: JSON.stringify({})
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    if (typeof iziToast !== 'undefined') {
                        iziToast.success({ message: data.message || 'Status updated', position: 'topRight' });
                    }
                } else {
                    this.checked = !this.checked;
                    toggleText.textContent = this.checked ? 'On' : 'Off';
                    if (typeof iziToast !== 'undefined') {
                        iziToast.error({ message: data.message || 'Failed to update status', position: 'topRight' });
                    }
                }
            })
            .catch(error => {
                console.error('Error:', error);
                this.checked = !this.checked;
                toggleText.textContent = this.checked ? 'On' : 'Off';
                if (typeof iziToast !== 'undefined') {
                    iziToast.error({ message: 'Failed to update status', position: 'topRight' });
                }
            });
        });
    });

    document.querySelectorAll('.edit-tutorial').forEach(function(btn) {
        btn.addEventListener('click', function() {
            var modal = document.getElementById('edit-tutorial-modal');
            var form = document.getElementById('edit-tutorial-form');
            form.action = '<?php echo e(route("backend.tutorials.update", ["id" => "__ID__"])); ?>'.replace('__ID__', this.getAttribute('data-id'));
            document.getElementById('edit-tutorial-title').value = this.getAttribute('data-title') || '';
            document.getElementById('edit-tutorial-url').value = this.getAttribute('data-url') || '#';
            document.getElementById('edit-tutorial-sort').value = this.getAttribute('data-sort') || '0';
            modal.classList.add('active');
        });
    });
});
</script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('backend.layout.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\Users\HP\Desktop\social\socialstack\core\resources\views/backend/tutorials/index.blade.php ENDPATH**/ ?>