<div class="feeds-page">
  <div class="feeds-header">
    <h2><i class="fa-solid fa-file-csv"></i> Supplemental Feeds</h2>
  </div>

  <?php if ( empty( $this -> items ) ): ?>
    <div class="empty-state-box">
      <i class="fa-solid fa-file-csv"></i>
      <p>Brak klientow z ustawionym Merchant Account ID.</p>
    </div>
  <?php else: ?>
    <div class="feeds-table-wrap">
      <table class="table" id="feeds-table">
        <thead>
          <tr>
            <th style="width: 60px;">#ID</th>
            <th>Klient</th>
            <th>Merchant ID</th>
            <th>Plik</th>
            <th style="width: 130px;">Status</th>
            <th style="width: 160px;">Ostatnia aktualizacja</th>
            <th style="width: 90px;">Rozmiar</th>
          </tr>
        </thead>
        <tbody>
          <?php foreach ( $this -> items as $item ): ?>
            <tr>
              <td><?= $item['client_id']; ?></td>
              <td><?= htmlspecialchars( $item['client_name'] ); ?></td>
              <td><span class="badge-id"><?= htmlspecialchars( $item['merchant_id'] ); ?></span></td>
              <td>
                <?php if ( $item['exists'] ): ?>
                  <a href="<?= htmlspecialchars( $item['url'] ); ?>" target="_blank" class="feed-link">
                    <i class="fa-solid fa-arrow-up-right-from-square"></i>
                    <?= htmlspecialchars( $item['filename'] ); ?>
                  </a>
                  <button type="button" class="btn-icon btn-icon-copy" onclick="copyFeedUrl(this, '<?= htmlspecialchars( $item['url'] ); ?>')" title="Kopiuj URL">
                    <i class="fa-regular fa-copy"></i>
                  </button>
                <?php else: ?>
                  <span class="text-muted"><?= htmlspecialchars( $item['filename'] ); ?></span>
                <?php endif; ?>
              </td>
              <td>
                <?php if ( $item['exists'] ): ?>
                  <span class="badge badge-success">Wygenerowany</span>
                <?php else: ?>
                  <span class="badge badge-secondary">Oczekuje</span>
                <?php endif; ?>
              </td>
              <td>
                <?php if ( $item['modified_at'] ): ?>
                  <?= $item['modified_at']; ?>
                <?php else: ?>
                  <span class="text-muted">—</span>
                <?php endif; ?>
              </td>
              <td>
                <?php if ( $item['exists'] ): ?>
                  <?= number_format( $item['size'] / 1024, 1 ); ?> KB
                <?php else: ?>
                  <span class="text-muted">—</span>
                <?php endif; ?>
              </td>
            </tr>
          <?php endforeach; ?>
        </tbody>
      </table>
    </div>
  <?php endif; ?>
</div>

<script type="text/javascript">
function copyFeedUrl( btn, url )
{
  navigator.clipboard.writeText( url ).then( function() {
    var $btn = $( btn );
    var $icon = $btn.find( 'i' );
    $icon.attr( 'class', 'fa-solid fa-check' );
    setTimeout( function() {
      $icon.attr( 'class', 'fa-regular fa-copy' );
    }, 1500 );
  });
}
</script>
