<?php
// view: pages/customers/index
?>
<?php $customers = $customers ?? []; $q = $q ?? ''; $edit_customer = $edit_customer ?? 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_customer ? 'تعديل عميل' : 'إضافة عميل' ?></strong>
        <?php if ($edit_customer): ?>
          <a class="btn btn-sm btn-outline-secondary" href="<?= e(url('/parties/customers')) ?>"><i class="fa-solid fa-xmark me-1"></i>إلغاء</a>
        <?php endif; ?>
      </div>
      <div class="card-body">
        <?php $canManageCustomer = $edit_customer ? can('customers.edit') : can('customers.create'); ?>
        <?php if (!$canManageCustomer): ?>
          <div class="alert alert-warning mb-0">ليس لديك صلاحية <?= $edit_customer ? 'تعديل' : 'إضافة' ?> العملاء.</div>
        <?php else: ?>
        <form method="post" class="row g-2">
          <input type="hidden" name="action" value="<?= $edit_customer ? 'update' : 'create' ?>">
          <?php if ($edit_customer): ?>
            <input type="hidden" name="id" value="<?= (int)$edit_customer['id'] ?>">
          <?php endif; ?>

          <div class="col-lg-3">
            <label class="form-label">كود العميل</label>
            <input class="form-control" name="code" placeholder="اتركه فارغًا للتوليد التلقائي" value="<?= e((string)($edit_customer['code'] ?? '')) ?>">
          </div>
          <div class="col-lg-6">
            <label class="form-label">اسم العميل</label>
            <input class="form-control" name="name" required value="<?= e((string)($edit_customer['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_customer ? (((int)$edit_customer['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_supplier" id="is_supplier" <?= $edit_customer ? (((int)($edit_customer['is_supplier'] ?? 0)===1) ? 'checked' : '') : '' ?>>
              <label class="form-check-label" for="is_supplier">هذا العميل أيضًا مورد</label>
            </div>
          </div>

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

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

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

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

          <div class="col-12 mt-2">
            <button class="btn btn-dark w-100"><?= $edit_customer ? 'حفظ التعديل' : 'إضافة' ?></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 ($customers as $c): ?>
                <tr>
                  <td><?= (int)$c['id'] ?></td>
                  <td><?= e((string)($c['code'] ?? '-')) ?></td>
                  <td>
                    <a class="text-decoration-none" href="<?= e(url('/parties/view')) ?>?id=<?= (int)$c['id'] ?>">
                      <?= e((string)$c['name']) ?>
                    </a>
                  </td>
                  <td><?= e((string)($c['phone'] ?? '-')) ?></td>
                  <td class="text-end"><?= format_money($c['opening_balance'] ?? 0) ?></td>
                  <td class="text-end"><?= format_money($c['credit_limit'] ?? 0) ?></td>
                  <td>
                    <?php if ((int)$c['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)$c['id'] ?>">
                      عرض
                    </a>
                    <a class="btn btn-sm btn-outline-secondary" target="_blank" href="<?= e(url('/prints/customer')) ?>?id=<?= (int)$c['id'] ?>" title="طباعة بيانات العميل">
                      🖨
                    </a>
                    <?php if (can('customers.edit')): ?>
                      <a class="btn btn-sm btn-outline-dark" href="<?= e(url('/parties/customers')) ?>?edit=<?= (int)$c['id'] ?>">تعديل</a>
                    <?php endif; ?>
                  </td>
                </tr>
              <?php endforeach; ?>
              <?php if (!$customers): ?>
                <tr><td colspan="8" class="text-center text-muted py-4">لا توجد بيانات</td></tr>
              <?php endif; ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
</div>
