@extends('admin.layouts.main')

@section('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>{{ $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="{{ route('dashboard') }}">
                                    <svg class="svg-color">
                                        <use href="{{ asset('assets/svg/iconly-sprite.svg#Home') }}"></use>
                                    </svg></a></li>
                            <li class="breadcrumb-item">Post</li>
                            <li class="breadcrumb-item active">{{ $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 -->
                            @if ($errors->any())
                                <div class="alert alert-danger">
                                    <ul>
                                        @foreach ($errors->all() as $error)
                                            <li>{{ $error }}</li>
                                        @endforeach
                                    </ul>
                                </div>
                            @endif

                            <form action="{{ route('post.store') }}" method="POST" enctype="multipart/form-data">
                                @csrf
                                <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 @error('category_id') is-invalid @enderror"
                                            name="category_id">
                                            <option value="">Select Category</option>
                                            @foreach ($categories as $category)
                                                <option value="{{ $category->id }}"
                                                    {{ old('category_id') == $category->id ? 'selected' : '' }}>
                                                    {{ $category->name }}</option>
                                            @endforeach
                                        </select>
                                        @error('category_id')
                                            <p class="invalid-feedback">{{ $message }}</p>
                                        @enderror
                                    </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 @error('title') is-invalid @enderror"
                                            name="title" id="title" value="{{ old('title') }}"
                                            placeholder="Add Title Here" required>
                                        @error('title')
                                            <p class="invalid-feedback">{{ $message }}</p>
                                        @enderror
                                    </div>

                                    <!-- Slug -->
                                    <div class="col-md-6 mb-3">
                                        <label for="slug">Slug</label>
                                        <input type="text" class="form-control @error('slug') is-invalid @enderror"
                                            name="slug" id="slug" value="{{ old('slug') }}"
                                            placeholder="add Your Custom slug">
                                        @error('slug')
                                            <p class="invalid-feedback">{{ $message }}</p>
                                        @enderror
                                    </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="{{ 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 @error('post_image') is-invalid @enderror" accept="image/*"
                                            onchange="previewImage(event)">

                                        @error('post_image')
                                            <div class="invalid-feedback">{{ $message }}</div>
                                        @enderror

                                        <div class="mt-2">
                                            <img id="imagePreview"
                                                src="{{ isset($post) && $post->post_image ? asset($post->post_image) : '' }}"
                                                class="img-thumbnail {{ isset($post) && $post->post_image ? '' : 'd-none' }}"
                                                style="max-height:200px;">
                                        </div>
                                    </div>


                                    {{-- tag here --}}
                                    <div class="col-md-6 mb-3">
                                        <label for="tag_id">Tags</label>
                                        <select class="form-select @error('tag_id') is-invalid @enderror" name="tag_id[]"
                                            id="tag_id" multiple>
                                            @foreach ($tags as $tag)
                                                <option value="{{ $tag->id }}"
                                                    {{ in_array($tag->id, old('tag_id', [])) ? 'selected' : '' }}>
                                                    {{ $tag->name }}</option>
                                            @endforeach
                                            <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>
                                        @error('tag_id')
                                            <p class="invalid-feedback">{{ $message }}</p>
                                        @enderror
                                    </div>


                                    <!-- Content -->
                                    <div class="row">
                                        <div class="col-12 mb-3">
                                            <label for="editor">Content *</label>
                                            <textarea name="content" id="editor" class="form-control @error('content') is-invalid @enderror" rows="8">{{ old('content') }}</textarea>
                                            @error('content')
                                                <div class="invalid-feedback d-block">{{ $message }}</div>
                                            @enderror
                                        </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="{{ 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="{{ 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">{{ 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" {{ old('status') == '1' ? 'selected' : '' }}>Active
                                            </option>
                                            <option value="0" {{ 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" {{ old('is_featured') == '0' ? 'selected' : '' }}>No
                                            </option>
                                            <option value="1" {{ 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>
                            {{-- Image Preview Script --}}
                            <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>
                            {{-- Tag Logic --}}
                            <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: '{{ route('tags.store') }}',
                                                method: 'POST',
                                                data: {
                                                    name: newTag,
                                                    _token: '{{ 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>

@endsection
