<?php
// public/officer/violations.php — قائمة المخالفات
require_once __DIR__.'/../../config/app.php';
require_once __DIR__.'/../../app/Models/Violation.php';

$user       = requireAuth(['officer']);
$violations = Violation::byOfficer($user['id']);

$pageTitle  = t('violations_list');
$activePage = BASE_URL . '/officer/violations.php';
require __DIR__ . '/../../app/Views/shared/layout.php';
?>

<div class="section-header">
  <div class="section-title">📋 <?= t('violations_list') ?></div>
  <a href="<?= BASE_URL ?>/officer/violation.php" class="btn btn-primary">+ <?= t('new_violation') ?></a>
</div>

<?php if ($f = flash('officer_msg')): ?>
<div class="alert alert-<?= $f['type'] ?>"><?= e($f['msg']) ?></div>
<?php endif; ?>

<?php if(empty($violations)): ?>
<div class="card">
  <div class="empty-state">
    <div class="empty-state-icon">📋</div>
    <h3><?= t('no_violations_yet') ?></h3>
    <a href="<?= BASE_URL ?>/officer/violation.php" class="btn btn-primary" style="margin-top:14px">+ <?= t('new_violation') ?></a>
  </div>
</div>
<?php else: ?>
<div class="card">
  <div class="table-wrapper">
    <table>
      <thead>
        <tr>
          <th>#</th>
          <th><?= t('plate_number') ?></th>
          <th><?= t('violation_type') ?></th>
          <th><?= t('fine_amount') ?></th>
          <th><?= t('status') ?></th>
          <th><?= t('issued_at') ?></th>
          <th><?= t('actions') ?></th>
        </tr>
      </thead>
      <tbody>
        <?php foreach($violations as $v): ?>
        <tr>
          <td style="font-family:monospace;font-size:12px;color:var(--text-muted)">VV-<?= str_pad($v['id'],6,'0',STR_PAD_LEFT) ?></td>
          <td><a href="<?= BASE_URL ?>/officer/search.php?plate=<?= urlencode($v['plate_number']) ?>" style="font-family:monospace;font-weight:800"><?= e($v['plate_number']) ?></a></td>
          <td><?= e($v['violation_type']) ?></td>
          <td style="font-weight:700"><?= number_format($v['amount'],2) ?> دج</td>
          <td>
            <?php if($v['status']==='paid'): ?>
              <span class="badge badge-success">✅ <?= t('paid') ?></span>
            <?php elseif($v['status']==='cancelled'): ?>
              <span class="badge badge-info">❌ <?= t('cancelled') ?></span>
            <?php else: ?>
              <span class="badge badge-warning">⏳ <?= t('unpaid') ?></span>
            <?php endif; ?>
          </td>
          <td style="font-size:12px;color:var(--text-muted)"><?= e(substr($v['issued_at'],0,16)) ?></td>
          <td>
            <a href="<?= BASE_URL ?>/officer/violation.php?action=view&id=<?= $v['id'] ?>" class="btn btn-sm btn-secondary">👁 <?= t('view') ?></a>
            <?php if($v['status']==='unpaid'): ?>
            <a href="<?= BASE_URL ?>/officer/payment.php?id=<?= $v['id'] ?>" class="btn btn-sm btn-primary">💳</a>
            <?php endif; ?>
          </td>
        </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</div>
<?php endif; ?>

<?php require __DIR__ . '/../../app/Views/shared/layout_end.php'; ?>
