@extends('layouts.admin')

@section('title', 'Manage Blog Posts')

@section('admin-content')
<style>
    /* Specific Blog Styles */
    .blog-page .category-badge { display: inline-flex; align-items: center; gap: 5px; padding: 5px 12px; background: #f3e5f5; color: #7b1fa2; border-radius: 50px; font-size: 0.8rem; font-weight: 600; }
    
    .blog-page .status-badge.published { background: #dcfce7; color: #166534; }
    .blog-page .status-badge.draft { background: #fef9c3; color: #854d0e; }
</style>

<div class="blog-page">
    <!-- Page Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="fw-bold mb-1">Manage Blog Posts</h4>
            <p class="text-muted mb-0 small">Create and manage content for the platform blog</p>
        </div>
        <div class="d-flex gap-2">
            <a href="{{ route('admin.blog.create') }}" class="btn btn-primary shadow-sm btn-lg d-flex align-items-center gap-2" 
               style="border-radius: 12px; font-size: 0.95rem;">
                <i class="bi bi-plus-lg"></i> Add New Post
            </a>
        </div>
    </div>

    @if(session('success'))
        <div class="alert alert-success border-0 shadow-sm rounded-3 mb-4 d-flex align-items-center">
            <i class="bi bi-check-circle-fill me-2 fs-5"></i>
            {{ session('success') }}
        </div>
    @endif

    <!-- Stats Overview -->
    <div class="row g-4 mb-4">
        <div class="col-md-4">
            <div class="stat-card d-flex align-items-center gap-3">
                <div class="stat-icon primary">
                    <i class="bi bi-stickies"></i>
                </div>
                <div>
                    <div class="stat-value">{{ $posts->count() }}</div>
                    <div class="stat-label">Total Posts</div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="stat-card d-flex align-items-center gap-3">
                <div class="stat-icon success">
                    <i class="bi bi-check-circle-fill"></i>
                </div>
                <div>
                    <div class="stat-value">{{ $posts->where('is_published', true)->count() }}</div>
                    <div class="stat-label">Published</div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="stat-card d-flex align-items-center gap-3">
                <div class="stat-icon warning">
                    <i class="bi bi-pencil-square"></i>
                </div>
                <div>
                    <div class="stat-value">{{ $posts->where('is_published', false)->count() }}</div>
                    <div class="stat-label">Drafts</div>
                </div>
            </div>
        </div>
    </div>

    <!-- Blog Posts Table -->
    <div class="data-table">
        <div class="table-responsive">
            <table class="table mb-0">
                <thead>
                    <tr>
                        <th class="ps-4">Article Details</th>
                        <th>Category</th>
                        <th class="text-center">Status</th>
                        <th>Published Date</th>
                        <th class="text-end pe-4">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($posts as $post)
                        <tr>
                            <td class="ps-4">
                                <h6 class="fw-bold text-dark mb-0 text-wrap" style="max-width: 350px;">
                                    {{ Str::limit($post->title, 60) }}
                                </h6>
                                <small class="text-muted">Slug: /{{ Str::slug($post->title) }}</small>
                            </td>
                            <td>
                                <span class="category-badge">
                                    <i class="bi bi-tag"></i> {{ $post->category->name ?? 'Uncategorized' }}
                                </span>
                            </td>
                            <td class="text-center">
                                @if($post->is_published)
                                    <span class="status-badge published">
                                        <i class="bi bi-check-circle-fill"></i> Published
                                    </span>
                                @else
                                    <span class="status-badge draft">
                                        <i class="bi bi-file-earmark"></i> Draft
                                    </span>
                                @endif
                            </td>
                            <td>
                                @if($post->published_at)
                                    <div class="text-dark small fw-medium">{{ $post->published_at->format('d M Y') }}</div>
                                    <small class="text-muted">{{ $post->published_at->diffForHumans() }}</small>
                                @else
                                    <span class="text-muted small">Not published</span>
                                @endif
                            </td>
                            <td class="text-end pe-4">
                                <div class="d-flex gap-1 justify-content-end">
                                    <a href="{{ route('admin.blog.edit', $post->id) }}" class="action-btn primary" title="Edit Post" data-bs-toggle="tooltip">
                                        <i class="bi bi-pencil"></i>
                                    </a>
                                    
                                    <form action="{{ route('admin.blog.destroy', $post->id) }}" method="POST" class="d-inline"
                                          onsubmit="return confirm('Are you sure you want to delete this post?');">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="action-btn danger" title="Delete Post" data-bs-toggle="tooltip">
                                            <i class="bi bi-trash"></i>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="5" class="text-center py-5">
                                <div class="py-4">
                                    <i class="bi bi-journal-text fs-1 text-muted opacity-50 d-block mb-3"></i>
                                    <h5 class="fw-bold text-muted">No Blog Posts</h5>
                                    <p class="text-muted small mb-0">Start writing your first blog post</p>
                                </div>
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        
        <div class="px-3 border-top">
            {{ $posts->links() }}
        </div>
    </div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
        tooltipTriggerList.map(function (el) {
            return new bootstrap.Tooltip(el);
        });
    });
</script>
@endsection
