<?php /** @var array $rows */ ?>
<div class="topbar">
  <div>
    <h2>العقود</h2>
    <div class="small">إدارة عقود المقاولين والخدمات.</div>
  </div>
  <div>
    <?php if (auth_can('manage_contracts')): ?>
      <a class="btn primary" href="<?= e(url('/index.php?page=contracts&action=create')) ?>">إضافة عقد</a>
    <?php endif; ?>
  </div>
</div>

<form class="card" method="get" action="<?= e(url('/index.php')) ?>">
  <input type="hidden" name="page" value="contracts">
  <div class="grid" style="grid-template-columns: 1fr 220px 180px 140px">
    <div>
      <label class="small">بحث</label>
      <input class="input" name="q" value="<?= e($_GET['q'] ?? '') ?>" placeholder="رقم العقد/العنوان/اسم المقاول">
    </div>
    <div>
      <label class="small">المقاول</label>
      <select class="input" name="contractor_id">
        <option value="0">الكل</option>
        <?php foreach (($contractors ?? []) as $ct): ?>
          <option value="<?= e((string)$ct['id']) ?>" <?= intval($_GET['contractor_id'] ?? 0)===intval($ct['id'])?'selected':'' ?>>
            <?= e($ct['name']) ?> (<?= e($ct['code']) ?>)
          </option>
        <?php endforeach; ?>
      </select>
    </div>
    <div>
      <label class="small">الحالة</label>
      <select class="input" name="status">
        <option value="">الكل</option>
        <?php foreach (['active'=>'نشط','expired'=>'منتهي','suspended'=>'معلق','draft'=>'مسودة'] as $k=>$v): ?>
          <option value="<?= e($k) ?>" <?= (($_GET['status'] ?? '')===$k)?'selected':'' ?>><?= e($v) ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <div style="display:flex;align-items:end;gap:8px">
      <button class="btn">بحث</button>
      <a class="btn" href="<?= e(url('/index.php?page=contracts')) ?>">إعادة ضبط</a>
    </div>
  </div>
</form>

<div class="card">
  <table class="table">
    <thead>
      <tr>
        <th>#</th>
        <th>رقم العقد</th>
        <th>العنوان</th>
        <th>المقاول</th>
        <th>من</th>
        <th>إلى</th>
        <th>الحالة</th>
        <th></th>
      </tr>
    </thead>
    <tbody>
      <?php if (!$rows): ?>
        <tr><td colspan="8" class="small">لا توجد بيانات.</td></tr>
      <?php endif; ?>
      <?php foreach ($rows as $r): ?>
        <tr>
          <td><?= e((string)$r['id']) ?></td>
          <td><span class="tag"><?= e($r['contract_no']) ?></span></td>
          <td><?= e($r['title']) ?></td>
          <td><?= e($r['contractor_name']) ?> (<?= e($r['contractor_code']) ?>)</td>
          <td><?= e($r['start_date'] ?? '-') ?></td>
          <td><?= e($r['end_date'] ?? '-') ?></td>
          <td>
            <?php $st = $r['status'] ?? 'active'; ?>
            <span class="tag <?= e($st==='active'?'ok':($st==='expired'?'danger':($st==='suspended'?'warn':''))) ?>">
              <?= e(['active'=>'نشط','expired'=>'منتهي','suspended'=>'معلق','draft'=>'مسودة'][$st] ?? $st) ?>
            </span>
          </td>
          <td style="white-space:nowrap">
            <a class="btn" href="<?= e(url('/index.php?page=contracts&action=edit&id='.(int)$r['id'])) ?>">تفاصيل</a>
            <?php if (auth_can('manage_contracts')): ?>
              <a class="btn danger" href="<?= e(url('/index.php?page=contracts&action=delete&id='.(int)$r['id'])) ?>">حذف</a>
            <?php endif; ?>
          </td>
        </tr>
      <?php endforeach; ?>
    </tbody>
  </table>
</div>
