<?php $title='إرجاع الفاتورة'; include __DIR__ . '/../layout/header.php'; ?>
<div class="card">
  <div style="display:flex;justify-content:space-between;gap:12px;flex-wrap:wrap">
    <div>
      <h2><?= e($invoice['invoice_no']) ?></h2>
      <div class="muted"><?= e($invoice['customer_name']) ?> — <?= e($invoice['customer_phone']) ?></div>
      <div class="muted"><?= e($invoice['delivery_date']) ?> → <?= e($invoice['return_date']) ?></div>
    </div>
    <a class="btn light" href="<?= e(base_url('?page=returns')) ?>">رجوع</a>
  </div>
</div>

<div class="card" style="display:grid;grid-template-columns:repeat(auto-fit,minmax(180px,1fr));gap:12px">
  <div>
    <div class="muted">الإجمالي</div>
    <strong><?= e(number_format((float)$invoice['grand_total'],3)) ?></strong>
  </div>
  <div>
    <div class="muted">المدفوع</div>
    <strong><?= e(number_format((float)$invoice['paid_total'],3)) ?></strong>
  </div>
  <div>
    <div class="muted">المتبقي</div>
    <strong style="color:#b91c1c"><?= e(number_format((float)$invoice['balance_due'],3)) ?></strong>
  </div>
  <div>
    <div class="muted">الحالة</div>
    <strong><?= e($invoice['invoice_status']) ?></strong>
  </div>
</div>

<form method="post" action="<?= e(base_url('?page=return_process')) ?>" class="card">
  <input type="hidden" name="_csrf" value="<?= e(csrf_token()) ?>">
  <input type="hidden" name="id" value="<?= (int)$invoice['id'] ?>">

  <h3 style="margin-top:0">القطع المؤجرة</h3>
  <table class="table">
    <thead><tr><th>القطعة</th><th>الفئة</th><th>النوع</th><th>الحالة</th><th>حالة الإرجاع</th><th>اختيار</th></tr></thead>
    <tbody>
      <?php foreach($items as $it): ?>
      <tr>
        <td><strong><?= e($it['item_code']) ?></strong><div class="muted"><?= e($it['item_name']) ?></div></td>
        <td><?= e($it['category_name']) ?></td>
        <td><?= e($it['line_type']) ?></td>
        <td><?= e($it['return_status']) ?></td>
        <td>
          <?php if($it['line_type']==='rental' && $it['return_status']==='pending'): ?>
            <select class="select" name="return_condition[<?= (int)$it['id'] ?>]">
              <option value="good">سليمة</option>
              <option value="damaged">متضررة</option>
              <option value="missing">مفقودة</option>
              <option value="needs_maintenance">بحاجة صيانة</option>
            </select>
          <?php else: ?>—<?php endif; ?>
        </td>
        <td><?php if($it['line_type']==='rental' && $it['return_status']==='pending'): ?><input type="checkbox" name="return_item_id[]" value="<?= (int)$it['id'] ?>"><?php else: ?>—<?php endif; ?></td>
      </tr>
      <?php endforeach; ?>
    </tbody>
  </table>

  <hr style="margin:18px 0;border:none;border-top:1px solid #e5e7eb">

  <h3 style="margin-top:0">تصفية مبلغ الفاتورة</h3>
  <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(220px,1fr));gap:12px">
    <div>
      <label class="label">مبلغ التصفية</label>
      <input class="input" type="number" step="0.001" min="0" max="<?= e((string)$invoice['balance_due']) ?>" name="settlement_amount" placeholder="0.000" value="<?= e(number_format((float)$invoice['balance_due'],3,'.','')) ?>">
      <div class="muted">يمكنك تركه 0 إذا كنت ستنفذ الإرجاع فقط.</div>
    </div>
    <div>
      <label class="label">طريقة الدفع</label>
      <select class="select" name="payment_method">
        <option value="cash">نقدي</option>
        <option value="card">بطاقة</option>
        <option value="transfer">تحويل</option>
      </select>
    </div>
    <div>
      <label class="label">مرجع التحويل</label>
      <input class="input" name="transfer_reference" placeholder="مثال: 9999999">
    </div>
    <div>
      <label class="label">ملاحظات الدفع</label>
      <input class="input" name="payment_notes" placeholder="تصفية من شاشة الإرجاع">
    </div>
  </div>

  <div style="display:flex;gap:10px;flex-wrap:wrap;margin-top:18px">
    <button class="btn">حفظ الإرجاع / التصفية</button>
  </div>
</form>
<?php include __DIR__ . '/../layout/footer.php'; ?>
