<?php
// view: pages/suppliers/index
?>
<?php $suppliers = $suppliers ?? []; $q = $q ?? ''; $edit_supplier = $edit_supplier ?? null; ?>

<div class="d-flex justify-content-between align-items-center mb-3">
  <h4 class="mb-0">الموردون</h4>
  <form class="d-flex gap-2" method="get">
    <input class="form-control" name="q" placeholder="بحث بالاسم أو الهاتف أو الكود" value="<?= e($q) ?>">
    <button class="btn btn-outline-dark">بحث</button>
  </form>
</div>

<div class="row g-3">
  <div class="col-12">
    <div class="card shadow-sm">
      <div class="card-header d-flex justify-content-between align-items-center">
        <strong><?= $edit_supplier ? 'تعديل مورد' : 'إضافة مورد' ?></strong>
        <?php if ($edit_supplier): ?>
          <a class="btn btn-sm btn-outline-secondary" href="<?= e(url('/parties/suppliers')) ?>"><i class="fa-solid fa-xmark me-1"></i>إلغاء</a>
        <?php endif; ?>
      </div>
      <div class="card-body">
        <?php $canManageSupplier = $edit_supplier ? can('suppliers.edit') : can('suppliers.create'); ?>
        <?php if (!$canManageSupplier): ?>
          <div class="alert alert-warning mb-0">ليس لديك صلاحية <?= $edit_supplier ? 'تعديل' : 'إضافة' ?> الموردين.</div>
        <?php else: ?>
        <form method="post" class="row g-2">
          <input type="hidden" name="action" value="<?= $edit_supplier ? 'update' : 'create' ?>">
          <?php if ($edit_supplier): ?>
            <input type="hidden" name="id" value="<?= (int)$edit_supplier['id'] ?>">
          <?php endif; ?>

          <div class="col-lg-3">
            <label class="form-label">كود المورد</label>
            <input class="form-control" name="code" placeholder="اتركه فارغًا للتوليد التلقائي" value="<?= e((string)($edit_supplier['code'] ?? '')) ?>">
          </div>
          <div class="col-lg-6">
            <label class="form-label">اسم المورد</label>
            <input class="form-control" name="name" required value="<?= e((string)($edit_supplier['name'] ?? '')) ?>">
          </div>
          <div class="col-lg-3">
            <label class="form-label">الحالة</label>
            <div class="form-check mt-2">
              <input class="form-check-input" type="checkbox" name="is_active" id="is_active" <?= $edit_supplier ? (((int)$edit_supplier['is_active']===1) ? 'checked' : '') : 'checked' ?>>
              <label class="form-check-label" for="is_active">نشط</label>
            </div>

            <div class="form-check mt-2">
              <input class="form-check-input" type="checkbox" name="is_customer" id="is_customer" <?= $edit_supplier ? (((int)($edit_supplier['is_customer'] ?? 0)===1) ? 'checked' : '') : '' ?>>
              <label class="form-check-label" for="is_customer">هذا المورد أيضًا عميل</label>
              <div class="form-text">(نفس الطرف يمكن أن يكون عميل/مورد)</div>
            </div>
          </div>

          <div class="col-lg-3">
            <label class="form-label">هاتف</label>
            <input class="form-control" name="phone" value="<?= e((string)($edit_supplier['phone'] ?? '')) ?>">
          </div>
          <div class="col-lg-3">
            <label class="form-label">هاتف 2</label>
            <input class="form-control" name="phone2" value="<?= e((string)($edit_supplier['phone2'] ?? '')) ?>">
          </div>
          <div class="col-lg-3">
            <label class="form-label">بريد إلكتروني</label>
            <input class="form-control" name="email" type="email" value="<?= e((string)($edit_supplier['email'] ?? '')) ?>">
          </div>
          <div class="col-lg-3">
            <label class="form-label">رقم ضريبي</label>
            <input class="form-control" name="tax_no" value="<?= e((string)($edit_supplier['tax_no'] ?? '')) ?>">
          </div>

          <div class="col-lg-8">
            <label class="form-label">العنوان</label>
            <input class="form-control" name="address" value="<?= e((string)($edit_supplier['address'] ?? '')) ?>">
          </div>
          <div class="col-lg-4">
            <label class="form-label">رصيد افتتاحي (اختياري)</label>
            <input class="form-control" name="opening_balance" type="number" step="0.01" value="<?= e((string)($edit_supplier['opening_balance'] ?? '0')) ?>">
          </div>

          <div class="col-lg-4">
            <label class="form-label">الحد الائتماني (اختياري)</label>
            <input class="form-control" name="credit_limit" type="number" step="0.01" value="<?= e((string)($edit_supplier['credit_limit'] ?? '0')) ?>">
          </div>

          <div class="col-12">
            <label class="form-label">ملاحظات</label>
            <input class="form-control" name="notes" value="<?= e((string)($edit_supplier['notes'] ?? '')) ?>">
          </div>

          <div class="col-12 mt-2">
            <button class="btn btn-dark w-100"><?= $edit_supplier ? 'حفظ التعديل' : 'إضافة' ?></button>
          </div>
        </form>
        <?php endif; ?>
      </div>
    </div>
  </div>

  <div class="col-12">
    <div class="card shadow-sm">
      <div class="card-header"><strong>قائمة الموردين (آخر 200)</strong></div>
      <div class="card-body">
        <div class="table-responsive">
          <table class="table table-bordered table-hover table-striped align-middle table-sm mb-0">
            <thead class="table-light">
              <tr>
                <th>#</th>
                <th>الكود</th>
                <th>اسم المورد</th>
                <th>الهاتف</th>
                <th class="text-end">رصيد افتتاحي</th>
                <th class="text-end">الحد الائتماني</th>
                <th>الحالة</th>
                <th></th>
              </tr>
            </thead>
            <tbody>
              <?php foreach ($suppliers as $s): ?>
                <tr>
                  <td><?= (int)$s['id'] ?></td>
                  <td><?= e((string)($s['code'] ?? '-')) ?></td>
                  <td>
                    <a class="text-decoration-none" href="<?= e(url('/parties/view')) ?>?id=<?= (int)$s['id'] ?>">
                      <?= e((string)$s['name']) ?>
                    </a>
                  </td>
                  <td><?= e((string)($s['phone'] ?? '-')) ?></td>
                  <td class="text-end"><?= format_money($s['opening_balance'] ?? 0) ?></td>
                  <td class="text-end"><?= format_money($s['credit_limit'] ?? 0) ?></td>
                  <td>
                    <?php if ((int)$s['is_active'] === 1): ?>
                      <span class="badge text-bg-success">نشط</span>
                    <?php else: ?>
                      <span class="badge text-bg-secondary">موقوف</span>
                    <?php endif; ?>
                  </td>
                  <td class="text-end text-nowrap">
                    <a class="btn btn-sm btn-outline-secondary" href="<?= e(url('/parties/view')) ?>?id=<?= (int)$s['id'] ?>">
                      عرض
                    </a>
                    <a class="btn btn-sm btn-outline-secondary" target="_blank" href="<?= e(url('/prints/supplier')) ?>?id=<?= (int)$s['id'] ?>" title="طباعة بيانات المورد">
                      🖨
                    </a>
                    <?php if (can('suppliers.edit')): ?>
                      <a class="btn btn-sm btn-outline-dark" href="<?= e(url('/parties/suppliers')) ?>?edit=<?= (int)$s['id'] ?>">تعديل</a>
                    <?php endif; ?>
                  </td>
                </tr>
              <?php endforeach; ?>
              <?php if (!$suppliers): ?>
                <tr><td colspan="8" class="text-center text-muted py-4">لا توجد بيانات</td></tr>
              <?php endif; ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
</div>
