<?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">
    <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>
    <div>
      <label class="small">رقم العقد</label>
      <input class="input" name="contract_no" value="<?= e($row['contract_no'] ?? '') ?>" placeholder="CN-2026-001">
    </div>
    <div>
      <label class="small">عنوان العقد</label>
      <input class="input" name="title" value="<?= e($row['title'] ?? '') ?>">
    </div>
    <div>
      <label class="small">الحالة</label>
      <select class="input" name="status">
        <?php foreach (['active'=>'نشط','expired'=>'منتهي','suspended'=>'معلق','draft'=>'مسودة'] as $k=>$v): ?>
          <option value="<?= e($k) ?>" <?= (($row['status'] ?? 'active')===$k)?'selected':'' ?>><?= e($v) ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <div>
      <label class="small">تاريخ البداية</label>
      <input class="input" type="date" name="start_date" value="<?= e($row['start_date'] ?? '') ?>">
    </div>
    <div>
      <label class="small">تاريخ النهاية</label>
      <input class="input" type="date" name="end_date" value="<?= e($row['end_date'] ?? '') ?>">
    </div>
  </div>

  <div style="margin-top:10px">
    <label class="small">شروط/ملاحظات</label>
    <textarea class="input" name="terms" rows="4"><?= e($row['terms'] ?? '') ?></textarea>
  </div>

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

<?php if (($row['id'] ?? 0) > 0): ?>
  <div class="card" id="services" style="margin-top:14px">
    <div class="topbar" style="margin-bottom:10px">
      <div>
        <h3 style="margin:0">الخدمات ضمن العقد</h3>
        <div class="small">إضافة خدمات (أعمال/بنود) مرتبطة بهذا العقد.</div>
      </div>
    </div>

    <form method="post" action="<?= e(url('/index.php?page=contracts&action=add_service&id='.(int)$row['id'])) ?>" class="grid" style="grid-template-columns: 1fr 120px 140px 1fr 120px;gap:10px;align-items:end">
      <?= csrf_field() ?>
      <div>
        <label class="small">اسم الخدمة</label>
        <input class="input" name="service_name" placeholder="صيانة دورية / إصلاح مضخة...">
      </div>
      <div>
        <label class="small">الوحدة</label>
        <input class="input" name="unit" placeholder="زيارة/قطعة">
      </div>
      <div>
        <label class="small">سعر الوحدة</label>
        <input class="input" name="unit_price" placeholder="0.00">
      </div>
      <div>
        <label class="small">ملاحظات</label>
        <input class="input" name="notes" placeholder="اختياري">
      </div>
      <div>
        <label class="small">نشط</label>
        <label style="display:flex;gap:8px;align-items:center;margin-top:6px">
          <input type="checkbox" name="is_active" value="1" checked>
          <span class="small">نعم</span>
        </label>
        <button class="btn primary" style="margin-top:8px;width:100%">إضافة</button>
      </div>
    </form>

    <hr style="margin:14px 0">

    <table class="table">
      <thead>
        <tr>
          <th>#</th>
          <th>الخدمة</th>
          <th>الوحدة</th>
          <th>السعر</th>
          <th>الحالة</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <?php if (!$services): ?>
          <tr><td colspan="6" class="small">لا توجد خدمات.</td></tr>
        <?php endif; ?>
        <?php foreach ($services as $s): ?>
          <tr>
            <td><?= e((string)$s['id']) ?></td>
            <td><?= e($s['service_name']) ?></td>
            <td><?= e($s['unit'] ?? '-') ?></td>
            <td><?= e($s['unit_price'] ?? '-') ?></td>
            <td><?= intval($s['is_active'])===1 ? '<span class="tag ok">نشط</span>' : '<span class="tag danger">موقوف</span>' ?></td>
            <td style="white-space:nowrap">
              <form method="post" action="<?= e(url('/index.php?page=contracts&action=toggle_service&id='.(int)$row['id'])) ?>" style="display:inline">
                <?= csrf_field() ?>
                <input type="hidden" name="service_id" value="<?= e((string)$s['id']) ?>">
                <button class="btn"><?= intval($s['is_active'])===1?'إيقاف':'تفعيل' ?></button>
              </form>
              <form method="post" action="<?= e(url('/index.php?page=contracts&action=delete_service&id='.(int)$row['id'])) ?>" style="display:inline" onsubmit="return confirm('حذف الخدمة؟')">
                <?= csrf_field() ?>
                <input type="hidden" name="service_id" value="<?= e((string)$s['id']) ?>">
                <button class="btn danger">حذف</button>
              </form>
            </td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
<?php endif; ?>
