@extends('layouts.admin')

@section('content')
  @php
    $pageCount = $pagedetails->count();
    $pagesWithImage = $pagedetails->where('pageimg', '!=', null)->count();
    $orderedPages = $pagedetails->filter(fn ($page) => filled($page->pageorder))->count();
  @endphp

  <div class="card-header">
    <h4>Manage Pages</h4>
    <a class="btn btn-primary float-right" href="{{ route('admin.addpage') }}">Add Page</a>
  </div>

  <div class="card-body">
    @include('admin.partials.flash-messages')

    <div class="portal-page-shell">
      <div class="portal-hero-card">
        <div class="portal-hero-main">
          <div class="portal-hero-copy">
            <h5>Page Management</h5>
            <p>Manage static content pages, control their order, and keep page images and content updated from one place.</p>
            <div class="portal-chip-list">
              <span class="portal-chip">{{ $pageCount }} Total Pages</span>
              <span class="portal-chip">{{ $pagesWithImage }} With Image</span>
              <span class="portal-chip">{{ $orderedPages }} Ordered</span>
            </div>
          </div>
        </div>
      </div>

      <div class="portal-form-shell">
        <div class="table-responsive">
          <table class="table table-striped table-hover" id="tableExport" style="width:100%;">
            <thead>
              <tr>
                <th>Page Name</th>
                <th>Slug</th>
                <th>Order</th>
                <th>Page Image</th>
                <th>Action</th>
              </tr>
            </thead>
            <tbody>
              @forelse($pagedetails as $page)
                <tr>
                  <td>{{ $page->pagename }}</td>
                  <td>{{ $page->pageslug }}</td>
                  <td>{{ filled($page->pageorder) ? $page->pageorder : '-' }}</td>
                  <td><img src="{{ $page->image_url }}" class="portal-table-thumb" alt="{{ $page->pagename }}"></td>
                  <td>
                    <a class="btn btn-success btn-sm" data-toggle="tooltip" data-placement="top" title="Edit Page" href="{{ route('admin.editpage', ['id' => $page->id]) }}">
                      <i class="fas fa-pencil-alt"></i>
                    </a>
                    <form action="{{ route('admin.deletepage', ['id' => $page->id]) }}" method="post" class="d-inline-block">
                      @csrf
                      <button type="submit" class="btn btn-danger deletedata" data-toggle="tooltip" data-placement="top" title="Delete Page" data-confirm-message="Are you sure you want to delete this page?">
                        <i class="fas fa-trash"></i>
                      </button>
                    </form>
                  </td>
                </tr>
              @empty
                <tr>
                  <td colspan="5" class="text-center text-muted">No pages found yet.</td>
                </tr>
              @endforelse
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
@endsection
