{include file="_partials/head.tpl"}

<div class="page-head">
  <div>
    <h1>Webhooks</h1>
    <p class="muted">Исходящие уведомления о событиях в сторонние системы (CRM, Telegram, Zapier).</p>
  </div>
</div>

<form method="post" action="/admin/webhooks/create" class="form">
  {csrf}
  <h3>Добавить webhook</h3>
  <div class="form-row">
    <div class="form-group">
      <label>Событие</label>
      <select name="event">
        [foreach var="events" as="ev"]
          <option value="{ev}">{ev}</option>
        [/foreach]
      </select>
    </div>
    <div class="form-group">
      <label>URL</label>
      <input type="url" name="url" placeholder="https://example.com/hook" required>
    </div>
  </div>
  <div class="form-row">
    <div class="form-group">
      <label>Секрет (для подписи HMAC-SHA256)</label>
      <input type="text" name="secret" placeholder="необязательно">
    </div>
    <div class="form-group">
      <label class="checkbox"><input type="checkbox" name="is_active" value="1" checked> Активен</label>
    </div>
  </div>
  <button class="btn btn-primary" type="submit">Добавить</button>
</form>

[if {webhooks}]
<table class="table" style="margin-top:16px">
  <thead><tr><th>#</th><th>Событие</th><th>URL</th><th>Активен</th><th>Посл. статус</th><th class="actions">Действия</th></tr></thead>
  <tbody>
    [foreach var="webhooks" as="w"]
      <tr>
        <td>{w.id}</td>
        <td><code>{w.event}</code></td>
        <td style="word-break:break-all">{w.url}</td>
        <td>[if {w.is_active}]да[else]нет[/if]</td>
        <td class="muted">{w.last_status}</td>
        <td class="actions">
          <form method="post" action="/admin/webhooks/{w.id}/toggle" style="display:inline">
            {csrf}<button type="submit">[if {w.is_active}]Выключить[else]Включить[/if]</button>
          </form>
          <form method="post" action="/admin/webhooks/{w.id}/delete" style="display:inline" data-confirm="Удалить webhook?" data-confirm-danger="1">
            {csrf}<button type="submit" class="danger">Удалить</button>
          </form>
        </td>
      </tr>
    [/foreach]
  </tbody>
</table>
[else]
<div class="empty-state" style="margin-top:16px">
  <div class="icon">⚡</div>
  <p>Webhook'ов нет.</p>
</div>
[/if]

{include file="_partials/foot.tpl"}
