<?php $profitExportQs = ['from'=>$from,'to'=>$to,'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/profit/print?'.http_build_query(array_merge($profitExportQs, ['format'=>'a4'])))) ?>"><i class="fa-solid fa-print"></i> طباعة</a>
    <a class="btn btn-outline-danger" href="<?= e(url('/reports/finance/profit/pdf?'.http_build_query($profitExportQs))) ?>">PDF</a>
    <a class="btn btn-outline-success" href="<?= e(url('/reports/finance/profit/export?'.http_build_query($profitExportQs))) ?>">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-3"><label class="form-label">من</label><input type="date" class="form-control" name="from" value="<?= e($from) ?>"></div>
    <div class="col-md-3"><label class="form-label">إلى</label><input type="date" class="form-control" name="to" value="<?= e($to) ?>"></div>
    <div class="col-md-4"><label class="form-label">بحث</label><input type="text" class="form-control" name="q" value="<?= e($q) ?>" placeholder="رقم فاتورة / عميل / مرتجع / ملاحظة"></div>
    <div class="col-md-2 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/profit')) ?>">مسح</a></div>
  </div>
</form>
<div class="row g-3 mb-3">
  <div class="col-lg-4 col-md-6"><div class="card p-3 h-100"><div class="text-muted small">إجمالي المبيعات</div><div class="fs-4 fw-bold text-primary"><?= e(nf($summary['gross_sales'])) ?></div></div></div>
  <div class="col-lg-4 col-md-6"><div class="card p-3 h-100"><div class="text-muted small">مرتجعات البيع</div><div class="fs-4 fw-bold text-warning"><?= e(nf($summary['sales_returns'])) ?></div></div></div>
  <div class="col-lg-4 col-md-6"><div class="card p-3 h-100"><div class="text-muted small">صافي المبيعات</div><div class="fs-4 fw-bold text-success"><?= e(nf($summary['net_sales'])) ?></div></div></div>
  <div class="col-lg-4 col-md-6"><div class="card p-3 h-100"><div class="text-muted small">تكلفة البضاعة المباعة</div><div class="fs-4 fw-bold text-danger"><?= e(nf($summary['net_cogs'])) ?></div><div class="small text-muted mt-1">بعد خصم تكلفة مرتجعات البيع: <?= e(nf($summary['return_cost'])) ?></div></div></div>
  <div class="col-lg-4 col-md-6"><div class="card p-3 h-100"><div class="text-muted small">مجمل الربح</div><div class="fs-4 fw-bold <?= ($summary['gross_profit']>=0?'text-success':'text-danger') ?>"><?= e(nf($summary['gross_profit'])) ?></div></div></div>
  <div class="col-lg-4 col-md-6"><div class="card p-3 h-100"><div class="text-muted small">إجمالي المصروفات</div><div class="fs-4 fw-bold text-danger"><?= e(nf($summary['expenses'])) ?></div></div></div>
</div>
<div class="card p-3 mb-3 border-2 <?= ($summary['net_profit']>=0?'border-success':'border-danger') ?>">
  <div class="text-muted small">صافي الربح</div>
  <div class="fs-2 fw-bold <?= ($summary['net_profit']>=0?'text-success':'text-danger') ?>"><?= e(nf($summary['net_profit'])) ?></div>
</div>
<div class="row g-3">
  <div class="col-lg-6">
    <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">
          <tbody>
            <tr><th class="bg-light">إجمالي المبيعات</th><td class="text-end fw-bold"><?= e(nf($summary['gross_sales'])) ?></td></tr>
            <tr><th class="bg-light">- مرتجعات البيع</th><td class="text-end fw-bold"><?= e(nf($summary['sales_returns'])) ?></td></tr>
            <tr><th class="bg-light">= صافي المبيعات</th><td class="text-end fw-bold text-success"><?= e(nf($summary['net_sales'])) ?></td></tr>
            <tr><th class="bg-light">تكلفة المبيعات</th><td class="text-end fw-bold"><?= e(nf($summary['sales_cost'])) ?></td></tr>
            <tr><th class="bg-light">- تكلفة المرتجعات</th><td class="text-end fw-bold"><?= e(nf($summary['return_cost'])) ?></td></tr>
            <tr><th class="bg-light">= صافي التكلفة</th><td class="text-end fw-bold text-danger"><?= e(nf($summary['net_cogs'])) ?></td></tr>
            <tr><th class="bg-light">= مجمل الربح</th><td class="text-end fw-bold <?= ($summary['gross_profit']>=0?'text-success':'text-danger') ?>"><?= e(nf($summary['gross_profit'])) ?></td></tr>
            <tr><th class="bg-light">- المصروفات</th><td class="text-end fw-bold text-danger"><?= e(nf($summary['expenses'])) ?></td></tr>
            <tr><th class="bg-light">= صافي الربح</th><td class="text-end fw-bold <?= ($summary['net_profit']>=0?'text-success':'text-danger') ?>"><?= e(nf($summary['net_profit'])) ?></td></tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
  <div class="col-lg-6">
    <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($expenseByCategory)): ?><tr><td colspan="2" class="text-center text-muted py-3">لا توجد مصروفات</td></tr><?php endif; ?>
            <?php foreach ($expenseByCategory 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>
<div class="row g-3 mt-1">
  <div class="col-lg-4"><div class="card p-3"><div class="fw-bold mb-2">عدد فواتير البيع</div><div class="fs-5 fw-bold"><?= e((string)count($salesRows)) ?></div></div></div>
  <div class="col-lg-4"><div class="card p-3"><div class="fw-bold mb-2">عدد مرتجعات البيع</div><div class="fs-5 fw-bold"><?= e((string)count($returnRows)) ?></div></div></div>
  <div class="col-lg-4"><div class="card p-3"><div class="fw-bold mb-2">عدد سندات المصروف</div><div class="fs-5 fw-bold"><?= e((string)count($expenseRows)) ?></div></div></div>
</div>
