@extends('admin.layouts.home')

@section('title', 'Edit Blog')

@section('content')
<div class="container-fluid">
    <!-- Page Heading -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 class="h3 mb-0 text-gray-800">Edit Blog: {{ $blog->title }}</h1>
        <a href="{{ route('admin.blogs.index') }}" class="btn btn-secondary">
            <i class="bi bi-arrow-left mr-1"></i> Back to Blogs
        </a>
    </div>

    <!-- Blog Form Card -->
    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">Blog Information</h6>
        </div>
        <div class="card-body">
            @if($errors->any())
            <div class="alert alert-danger">
                <ul class="mb-0">
                    @foreach($errors->all() as $error)
                    <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
            @endif

            <form action="{{ route('admin.blogs.update', $blog) }}" method="POST" enctype="multipart/form-data">
                @csrf
                @method('PUT')
                <div class="row">
                    <div class="col-md-8">
                        <!-- Basic Information -->
                        <div class="card mb-4">
                            <div class="card-header">
                                <h6 class="mb-0">Basic Information</h6>
                            </div>
                            <div class="card-body">
                                <div class="form-group">
                                    <label for="title">Title <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control" id="title" name="title" value="{{ old('title', $blog->title) }}" required>
                                </div>
                                
                                <div class="form-group">
                                    <label for="description">Short Description <span class="text-danger">*</span></label>
                                    <textarea class="form-control" id="description" name="description" rows="3" required>{{ old('description', $blog->description) }}</textarea>
                                    <small class="form-text text-muted">A brief summary of the blog (max 500 characters).</small>
                                </div>
                                
                                <div class="form-group">
                                    <label for="content">Content <span class="text-danger">*</span></label>
                                    <textarea class="form-control" id="content" name="content" rows="10" required>{{ old('content', $blog->content) }}</textarea>
                                </div>
                            </div>
                        </div>
                        
                        <div class="card mb-4">
                            <div class="card-header">
                                <h6 class="mb-0">SEO Information</h6>
                            </div>
                            <div class="card-body">
                                <div class="form-group">
                                    <label for="slug">Slug</label>
                                    <input type="text" class="form-control" id="slug" value="{{ $blog->slug }}" disabled>
                                    <small class="form-text text-muted">The slug is automatically generated from the title and used in the URL.</small>
                                </div>
                                
                                <div class="form-group">
                                    <label for="meta_title">Meta Title</label>
                                    <input type="text" class="form-control" id="meta_title" name="meta_title" value="{{ old('meta_title', $blog->meta_title) }}" maxlength="60">
                                    <small class="form-text text-muted">Recommended: 50-60 characters</small>
                                </div>
                                
                                <div class="form-group">
                                    <label for="meta_description">Meta Description</label>
                                    <textarea class="form-control" id="meta_description" name="meta_description" rows="3" maxlength="160">{{ old('meta_description', $blog->meta_description) }}</textarea>
                                    <small class="form-text text-muted">Recommended: 150-160 characters</small>
                                </div>
                                
                                <div class="form-group">
                                    <label for="meta_keywords">Meta Keywords</label>
                                    <input type="text" class="form-control" id="meta_keywords" name="meta_keywords" value="{{ old('meta_keywords', $blog->meta_keywords) }}" placeholder="keyword1, keyword2, keyword3">
                                    <small class="form-text text-muted">Separate keywords with commas</small>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="col-md-4">
                        <!-- Sidebar Options -->
                        <div class="card mb-4">
                            <div class="card-header">
                                <h6 class="mb-0">Publishing Options</h6>
                            </div>
                            <div class="card-body">
                                <div class="form-group">
                                    <div class="custom-control custom-switch">
                                        <input type="checkbox" class="custom-control-input" id="active" name="active" value="1" {{ old('active', $blog->active) ? 'checked' : '' }}>
                                        <label class="custom-control-label" for="active">Publish</label>
                                    </div>
                                    <small class="form-text text-muted">If enabled, this blog will be visible to users.</small>
                                </div>
                                
                                <div class="form-group">
                                    <div class="custom-control custom-switch">
                                        <input type="checkbox" class="custom-control-input" id="is_featured" name="is_featured" value="1" {{ old('is_featured', $blog->is_featured) ? 'checked' : '' }}>
                                        <label class="custom-control-label" for="is_featured">Featured</label>
                                    </div>
                                    <small class="form-text text-muted">Mark this blog as featured.</small>
                                </div>

                                <div class="form-group">
                                    <label for="status">Status</label>
                                    <select name="status" id="status" class="form-control">
                                        <option value="Published" {{ old('status', $blog->status) == 'Published' ? 'selected' : '' }}>Published</option>
                                        <option value="Draft" {{ old('status', $blog->status) == 'Draft' ? 'selected' : '' }}>Draft</option>
                                        <option value="Pending" {{ old('status', $blog->status) == 'Pending' ? 'selected' : '' }}>Pending Review</option>
                                    </select>
                                </div>

                                <div class="form-group">
                                    <label for="type">Type</label>
                                    <input type="text" class="form-control" id="type" name="type" value="{{ old('type', $blog->type ?? 'blog') }}" placeholder="e.g., blog, article, news">
                                </div>
                                
                                <div class="form-group mb-0">
                                    <p class="mb-1"><strong>Created:</strong> {{ $blog->created_at->format('M d, Y H:i') }}</p>
                                    <p class="mb-0"><strong>Last Updated:</strong> {{ $blog->updated_at->format('M d, Y H:i') }}</p>
                                </div>
                            </div>
                        </div>
                        
                        <div class="card mb-4">
                            <div class="card-header">
                                <h6 class="mb-0">Category</h6>
                            </div>
                            <div class="card-body">
                                <div class="form-group">
                                    <label for="category_id">Category</label>
                                    <select name="category_id" id="category_id" class="form-control @error('category_id') is-invalid @enderror">
                                        <option value="">Select Category (Optional)</option>
                                        @foreach($categories as $category)
                                            <option value="{{ $category->id }}" {{ old('category_id', $blog->category_id) == $category->id ? 'selected' : '' }}>
                                                {{ $category->name }}
                                            </option>
                                        @endforeach
                                    </select>
                                    @error('category_id')
                                        <span class="invalid-feedback">{{ $message }}</span>
                                    @enderror
                                </div>
                            </div>
                        </div>
                        
                        <div class="card mb-4">
                            <div class="card-header">
                                <h6 class="mb-0">Thumbnail</h6>
                            </div>
                            <div class="card-body">
                                <div class="form-group">
                                    <div class="img-thumbnail mb-3 d-flex align-items-center justify-content-center" style="height: 200px; background-color: #f8f9fa;">
                                        @if($blog->thumbnail)
                                            <img src="{{ asset('storage/' . $blog->thumbnail) }}" class="h-100" style="object-fit: contain;">
                                        @else
                                            <div class="text-center text-muted">
                                                <i class="bi bi-image" style="font-size: 3rem;"></i>
                                                <p class="mb-0">No image selected</p>
                                            </div>
                                        @endif
                                    </div>
                                    <input type="file" class="form-control-file" id="thumbnail" name="thumbnail">
                                    <small class="form-text text-muted">Recommended size: 1200x630 pixels. Max size: 2MB.</small>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="text-center mt-4">
                    <button type="submit" class="btn btn-primary btn-lg px-5">
                        <i class="bi bi-save mr-2"></i> Update Blog
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script src="https://cdn.tiny.cloud/1/no-api-key/tinymce/5/tinymce.min.js" referrerpolicy="origin"></script>
<script>
    // Initialize TinyMCE
    tinymce.init({
        selector: '#content',
        height: 500,
        menubar: true,
        plugins: [
            'advlist autolink lists link image charmap print preview anchor',
            'searchreplace visualblocks code fullscreen',
            'insertdatetime media table paste code help wordcount'
        ],
        toolbar: 'undo redo | formatselect | ' +
        'bold italic backcolor | alignleft aligncenter ' +
        'alignright alignjustify | bullist numlist outdent indent | ' +
        'removeformat | help',
    });
    
    // Preview uploaded image
    document.getElementById('thumbnail').onchange = function(e) {
        const preview = document.querySelector('.img-thumbnail');
        const file = e.target.files[0];
        
        if (file) {
            const reader = new FileReader();
            
            reader.onload = function(e) {
                preview.innerHTML = `<img src="${e.target.result}" class="h-100" style="object-fit: contain;">`;
            }
            
            reader.readAsDataURL(file);
        }
    };
</script>
@endpush 