<!doctype html>
<html lang="ar" dir="rtl">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>تقرير شامل</title>
  <style>
    @page { size: A4; margin: 14mm; }
    *{box-sizing:border-box}
    body{font-family:Arial, sans-serif;color:#111827}

    /* إخفاء أي عناصر غير مطلوبة في الطباعة (إجباري) */
    @media print{
      .noPrint{display:none !important; visibility:hidden !important;}
      a[href]:after{content:""} /* يمنع طباعة الروابط كنص إضافي في بعض المتصفحات */
    }

    /* ===== Print Header ===== */
    .reportHeader{
      display:flex;
      justify-content:space-between;
      align-items:flex-start;
      gap:12px;
      padding-bottom:10px;
      border-bottom:1px solid #e5e7eb;
      margin-bottom:10px;
    }
    .business{min-width:260px}
    .businessName{
      font-weight:900;
      font-size:24px;
      line-height:1.2;
      margin:0;
    }
    .reportTitle{
      margin-top:6px;
      font-weight:900;
      font-size:14px;
      color:#111827;
    }
    .meta{
      flex:1;
      font-size:12px;
      color:#374151;
      line-height:1.9;
      margin-top:2px;
    }
    .lbl{color:#6b7280;font-weight:900}
    .logo{
      width:110px;
      display:flex;
      justify-content:flex-start; /* في RTL هذا يعني أقصى اليسار */
    }
    .logo img{
      width:110px;
      height:70px;
      object-fit:contain;
    }

    .printMetaLine{
      display:flex;
      justify-content:space-between;
      gap:10px;
      margin:8px 0 12px;
      font-size:12px;
      color:#374151;
    }
    .printMetaLine > div{white-space:nowrap}

    /* ===== Cards / Tables ===== */
    .box{border:1px solid #e5e7eb;border-radius:12px;padding:12px;margin-bottom:10px}
    table{width:100%;border-collapse:collapse;table-layout:fixed}
    th,td{
      border-bottom:1px solid #e5e7eb;
      padding:9px 10px;
      text-align:right;
      font-size:13px;
      vertical-align:top;
      word-break:break-word;
    }
    th{background:#f9fafb;font-weight:900}
    .num{font-weight:900}

    .k{display:grid;grid-template-columns:repeat(2,1fr);gap:10px;margin-top:10px}
    .k .b{border:1px solid #e5e7eb;border-radius:12px;padding:12px}
    .k .tt{color:#6b7280;font-size:12px;font-weight:900}
    .k .vv{margin-top:6px;font-weight:900;font-size:16px}

    /* ===== Top bar + Sub bar داخل Card أبيض (تظهر بالشاشة فقط) ===== */
    .topCard{
      background:#fff;
      border:1px solid #e5e7eb;
      border-radius:14px;
      padding:12px;
      margin-bottom:10px;
    }
    .topBar{
      display:flex;gap:8px;align-items:center;justify-content:space-between;
      flex-wrap:wrap;margin-bottom:10px;
    }
    .leftBtns{display:flex;gap:8px;align-items:center;flex-wrap:wrap}
    .btnHome{
      padding:10px 12px;border-radius:10px;border:1px solid #2563eb;
      background:#2563eb;color:#fff;font-weight:900;cursor:pointer;
      text-decoration:none;display:inline-flex;align-items:center;
    }
    .btnPrint{
      padding:10px 12px;border-radius:10px;border:1px solid #d1d5db;
      background:#111827;color:#fff;font-weight:900;cursor:pointer;
      display:inline-flex;align-items:center;
    }
    .btnLogout{
      padding:10px 12px;border-radius:10px;border:1px solid #dc2626;
      background:#dc2626;color:#fff;font-weight:900;cursor:pointer;
      display:inline-flex;align-items:center;
    }

    .subTabs{
      display:flex;gap:8px;align-items:center;flex-wrap:wrap;
    }
    .tabBtn{
      padding:10px 12px;border-radius:10px;border:1px solid #d1d5db;
      background:#fff;color:#111827;font-weight:900;cursor:pointer;
      text-decoration:none;display:inline-flex;align-items:center;
    }
    .tabBtn.active{background:#111827;color:#fff;border-color:#111827}
  </style>
</head>
<body>

<?php
  $company = null;
  try {
    $company = \Illuminate\Support\Facades\DB::table('company_settings')->where('id', 1)->first();
  } catch (\Throwable $e) { $company = null; }

  $businessName = $company->business_name ?? 'اسم النشاط التجاري';
  $logoUrl = (!empty($company->logo_path)) ? asset('storage/'.$company->logo_path) : null;

  // المستخدم (عود Name)
  $printedBy = auth()->check() ? (auth()->user()->name ?? '') : '';

  // التاريخ بصيغة dd/MM/yyyy مع الوقت
  $printedAt = now()->format('d/m/Y H:i');

  // الفرع حسب الفلتر
  $branchLabel = 'كل الفروع';
  if ((int)($branchId ?? 0)) {
    $b = $branches->firstWhere('id', (int)$branchId);
    $branchLabel = $b ? ($b->branch_no.' - '.$b->name) : ('فرع #' . (int)$branchId);
  }
?>

<!-- ✅ Top bar + Sub bar (شاشة فقط) -->
<div class="topCard noPrint">
  <div class="topBar">
    <div class="leftBtns">
      <a class="btnHome" href="<?php echo e(Route::has('dashboard.sales') ? route('dashboard.sales') : '#'); ?>">الرئيسية</a>
      <button class="btnPrint" type="button" onclick="window.print()">طباعة / حفظ PDF</button>
    </div>

    <form method="POST" action="<?php echo e(Route::has('logout') ? route('logout') : '#'); ?>" style="display:inline">
      <?php echo csrf_field(); ?>
      <button class="btnLogout" type="submit">خروج</button>
    </form>
  </div>

  <div class="subTabs">
    <a class="tabBtn active" href="<?php echo e(Route::has('dashboard.reports.print') ? route('dashboard.reports.print', ['branch_id'=>$branchId,'from'=>$from,'to'=>$to]) : '#'); ?>">تقرير شامل</a>
    <a class="tabBtn" href="<?php echo e(Route::has('dashboard.reports.sales') ? route('dashboard.reports.sales', ['branch_id'=>$branchId,'from'=>$from,'to'=>$to]) : '#'); ?>">تقرير المبيعات</a>
    <a class="tabBtn" href="<?php echo e(Route::has('dashboard.reports.purchases') ? route('dashboard.reports.purchases', ['branch_id'=>$branchId,'from'=>$from,'to'=>$to]) : '#'); ?>">تقرير المشتريات</a>
    <a class="tabBtn" href="<?php echo e(Route::has('dashboard.reports.expenses') ? route('dashboard.reports.expenses', ['branch_id'=>$branchId,'from'=>$from,'to'=>$to]) : '#'); ?>">تقرير المصروفات</a>
  </div>
</div>

<!-- ✅ Report Header (هو أول شيء في الطباعة) -->
<div class="reportHeader">
  <!-- يمين: اسم النشاط -->
  <div class="business">
    <div class="businessName"><?php echo e($businessName); ?></div>
    <div class="reportTitle">تقرير شامل (مبيعات / مشتريات / مصروفات)</div>
  </div>

  <!-- وسط: بيانات الفترة والفرع -->
  <div class="meta">
    <div><span class="lbl">الفترة:</span> <?php echo e($from); ?> → <?php echo e($to); ?></div>
    <div><span class="lbl">الفرع:</span> <?php echo e($branchLabel); ?></div>
  </div>

  <!-- يسار: الشعار -->
  <div class="logo">
    <?php if($logoUrl): ?>
      <img src="<?php echo e($logoUrl); ?>" alt="Logo">
    <?php endif; ?>
  </div>
</div>

<div class="printMetaLine">
  <div><span class="lbl">طبع بواسطة:</span> <?php echo e($printedBy ?: '-'); ?></div>
  <div><span class="lbl">تاريخ الطباعة:</span> <?php echo e($printedAt); ?></div>
</div>

<div class="box">
  <table>
    <thead>
      <tr>
        <th>القسم</th>
        <th>قبل الضريبة</th>
        <th>الضريبة</th>
        <th>بعد الضريبة</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td style="font-weight:900">المبيعات</td>
        <td class="num"><?php echo e(number_format($sales['subtotal'] ?? 0, 2)); ?></td>
        <td class="num"><?php echo e(number_format($sales['vat'] ?? 0, 2)); ?></td>
        <td class="num"><?php echo e(number_format($sales['total'] ?? 0, 2)); ?></td>
      </tr>
      <tr>
        <td style="font-weight:900">المشتريات</td>
        <td class="num"><?php echo e(number_format($purchases['subtotal'] ?? 0, 2)); ?></td>
        <td class="num"><?php echo e(number_format($purchases['vat'] ?? 0, 2)); ?></td>
        <td class="num"><?php echo e(number_format($purchases['total'] ?? 0, 2)); ?></td>
      </tr>
      <tr>
        <td style="font-weight:900">المصروفات</td>
        <td class="num"><?php echo e(number_format($expenses['subtotal'] ?? 0, 2)); ?></td>
        <td class="num"><?php echo e(number_format($expenses['vat'] ?? 0, 2)); ?></td>
        <td class="num"><?php echo e(number_format($expenses['total'] ?? 0, 2)); ?></td>
      </tr>
    </tbody>
  </table>

  <div class="k">
    <div class="b">
      <div class="tt">الصافي بعد الضريبة</div>
      <div class="vv"><?php echo e(number_format($netTotal ?? 0, 2)); ?></div>
    </div>
    <div class="b">
      <div class="tt">صافي الضريبة</div>
      <div class="vv"><?php echo e(number_format($netVat ?? 0, 2)); ?></div>
    </div>
  </div>
</div>

<script>
(function () {
  try {
    var params = new URLSearchParams(window.location.search);
    if (params.get('autoprint') === '1') {
      window.addEventListener('load', function () {
        setTimeout(function () { window.print(); }, 300);
      });
    }
  } catch (e) {}
})();
</script>

</body>
</html>
<?php /**PATH /home/mylaundry26/public_html/apps/ngharbia/resources/views/reports_print.blade.php ENDPATH**/ ?>