<?php /** @var array $row */ ?>
<div class="topbar">
  <div>
    <h2><?= e($title) ?></h2>
    <div class="small">ربط الصيانة بأصل + مكلّف داخلي + مقاول/عقد.</div>
  </div>
</div>

<form class="card" method="post">
  <?= csrf_field() ?>

  <div class="grid" style="grid-template-columns: 1fr 1fr 1fr">
    <div>
      <label class="small">الأصل</label>
      <select class="input" name="asset_id" required>
        <option value="0">- اختر -</option>
        <?php foreach (($assets ?? []) as $a): ?>
          <option value="<?= e((string)$a['id']) ?>" <?= intval($row['asset_id'] ?? 0)===intval($a['id'])?'selected':'' ?>>
            <?= e($a['tag']) ?>
          </option>
        <?php endforeach; ?>
      </select>
    </div>
    <div>
      <label class="small">التصنيف</label>
      <input class="input" name="category" value="<?= e($row['category'] ?? '') ?>" placeholder="كهرباء/مياه/سياج...">
    </div>
    <div>
      <label class="small">تاريخ البلاغ</label>
      <?php
        $ra = (string)($row['reported_at'] ?? '');
        // MySQL: 2025-12-28 14:30:00 => HTML: 2025-12-28T14:30
        if ($ra !== '' && strpos($ra, ' ') !== false) {
          $ra = str_replace(' ', 'T', $ra);
        }
        if (strlen($ra) > 16) $ra = substr($ra, 0, 16);
      ?>
      <input class="input" type="datetime-local" name="reported_at" value="<?= e($ra) ?>">
    </div>
  </div>

  <div style="margin-top:10px">
    <label class="small">العنوان</label>
    <input class="input" name="title" value="<?= e($row['title'] ?? '') ?>" required>
  </div>

  <div style="margin-top:10px">
    <label class="small">الوصف</label>
    <textarea class="input" name="description" rows="4"><?= e($row['description'] ?? '') ?></textarea>
  </div>

  <div class="grid" style="grid-template-columns: 1fr 1fr 1fr; margin-top:10px">
    <div>
      <label class="small">الأولوية</label>
      <select class="input" name="priority">
        <?php foreach (['low'=>'low','medium'=>'medium','high'=>'high'] as $k=>$v): ?>
          <option value="<?= e($k) ?>" <?= (($row['priority'] ?? 'medium')===$k)?'selected':'' ?>><?= e($v) ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <div>
      <label class="small">الحالة</label>
      <select class="input" name="status">
        <?php foreach (['open'=>'open','in_progress'=>'in_progress','closed'=>'closed'] as $k=>$v): ?>
          <option value="<?= e($k) ?>" <?= (($row['status'] ?? 'open')===$k)?'selected':'' ?>><?= e($v) ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <div>
      <label class="small">تاريخ الاستحقاق</label>
      <input class="input" type="date" name="due_date" value="<?= e($row['due_date'] ? substr($row['due_date'],0,10) : '') ?>">
    </div>
  </div>

  <div class="grid" style="grid-template-columns: 1fr 1fr 1fr; margin-top:10px">
    <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($row['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($row['contractor_id'] ?? 0)===intval($ct['id'])?'selected':'' ?>>
            <?= e($ct['name']) ?> (<?= e($ct['code']) ?>)
          </option>
        <?php endforeach; ?>
      </select>
      <div class="small" style="margin-top:6px">
        <?= auth_can('view_contractors') ? '<a class="link" href="'.e(url('/index.php?page=contractors')).'">فتح قائمة المقاولين</a>' : '' ?>
      </div>
    </div>
    <div>
      <label class="small">العقد</label>
      <select class="input" name="contract_id">
        <option value="0">-</option>
        <?php foreach (($contracts ?? []) as $c): ?>
          <option value="<?= e((string)$c['id']) ?>" <?= intval($row['contract_id'] ?? 0)===intval($c['id'])?'selected':'' ?>>
            <?= e($c['contract_no']) ?> — <?= e($c['title']) ?> (<?= e($c['contractor_name']) ?>)
          </option>
        <?php endforeach; ?>
      </select>
      <div class="small" style="margin-top:6px">
        ملاحظة: إذا اخترت عقداً بدون مقاول سيتم استنتاج المقاول تلقائياً.
      </div>
    </div>
  </div>

  <div class="grid" style="grid-template-columns: 1fr 1fr 1fr; margin-top:10px">
    <div>
      <label class="small">المبلّغ</label>
      <select class="input" name="reported_by">
        <option value="0">-</option>
        <?php foreach (($users ?? []) as $u): ?>
          <option value="<?= e((string)$u['id']) ?>" <?= intval($row['reported_by'] ?? 0)===intval($u['id'])?'selected':'' ?>>
            <?= e($u['name']) ?>
          </option>
        <?php endforeach; ?>
      </select>
    </div>
    <div>
      <label class="small">تكلفة تقديرية</label>
      <input class="input" name="estimated_cost" value="<?= e($row['estimated_cost'] ?? '') ?>">
    </div>
    <div>
      <label class="small">تكلفة فعلية</label>
      <input class="input" name="actual_cost" value="<?= e($row['actual_cost'] ?? '') ?>">
    </div>
  </div>

  <div class="actions" style="margin-top:12px">
    <button class="btn primary">حفظ</button>
    <a class="btn" href="<?= e(url('/index.php?page=maintenance')) ?>">عودة</a>
  </div>
</form>

<?php if (($row['contractor_assigned_at'] ?? '') !== ''): ?>
  <div class="card" style="margin-top:14px">
    <div class="small">
      تم إسناد المقاول/العقد بتاريخ: <b><?= e($row['contractor_assigned_at']) ?></b>
    </div>
  </div>
<?php endif; ?>
