<?php
require_once __DIR__.'/../../config/app.php';
require_once __DIR__.'/../../app/Models/Violation.php';
require_once __DIR__.'/../../app/Models/ActivityLog.php';

$user = requireAuth(['admin']);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    verifyCsrf();
    $id = (int)($_POST['id'] ?? 0);
    if (($_POST['action'] ?? '')==='cancel' && $id) {
        Violation::cancel($id);
        ActivityLog::record($user['id'],'cancel_violation','violation',$id);
        flash('dash', t('success'));
    }
    redirect(BASE_URL.'/admin/violations.php');
}

$filter = $_GET['status'] ?? '';
$sql = "SELECT v.*, o.name AS officer_name
        FROM violations v LEFT JOIN users o ON o.id=v.officer_id WHERE 1=1";
$p=[];
if (in_array($filter,['unpaid','paid','cancelled'],true)) { $sql.=" AND v.status=?"; $p[]=$filter; }
$sql.=" ORDER BY v.issued_at DESC LIMIT 500";
$st=db()->prepare($sql); $st->execute($p); $rows=$st->fetchAll();
$totalAmt = 0; foreach($rows as $r) if($r['status']==='paid') $totalAmt += (float)$r['amount'];

$pageTitle  = t('all_violations');
$activePage = BASE_URL.'/admin/violations.php';
require __DIR__.'/../../app/Views/shared/layout.php';
?>
<div class="page-header"><h2>📊 <?= t('all_violations') ?> <span style="font-size:14px;color:var(--text-muted)">(<?= count($rows) ?>)</span></h2></div>

<div style="display:flex;gap:8px;margin-bottom:16px;flex-wrap:wrap">
  <?php foreach(['' => t('all'), 'unpaid'=>'unpaid', 'paid'=>'paid', 'cancelled'=>'cancelled'] as $k=>$lbl): ?>
  <a href="?status=<?= $k ?>" class="btn btn-sm <?= $filter===$k?'btn-primary':'btn-secondary' ?>"><?= e($lbl) ?></a>
  <?php endforeach; ?>
</div>

<div class="card"><div style="overflow-x:auto">
  <table class="table" style="width:100%;border-collapse:collapse">
    <thead><tr style="border-bottom:1px solid var(--border)">
      <th style="padding:12px"><?= t('plate_number') ?></th>
      <th style="padding:12px"><?= lang()==='ar'?'نوع المخالفة':(lang()==='fr'?'Type d\'infraction':'Violation type') ?></th>
      <th style="padding:12px"><?= t('officer_role') ?></th>
      <th style="padding:12px"><?= t('amount') ?></th>
      <th style="padding:12px"><?= t('status') ?></th>
      <th style="padding:12px"><?= t('date') ?></th>
      <th style="padding:12px"><?= t('actions') ?></th>
    </tr></thead>
    <tbody>
    <?php if(empty($rows)): ?>
      <tr><td colspan="7" style="padding:24px;text-align:center;color:var(--text-muted)"><?= t('no_data') ?></td></tr>
    <?php else: foreach($rows as $r):
      $cls=['unpaid'=>'badge-danger','paid'=>'badge-success','cancelled'=>'badge-info'][$r['status']]??'badge-info'; ?>
      <tr style="border-bottom:1px solid var(--border)">
        <td style="padding:12px"><span class="vehicle-plate-badge"><?= e($r['plate_number']) ?></span></td>
        <td style="padding:12px"><?= e($r['violation_type']) ?></td>
        <td style="padding:12px"><?= e($r['officer_name'] ?: '—') ?></td>
        <td style="padding:12px;font-weight:600"><?= number_format((float)$r['amount'],0) ?> DZD</td>
        <td style="padding:12px"><span class="badge <?= $cls ?>"><?= e($r['status']) ?></span></td>
        <td style="padding:12px;font-size:13px;color:var(--text-muted)"><?= e(substr($r['issued_at'],0,10)) ?></td>
        <td style="padding:12px">
          <?php if($r['status']==='unpaid'): ?>
          <form method="POST" style="display:inline" onsubmit="return confirm('<?= e(t('cancel')) ?>?')">
            <?= csrfField() ?>
            <input type="hidden" name="action" value="cancel">
            <input type="hidden" name="id" value="<?= (int)$r['id'] ?>">
            <button class="btn btn-sm btn-secondary">✖</button>
          </form>
          <?php else: ?>—<?php endif; ?>
        </td>
      </tr>
    <?php endforeach; endif; ?>
    </tbody>
  </table>
</div></div>
<div class="alert alert-success" style="margin-top:16px">
  <?= t('total_revenue') ?>: <strong><?= number_format($totalAmt,0) ?> DZD</strong>
</div>
<?php require __DIR__.'/../../app/Views/shared/layout_end.php'; ?>
