<?php /** @var array $rows */ ?>
<div class="topbar">
  <div>
    <h2>الصيانة</h2>
    <div class="small">متابعة طلبات الصيانة وربطها بالمقاولين والعقود.</div>
  </div>
  <div style="display:flex;gap:8px;flex-wrap:wrap;justify-content:flex-end">
    <?php if (auth_can('manage_maintenance')): ?>
      <a class="btn primary" href="<?= e(url('/index.php?page=maintenance&action=create')) ?>">إضافة صيانة</a>
    <?php endif; ?>
    <?php if (auth_can('export_maintenance_xlsx')): ?>
      <a class="btn" href="<?= e(url('/index.php?page=export_xlsx&entity=maintenance&mode=filtered&'.http_build_query([

          'q'=>($_GET['q'] ?? ''),
          'status'=>($_GET['status'] ?? ''),
          'priority'=>($_GET['priority'] ?? ''),
          'assigned_to'=>($_GET['assigned_to'] ?? 0),
          'contractor_id'=>($_GET['contractor_id'] ?? 0),
          'from'=>($_GET['from'] ?? ''),
          'to'=>($_GET['to'] ?? ''),
      
      ]))) ?>">تصدير Excel (حسب الفلاتر)</a>
      <a class="btn" href="<?= e(url('/index.php?page=export_xlsx&entity=maintenance&mode=all')) ?>">تصدير Excel (كل البيانات)</a>
    <?php endif; ?>
    <?php if (auth_can('export_maintenance_pdf')): ?>
      <a class="btn" href="<?= e(url('/report_pdf.php')) ?>">تصدير صيانة PDF</a>
    <?php endif; ?>
  </div>
</div>

<form class="card" method="get" action="<?= e(url('/index.php')) ?>">
  <input type="hidden" name="page" value="maintenance">
  <div class="grid" style="grid-template-columns: 1fr 160px 160px 220px 220px 140px">
    <div>
      <label class="small">بحث</label>
      <input class="input" name="q" value="<?= e($_GET['q'] ?? '') ?>" placeholder="Tag / عنوان / تصنيف">
    </div>
    <div>
      <label class="small">الحالة</label>
      <select class="input" name="status">
        <option value="">كل الحالات</option>
        <option value="open" <?= (($_GET['status'] ?? '')==='open')?'selected':'' ?>>open</option>
        <option value="in_progress" <?= (($_GET['status'] ?? '')==='in_progress')?'selected':'' ?>>in_progress</option>
        <option value="closed" <?= (($_GET['status'] ?? '')==='closed')?'selected':'' ?>>closed</option>
      </select>
    </div>
    <div>
      <label class="small">الأولوية</label>
      <select class="input" name="priority">
        <option value="">كل</option>
        <option value="low" <?= (($_GET['priority'] ?? '')==='low')?'selected':'' ?>>low</option>
        <option value="medium" <?= (($_GET['priority'] ?? '')==='medium')?'selected':'' ?>>medium</option>
        <option value="high" <?= (($_GET['priority'] ?? '')==='high')?'selected':'' ?>>high</option>
      </select>
    </div>
    <div>
      <label class="small">المكلّف الداخلي</label>
      <select class="input" name="assigned_to">
        <option value="0">الكل</option>
        <?php foreach (($users ?? []) as $u): ?>
          <option value="<?= e((string)$u['id']) ?>" <?= intval($_GET['assigned_to'] ?? 0)===intval($u['id'])?'selected':'' ?>>
            <?= e($u['name']) ?>
          </option>
        <?php endforeach; ?>
      </select>
    </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 style="display:flex;align-items:end;gap:8px">
      <button class="btn">بحث</button>
      <a class="btn" href="<?= e(url('/index.php?page=maintenance')) ?>">إعادة ضبط</a>
    </div>
  </div>

  <div class="grid" style="grid-template-columns: 160px 160px 1fr; margin-top:10px">
    <div>
      <label class="small">من</label>
      <input class="input" type="date" name="from" value="<?= e($_GET['from'] ?? '') ?>">
    </div>
    <div>
      <label class="small">إلى</label>
      <input class="input" type="date" name="to" value="<?= e($_GET['to'] ?? '') ?>">
    </div>
    <div class="small" style="display:flex;align-items:end;justify-content:flex-end">النتائج: <?= e((string)count($rows)) ?></div>
  </div>
</form>

<div class="card">
  <table class="table">
    <thead>
      <tr>
        <th>#</th>
        <th>Tag</th>
        <th>التصنيف</th>
        <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="11" class="small">لا توجد بيانات.</td></tr>
      <?php endif; ?>
      <?php foreach ($rows as $r): ?>
        <tr>
          <td><?= e((string)$r['id']) ?></td>
          <td><span class="tag"><?= e($r['asset_tag']) ?></span></td>
          <td><?= e($r['category'] ?? '-') ?></td>
          <td><?= e($r['title']) ?></td>
          <td><?= e($r['priority']) ?></td>
          <td><?= status_tag($r['status']) ?></td>
          <td><?= e($r['assigned_name'] ?? '-') ?></td>
          <td><?= e($r['contractor_name'] ?? '-') ?></td>
          <td><?= e($r['contract_no'] ?? '-') ?></td>
          <td><?= e($r['reported_at']) ?></td>
          <td style="white-space:nowrap">
            <a class="btn" href="<?= e(url('/index.php?page=maintenance&action=edit&id='.(int)$r['id'])) ?>">تعديل</a>
            <?php if (auth_can('manage_maintenance')): ?>
              <a class="btn danger" href="<?= e(url('/index.php?page=maintenance&action=delete&id='.(int)$r['id'])) ?>">حذف</a>
            <?php endif; ?>
          </td>
        </tr>
      <?php endforeach; ?>
    </tbody>
  </table>
</div>
