<?php
$reportStats = $stats ?? ['total'=>0,'success'=>0,'failed'=>0];
?>
<div class="promo-report-stats-meta" hidden
     data-total="<?=(int) $reportStats['total'];?>"
     data-success="<?=(int) $reportStats['success'];?>"
     data-delivered="<?=(int) ($reportStats['delivered'] ?? 0);?>"
     data-read="<?=(int) ($reportStats['read'] ?? 0);?>"
     data-failed="<?=(int) $reportStats['failed'];?>"></div>
<div class="table-responsive-sm">
  <table class="promo-history-data-table" id="promo_report_table">
    <thead>
      <tr>
        <th class="number">#</th>
        <th>WAKTU</th>
        <th>NAMA PENERIMA</th>
        <th>NO WHATSAPP</th>
        <th>STATUS</th>
        <th>PESAN TERKIRIM / KETERANGAN</th>
      </tr>
    </thead>
    <tbody>
      <?php if (!empty($result)) { $no = 1 + (int)($offset ?? 0); foreach ($result as $row) {
        $customerName = !empty($row->customer_name) ? $row->customer_name : '-';
        if (!empty($row->id_konsumen) && function_exists('cekKonsumen')) {
          $customer = cekKonsumen($row->id_konsumen);
          if (!empty($customer['nama'])) $customerName = $customer['nama'];
        }
        $statusRaw = strtolower(trim((string)$row->status));
        $stateRaw = strtolower(trim((string)($row->state ?? '')));
        $statusRanks = ['0'=>0,'pending'=>10,'waiting'=>20,'processing'=>30,'1'=>40,'sent'=>40,'2'=>50,'delivered'=>50,'3'=>60,'read'=>60];
        $statusValue = (($statusRanks[$statusRaw] ?? 0) > ($statusRanks[$stateRaw] ?? 0)) ? $statusRaw : ($stateRaw !== '' ? $stateRaw : $statusRaw);
        $numericStates = ['1'=>'sent','2'=>'delivered','3'=>'read'];
        if (isset($numericStates[$statusValue])) $statusValue = $numericStates[$statusValue];
        $isSuccess = in_array($statusValue, ['sent','delivered','read','success','true','done','berhasil'], true);
        $isFailed = in_array($statusValue, ['0','failed','gagal','false','error','invalid','expired','url unreachable'], true);
        $statusClass = $isSuccess ? 'success' : ($isFailed ? 'failed' : 'pending');
        $labels = ['sent'=>'Terkirim','delivered'=>'Disampaikan','read'=>'Dibaca','pending'=>'Menunggu','waiting'=>'Menunggu','processing'=>'Diproses'];
        $statusLabel = $labels[$statusValue] ?? ($isSuccess ? 'Berhasil' : ($isFailed ? 'Gagal' : ucfirst($statusValue ?: 'Menunggu')));
        $rawLabels = ['0'=>'pending','1'=>'sent','2'=>'delivered','3'=>'read'];
        $statusDetail = $rawLabels[$statusRaw] ?? ($statusRaw ?: '-');
        $stateDetail = $rawLabels[$stateRaw] ?? ($stateRaw ?: '-');
        $dateLabel = '-';
        if (!empty($row->create_date)) {
          $timestamp = strtotime($row->create_date);
          if ($timestamp) $dateLabel = date(strpos((string)$row->create_date, ':') !== false ? 'd/m/Y H:i' : 'd/m/Y', $timestamp);
        }
      ?>
      <tr>
        <td class="number"><?=$no++;?></td>
        <td class="time"><?=html_escape($dateLabel);?></td>
        <td class="recipient"><?=html_escape($customerName);?></td>
        <td class="whatsapp"><i class="fa fa-whatsapp"></i> <?=html_escape($row->target);?></td>
        <td class="promo-history-status-cell">
          <span class="promo-history-status <?=$statusClass;?>"><?=$statusLabel;?></span>
          <small>Status: <?=html_escape($statusDetail);?> &middot; State: <?=html_escape($stateDetail);?></small>
        </td>
        <td class="message"><?=nl2br(html_escape((string)$row->message));?></td>
      </tr>
      <?php } } else { ?>
      <tr class="empty-row"><td colspan="6"><i class="fa fa-clock-o"></i><em>Belum ada riwayat pengiriman promosi pada sesi ini.</em></td></tr>
      <?php } ?>
    </tbody>
  </table>
</div>
<?php if (!empty($pagination)) { ?><div class="promo-history-pagination"><?=$pagination;?></div><?php } ?>
