@extends('admin.masterAdmin')

@section('content')
    <div class="card">
        <div class="card-header">{{ isset($blog) ? 'Edit' : 'Create' }} Blog</div>
        <div class="card-body">
            <form action="{{ isset($blog) ? route('blogs.update', $blog->id) : route('blogs.store') }}" method="POST"
                enctype="multipart/form-data">
                @csrf
                @if (isset($blog))
                    @method('PUT')
                @endif

                <div class="mb-3">
                    <label>Title</label>
                    <input type="text" name="title" class="form-control" value="{{ old('title', $blog->title ?? '') }}"
                        required>
                </div>

                <div class="mb-3">
                    <label>Author</label>
                    <input type="text" name="author" class="form-control"
                        value="{{ old('author', $blog->author ?? '') }}" required>
                </div>

                <div class="mb-3">
                    <label>Category</label>
                    <select name="category_id" class="form-control" required>
                        <option value="">-- Select Category --</option>
                        @foreach ($categories as $category)
                            <option value="{{ $category->id }}"
                                {{ old('category_id', $blog->category_id ?? '') == $category->id ? 'selected' : '' }}>
                                {{ ucfirst($category->name) }}
                            </option>
                        @endforeach
                    </select>
                </div>

                <div class="mb-3">
                    <label>Tag</label>
                    <select name="tag_id" class="form-control" required>
                        <option value="">-- Select Tag --</option>
                        @foreach ($tags as $tag)
                            <option value="{{ $tag->id }}"
                                {{ old('tag_id', $blog->tag_id ?? '') == $tag->id ? 'selected' : '' }}>
                                {{ ucfirst($tag->name) }}
                            </option>
                        @endforeach
                    </select>
                </div>

                <div class="mb-3">
                    <label>Content</label>
                    <textarea name="content" id="content" class="form-control my-editor" rows="5" required>
                        {{ old('content', $blog->content ?? '') }}
                    </textarea>
                </div>

                <div class="mb-3">
                    <label>Status</label>
                    <select name="status" class="form-control" required>
                        <option value="draft" {{ old('status', $blog->status ?? '') == 'draft' ? 'selected' : '' }}>Draft
                        </option>
                        <option value="published"
                            {{ old('status', $blog->status ?? '') == 'published' ? 'selected' : '' }}>
                            Published</option>
                    </select>
                </div>

                <div class="mb-3">
                    <label>Image</label>
                    <input type="file" name="image" accept="image/*" class="form-control">
                    @if (isset($blog) && $blog->image)
                        <div class="mt-2">
                            <img src="{{ asset($blog->image) }}" width="120" alt="Blog Image">
                        </div>
                    @endif
                </div>

                <button class="btn btn-success mt-3">{{ isset($blog) ? 'Update' : 'Save' }}</button>
                <a href="{{ route('blogs.index') }}" class="btn btn-secondary mt-3">Cancel</a>
            </form>

        </div>
    </div>
    <script>
        $('#content').summernote({
            placeholder: 'Write something...',
            tabsize: 2,
            height: 100
        });
        $('.dropdown-toggle').dropdown()
    </script>
@endsection
