<?php
$expenseExportQs = ['from'=>$from,'to'=>$to,'category_id'=>$category_id,'item_id'=>$item_id,'account_source_type'=>$account_source_type,'cashbox_id'=>$cashbox_id,'bank_account_id'=>$bank_account_id,'q'=>$q];
?>
<div class="d-flex justify-content-between align-items-center mb-3">
  <h4 class="mb-0">تقارير المصروفات</h4>
  <div class="btn-group">
    <a class="btn btn-outline-primary" target="_blank" href="<?= e(url('/reports/finance/expenses/print?'.http_build_query(array_merge($expenseExportQs, ['format'=>'a4'])))) ?>"><i class="fa-solid fa-print"></i> طباعة</a>
    <a class="btn btn-outline-danger" href="<?= e(url('/reports/finance/expenses/pdf?'.http_build_query($expenseExportQs))) ?>">PDF</a>
    <a class="btn btn-outline-success" href="<?= e(url('/reports/finance/expenses/export?'.http_build_query($expenseExportQs))) ?>">Excel</a>
  </div>
</div>
<form class="card p-3 mb-3" method="get">
  <div class="row g-2 align-items-end">
    <div class="col-md-2"><label class="form-label">من</label><input type="date" class="form-control" name="from" value="<?= e($from) ?>"></div>
    <div class="col-md-2"><label class="form-label">إلى</label><input type="date" class="form-control" name="to" value="<?= e($to) ?>"></div>
    <div class="col-md-2"><label class="form-label">مجموعة المصروف</label><select class="form-select" name="category_id"><option value="0">الكل</option><?php foreach ($categories as $c): ?><option value="<?= (int)$c['id'] ?>" <?= ((int)$category_id===(int)$c['id']?'selected':'') ?>><?= e($c['name']) ?></option><?php endforeach; ?></select></div>
    <div class="col-md-3"><label class="form-label">بند المصروف</label><select class="form-select" name="item_id"><option value="0">الكل</option><?php foreach ($items as $i): ?><option value="<?= (int)$i['id'] ?>" <?= ((int)$item_id===(int)$i['id']?'selected':'') ?>><?= e($i['category_name'].' / '.$i['name']) ?></option><?php endforeach; ?></select></div>
    <div class="col-md-3"><label class="form-label">بحث</label><input type="text" class="form-control" name="q" value="<?= e($q) ?>" placeholder="رقم السند / بيان / مستفيد"></div>
    <div class="col-md-3"><label class="form-label">جهة الصرف</label><select class="form-select" name="account_source_type"><option value="">الكل</option><option value="cashbox" <?= $account_source_type==='cashbox'?'selected':'' ?>>خزنة</option><option value="bank_account" <?= $account_source_type==='bank_account'?'selected':'' ?>>حساب بنكي</option></select></div>
    <div class="col-md-3"><label class="form-label">الخزنة</label><select class="form-select" name="cashbox_id"><option value="0">الكل</option><?php foreach ($cashboxes as $cb): ?><option value="<?= (int)$cb['id'] ?>" <?= ((int)$cashbox_id===(int)$cb['id']?'selected':'') ?>><?= e($cb['name']) ?></option><?php endforeach; ?></select></div>
    <div class="col-md-3"><label class="form-label">الحساب البنكي</label><select class="form-select" name="bank_account_id"><option value="0">الكل</option><?php foreach ($bankAccounts as $ba): ?><option value="<?= (int)$ba['id'] ?>" <?= ((int)$bank_account_id===(int)$ba['id']?'selected':'') ?>><?= e($ba['name']) ?></option><?php endforeach; ?></select></div>
    <div class="col-md-3 d-flex gap-2"><button class="btn btn-outline-primary w-100" type="submit">عرض</button><a class="btn btn-outline-secondary" href="<?= e(url('/reports/finance/expenses')) ?>">مسح</a></div>
  </div>
