<?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.post', $post->id)); ?>"
                                enctype="multipart/form-data">
                                <?php echo csrf_field(); ?>
                                <?php echo method_field('PUT'); ?>

                                <div class="row">

                                    <!-- Category -->
                                    <div class="col-md-6 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', $post->category_id) ? 'selected' : ''); ?>>
                                                    <?php echo e($category->name); ?>

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

                                    <!-- Title -->
                                    <div class="col-md-6 mb-3">
                                        <label>Title</label>
                                        <input type="text" name="title" class="form-control"
                                            value="<?php echo e(old('title', $post->title)); ?>" required />
                                    </div>

                                    <!-- Slug -->
                                    <div class="col-md-12 mb-3">
                                        <label>Slug</label>
                                        <input type="text" name="slug" class="form-control"
                                            value="<?php echo e(old('slug', $post->slug)); ?>" required />
                                    </div>


                                    <!-- Image Upload & Preview -->
                                    <div class="col-md-6 mb-3">
                                        <label>Image</label>
                                        <input type="file" name="featured_image" class="form-control"
                                            onchange="previewImage(event)" />
                                        <?php if($post->featured_image): ?>
                                            <img id="imagePreview" src="<?php echo e(asset('storage/' . $post->featured_image)); ?>"
                                                style="max-width:150px; margin-top:10px;" />
                                        <?php else: ?>
                                            <img id="imagePreview" style="display:none; max-width:150px; margin-top:10px;" />
                                        <?php endif; ?>
                                    </div>

                                    
                                    <div class="col-md-6 mb-3">
                                        <label for="tag_id">Tags</label>
                                        <select class="form-select <?php $__errorArgs = ['tag_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); ?>" name="tag_id[]"
                                            id="tag_id" multiple>
                                            <?php $__currentLoopData = $tags; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $tag): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                <option value="<?php echo e($tag->id); ?>" <?php echo e(in_array($tag->id, old('tag_id', $post->tags->pluck('id')->toArray())) ? 'selected' : ''); ?>>
                                                    <?php echo e($tag->name); ?>

                                                </option>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                            <option value="other" <?php echo e(in_array('other', old('tag_id', [])) ? 'selected' : ''); ?>>Other</option>
                                        </select>
                                        <?php $__errorArgs = ['tag_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 id="manual_tag_container" class="mt-2" style="display: none;">
                                            <div class="input-group">
                                                <input type="text" class="form-control" id="manual_tag_input"
                                                    placeholder="Enter new tag">
                                                <button type="button" class="btn btn-outline-primary"
                                                    id="add_tag_button">Add</button>
                                            </div>
                                        </div>
                                        <?php $__errorArgs = ['tag_id'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> <p class="invalid-feedback"><?php echo e($message); ?></p> <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                                    </div>

                                    <!-- Content -->
                                    <div class="col-md-12 mb-3">
                                        <label for="editor">Content</label>
                                        <textarea name="content" class="form-control" rows="7" id="editor"
                                            required><?php echo e(old('content', $post->content)); ?></textarea>
                                    </div>


                                    <!-- Meta Keywords -->
                                    <div class="col-md-8 mb-3">
                                        <label>Meta Keywords</label>
                                        <input type="text" name="meta_keywords" class="form-control"
                                            value="<?php echo e(old('meta_keywords', $post->meta_keywords)); ?>" />
                                    </div>

                                    <!-- Meta Description -->
                                    <div class="col-md-12 mb-3">
                                        <label>Meta Description</label>
                                        <textarea name="meta_description"
                                            class="form-control"><?php echo e(old('meta_description', $post->meta_description)); ?></textarea>
                                    </div>

                                    <!-- Publish Date -->
                                    <div class="col-md-5 mb-3">
                                        <label>Publish Date & Time</label>
                                        <input type="datetime-local" name="published_at" class="form-control"
                                            value="<?php echo e(old('published_at', $post->published_at ? date('Y-m-d\TH:i', strtotime($post->published_at)) : '')); ?>" />
                                    </div>

                                    <div class="col-md-4 mb-3">
                                        <label for="is_featured">Is Featured</label>
                                        <select class="form-select" name="is_featured">
                                            <option value="1" <?php echo e(old('is_featured', $post->is_featured) == '1' ? 'selected' : ''); ?>>
                                                Yes</option>
                                            <option value="0" <?php echo e(old('is_featured', $post->is_featured) == '0' ? 'selected' : ''); ?>>
                                                No</option>
                                        </select>
                                    </div>
                                    <div class="col-md-3 mb-3">
                                        <label for="status">Status</label>
                                        <select class="form-select" name="status">
                                            <option value="1" <?php echo e(old('status', $post->status) == '1' ? 'selected' : ''); ?>>
                                                Published</option>
                                            <option value="0" <?php echo e(old('status', $post->status) == '0' ? 'selected' : ''); ?>>
                                                Draft</option>
                                        </select>
                                    </div>


                                    <!-- Submit Button -->
                                    <div class="col-md-12 mb-3">
                                        <button type="submit" class="btn btn-primary">Update Post</button>
                                    </div>
                                </div>
                            </form>


                            <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
                            <script>
                                function previewImage(event) {
                                    const reader = new FileReader();
                                    reader.onload = function () {
                                        const output = document.getElementById('imagePreview');
                                        output.src = reader.result;
                                        output.style.display = 'block';
                                    };
                                    reader.readAsDataURL(event.target.files[0]);
                                }
                            </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>
                            <script src="https://cdn.ckeditor.com/ckeditor5/39.0.1/classic/ckeditor.js"></script>

                            <!-- Initialize CKEditor -->
                            <script>
                                ClassicEditor
                                    .create(document.querySelector('#editor'))
                                    .catch(error => {
                                        console.error('There was a problem initializing the editor:', error);
                                    });
                            </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 A:\xamp102024\htdocs\sachin\androidwale.com\resources\views/admin/post/edit.blade.php ENDPATH**/ ?>