<?php
    use App\Enums\LinkStatus;

    $waitingGroup = \App\Models\Group::firstWhere('name', "{$campaign->name} - En attente");
    $paidGroup    = \App\Models\Group::firstWhere('name', "{$campaign->name} - Payés");

    $paidLinks      = $campaign->links->where('status', LinkStatus::Paid);
    $paidDonateurs  = $paidLinks->pluck('donateur_id')->unique();
    $campaignDonateurs = $campaign->links->pluck('donateur_id')->unique();

    $optOutLinks   = $campaign->links->where('no_relance', true);
    $toRelanceLinks = $campaign->links
        ->where('status', LinkStatus::Pending)
        ->where('no_relance', false);
?>

<div class="max-w-5xl mx-auto p-6 bg-white shadow rounded">
  <h2 class="text-2xl font-semibold mb-6">Détails Campagne : <?php echo e($campaign->name); ?></h2>

  <?php if($campaign->contact_email || $campaign->contact_phone): ?>
    <p class="text-sm text-gray-600 mb-4">
      Contact :
      <?php if($campaign->contact_email): ?>
        <a href="mailto:<?php echo e($campaign->contact_email); ?>" class="underline"><?php echo e($campaign->contact_email); ?></a>
      <?php endif; ?>
      <?php if($campaign->contact_phone): ?>
        <?php if($campaign->contact_email): ?>&nbsp;|&nbsp;<?php endif; ?>
        <a href="tel:+<?php echo e($campaign->contact_phone); ?>" class="underline"><?php echo e($campaign->contact_phone); ?></a>
      <?php endif; ?>
    </p>
  <?php endif; ?>

  <?php if($campaign->initiator_name || $campaign->initiator_position): ?>
    <p class="text-sm text-gray-600 mb-4">
      Initiateur :
      <?php if($campaign->initiator_name): ?>
        <?php echo e($campaign->initiator_name); ?>

      <?php endif; ?>
      <?php if($campaign->initiator_position): ?>
        <?php if($campaign->initiator_name): ?>&nbsp;|&nbsp;<?php endif; ?> <?php echo e($campaign->initiator_position); ?>

      <?php endif; ?>
    </p>
  <?php endif; ?>

  
  <div class="grid grid-cols-2 md:grid-cols-4 gap-4 mb-8">
    <div class="p-4 bg-blue-50 rounded">
      <h3 class="text-sm text-gray-500 uppercase">Liens générés</h3>
      <p class="text-3xl font-bold"><?php echo e($campaign->links->count()); ?></p>
    </div>
    <div class="p-4 bg-green-50 rounded">
      <h3 class="text-sm text-gray-500 uppercase">Donateurs payés</h3>
      <p class="text-3xl font-bold"><?php echo e($paidDonateurs->count()); ?></p>
    </div>
    <div class="p-4 bg-yellow-50 rounded">
      <h3 class="text-sm text-gray-500 uppercase">En attente</h3>
      <p class="text-3xl font-bold"><?php echo e($campaign->links->where('status','pending')->count()); ?></p>
    </div>
    <div class="p-4 bg-indigo-50 rounded">
      <h3 class="text-sm text-gray-500 uppercase">Total collecté</h3>
      <p class="text-3xl font-bold">
        <?php echo e(number_format($campaign->links->flatMap->payments->pluck('amount')->sum(), 0, ',', ' ')); ?> XOF
      </p>
    </div>
  </div>

  <?php
      $progress = $campaignDonateurs->count() > 0
          ? round(($paidDonateurs->count() / $campaignDonateurs->count()) * 100, 1)
          : 0;
  ?>

  <div class="mb-6 bg-gray-50 p-4 rounded flex flex-wrap gap-4 items-center">
    <div class="mb-8 w-full">
        <div class="w-full bg-gray-200 h-3 rounded">
            <div class="bg-green-500 h-3 rounded" style="width: <?php echo e($progress); ?>%"></div>
        </div>
        <p class="text-xs text-gray-600 mt-1"><?php echo e($progress); ?>% des donateurs ont payé</p>
    </div>

    
    <div class="flex flex-wrap gap-4 mb-6">
      <?php if($waitingGroup): ?>
        <a href="<?php echo e(route('groups.export_members', $waitingGroup)); ?>"
           class="px-4 py-2 bg-yellow-600 text-white rounded hover:bg-yellow-700 whitespace-nowrap">
          Exporter « En attente » (<?php echo e($waitingGroup->donateurs->count()); ?>)
        </a>
        <?php if($toRelanceLinks->count()): ?>
          <?php
              $uniqueRelanceCount = $toRelanceLinks
                  ->pluck('donateur_id')
                  ->unique()
                  ->count();

              $donateursToNotRelance = $optOutLinks
                ->pluck('donateur')
                ->unique('id');
          ?>
          <form action="<?php echo e(route('campaigns.remind', $campaign)); ?>"
            method="POST"
            class="inline"
            onsubmit="return confirm('Confirmez-vous l\u2019envoi de la relance à <?php echo e($uniqueRelanceCount); ?> donateur<?php echo e($uniqueRelanceCount > 1 ? 's' : ''); ?> ?')">
            <?php echo csrf_field(); ?>

            <button type="submit"
              class="px-4 py-2 bg-red-600 text-white rounded hover:bg-red-700">
              📣 Relancer les <?php echo e($uniqueRelanceCount); ?> volontaires
            </button>
          </form>
        <?php endif; ?>
      <?php endif; ?>

      <?php if($paidGroup): ?>
        <a href="<?php echo e(route('groups.export_members', $paidGroup)); ?>"
           class="px-4 py-2 bg-green-600 text-white rounded hover:bg-green-700 whitespace-nowrap">
          Exporter « Payés » (<?php echo e($paidGroup->donateurs->count()); ?>)
        </a>
      <?php endif; ?>

      <a href="<?php echo e(route('reports.export', array_merge(request()->query(), ['type'=>'all']))); ?>"
         class="px-4 py-2 bg-gray-600 text-white rounded hover:bg-gray-700 whitespace-nowrap">
        Exporter Tout
      </a>
    </div>
  </div>

  
    <?php if($waitingGroup): ?>
    <details class="mb-4 border rounded">
      <summary class="px-4 py-2 bg-yellow-100 cursor-pointer font-medium">
        Donateurs en attente (<?php echo e($waitingGroup->donateurs->count()); ?>)
      </summary>
      <div class="p-4">
        <select class="w-full border-gray-300 rounded shadow-sm p-2" size="8" readonly>
          <?php $__currentLoopData = $waitingGroup->donateurs; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $don): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
            <option value="<?php echo e($don->id); ?>">
              <?php echo e($don->nom); ?> <?php echo e($don->prenom); ?><?php if($don->email): ?> (<?php echo e($don->email); ?>)<?php endif; ?>
            </option>
          <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        </select>
      </div>
    </details>
    <?php endif; ?>

    <?php if(isset($donateursToNotRelance) && $donateursToNotRelance): ?>
    <details class="mb-4 border rounded">
      <summary class="px-4 py-2 bg-yellow-100 cursor-pointer font-medium">
        Donateurs ne voulant plus être relancé (<?php echo e($donateursToNotRelance->count()); ?>)
      </summary>
      <div class="p-4">
        <select class="w-full border-gray-300 rounded shadow-sm p-2" size="8" readonly>
          <?php $__currentLoopData = $donateursToNotRelance->values(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $donateur): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
            <option value="<?php echo e($donateur->id); ?>">
              <?php echo e($donateur->nom); ?> <?php echo e($donateur->prenom); ?><?php if($donateur->email): ?> (<?php echo e($donateur->email); ?>)<?php endif; ?>
            </option>
          <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        </select>
      </div>
    </details>
    <?php endif; ?>

    <?php if($paidGroup): ?>
    <details class="mb-4 border rounded">
      <summary class="px-4 py-2 bg-green-100 cursor-pointer font-medium">
        Donateurs ayant payés (<?php echo e($paidGroup->donateurs->count()); ?>)
      </summary>
      <div class="p-4">
        <select class="w-full border-gray-300 rounded shadow-sm p-2" size="8" readonly>
          <?php $__currentLoopData = $paidGroup->donateurs; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $don): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
            <?php
                $amount = $don->payments()
                    ->whereHas('link', fn($q) => $q->where('campaign_id', $campaign->id))
                    ->sum('amount');
            ?>
            <option value="<?php echo e($don->id); ?>">
              <?php echo e($don->nom); ?> <?php echo e($don->prenom); ?><?php if($don->email): ?> (<?php echo e($don->email); ?>) (<?php echo e(number_format($amount, 0, ',', ' ')); ?> XOF)<?php endif; ?>
            </option>
          <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        </select>
      </div>
    </details>
    <?php endif; ?>
  </div>

  <div class="text-right">
    <a href="<?php echo e(route('campaigns.index')); ?>"
       class="px-4 py-2 bg-gray-200 rounded hover:bg-gray-300">
      ← Retour aux campagnes
    </a>
  </div>
</div>
<?php /**PATH /home/ecamesuac/public_html/notifications_colloque_uac/resources/views/campaigns/partials/details.blade.php ENDPATH**/ ?>