</form>
<div class="row g-3 mb-3">
  <div class="col-md-4"><div class="card p-3"><div class="text-muted small">إجمالي المصروفات</div><div class="fs-4 fw-bold text-danger"><?= e(nf($totals['amount'])) ?></div></div></div>
  <div class="col-md-4"><div class="card p-3"><div class="text-muted small">عدد السندات</div><div class="fs-4 fw-bold"><?= e((string)count($rows)) ?></div></div></div>
  <div class="col-md-4"><div class="card p-3"><div class="text-muted small">متوسط السند</div><div class="fs-4 fw-bold"><?= e(nf(count($rows)?($totals['amount']/count($rows)):0)) ?></div></div></div>
</div>
<div class="row g-3 mb-3">
  <div class="col-lg-5">
    <div class="card p-3 h-100">
      <div class="fw-bold mb-2">حسب مجموعة المصروف</div>
      <div class="table-responsive">
        <table class="table table-sm table-bordered mb-0"><thead class="table-light"><tr><th>المجموعة</th><th class="text-end">الإجمالي</th></tr></thead><tbody>
          <?php if (empty($byCategory)): ?><tr><td colspan="2" class="text-center text-muted py-3">لا توجد بيانات</td></tr><?php endif; ?>
          <?php foreach ($byCategory as $name => $amt): ?><tr><td><?= e($name) ?></td><td class="text-end fw-bold"><?= e(nf($amt)) ?></td></tr><?php endforeach; ?>
        </tbody></table>
      </div>
    </div>
  </div>
  <div class="col-lg-7">
    <div class="card p-3 h-100">
      <div class="fw-bold mb-2">حسب بند المصروف</div>
      <div class="table-responsive">
        <table class="table table-sm table-bordered mb-0"><thead class="table-light"><tr><th>البند</th><th class="text-end">الإجمالي</th></tr></thead><tbody>
          <?php if (empty($byItem)): ?><tr><td colspan="2" class="text-center text-muted py-3">لا توجد بيانات</td></tr><?php endif; ?>
          <?php foreach ($byItem as $key => $amt): [$cat,$item] = explode('||', $key, 2); ?><tr><td><div class="fw-semibold"><?= e($item) ?></div><small class="text-muted"><?= e($cat) ?></small></td><td class="text-end fw-bold"><?= e(nf($amt)) ?></td></tr><?php endforeach; ?>
        </tbody></table>
      </div>
    </div>
  </div>
</div>
<div class="card p-3">
  <div class="fw-bold mb-2">تفاصيل المصروفات</div>
  <div class="table-responsive">
    <table class="table table-sm table-bordered align-middle mb-0">
      <thead class="table-light"><tr><th>رقم السند</th><th>التاريخ</th><th>المجموعة</th><th>البند</th><th>الصرف من</th><th>المستفيد</th><th>البيان</th><th class="text-end">المبلغ</th><th></th></tr></thead>
      <tbody>
        <?php if (empty($rows)): ?><tr><td colspan="9" class="text-center text-muted py-4">لا توجد بيانات</td></tr><?php endif; ?>
        <?php foreach ($rows as $r): ?>
          <tr>
            <td><?= e($r['voucher_no']) ?></td>
            <td><?= e(date('Y-m-d', strtotime((string)$r['voucher_date']))) ?></td>
            <td><?= e($r['expense_category_name']) ?></td>
            <td><?= e($r['expense_item_name']) ?></td>
            <td><?= e(((string)($r['account_source_type'] ?? 'cashbox')) === 'bank_account' ? ((string)($r['bank_account_name'] ?? '')) : ((string)($r['cashbox_name'] ?? ''))) ?></td>
            <td><?= e($r['payee_name'] ?: '-') ?></td>
            <td><?= e($r['notes'] ?: ($r['purpose'] ?: '-')) ?></td>
            <td class="text-end fw-bold"><?= e(nf($r['amount'])) ?></td>
            <td class="text-end"><a class="btn btn-sm btn-outline-primary" href="<?= e(url('/finance/cash-payments/view?id='.(int)$r['id'])) ?>">عرض</a></td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</div>
