
<?php $__env->startSection('pageTitle', isset($pageTitle) ? $pageTitle : 'Page Title Here'); ?>
<?php $__env->startSection('content'); ?>

<div class="page-header">
    <div class="row">
        <div class="col-md-6 col-sm-12">
            <div class="title">
                <h4>Add Post</h4>
            </div>
            <nav aria-label="breadcrumb" role="navigation">
                <ol class="breadcrumb">
                    <li class="breadcrumb-item">
                        <a href="<?php echo e(route('admin.dashboard')); ?>">Home</a>
                    </li>
                    <li class="breadcrumb-item active" aria-current="page">
                        Add post
                    </li>
                </ol>
            </nav>
        </div>
        <div class="col-md-6 col-sm-12 text-right">
            <a href="<?php echo e(route('admin.posts')); ?>" class="btn btn-primary">View all posts</a>
        </div>
    </div>
</div>

<form action="<?php echo e(route('admin.create_post')); ?>" method="POST" autocomplete="off" enctype="multipart/form-data" id="addPostForm">
    <?php echo csrf_field(); ?>
<div class="row">
    <div class="col-md-9">
        <div class="card card-box mb-2">
            <div class="card-body">
                <div class="form-group">
                    <label for=""><b>Title</b>:</label>
                    <input type="text" name="title" placeholder="Enter post title"  class="form-control">
                    <span class="text-danger error-text title_error">
                    </span>
                </div>
                <div class="form-group">
                <label for=""><b>Content</b>:</label>
                <textarea name="content" id="content" cols="30" rows="10" class="ckeditor5 form-control" placeholder="Enter post content here..."></textarea>
                <span class="text-danger error-text content_error"></span>
                </div>
            </div>
        </div>
        <div class="card card-box mb-2">
            <div class="card-header weight-500">SEO</div>
                <div class="card-body">
                    <div class="form-group">
                        <label for=""><b>Post meta keywords</b>: <small>(Separated by comma.)</small></label>
                        <input type="text" name="meta_keywords" placeholder="Enter post mete keywords"  class="form-control">
                    </div>
                    <div class="form-group">
                <label for=""><b>Post meta description</b>:</label>
                <textarea name="meta_description"  cols="30" rows="10" class="form-control" placeholder="Enter post meta description here..."></textarea>
                <span class="text-danger error-text content_error"></span>
                </div>
                </div>
            
        </div>
    </div>
    <div class="col-md-3">
    <div class="card card-box mb-2">
        <div class="card-body">
            <div class="form-group">
                <label for=""><b>Post Category</b>:</label>
                <select name="category" id="" class="custom-select form-control">
                    <option value="">Choose...</option>
                   <?php echo $categories_html; ?>

                </select>
                <span class="text-danger error-text category_error"></span>
            </div>
            <div class="form-group">
                <label for=""><b>Post Featured image</b>:</label>
                <input type="file" name="featured_image" class="form-control-file form-control" height="auto">
                <span class="text-danger error-text featured_image_error"></span>
            </div>
            <div class="d-block mb-3" style="max-width:250px">
                <img src="" alt="" class="img-thumbnail" id="featured_image_preview">
            </div>
            <div class="form-group">
                <label for=""><b>Tags</b>:</label>
                <input type="text" name="tags" class="form-control" data-role="tagsinput">
            </div>
            <hr>
            <div class="form-group">
                <label for=""><b>Visibility</b>:</label>
                <div class="custom-control custom-radio mb-5">
                    <input type="radio" name="visibility" id="customRadio1" class="custom-control-input" value="1" checked>
                    <label for="customRadio1" class="custom-control-label">Public</label>
                </div>
                <div class="custom-control custom-radio mb-5">
                    <input type="radio" name="visibility" id="customRadio2" class="custom-control-input" value="0">
                    <label for="customRadio2" class="custom-control-label">Private</label>
                </div>
            </div>
        </div>
    </div>
    </div>
</div>
<div class="mb-3">
    <button type="submit" class="btn btn-primary">Create Post</button>
</div>
</form>

<?php $__env->stopSection(); ?>
<?php $__env->startPush('stylesheets'); ?>
<link rel="stylesheet" href="/back/src/plugins/bootstrap-tagsinput/bootstrap-tagsinput.css">
<?php $__env->stopPush(); ?>
<?php $__env->startPush('scripts'); ?>
<script src="/back/src/plugins/bootstrap-tagsinput/bootstrap-tagsinput.js"></script>
<script src="https://cdn.ckeditor.com/4.22.1/standard/ckeditor.js"></script>
<script>
    document.addEventListener("DOMContentLoaded", function() {
        const inputFile = document.querySelector('input[type="file"][name="featured_image"]');
        const previewImage = document.getElementById("featured_image_preview");

        inputFile.addEventListener("change", function(event) {
            const file = event.target.files[0];
            if (file) {
                const reader = new FileReader();
                reader.onload = function(e) {
                    previewImage.src = e.target.result;
                };
                reader.readAsDataURL(file);
            }
        });

        
    });
    $(document).ready(function() {
        $('#addPostForm').submit(function(e) {
            e.preventDefault(); // Prevent default form submission

            var form = this;
            // var errorElement = $(form).find('span.text-danger');

            // Reset error message
            // errorElement.text('');
                var content = CKEDITOR.instances.content.getData();
                var formData = new FormData(form);
                formData.append('content', content);
                $.ajax({
                    url: $(form).attr('action'),
                    method: $(form).attr('method'),
                    data: formData,
                    processData: false,
                    contentType: false,
                    dataType: 'json',
                    beforeSend: function() {
                        $(form).find('span.error-text').text('');
                    },
                    success: function(data) {
                        try {
                            if (data.status === 1) {
                                $(form)[0].reset();
                                CKEDITOR.instances.content.setData('');
                                $('img#featured_image_preview').attr('src','');
                                $('input[name="tags"]').tagsinput('removeAll');
                                toastr.success(data.message);

                                // Update all instances of the site logo
                                // $('img.site_logo').each(function() {
                                //     $(this).attr('src', '/' + data.image_path + '?' + new Date().getTime());
                                // });
                            } else {
                                toastr.error(data.message);
                            }
                        } catch (e) {
                            console.error('Parsing error:', e);
                            toastr.error('Unexpected response format.');
                        }
                    },
                    error: function(data) {
                        $.each(data.responseJSON.errors, function(prefix, val){
                            $(form).find('span.'+prefix+'_error').text(val[0]);
                        });
                    }
                });
       
              
        });
    });
    document.addEventListener("DOMContentLoaded", function() {
        // Initialize CKEditor on the correct textarea
        if (document.querySelector("#content")) {
            CKEDITOR.replace('content');
        } else {
            console.error("CKEditor textarea not found!");
        }
    });
    
</script>
<?php $__env->stopPush(); ?>
<?php echo $__env->make('back.layout.page-layout', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\xampp\htdocs\larablog\resources\views\back\pages\post\add_post.blade.php ENDPATH**/ ?>