<?php
// Variables: date_from,date_to,warehouse_id,warehouses,rows,totals
?>

<div class="content-header">
  <div class="container-fluid">
    <div class="row mb-2">
      <div class="col-sm-6">
        <h1 class="m-0">مشتريات حسب مورد</h1>
      </div>
      <div class="col-sm-6">
        <ol class="breadcrumb float-sm-right">
          <li class="breadcrumb-item"><a href="<?= e(url('/admin/dashboard')) ?>">الرئيسية</a></li>
          <li class="breadcrumb-item active">تقارير المشتريات</li>
        </ol>
      </div>
    </div>
  </div>
</div>

<section class="content">
  <div class="container-fluid">
    <div class="card">
      <div class="card-body">
        <form method="get" class="row g-2 align-items-end">
          <div class="col-md-3">
            <label class="form-label">من</label>
            <input type="date" name="from" class="form-control" value="<?= e($date_from) ?>">
          </div>
          <div class="col-md-3">
            <label class="form-label">إلى</label>
            <input type="date" name="to" class="form-control" value="<?= e($date_to) ?>">
          </div>
          <div class="col-md-4">
            <label class="form-label">المخزن</label>
            <select name="warehouse_id" class="form-select">
              <option value="0">كل المخازن</option>
              <?php foreach (($warehouses ?? []) as $w): ?>
                <option value="<?= e((string)$w['id']) ?>" <?= ((int)$warehouse_id === (int)$w['id']) ? 'selected' : '' ?>><?= e($w['name']) ?></option>
              <?php endforeach; ?>
            </select>
          </div>
          <div class="col-md-2 d-grid">
            <button class="btn btn-primary">عرض</button>
          </div>
        </form>
      </div>
    </div>

    <div class="card">
      <div class="card-header d-flex align-items-center justify-content-between">
        <strong>النتائج</strong>
        <a class="btn btn-outline-secondary btn-sm" target="_blank" href="<?= e(url('/reports/purchases/by-supplier/print?' . http_build_query(array_merge($_GET, ['format'=>'a4'])))) ?>">طباعة</a> <a class="btn btn-outline-danger btn-sm" href="<?= e(url('/reports/purchases/by-supplier/pdf?' . http_build_query($_GET))) ?>">PDF</a> <a class="btn btn-outline-success btn-sm" href="<?= e(url('/reports/purchases/by-supplier/export?' . http_build_query($_GET))) ?>">Excel</a>
      </div>
      <div class="card-body table-responsive">
        <table class="table table-bordered table-sm align-middle">
          <thead>
            <tr>
              <th>المورد</th>
              <th class="text-center">عدد الفواتير</th>
              <th class="text-end">قبل الخصم</th>
              <th class="text-end">الخصم</th>
              <th class="text-end">VAT</th>
              <th class="text-end">ص/ت</th>
              <th class="text-end">الإجمالي</th>
            </tr>
          </thead>
          <tbody>
            <?php if (empty($rows)): ?>
              <tr><td colspan="7" class="text-center text-muted">لا يوجد بيانات</td></tr>
            <?php else: foreach ($rows as $r): ?>
              <tr>
                <td><?= e($r['supplier_name']) ?></td>
                <td class="text-center"><?= e((string)$r['inv_count']) ?></td>
                <td class="text-end"><?= e(format_money($r['subtotal'])) ?></td>
                <td class="text-end"><?= e(format_money($r['discount_amount'])) ?></td>
                <td class="text-end"><?= e(format_money($r['tax_amount'])) ?></td>
                <td class="text-end"><?= e(format_money($r['wht_amount'] ?? 0)) ?></td>
                <td class="text-end fw-bold"><?= e(format_money($r['total'])) ?></td>
              </tr>
            <?php endforeach; endif; ?>
          </tbody>
          <tfoot>
            <tr>
              <th>الإجمالي</th>
              <th class="text-center"><?= e((string)($totals['inv_count'] ?? 0)) ?></th>
              <th class="text-end"><?= e(format_money($totals['subtotal'] ?? 0)) ?></th>
              <th class="text-end"><?= e(format_money($totals['discount_amount'] ?? 0)) ?></th>
              <th class="text-end"><?= e(format_money($totals['tax_amount'] ?? 0)) ?></th>
              <th class="text-end"><?= e(format_money($totals['wht_amount'] ?? 0)) ?></th>
              <th class="text-end fw-bold"><?= e(format_money($totals['total'] ?? 0)) ?></th>
            </tr>
          </tfoot>
        </table>
      </div>
    </div>
  </div>
</section>
