@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">{{ $isSuperAdmin ? 'All Shared Subscriptions' : 'My Shared Subscriptions' }}</h5>
      @php
        $legacyRole = strtolower(trim((string) auth()->user()->role));
        $canCreateSubscription = auth()->user()->can('create subscription')
            || auth()->user()->hasAnyRole(['user', 'member', 'owner', 'admin', 'super admin'])
            || in_array($legacyRole, ['user', 'member', 'owner', 'admin', 'super admin'], true);
      @endphp
      @if(! $isSuperAdmin && $canCreateSubscription)
      <a href="{{ route('subscriptions.create') }}" class="btn btn-sm btn-primary">New Listing</a>
      @endif
    </div>

    @can('view subscription')
      <div class="table-responsive">
        <table @if($subscriptions->count() > 0) id="dashboard-shared-subscriptions-table" @endif class="table">
          <thead>
            <tr>
              @if($isSuperAdmin)<th>Owner</th>@endif
              <th>Service</th>
              <th>Plan</th>
              <th>Price/Slot</th>
              <th>Slots</th>
              <th>Visibility</th>
              <th>Status</th>
              <th>Action</th>
            </tr>
          </thead>
          <tbody>
            @forelse($subscriptions as $item)
            <tr>
              @if($isSuperAdmin)<td>{{ $item->owner?->name ?? 'N/A' }}</td>@endif
              <td>{{ $item->service?->name ?? 'N/A' }}</td>
              <td>{{ $item->servicePlan?->name ?? 'N/A' }}</td>
              <td>${{ number_format($item->price_per_slot,2) }}</td>
              <td>{{ $item->available_slots }}/{{ $item->total_slots }}</td>
              <td>{{ $item->is_public ? 'Public' : 'Private' }}</td>
              <td>
                <span class="badge bg-label-info">
                  {{ $item->status === 'draft' ? 'Pending' : ucfirst($item->status) }}
                </span>
              </td>
              <td>
                @if($isSuperAdmin)
                  <div class="d-flex gap-2">
                    @can('update subscription')
                      <a href="{{ route('admin.shared-subscriptions.edit', $item) }}" class="btn btn-sm btn-outline-primary">Manage</a>
                    @endcan

                    @can('delete subscription')
                      <form method="POST" action="{{ route('admin.shared-subscriptions.destroy', $item) }}">
                        @csrf
                        @method('DELETE')
                        <button type="submit" class="btn btn-sm btn-outline-danger" onclick="return confirm('Delete this shared subscription?')">Delete</button>
                      </form>
                    @endcan
                  </div>
                @else
                  <span class="text-muted">-</span>
                @endif
              </td>
            </tr>
            @empty
            <tr><td colspan="{{ $isSuperAdmin ? 8 : 7 }}" class="text-center py-4">No shared subscriptions yet.</td></tr>
            @endforelse
          </tbody>
        </table>
      </div>
    @else
      <div class="card-body"><div class="alert alert-warning mb-0">You do not have permission to view subscriptions.</div></div>
    @endcan
  </div>
</div>
@endsection

@if($subscriptions->count() > 0)
@push('script')
<script>
$(document).ready(function () {
  const $table = $('#dashboard-shared-subscriptions-table');
  if (!$table.length) return;
  const serviceColumnIndex = {{ $isSuperAdmin ? 1 : 0 }};

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