@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">Service Plans</h5>
      @can('create plans')
        <a href="{{ route('admin.service-plans.create') }}" class="btn btn-sm btn-primary">Add Plan</a>
      @endcan
    </div>

    <div class="table-responsive">
      <table @if($servicePlans->count() > 0) id="service-plans-table" @endif class="table">
        <thead>
          <tr>
            <th>Service</th>
            <th>Name</th>
            <th>Price</th>
            <th>Max Slots</th>
            <th>Status</th>
            <th>Action</th>
          </tr>
        </thead>
        <tbody>
          @forelse($servicePlans as $plan)
            <tr>
              <td>{{ $plan->service->name ?? 'N/A' }}</td>
              <td>{{ $plan->name }}</td>
              <td>${{ number_format($plan->price, 2) }}</td>
              <td>{{ $plan->max_slots }}</td>
              <td>{{ $plan->is_active ? 'Active' : 'Pending' }}</td>
              <td>
                @canany(['update plans', 'delete plans'])
                  <div class="d-flex gap-2">
                    @can('update plans')
                      <a href="{{ route('admin.service-plans.edit', $plan) }}" class="btn btn-sm btn-outline-primary">Edit</a>
                    @endcan

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

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

@if($servicePlans->count() > 0)
@push('script')
<script>
$(document).ready(function () {
  const $table = $('#service-plans-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
