<div class="campaigns-page">
  <div class="campaigns-header">
    <h2><i class="fa-solid fa-triangle-exclamation"></i> Alerty</h2>
  </div>

  <form method="get" action="/campaign_alerts" class="campaigns-filters" style="margin-bottom:16px;">
    <div class="filter-group">
      <label for="client_id"><i class="fa-solid fa-building"></i> Klient</label>
      <select id="client_id" name="client_id" class="form-control" onchange="this.form.submit()">
        <option value="">- wszyscy klienci -</option>
        <?php foreach ( $this -> clients as $client ): ?>
          <option value="<?= (int) $client['id']; ?>" <?= (int) $this -> selected_client_id === (int) $client['id'] ? 'selected' : ''; ?>>
            <?= htmlspecialchars( (string) $client['name'] ); ?>
          </option>
        <?php endforeach; ?>
      </select>
    </div>
  </form>

  <form method="post" action="/campaign_alerts/delete_selected" id="campaign_alerts_bulk_form">
    <input type="hidden" name="client_id" value="<?= (int) $this -> selected_client_id; ?>">
    <input type="hidden" name="page" value="<?= (int) $this -> page; ?>">

    <div style="display:flex;justify-content:flex-end;margin-bottom:10px;">
      <button type="submit" id="delete_selected_alerts_btn" class="btn btn-danger btn-sm" disabled>
        <i class="fa-solid fa-trash"></i> Usun zaznaczone (<span id="selected_alerts_count">0</span>)
      </button>
    </div>

    <div class="campaigns-table-wrap">
      <table class="table" id="campaign_alerts_table">
        <thead>
          <tr>
            <th style="width:40px;text-align:center;">
              <input type="checkbox" id="alerts_select_all" aria-label="Zaznacz wszystkie alerty">
            </th>
            <th>Data</th>
            <th>Klient</th>
            <th style="white-space:nowrap">Kampania</th>
            <th>Grupa reklam</th>
            <th>Komunikat</th>
          </tr>
        </thead>
        <tbody>
          <?php if ( empty( $this -> alerts ) ): ?>
            <tr>
              <td colspan="6" class="text-center">Brak alertow.</td>
            </tr>
          <?php else: ?>
            <?php foreach ( $this -> alerts as $row ): ?>
              <?php
                $alert_type = trim( (string) ( $row['alert_type'] ?? '' ) );
                $campaign_name = trim( (string) ( $row['campaign_name'] ?? '' ) );
                $ad_group_name = trim( (string) ( $row['ad_group_name'] ?? '' ) );

                if ( $alert_type === 'products_missing_in_merchant_center' )
                {
                  $campaign_name = 'Produkt (Merchant Center)';
                  $ad_group_name = '---';
                }
                else
                {
                  if ( $campaign_name === '' )
                  {
                    $campaign_external_id = (int) ( $row['campaign_external_id'] ?? 0 );
                    $campaign_name = $campaign_external_id > 0 ? ( 'Kampania #' . $campaign_external_id ) : '--- konto ---';
                  }

                  if ( $ad_group_name === '' )
                  {
                    $ad_group_external_id = (int) ( $row['ad_group_external_id'] ?? 0 );
                    $ad_group_name = $ad_group_external_id > 0 ? ( 'Grupa reklam #' . $ad_group_external_id ) : '---';
                  }
                }

                $client_name = trim( (string) ( $row['client_name'] ?? '' ) );
                if ( $client_name === '' )
                {
                  $client_name = 'Klient #' . (int) ( $row['client_id'] ?? 0 );
                }
              ?>
              <tr>
                <td style="text-align:center;">
                  <input type="checkbox" name="alert_ids[]" value="<?= (int) ( $row['id'] ?? 0 ); ?>" class="alert-select-row" aria-label="Zaznacz alert #<?= (int) ( $row['id'] ?? 0 ); ?>">
                </td>
                <td style="white-space:nowrap"><?= htmlspecialchars( (string) ( $row['date_detected'] ?? '' ) ); ?></td>
                <td><?= htmlspecialchars( $client_name ); ?></td>
                <td style="white-space:nowrap"><?= htmlspecialchars( $campaign_name ); ?></td>
                <td><?= htmlspecialchars( $ad_group_name ); ?></td>
                <td><?= htmlspecialchars( (string) ( $row['message'] ?? '' ) ); ?></td>
              </tr>
            <?php endforeach; ?>
          <?php endif; ?>
        </tbody>
      </table>

      <?php if ( (int) $this -> total_pages > 1 ): ?>
        <div class="dt-layout-row" style="display:flex !important;justify-content:flex-end;">
          <div class="dt-paging">
            <nav>
              <ul class="pagination">
                <?php
                  $page = (int) $this -> page;
                  $total_pages = (int) $this -> total_pages;
                  $client_id = (int) $this -> selected_client_id;
                  $qs = $client_id > 0 ? '&client_id=' . $client_id : '';
                ?>
                <li class="page-item <?= $page <= 1 ? 'disabled' : ''; ?>">
                  <a class="page-link" href="/campaign_alerts?page=<?= $page - 1; ?><?= $qs; ?>">&laquo;</a>
                </li>
                <?php
                  $start_p = max( 1, $page - 2 );
                  $end_p = min( $total_pages, $page + 2 );
                  if ( $start_p > 1 ):
                ?>
                  <li class="page-item"><a class="page-link" href="/campaign_alerts?page=1<?= $qs; ?>">1</a></li>
                  <?php if ( $start_p > 2 ): ?>
                    <li class="page-item disabled"><span class="page-link">...</span></li>
                  <?php endif; ?>
                <?php endif; ?>
                <?php for ( $i = $start_p; $i <= $end_p; $i++ ): ?>
                  <li class="page-item <?= $i === $page ? 'active' : ''; ?>">
                    <a class="page-link" href="/campaign_alerts?page=<?= $i; ?><?= $qs; ?>"><?= $i; ?></a>
                  </li>
                <?php endfor; ?>
                <?php if ( $end_p < $total_pages ): ?>
                  <?php if ( $end_p < $total_pages - 1 ): ?>
                    <li class="page-item disabled"><span class="page-link">...</span></li>
                  <?php endif; ?>
                  <li class="page-item"><a class="page-link" href="/campaign_alerts?page=<?= $total_pages; ?><?= $qs; ?>"><?= $total_pages; ?></a></li>
                <?php endif; ?>
                <li class="page-item <?= $page >= $total_pages ? 'disabled' : ''; ?>">
                  <a class="page-link" href="/campaign_alerts?page=<?= $page + 1; ?><?= $qs; ?>">&raquo;</a>
                </li>
              </ul>
            </nav>
          </div>
        </div>
      <?php endif; ?>
    </div>
  </form>
