<?php $__env->startSection('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"><?php echo e($isSuperAdmin ? 'All Joined Subscriptions' : 'My Joined Subscriptions'); ?></h5>
      <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('join subscription')): ?>
        <a href="<?php echo e(route('subscriptions.index')); ?>" class="btn btn-sm btn-primary">Add Plan</a>
      <?php endif; ?>
    </div>

    <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('view subscription')): ?>
      <div class="table-responsive">
        <table <?php if($subscriptions->count() > 0): ?> id="dashboard-joined-subscriptions-table" <?php endif; ?> class="table">
          <thead>
            <tr>
              <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($isSuperAdmin): ?><th>Member</th><?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
              <th>Service</th>
              <th>Plan</th>
              <th>Owner</th>
              <th>Slots</th>
              <th>Amount</th>
              <th>Status</th>
              <th>Action</th>
            </tr>
          </thead>
          <tbody>
            <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php $__empty_1 = true; $__currentLoopData = $subscriptions; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
            <tr>
              <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($isSuperAdmin): ?><td><?php echo e($item->user?->name ?? 'N/A'); ?></td><?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
              <td><?php echo e($item->sharedSubscription?->service?->name ?? 'N/A'); ?></td>
              <td><?php echo e($item->sharedSubscription?->servicePlan?->name ?? 'N/A'); ?></td>
              <td><?php echo e($item->sharedSubscription?->owner?->name ?? 'N/A'); ?></td>
              <td><?php echo e($item->slots_taken); ?></td>
              <td>$<?php echo e(number_format($item->subscription_amount,2)); ?></td>
              <td>
                <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($isSuperAdmin && auth()->user()->can('update subscription member status')): ?>
                  <div>
                    <select
                      class="form-select form-select-sm js-member-status"
                      style="min-width: 150px;"
                      data-update-url="<?php echo e(route('dashboard.subscription-members.update-status', $item)); ?>"
                      data-current="<?php echo e($item->status); ?>"
                    >
                      <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php $__currentLoopData = ['pending', 'active', 'cancel_requested', 'cancelled', 'expired']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $status): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value="<?php echo e($status); ?>" <?php if($item->status === $status): echo 'selected'; endif; ?>><?php echo e(ucfirst(str_replace('_', ' ', $status))); ?></option>
                      <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
                    </select>
                    <small class="js-status-message text-success d-none"></small>
                  </div>
                <?php else: ?>
                    <span class="badge bg-label-primary">
                      <?php echo e(ucfirst(str_replace('_', ' ', $item->status))); ?>

                    </span>
                <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
              </td>
             <td>
                <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->any(['update subscription member status', 'delete subscription'])): ?>
                  <div class="d-flex gap-1">
                    <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('update subscription member status')): ?>
                      <a href="<?php echo e(route('dashboard.subscription-members.edit', $item)); ?>" class="btn btn-sm btn-outline-primary">
                        Edit
                      </a>
                    <?php endif; ?>

                    <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('delete subscription')): ?>
                      <form method="POST" action="<?php echo e(route('dashboard.subscription-members.destroy', $item)); ?>">
                        <?php echo csrf_field(); ?>
                        <?php echo method_field('DELETE'); ?>
                        <button
                          class="btn btn-sm btn-outline-danger"
                          onclick="return confirm('Delete this joined subscription?')">
                          Delete
                        </button>
                      </form>
                    <?php endif; ?>
                  </div>
                <?php else: ?>
                  <span class="badge bg-label-primary">--</span>
                <?php endif; ?>
              </td>
            </tr>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
            <tr><td colspan="<?php echo e($isSuperAdmin ? 8 : 7); ?>" class="text-center py-4">No joined subscriptions found.</td></tr>
            <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
          </tbody>
        </table>
      </div>
    <?php else: ?>
      <div class="card-body"><div class="alert alert-warning mb-0">You do not have permission to view subscriptions.</div></div>
    <?php endif; ?>

    <div class="card-body"><?php echo e($subscriptions->links()); ?></div>
  </div>
</div>
<?php $__env->stopSection(); ?>

<?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($subscriptions->count() > 0): ?>
<?php $__env->startPush('script'); ?>
<script>
$(document).ready(function () {
  const $table = $('#dashboard-joined-subscriptions-table');
  if ($table.length) {
    const serviceColumnIndex = <?php echo e($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 }]
    });
  }

  const csrfToken = '<?php echo e(csrf_token()); ?>';

  $(document).on('change', '.js-member-status', function () {
    const $select = $(this);
    const selectedStatus = $select.val();
    const updateUrl = $select.data('update-url');
    const previousStatus = $select.data('current');
    const $message = $select.closest('td').find('.js-status-message');

    $select.prop('disabled', true);
    $message.addClass('d-none').removeClass('text-success text-danger').text('');

    $.ajax({
      url: updateUrl,
      method: 'PATCH',
      dataType: 'json',
      data: {
        _token: csrfToken,
        status: selectedStatus
      }
    }).done(function (response) {
      $select.data('current', response.status || selectedStatus);
      $message.text(response.message || 'Status updated successfully.').addClass('text-success').removeClass('d-none');
    }).fail(function (xhr) {
      $select.val(previousStatus);

      let errorMessage = 'Status update failed.';
      if (xhr.responseJSON && xhr.responseJSON.errors && xhr.responseJSON.errors.status && xhr.responseJSON.errors.status.length) {
        errorMessage = xhr.responseJSON.errors.status[0];
      } else if (xhr.responseJSON && xhr.responseJSON.message) {
        errorMessage = xhr.responseJSON.message;
      }

      $message.text(errorMessage).addClass('text-danger').removeClass('d-none');
    }).always(function () {
      $select.prop('disabled', false);

      setTimeout(function () {
        $message.addClass('d-none').text('');
      }, 2500);
    });
  });
});
</script>
<?php $__env->stopPush(); ?>
<?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>

<?php echo $__env->make('layouts.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/wwwlifelinehealt/public_html/marketplace.lifelinehealthcare.com.pk/resources/views/dashboard/my-subscriptions.blade.php ENDPATH**/ ?>