@extends('layouts.app')

@section('content')
<div class="container-xxl flex-grow-1 container-p-y">
  <div class="card">
    <div class="card-header d-flex justify-content-between align-items-center">
      <h5 class="mb-0">Services</h5>
      @can('create services')
        <a href="{{ route('admin.services.create') }}" class="btn btn-sm btn-primary">Add Service</a>
      @endcan
    </div>

    <div class="table-responsive">
      <table @if($services->count() > 0) id="services-table" @endif class="table">
        <thead>
          <tr>
            <th>Name</th>
            <th>Category</th>
            <th>Slug</th>
            <th>Status</th>
            <th>Action</th>
          </tr>
        </thead>
        <tbody>
          @forelse($services as $service)
            <tr>
              <td>{{ $service->name }}</td>
              <td>{{ $service->category->name ?? 'N/A' }}</td>
              <td>{{ $service->slug }}</td>
              <td>{{ $service->is_active ? 'Active' : 'Pending' }}</td>
              <td>
                <div class="d-flex gap-2">
                  @can('update services')
                    <a href="{{ route('admin.services.edit', $service) }}" class="btn btn-sm btn-outline-primary">Edit</a>
                  @endcan

                  @can('delete services')
                    <form method="POST" action="{{ route('admin.services.destroy', $service) }}">
                      @csrf
                      @method('DELETE')
                      <button type="submit" class="btn btn-sm btn-outline-danger" onclick="return confirm('Delete this service?')">Delete</button>
                    </form>
                  @endcan
                </div>
              </td>
            </tr>
          @empty
            <tr><td colspan="5" class="text-center py-4">No services found.</td></tr>
          @endforelse
        </tbody>
      </table>
    </div>

    <div class="card-body">{{ $services->links() }}</div>
  </div>
</div>
@endsection

@if($services->count() > 0)
@push('script')
<script>
$(document).ready(function () {
  const $table = $('#services-table');
  if (!$table.length) return;

  $table.DataTable({
    responsive: false,
    scrollX: true,
    pageLength: 10,
    lengthMenu: [5, 10, 25, 50, 100],
    order: [[0, 'asc']],
    columnDefs: [{ orderable: false, targets: -1 }]
  });
});
</script>
@endpush
@endif