</div>

<script type="text/javascript">
$( function()
{
  var form = $( '#campaign_alerts_bulk_form' );
  var select_all = $( '#alerts_select_all' );
  var row_checkboxes = form.find( '.alert-select-row' );
  var delete_button = $( '#delete_selected_alerts_btn' );
  var selected_count = $( '#selected_alerts_count' );

  function update_selected_state()
  {
    var checked = row_checkboxes.filter( ':checked' ).length;
    var all = row_checkboxes.length;

    selected_count.text( checked );
    delete_button.prop( 'disabled', checked === 0 );

    if ( all === 0 )
    {
      select_all.prop( 'checked', false );
      select_all.prop( 'indeterminate', false );
      return;
    }

    select_all.prop( 'checked', checked === all );
    select_all.prop( 'indeterminate', checked > 0 && checked < all );
  }

  select_all.on( 'change', function()
  {
    row_checkboxes.prop( 'checked', this.checked );
    update_selected_state();
  } );

  row_checkboxes.on( 'change', function()
  {
    update_selected_state();
  } );

  form.on( 'submit', function( e )
  {
    var checked = row_checkboxes.filter( ':checked' ).length;

    if ( checked === 0 )
    {
      e.preventDefault();
      return;
    }

    if ( !confirm( 'Czy na pewno usunac zaznaczone alerty (' + checked + ')?' ) )
    {
      e.preventDefault();
    }
  } );

  update_selected_state();
} );
</script>
