@extends('layouts.admin')

@section('title', 'Manage Pages')

@section('admin-content')
<style>
    /* Specific Pages Styles */
    .pages-page .slug-badge { font-family: 'Consolas', 'Monaco', monospace; background: #e3f2fd; color: #1565c0; padding: 4px 10px; border-radius: 6px; font-size: 0.85rem; }
    
    .pages-page .status-badge.active { background: #dcfce7; color: #166534; }
    .pages-page .status-badge.inactive { background: #f1f5f9; color: #475569; }
</style>

<div class="pages-page">
    <!-- Page Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="fw-bold mb-1">Manage Static Pages</h4>
            <p class="text-muted mb-0 small">Create and manage content pages (e.g., About Us, Terms)</p>
        </div>
        <div class="d-flex gap-2">
            <a href="{{ route('admin.pages.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> Create New Page
            </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') }}
            <button type="button" class="btn-close ms-auto" data-bs-dismiss="alert"></button>
        </div>
    @endif

    <!-- Stats -->
    <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-file-earmark-richtext"></i>
                </div>
                <div>
                    <div class="stat-value">{{ $pages->count() }}</div>
                    <div class="stat-label">Total Pages</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">{{ $pages->where('is_active', true)->count() }}</div>
                    <div class="stat-label">Active 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 secondary">
                    <i class="bi bi-eye-slash-fill"></i>
                </div>
                <div>
                    <div class="stat-value">{{ $pages->where('is_active', false)->count() }}</div>
                    <div class="stat-label">Inactive / Drafts</div>
                </div>
            </div>
        </div>
    </div>

    <!-- Pages Table -->
    <div class="data-table">
        <div class="table-responsive">
            <table class="table mb-0">
                <thead>
                    <tr>
                        <th class="ps-4">Page Title</th>
                        <th>URL Slug</th>
                        <th class="text-center">Status</th>
                        <th>Last Modified</th>
                        <th class="text-end pe-4">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($pages as $page)
                        <tr>
                            <td class="ps-4 fw-bold text-dark">{{ $page->title }}</td>
                            <td>
                                <span class="slug-badge">/p/{{ $page->slug }}</span>
                            </td>
                            <td class="text-center">
                                @if($page->is_active)
                                    <span class="status-badge active">
                                        <i class="bi bi-check-circle-fill"></i> Active
                                    </span>
                                @else
                                    <span class="status-badge inactive">
                                        <i class="bi bi-slash-circle"></i> Inactive
                                    </span>
                                @endif
                            </td>
                            <td>
                                <div class="text-dark small fw-medium">{{ $page->updated_at->format('M d, Y') }}</div>
                                <small class="text-muted">{{ $page->updated_at->diffForHumans() }}</small>
                            </td>
                            <td class="text-end pe-4">
                                <div class="d-flex gap-1 justify-content-end">
                                    <a href="{{ route('public.pages.show', $page->slug) }}" target="_blank" class="action-btn secondary" title="View Public Page" data-bs-toggle="tooltip">
                                        <i class="bi bi-eye"></i>
                                    </a>
                                    
                                    <a href="{{ route('admin.pages.edit', $page->id) }}" class="action-btn primary" title="Edit Page" data-bs-toggle="tooltip">
                                        <i class="bi bi-pencil"></i>
                                    </a>
                                    
                                    <form action="{{ route('admin.pages.destroy', $page->id) }}" method="POST" class="d-inline"
                                          onsubmit="return confirm('Are you sure you want to delete this page? This action cannot be undone.');">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="action-btn danger" title="Delete Page" 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-file-earmark-x fs-1 text-muted opacity-50 d-block mb-3"></i>
                                    <h5 class="fw-bold text-muted">No Pages Found</h5>
                                    <p class="text-muted small mb-0">Create your first custom page to get started</p>
                                </div>
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        
        <div class="px-3 border-top">
            {{ $pages->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
