<div class="ab-table-card">
  <div class="table-responsive">
    <table class="table ab-table align-middle">
      <thead><tr><th>Automation</th><th>Trigger</th><th>Steps</th><th>Status</th><th></th></tr></thead>
      <tbody>
      <?php foreach ($rows as $w): ?>
        <tr>
          <td class="ab-cell-primary"><?= e($w['name']) ?></td>
          <td class="ab-cell-sub"><?= e(ucwords(str_replace('_',' ',$w['trigger_type']))) ?></td>
          <td><?= (int) $w['step_count'] ?> step(s)</td>
          <td><span class="ab-badge <?= $w['is_active'] ? 'bg-success-soft' : 'bg-secondary-soft' ?>"><span class="dot"></span><?= $w['is_active'] ? 'Active' : 'Paused' ?></span></td>
          <td>
            <button class="btn btn-light-soft btn-sm ab-toggle-workflow" data-id="<?= (int) $w['id'] ?>"><?= $w['is_active'] ? 'Pause' : 'Activate' ?></button>
            <button class="btn btn-icon btn-light-soft btn-sm ab-view-runs" data-id="<?= (int) $w['id'] ?>" data-name="<?= e($w['name']) ?>"><i class="bi bi-eye"></i></button>
          </td>
        </tr>
      <?php endforeach; ?>
      <?php if (empty($rows)): ?><tr><td colspan="5" class="text-center p-4 ab-text-muted">No automation rules yet.</td></tr><?php endif; ?>
      </tbody>
    </table>
  </div>
</div>

<div class="modal fade" id="abRunsModal" tabindex="-1"><div class="modal-dialog modal-lg modal-dialog-centered"><div class="modal-content">
  <div class="modal-header"><h5 class="modal-title" id="abRunsModalTitle">Runs</h5><button class="btn-close" data-bs-dismiss="modal"></button></div>
  <div class="modal-body"><div id="abRunsList" class="table-responsive"></div></div>
</div></div></div>

<script>
document.querySelectorAll('.ab-toggle-workflow').forEach(function (btn) {
  btn.addEventListener('click', function () {
    abFetch('<?= base_url('automation') ?>/' + this.dataset.id + '/toggle', { method: 'POST' })
      .then(function (res) { abToast(res.message); setTimeout(function () { window.location.reload(); }, 500); })
      .catch(function (err) { abToast(err.message, 'danger'); });
  });
});

document.querySelectorAll('.ab-view-runs').forEach(function (btn) {
  btn.addEventListener('click', function () {
    document.getElementById('abRunsModalTitle').textContent = btn.dataset.name + ' — Recent Runs';
    abFetch('<?= base_url('automation') ?>/' + btn.dataset.id).then(function (res) {
      var runs = res.data.runs;
      var list = document.getElementById('abRunsList');
      if (!runs.length) { list.innerHTML = '<div class="ab-empty small py-3">No runs yet.</div>'; }
      else {
        list.innerHTML = '<table class="table ab-table"><thead><tr><th>Entity</th><th>Status</th><th>Started</th><th>Completed</th></tr></thead><tbody>' +
          runs.map(function (r) {
            var badge = { running: 'bg-primary-soft', completed: 'bg-success-soft', failed: 'bg-danger-soft' }[r.status] || 'bg-secondary-soft';
            return '<tr><td>' + r.entity_type + ' #' + r.entity_id + '</td><td><span class="ab-badge ' + badge + '">' + r.status + '</span></td>' +
              '<td class="ab-cell-sub">' + r.started_at + '</td><td class="ab-cell-sub">' + (r.completed_at || '—') + '</td></tr>';
          }).join('') + '</tbody></table>';
      }
      new bootstrap.Modal(document.getElementById('abRunsModal')).show();
    });
  });
});
</script>
