<?php
$return = $return ?? null;
$lines = $lines ?? [];
if (!$return) return;
$st = strtoupper((string)$return['status']);
$badge = $st==='POSTED'?'success':($st==='DRAFT'?'warning':'secondary');
?>
<div class="d-flex justify-content-between align-items-center mb-3">
  <div>
    <h4 class="mb-1">مرتجع شراء: <span class="fw-bold"><?= e((string)$return['ret_no']) ?></span></h4>
    <div class="text-muted small">
      المصدر: <?= e((string)$return['source_inv_no']) ?> —
      المورد: <?= e((string)$return['supplier_name']) ?> —
      المخزن: <?= e((string)$return['warehouse_name']) ?> —
      التاريخ: <?= e(substr((string)$return['ret_date'],0,10)) ?>
    </div>
  </div>

  <div class="d-flex gap-2 flex-wrap">
    <a class="btn btn-outline-secondary" href="<?= e($purchase_returns_url ?? url('/purchases/returns')) ?>"><i class="fa-solid fa-arrow-right me-1"></i>رجوع</a>

    <?php if (can('purchase_returns.print')): ?>
      <a class="btn btn-outline-primary" target="_blank" href="<?= e(url('/prints/purchase-return?id='.(int)$return['id'])) ?>">
        <i class="fa-solid fa-print me-1"></i>طباعة
      </a>
    <?php endif; ?>

    <?php if ($st==='DRAFT' && can('purchase_returns.create')): ?>
      <a class="btn btn-primary" href="<?= e(($purchase_return_form_url ?? url('/purchases/returns/form')) . '?id='.(int)$return['id']) ?>">
        <i class="fa-solid fa-pen-to-square me-1"></i>تعديل
      </a>
    <?php endif; ?>

    <?php if ($st==='DRAFT' && can('purchase_returns.post')): ?>
      <form method="post" class="d-inline" onsubmit="return confirm('تأكيد ترحيل مرتجع الشراء؟');">
        <?= csrf_field() ?>
        <input type="hidden" name="action" value="post">
        <button class="btn btn-success">
          <i class="fa-solid fa-circle-check me-1"></i>ترحيل
        </button>
      </form>
    <?php endif; ?>

    <?php if ($st==='DRAFT' && can('purchase_returns.create')): ?>
      <form method="post" class="d-inline" onsubmit="return confirm('تأكيد إلغاء المرتجع (مسودة)؟');">
        <?= csrf_field() ?>
        <input type="hidden" name="action" value="cancel">
        <button class="btn btn-outline-danger">
          <i class="fa-solid fa-ban me-1"></i>إلغاء
        </button>
      </form>
    <?php endif; ?>
  </div>
</div>

<div class="row g-3">
  <div class="col-md-8">
    <div class="card">
      <div class="card-header d-flex justify-content-between align-items-center">
        <div class="fw-bold">بنود المرتجع</div>
        <span class="badge text-bg-<?= $badge ?>"><?= e($st) ?></span>
      </div>
      <div class="card-body p-0">
        <div class="table-responsive">
          <table class="table table-bordered align-middle mb-0">
            <thead class="table-light">
              <tr>
                <th style="width:60px">م</th>
                <th>الصنف</th>
                <th style="width:160px">الوحدة</th>
                <th style="width:120px">الكمية</th>
                <th style="width:140px">السعر</th>
                <th style="width:140px">الإجمالي</th>
              </tr>
            </thead>
            <tbody>
              <?php $i=1; foreach ($lines as $ln): ?>
                <tr>
                  <td class="text-center"><?= $i++ ?></td>
                  <td><?= e((string)$ln['item_name']) ?></td>
                  <td class="text-center"><?= e((string)($ln['unit_name'] ?? '')) ?></td>
                  <td class="text-center"><?= e(format_qty((float)$ln['qty'])) ?></td>
                  <td class="text-center"><?= e(format_money((float)$ln['unit_price'])) ?></td>
                  <td class="text-center"><?= e(format_money((float)$ln['line_total'])) ?></td>
                </tr>
              <?php endforeach; ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>

    <?php if (!empty($return['notes'])): ?>
      <div class="card mt-3">
        <div class="card-header fw-bold">ملاحظات</div>
        <div class="card-body"><?= e((string)$return['notes']) ?></div>
      </div>
    <?php endif; ?>
  </div>

  <div class="col-md-4">
    <div class="card">
      <div class="card-header fw-bold">ملخص</div>
      <div class="card-body">
        <div class="d-flex justify-content-between"><span class="fw-bold">الإجمالي</span><span class="fw-bold fs-5"><?= e(format_money((float)$return['total'])) ?></span></div>
      </div>
    </div>
  </div>
</div>
