

<?php $__env->startSection('section'); ?>
    <style>
        .ck-editor__editable {
            min-height: 300px;
        }

        .ck.ck-editor {
            width: 100%;
        }
    </style>

    <div class="page-body">
        <div class="container-fluid">
            <!-- Page Title & Breadcrumb -->
            <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">Post</li>
                            <li class="breadcrumb-item active"><?php echo e($title); ?></li>
                        </ol>
                    </nav>
                </div>
            </div>

            <!-- Form Start -->
            <div class="row">
                <div class="col-sm-12">
                    <div class="card">
                        <div class="card-body">

                            <!-- 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; ?>

                            <form action="<?php echo e(route('post.store')); ?>" method="POST" enctype="multipart/form-data">
                                <?php echo csrf_field(); ?>
                                <div class="row">


                                    <!-- Category -->
                                    <div class="col-md-6 mb-3">
                                        <label for="category_id">Category <span class="text-danger">*</span></label>
                                        <select class="form-select <?php $__errorArgs = ['category_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="category_id">
                                            <option value="">Select Category</option>
                                            <?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(old('category_id') == $category->id ? 'selected' : ''); ?>>
                                                    <?php echo e($category->name); ?></option>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                        </select>
                                        <?php $__errorArgs = ['category_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>

                                    <!-- Title -->
                                    <div class="col-md-6 mb-3">

                                        <label for="title">Title <span class="text-danger">*</span></label>
                                        <input type="text" class="form-control <?php $__errorArgs = ['title'];
$__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="title" id="title" value="<?php echo e(old('title')); ?>"
                                            placeholder="Add Title Here" required>
                                        <?php $__errorArgs = ['title'];
$__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>

                                    <!-- Slug -->
                                    <div class="col-md-6 mb-3">
                                        <label for="slug">Slug</label>
                                        <input type="text" class="form-control <?php $__errorArgs = ['slug'];
$__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="slug" id="slug" value="<?php echo e(old('slug')); ?>"
                                            placeholder="add Your Custom slug">
                                        <?php $__errorArgs = ['slug'];
$__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>

                                    <!-- Published At -->
                                    <div class="col-md-6 mb-3">
                                        <label for="published_at">Published At</label>
                                        <input type="date" class="form-control" name="published_at"
                                            value="<?php echo e(old('published_at')); ?>">
                                    </div>


                                    <!-- Featured Image Upload -->
                                    <div class="col-md-6 mb-3">
                                        <label for="post_image" class="form-label">Featured Image</label>

                                        <input type="file" name="post_image" id="post_image"
                                            class="form-control <?php $__errorArgs = ['post_image'];
$__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); ?>" accept="image/*"
                                            onchange="previewImage(event)">

                                        <?php $__errorArgs = ['post_image'];
$__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="mt-2">
                                            <img id="imagePreview"
                                                src="<?php echo e(isset($post) && $post->post_image ? asset($post->post_image) : ''); ?>"
                                                class="img-thumbnail <?php echo e(isset($post) && $post->post_image ? '' : 'd-none'); ?>"
                                                style="max-height:200px;">
                                        </div>
                                    </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', [])) ? 'selected' : ''); ?>>
                                                    <?php echo e($tag->name); ?></option>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                            <option value="other">Other</option>
                                        </select>
                                        <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="row">
                                        <div class="col-12 mb-3">
                                            <label for="editor">Content *</label>
                                            <textarea name="content" id="editor" 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); ?>" rows="8"><?php echo e(old('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 d-block"><?php echo e($message); ?></div>
                                            <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                                        </div>
                                    </div>


                                    <!-- Meta Fields -->
                                    <div class="col-md-4 mb-3">
                                        <label for="meta_title">Meta Title</label>
                                        <input type="text" class="form-control" name="meta_title"
                                            placeholder="Enter meta title" value="<?php echo e(old('meta_title')); ?>">
                                    </div>
                                    <div class="col-md-8 mb-3">
                                        <label for="meta_keywords">Meta Keywords</label>
                                        <input type="text" class="form-control" name="meta_keywords"
                                            placeholder="Enter meta keywords" value="<?php echo e(old('meta_keywords')); ?>">
                                    </div>

                                    <div class="col-md-12 mb-3">
                                        <label for="meta_description">Meta Description</label>
                                        <textarea class="form-control" name="meta_description" rows="4" placeholder="Enter meta description"><?php echo e(old('meta_description')); ?></textarea>
                                    </div>
                                    <!-- Publish Status -->

                                    <!-- Status -->
                                    <div class="col-md-6 mb-3">
                                        <label for="status">Status</label>
                                        <select class="form-select" name="status">
                                            <option value="1" <?php echo e(old('status') == '1' ? 'selected' : ''); ?>>Active
                                            </option>
                                            <option value="0" <?php echo e(old('status') == '0' ? 'selected' : ''); ?>>Inactive
                                            </option>
                                        </select>
                                    </div>
                                    <!-- Status -->
                                    <div class="col-md-6 mb-3">
                                        <label for="is_featured">Is Featured</label>
                                        <select class="form-select" name="is_featured">
                                            <option value="0" <?php echo e(old('is_featured') == '0' ? 'selected' : ''); ?>>No
                                            </option>
                                            <option value="1" <?php echo e(old('is_featured') == '1' ? 'selected' : ''); ?>>Yes
                                            </option>
                                        </select>
                                    </div>


                                    <!-- Submit -->
                                    <div class="col-md-12 text-end">
                                        <button type="submit" class="btn btn-success">Publish Blog</button>
                                    </div>
                                </div>
                            </form>
                            
                            <script>
                                function previewImage(event) {
                                    const input = event.target;
                                    const preview = document.getElementById('imagePreview');

                                    if (input.files && input.files[0]) {
                                        const reader = new FileReader();

                                        reader.onload = function(e) {
                                            preview.src = e.target.result;
                                            preview.classList.remove('d-none');
                                        };

                                        reader.readAsDataURL(input.files[0]);
                                    }
                                }
                            </script>
                            
                            <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
                            <script>
                                $(document).ready(function() {
                                    $('#tag_id').change(function() {
                                        if ($(this).val().includes('other')) {
                                            $('#manual_tag_container').show();
                                        } else {
                                            $('#manual_tag_container').hide();
                                        }
                                    });

                                    $('#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,
                                                    _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>
    </div>
    <!-- CKEditor 5 CDN -->
    <!-- CKEditor 5 Classic Build -->
    <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>

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

<?php echo $__env->make('admin.layouts.main', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH D:\NEWXAMP\htdocs\nitinnewsproject\resources\views/admin/Post/create.blade.php ENDPATH**/ ?>