<?php $__env->startSection('section'); ?>
    <!-- Page sidebar end-->
    <div class="page-body">
        <div class="container-fluid">
            <div class="row page-title">
                <div class="col-sm-6">
                    <h3><?php echo e($title); ?></h3>
                </div>
                <div class="col-sm-6">
                    <nav>
                        <ol class="breadcrumb justify-content-sm-end align-items-center">
                            <li class="breadcrumb-item"> <a href="<?php echo e(route('dashboard')); ?>">
                                    <svg class="svg-color">
                                        <use href="<?php echo e(asset('assets/svg/iconly-sprite.svg#Home')); ?>"></use>
                                    </svg></a></li>
                            <li class="breadcrumb-item">Category</li>

                            <li class="breadcrumb-item ">Title List</li>
                            <li class="breadcrumb-item active"><?php echo e($title); ?></li>
                        </ol>
                    </nav>
                </div>
            </div>
        </div>
        <!-- Container-fluid starts-->
        <div class="container-fluid">
            <div class="row">
                <div class="col-sm-12">
                    <div class="card">
                        <div class="card-body">
                            <!-- Display Validation Errors -->
                            <?php if($errors->any()): ?>
                                <div class="alert alert-danger">
                                    <ul>
                                        <?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; ?>
                            <!-- Blade Form (edit.blade.php) -->
<form method="POST" action="<?php echo e(route('update.title', $blog->id)); ?>" enctype="multipart/form-data">
    <?php echo csrf_field(); ?>
    <?php echo method_field('PUT'); ?>

    <!-- Title and Category -->
    <div class="mb-3">
        <label>Title</label>
        <input type="text" name="title" class="form-control" value="<?php echo e(old('title', $blog->title)); ?>" required />
    </div>
    <div class="mb-3">
        <label>Category</label>
        <select name="category_id" class="form-control" required>
            <?php $__currentLoopData = $categories; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $category): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <option value="<?php echo e($category->id); ?>" <?php echo e($category->id == old('category_id', $blog->category_id) ? 'selected' : ''); ?>>
                    <?php echo e($category->name); ?>

                </option>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        </select>
    </div>

    <!-- Dynamic Heading, Paragraph, Code Snippet, Language Sections -->
    <div id="dynamic-sections">
        <?php $__currentLoopData = $blog->headings; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index => $heading): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
        <div class="section">
            <hr>
            <div class="row">
                <div class="col-8">
                    <div class="mb-3">
                        <label>Heading <span class="heading-number"><?php echo e($index + 1); ?></span>:</label>
                        <input class="form-control" type="text" name="headings[]" value="<?php echo e(old('headings.' . $index, $heading->heading)); ?>" required />
                    </div>
                </div>
                <div class="col-4">
                    <div class="mb-3">
                        <label>Language:</label>
                        <input class="form-control" type="text" name="languages[]" value="<?php echo e(old('languages.' . $index, $heading->codeSnippets->first()->language ?? '')); ?>" />
                    </div>
                </div>
                <div class="mb-3">
                    <label>Paragraph:</label>
                    <textarea class="form-control" name="paragraphs[]" rows="6"><?php echo e(old('paragraphs.' . $index, $heading->paragraphs->first()->content ?? '')); ?></textarea>
                </div>
                <div class="mb-3">
                    <label>Code Snippet:</label>
                    <textarea class="form-control" name="code_snippets[]" rows="6"><?php echo e(old('code_snippets.' . $index, $heading->codeSnippets->first()->code ?? '')); ?></textarea>
                </div>
            </div>
        </div>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
    </div>

    <div class="mb-3">
        <button type="button" id="add-more" class="btn btn-secondary">Add More</button>
        <button type="button" id="remove-last" class="btn btn-danger">Remove Last</button>
    </div>

    <!-- Image Upload (if applicable) -->
    <div class="mb-3">
        <label>Image</label>
        <input type="file" name="image" class="form-control" onchange="previewImage(event)" />
        <img id="imagePreview" style="display:none; max-width: 150px; margin-top: 10px;" />
    </div>

    <!-- Submit -->
    <div class="mb-3">
        <button type="submit" class="btn btn-primary">Update</button>
    </div>
</form>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    $(document).ready(function() {
        // Add a new section (heading, paragraph, code snippet, and language)
        $('#add-more').click(function() {
            const container = $('#dynamic-sections');
            const sections = container.find('.section');
            const newIndex = sections.length + 1;

            const newSection = `
                <hr>
                <div class="section">
                    <div class="row">
                        <div class="col-8">
                            <div class="mb-3">
                                <label>Heading <span class="heading-number">${newIndex}</span>:</label>
                                <input class="form-control" type="text" name="headings[]">
                            </div>
                        </div>
                        <div class="col-4">
                            <div class="mb-3">
                                <label>Language:</label>
                                <input class="form-control" type="text" name="languages[]">
                            </div>
                        </div>
                        <div class="mb-3">
                            <label>Paragraph:</label>
                            <textarea class="form-control" name="paragraphs[]" rows="6"></textarea>
                        </div>
                        <div class="mb-3">
                            <label>Code Snippet:</label>
                            <textarea class="form-control" name="code_snippets[]" rows="6"></textarea>
                        </div>
                    </div>
                </div>
            `;

            container.append(newSection);
        });

        // Remove the last added section
        $('#remove-last').click(function() {
            const container = $('#dynamic-sections');
            const sections = container.find('.section');

            if (sections.length > 1) {
                sections.last().remove();
            }
        });
    });

    // Function to display the selected image as a preview
    function previewImage(event) {
        const file = event.target.files[0];
        const reader = new FileReader();

        reader.onload = function(e) {
            const imagePreview = document.getElementById('imagePreview');
            imagePreview.src = e.target.result;
            imagePreview.style.display = 'block'; // Show the preview image
        };

        if (file) {
            reader.readAsDataURL(file);
        }
    }
</script>

                            

                            <script>
                                $(document).ready(function() {
                                    // Show the input field when 'Other' is selected
                                    $('#tag_id').change(function() {
                                        if ($(this).val().includes('other')) {
                                            $('#manual_tag_container').show();
                                        } else {
                                            $('#manual_tag_container').hide();
                                        }
                                    });

                                    // Add a new tag using AJAX
                                    $('#add_tag_button').click(function() {
                                        var newTag = $('#manual_tag_input').val();
                                        if (newTag.trim() !== '') {
                                            $.ajax({
                                                url: '<?php echo e(route('tags.store')); ?>',
                                                method: 'POST',
                                                data: {
                                                    name: newTag, // Send 'name' instead of 'tag'
                                                    _token: '<?php echo e(csrf_token()); ?>'
                                                },
                                                success: function(response) {
                                                    if (response.success) {
                                                        $('#tag_id').append('<option value="' + response.tag.id +
                                                            '" selected>' + response.tag.name + '</option>');
                                                        $('#manual_tag_input').val('');
                                                        $('#manual_tag_container').hide();
                                                    }
                                                },
                                                error: function() {
                                                    alert('Failed to add tag');
                                                }
                                            });
                                        }
                                    });
                                });
                            </script>


                        </div>
                    </div>

                </div>
            </div>
        </div>
        <!-- Container-fluid starts -->
    </div>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('admin.layouts.main', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /home/ewx7upyjsani/public_html/androidwale.com/resources/views/admin/title/edit.blade.php ENDPATH**/ ?>