<?php $__env->startSection('content'); ?>
<div class="max-w-4xl mx-auto p-8 bg-white shadow-lg rounded-lg">
  <h2 class="text-2xl font-semibold mb-4">Envoyer la campagne : « <?php echo e($campaign->name); ?> »</h2>

  <div class="mb-6 p-4 bg-gray-50 border rounded text-gray-700 whitespace-pre-line">
    <?php echo e($campaign->message); ?>

  </div>

  <form id="send-campaign-form" action="<?php echo e(route('campaigns.send', $campaign)); ?>" method="POST" class="space-y-6">
    <?php echo csrf_field(); ?>

    
    <div>
      <label for="donateurs" class="block text-sm font-medium text-gray-700">Sélectionner des donateurs</label>
      <select id="donateurs" name="donateurs[]" multiple
              class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm p-2 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 h-40">
        <?php $__currentLoopData = $donateurs; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $d): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
          <option value="<?php echo e($d->id); ?>" <?php echo e(in_array($d->id, old('donateurs', [])) ? 'selected' : ''); ?>>
            <?php echo e($d->nom); ?> <?php echo e($d->prenom); ?> <?php if($d->email): ?> (<?php echo e($d->email); ?>) <?php endif; ?>
          </option>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
      </select>
      <?php $__errorArgs = ['donateurs'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?><p class="mt-1 text-sm text-red-600"><?php echo e($message); ?></p><?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
    </div>

    
    <div>
      <label for="groups" class="block text-sm font-medium text-gray-700">Ou sélectionner des groupes</label>
      <select id="groups" name="groups[]" multiple
              class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm p-2 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 h-40">
        <?php $__currentLoopData = $groups; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $g): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
          <option value="<?php echo e($g->id); ?>" data-members='<?php echo json_encode($g->donateurs->pluck("id"), 15, 512) ?>' <?php echo e(in_array($g->id, old('groups', [])) ? 'selected' : ''); ?>>
            <?php echo e($g->name); ?> (<?php echo e($g->donateurs->count()); ?>)
          </option>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
      </select>
      <?php $__errorArgs = ['groups'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?><p class="mt-1 text-sm text-red-600"><?php echo e($message); ?></p><?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
    </div>

    <div class="text-right text-sm text-gray-700">
      Destinataires uniques&nbsp;: <span id="recipient-count" class="font-semibold">0</span>
    </div>

    
    <div class="flex justify-end space-x-4">
      <a href="<?php echo e(route('campaigns.index')); ?>"
         class="px-4 py-2 bg-gray-200 text-gray-700 rounded hover:bg-gray-300">Annuler</a>
      <button type="button" onclick="openModal('confirm-send')"
         class="px-4 py-2 bg-indigo-600 text-white rounded hover:bg-indigo-700 shadow">Lancer l'envoi</button>
      </div>
    </form>
  </div>

<script>
  document.addEventListener('DOMContentLoaded', function () {
    const donateursSelect = document.getElementById('donateurs');
    const groupsSelect    = document.getElementById('groups');
    const countSpan       = document.getElementById('recipient-count');

    function updateCount() {
      const ids = new Set();

      // Donateurs sélectionnés
      Array.from(donateursSelect.selectedOptions).forEach(opt => {
        ids.add(opt.value);
      });

      // Donateurs issus des groupes sélectionnés
      Array.from(groupsSelect.selectedOptions).forEach(opt => {
        const members = JSON.parse(opt.dataset.members || '[]');
        members.forEach(id => ids.add(String(id)));
      });

      countSpan.textContent = ids.size;
    }

    donateursSelect.addEventListener('change', updateCount);
    groupsSelect.addEventListener('change', updateCount);

    updateCount();
  });
</script>
<?php if (isset($component)) { $__componentOriginal2cfaf2d8c559a20e3495c081df2d0b10 = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginal2cfaf2d8c559a20e3495c081df2d0b10 = $attributes; } ?>
<?php $component = Illuminate\View\AnonymousComponent::resolve(['view' => 'components.confirm-modal','data' => ['id' => 'confirm-send','formId' => 'send-campaign-form','message' => 'Lancer l\'envoi de la campagne ?','confirmText' => 'Envoyer']] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? (array) $attributes->getIterator() : [])); ?>
<?php $component->withName('confirm-modal'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag && $constructor = (new ReflectionClass(Illuminate\View\AnonymousComponent::class))->getConstructor()): ?>
<?php $attributes = $attributes->except(collect($constructor->getParameters())->map->getName()->all()); ?>
<?php endif; ?>
<?php $component->withAttributes(['id' => 'confirm-send','formId' => 'send-campaign-form','message' => 'Lancer l\'envoi de la campagne ?','confirmText' => 'Envoyer']); ?>
<?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__attributesOriginal2cfaf2d8c559a20e3495c081df2d0b10)): ?>
<?php $attributes = $__attributesOriginal2cfaf2d8c559a20e3495c081df2d0b10; ?>
<?php unset($__attributesOriginal2cfaf2d8c559a20e3495c081df2d0b10); ?>
<?php endif; ?>
<?php if (isset($__componentOriginal2cfaf2d8c559a20e3495c081df2d0b10)): ?>
<?php $component = $__componentOriginal2cfaf2d8c559a20e3495c081df2d0b10; ?>
<?php unset($__componentOriginal2cfaf2d8c559a20e3495c081df2d0b10); ?>
<?php endif; ?>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/ecamesuac/public_html/notifications_colloque_uac/resources/views/campaigns/send.blade.php ENDPATH**/ ?>