<!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>
    /* ========= Themes (3 حالات) ========= */

    :root{ /* LIGHT (هو القديم) */
      --bg:#f6f7fb;
      --card:#ffffff;
      --text:#111827;
      --muted:#6b7280;
      --border:#e5e7eb;
      --shadow:0 10px 25px rgba(0,0,0,.06);

      --btn:#111827; --btnText:#ffffff;
      --btn2Bg:#ffffff; --btn2Text:#111827; --btn2Border:#d1d5db;

      --homeBg:#2563eb; --homeText:#ffffff;
      --dangerBg:#dc2626; --dangerText:#ffffff;

      --okBg:#ecfdf3; --okBorder:#bbf7d0; --okText:#166534;
      --errBg:#fef2f2; --errBorder:#fecaca; --errText:#991b1b;

      --tableBg:#ffffff;
      --tableHead:#f9fafb;
      --tableBorder:#e5e7eb;
      --tableHover:#f3f4f6;

      --inputBg:#ffffff;
      --inputText:#111827;
      --focusRing:rgba(17,24,39,.10);
    }

    html[data-theme="dim"]{ /* مريح */
      --bg:#0f172a;
      --card:#111c33;
      --text:#e5e7eb;
      --muted:#9ca3af;
      --border:rgba(255,255,255,.10);
      --shadow:0 14px 35px rgba(0,0,0,.30);

      --btn:#111827; --btnText:#ffffff;
      --btn2Bg:rgba(255,255,255,.06); --btn2Text:#ffffff; --btn2Border:rgba(255,255,255,.14);

      --homeBg:#2563eb; --homeText:#ffffff;
      --dangerBg:#ef4444; --dangerText:#ffffff;

      --okBg:rgba(34,197,94,.14); --okBorder:rgba(34,197,94,.35); --okText:#ffffff;
      --errBg:rgba(239,68,68,.14); --errBorder:rgba(239,68,68,.35); --errText:#ffffff;

      --tableBg:#0b1327;
      --tableHead:rgba(255,255,255,.07);
      --tableBorder:rgba(255,255,255,.10);
      --tableHover:rgba(255,255,255,.05);

      --inputBg:#0b1327;
      --inputText:#e5e7eb;
      --focusRing:rgba(99,102,241,.20);
    }

    html[data-theme="dark"]{ /* داكن */
      --bg:#0b1220;
      --card:#0f172a;
      --text:#ffffff;
      --muted:#cbd5e1;
      --border:rgba(255,255,255,.16);
      --shadow:0 16px 40px rgba(0,0,0,.40);

      --btn:#111827; --btnText:#ffffff;
      --btn2Bg:rgba(255,255,255,.08); --btn2Text:#ffffff; --btn2Border:rgba(255,255,255,.20);

      --homeBg:#2563eb; --homeText:#ffffff;
      --dangerBg:#ef4444; --dangerText:#ffffff;

      --okBg:rgba(34,197,94,.14); --okBorder:rgba(34,197,94,.35); --okText:#ffffff;
      --errBg:rgba(239,68,68,.14); --errBorder:rgba(239,68,68,.35); --errText:#ffffff;

      --tableBg:#0b1327;
      --tableHead:rgba(255,255,255,.10);
      --tableBorder:rgba(255,255,255,.18);
      --tableHover:rgba(255,255,255,.06);

      --inputBg:#0b1327;
      --inputText:#ffffff;
      --focusRing:rgba(99,102,241,.22);
    }

    *{box-sizing:border-box}
    body{
      font-family:Arial, sans-serif;
      margin:32px;
      background:var(--bg);
      color:var(--text);
    }
    .container{max-width:1200px;margin:0 auto}

    .bar{display:flex;justify-content:space-between;align-items:flex-start;gap:12px;margin-bottom:16px;flex-wrap:wrap}
    .title{font-weight:900;font-size:18px}
    .subtitle{color:var(--muted);font-size:13px;margin-top:4px}

    .navRow{
      display:flex;
      gap:8px;
      flex-wrap:nowrap;
      overflow:auto;
      max-width:100%;
      padding-bottom:6px;
      -webkit-overflow-scrolling:touch;
      align-items:center;
    }
    .navRow > *{flex:0 0 auto; white-space:nowrap;}

    .btn{
      padding:10px 12px;border-radius:12px;background:var(--btn);color:var(--btnText);
      border:0;cursor:pointer;font-weight:900;font-size:13px;line-height:1;
    }
    .btn:hover{opacity:.95}

    .btn2{
      padding:10px 12px;border-radius:12px;background:var(--btn2Bg);color:var(--btn2Text);
      border:1px solid var(--btn2Border);cursor:pointer;text-decoration:none;
      font-weight:900;font-size:13px;line-height:1;display:inline-flex;align-items:center;gap:8px;
    }

    .active{background:#111827;color:#fff;border-color:#111827}
    html[data-theme="dim"] .active,
    html[data-theme="dark"] .active{
      background:rgba(37,99,235,.22);
      border-color:rgba(37,99,235,.55);
      color:#fff;
    }

    .btnHome{
      padding:10px 12px;border-radius:12px;background:var(--homeBg);color:var(--homeText);
      border:1px solid rgba(0,0,0,.08);cursor:pointer;text-decoration:none;
      font-weight:900;font-size:13px;line-height:1;display:inline-flex;align-items:center;gap:8px;
    }

    .btnDanger{
      padding:10px 12px;border-radius:12px;background:var(--dangerBg);color:var(--dangerText);
      border:0;cursor:pointer;font-weight:900;font-size:13px;line-height:1;
    }

    /* ✅ زر واحد فقط للثيم */
    .themeBtn{display:inline-flex;align-items:center;gap:8px}

    .card{
      background:var(--card);border:1px solid var(--border);border-radius:14px;
      box-shadow:var(--shadow);padding:16px;margin-bottom:14px;
    }

    .alert{padding:12px;border-radius:12px;border:1px solid;font-weight:900;margin-bottom:14px}
    .alert.ok{background:var(--okBg);border-color:var(--okBorder);color:var(--okText)}
    .alert.err{background:var(--errBg);border-color:var(--errBorder);color:var(--errText)}

    label{display:block;font-size:12px;color:var(--muted);font-weight:900;margin-bottom:6px}
    input,select{
      width:100%;
      padding:11px 12px;
      border:1px solid var(--border);
      border-radius:12px;
      outline:none;
      font-size:14px;
      background:var(--inputBg);
      color:var(--inputText);
    }
    html[data-theme="light"] input,
    html[data-theme="light"] select{background:#fff;color:#111827}

    input:focus,select:focus{
      box-shadow:0 0 0 3px var(--focusRing);
    }

    .grid{
      display:grid;
      grid-template-columns: 1fr 1fr 1fr 1fr 0.7fr;
      gap:12px;
      align-items:end;
    }
    @media (max-width: 980px){ body{margin:18px} .grid{grid-template-columns: 1fr 1fr;} }
    @media (max-width: 560px){ .grid{grid-template-columns: 1fr;} }

    .hr{height:1px;background:var(--border);margin:14px 0}

    .kpiGrid{
      display:grid;
      grid-template-columns: repeat(4, 1fr);
      gap:10px;
      margin-top:12px;
    }
    @media (max-width: 980px){ .kpiGrid{grid-template-columns: repeat(2, 1fr);} }
    @media (max-width: 560px){ .kpiGrid{grid-template-columns: 1fr;} }

    .kpi{border:1px solid var(--border);border-radius:14px;padding:12px;background:var(--tableBg)}
    .kpi .t{color:var(--muted);font-size:12px;font-weight:900}
    .kpi .v{margin-top:8px;font-size:16px;font-weight:900;color:var(--text)}

    .lineGrid{
      display:grid;
      grid-template-columns: 1.6fr 0.5fr 0.6fr auto;
      gap:10px;
      align-items:end;
      margin-top:10px;
    }
    @media (max-width: 980px){ .lineGrid{grid-template-columns: 1fr 1fr;} }
    @media (max-width: 560px){ .lineGrid{grid-template-columns: 1fr;} }

    .small{color:var(--muted);font-size:12px;font-weight:800}

    .tableWrap{
      overflow:auto;
      border-radius:12px;
      border:1px solid var(--tableBorder);
      margin-top:10px;
      background:var(--tableBg);
    }
    table{width:100%;border-collapse:collapse;min-width:1050px;background:var(--tableBg)}
    th,td{
      padding:10px 12px;
      border-bottom:1px solid var(--tableBorder);
      text-align:right;
      vertical-align:middle;
      color:var(--text);
    }
    th{background:var(--tableHead);font-weight:900;font-size:14px;white-space:nowrap}
    td{font-size:13px}
    tbody tr:hover td{background:var(--tableHover)}
    .num{font-weight:900}

    .rowActions{display:flex;gap:8px;flex-wrap:wrap;justify-content:flex-end;margin-top:12px}

    .miniDanger{
      background:var(--dangerBg);color:#fff;border:0;
      padding:8px 10px;border-radius:12px;cursor:pointer;font-weight:900;font-size:12px;
    }
  </style>

  <!-- ✅ Apply saved theme ASAP -->
  <script>
    (function(){
      const key = "ui_theme_3";
      const saved = localStorage.getItem(key);
      const t = (saved === "dim" || saved === "dark") ? saved : "light";
      document.documentElement.setAttribute("data-theme", t);
    })();
  </script>
</head>

<body>
<div class="container">

  <div class="bar">
    <div>
      <div class="title">المشتريات</div>
      <div class="subtitle">فاتورة واحدة = عدة أصناف</div>
    </div>

    <div class="navRow">
      <a class="btnHome" href="<?php echo e(route('dashboard')); ?>">الرئيسية</a>

      <a class="btn2" href="<?php echo e(route('dashboard.sales')); ?>">المبيعات</a>
      <a class="btn2 active" href="<?php echo e(route('purchases.index')); ?>">المشتريات</a>
      <a class="btn2" href="<?php echo e(route('expenses.index')); ?>">المصروفات</a>
      <a class="btn2" href="<?php echo e(route('dashboard.reports')); ?>">التقارير</a>
      <a class="btn2" href="<?php echo e(route('settings.index')); ?>">الإعدادات</a>

      <?php if(auth()->check() && (auth()->user()->role ?? 'user') === 'admin'): ?>
        <a class="btn2" href="<?php echo e(route('branches.index')); ?>">إدارة الفروع</a>
        <a class="btn2" href="<?php echo e(route('users.index')); ?>">إدارة المستخدمين</a>
      <?php endif; ?>

      <!-- ✅ زر واحد فقط يتبدّل (فاتح → مريح → داكن) -->
      <button class="btn2 themeBtn" type="button" id="themeToggle">
        <span id="themeIcon">☀️</span>
        <span id="themeText">فاتح</span>
      </button>

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

  <div class="card">
    <div class="navRow">
      <a class="btn2 active" href="<?php echo e(route('purchases.index', ['branch_id' => $branchId])); ?>">إضافة فاتورة</a>
      <a class="btn2" href="<?php echo e(route('purchases.invoices', ['branch_id' => $branchId])); ?>">فواتير المشتريات</a>
    </div>
  </div>

  <?php if(session('ok')): ?>    <div class="alert ok"><?php echo e(session('ok')); ?></div> <?php endif; ?>
  <?php if(session('error')): ?> <div class="alert err"><?php echo e(session('error')); ?></div> <?php endif; ?>
  <?php if($errors->any()): ?>   <div class="alert err"><?php echo e($errors->first()); ?></div> <?php endif; ?>

  <div class="card">
    <div style="font-weight:900;margin-bottom:10px">بيانات الفاتورة</div>

    <form method="POST" action="<?php echo e(route('purchases.store')); ?>" id="purchaseForm">
      <?php echo csrf_field(); ?>

      <div class="grid">
        <div>
          <label>الفرع</label>
          <select name="branch_id" id="branch_id" required>
            <option value="">اختر</option>
            <?php $__currentLoopData = $branches; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $b): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
              <option value="<?php echo e($b->id); ?>" <?php if((int)old('branch_id', $branchId) === (int)$b->id): echo 'selected'; endif; ?>>
                <?php echo e($b->branch_no); ?> - <?php echo e($b->name); ?>

              </option>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
          </select>
        </div>

        <div>
          <label>المورد</label>
          <select name="supplier_id" id="supplier_id" required>
            <option value="">اختر</option>
            <?php $__currentLoopData = $suppliers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $s): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
              <option value="<?php echo e($s->id); ?>" <?php if((int)old('supplier_id') === (int)$s->id): echo 'selected'; endif; ?>>
                <?php echo e($s->name); ?>

              </option>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
          </select>
        </div>

        <div>
          <label>رقم فاتورة المورد (إجباري)</label>
          <input type="text" name="supplier_bill_no" id="supplier_bill_no" value="<?php echo e(old('supplier_bill_no')); ?>" required>
        </div>

        <div>
          <label>تاريخ فاتورة المورد</label>
          <input type="date" name="bill_date" id="bill_date" value="<?php echo e(old('bill_date')); ?>">
        </div>

        <div>
          <label>الضريبة %</label>
          <input type="number" step="0.01" name="vat_rate" id="vat_rate" value="<?php echo e(old('vat_rate', 15)); ?>" required>
        </div>
      </div>

      <div class="kpiGrid">
        <div class="kpi"><div class="t">عدد الأصناف</div><div class="v"><span id="kpi_items_count">0</span></div></div>
        <div class="kpi"><div class="t">الإجمالي قبل الضريبة</div><div class="v"><span id="kpi_subtotal">0.00</span></div></div>
        <div class="kpi"><div class="t">قيمة الضريبة</div><div class="v"><span id="kpi_vat">0.00</span></div></div>
        <div class="kpi"><div class="t">الإجمالي شامل</div><div class="v"><span id="kpi_total">0.00</span></div></div>
      </div>

      <div class="hr"></div>

      <div style="display:flex;justify-content:space-between;align-items:center;gap:10px;flex-wrap:wrap">
        <div style="font-weight:900;">إضافة صنف للفاتورة</div>
        <div class="small">السعر المدخل: شامل الضريبة</div>
      </div>

      <div class="lineGrid">
        <div>
          <label>الصنف</label>
          <select id="line_item_id">
            <option value="">اختر</option>
            <?php $__currentLoopData = $items; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $it): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
              <option value="<?php echo e($it->id); ?>" data-default-gross="<?php echo e((float)($it->default_price_gross ?? 0)); ?>">
                <?php echo e($it->name); ?>

              </option>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
          </select>
        </div>

        <div>
          <label>الكمية</label>
          <input type="number" step="0.01" min="0.01" id="line_qty" value="1">
        </div>

        <div>
          <label>سعر شامل الضريبة</label>
          <input type="number" step="0.01" min="0" id="line_price_gross">
        </div>

        <div style="display:flex;gap:8px;flex-wrap:wrap;justify-content:flex-end">
          <button class="btn2" type="button" id="addLineBtn">إضافة</button>
          <button class="btn2" type="button" id="clearLinesBtn">مسح الأصناف</button>
        </div>
      </div>

      <div class="tableWrap">
        <table id="itemsTable">
          <thead>
            <tr>
              <th>#</th>
              <th>الصنف</th>
              <th>الكمية</th>
              <th>سعر شامل</th>
              <th>قبل الضريبة</th>
              <th>الضريبة</th>
              <th>الإجمالي</th>
              <th>حذف</th>
            </tr>
          </thead>
          <tbody id="itemsTbody">
            <tr id="emptyRow">
              <td colspan="8" style="text-align:center;color:var(--muted);font-weight:900;padding:16px">
                لم يتم إضافة أصناف بعد
              </td>
            </tr>
          </tbody>
        </table>
      </div>

      <!-- ✅ رسالة التحقق (تظهر فقط عند محاولة الحفظ بدون أصناف) -->
      <div id="formMsg" class="alert err" style="display:none">أضف صنف واحد على الأقل</div>

      <div class="rowActions">
        <button class="btn" type="submit">حفظ كامل الفاتورة</button>
      </div>

      <div class="small" style="margin-top:10px">أضف صنف واحد على الأقل</div>
    </form>
  </div>

</div>

<script>
/* ===== Theme Toggle (زر واحد) ===== */
(function(){
  const key = "ui_theme_3";
  const order = ["light","dim","dark"];
  const meta = {
    light: { icon:"☀️", text:"فاتح" },
    dim:   { icon:"🌙", text:"مريح" },
    dark:  { icon:"🌑", text:"داكن" }
  };

  const html = document.documentElement;
  const btn  = document.getElementById("themeToggle");
  const ic   = document.getElementById("themeIcon");
  const tx   = document.getElementById("themeText");

  function apply(t){
    html.setAttribute("data-theme", t);
    localStorage.setItem(key, t);
    if (ic) ic.textContent = meta[t].icon;
    if (tx) tx.textContent = meta[t].text;
  }

  const saved = localStorage.getItem(key);
  apply(order.includes(saved) ? saved : (html.getAttribute("data-theme") || "light"));

  if(btn){
    btn.addEventListener("click", function(){
      const cur = html.getAttribute("data-theme") || "light";
      const idx = order.indexOf(cur);
      const next = order[(idx + 1) % order.length];
      apply(next);
    });
  }
})();

/* ===== Purchases Logic (كما هو) ===== */
(function(){
  const form = document.getElementById('purchaseForm');
  const tbody = document.getElementById('itemsTbody');
  const vatRateEl = document.getElementById('vat_rate');

  const lineItemEl = document.getElementById('line_item_id');
  const lineQtyEl = document.getElementById('line_qty');
  const linePriceEl = document.getElementById('line_price_gross');

  const addLineBtn = document.getElementById('addLineBtn');
  const clearLinesBtn = document.getElementById('clearLinesBtn');

  const kpiCount = document.getElementById('kpi_items_count');
  const kpiSubtotal = document.getElementById('kpi_subtotal');
  const kpiVat = document.getElementById('kpi_vat');
  const kpiTotal = document.getElementById('kpi_total');

  const formMsg = document.getElementById('formMsg');

  let idx = 0;

  function num(v){ v = parseFloat(v); return isNaN(v) ? 0 : v; }
  function round2(v){ return Math.round((v + Number.EPSILON) * 100) / 100; }
  function fmt(v){ return round2(v).toFixed(2); }
  function vatDiv(){ const r = num(vatRateEl.value); return 1 + (r / 100); }

  function computeLine(qty, priceGross){
    const div = vatDiv();
    const lineTotal = qty * priceGross;
    const lineSub = div > 0 ? (lineTotal / div) : lineTotal;
    const lineVat = lineTotal - lineSub;
    return { sub: round2(lineSub), vat: round2(lineVat), total: round2(lineTotal) };
  }

  function renumber(){
    const rows = tbody.querySelectorAll('tr[data-row="1"]');
    rows.forEach((tr, i) => tr.querySelector('.cell-no').textContent = String(i+1));
  }

  function recalcAll(){
    let count=0, subtotal=0, vat=0, total=0;
    const rows = tbody.querySelectorAll('tr[data-row="1"]');
    rows.forEach(tr=>{
      count++;
      subtotal += num(tr.querySelector('.cell-sub').textContent);
      vat      += num(tr.querySelector('.cell-vat').textContent);
      total    += num(tr.querySelector('.cell-total').textContent);
    });
    kpiCount.textContent = String(count);
    kpiSubtotal.textContent = fmt(subtotal);
    kpiVat.textContent = fmt(vat);
    kpiTotal.textContent = fmt(total);
  }

  function hasLines(){
    return tbody.querySelectorAll('tr[data-row="1"]').length > 0;
  }

  function showMsg(){
    if (!formMsg) return;
    formMsg.textContent = 'أضف صنف واحد على الأقل';
    formMsg.style.display = 'block';
    formMsg.scrollIntoView({ behavior: 'smooth', block: 'start' });
  }

  function hideMsg(){
    if (!formMsg) return;
    formMsg.style.display = 'none';
  }

  function addLine(){
    const itemId = lineItemEl.value;
    const opt = lineItemEl.options[lineItemEl.selectedIndex];
    const itemName = opt ? opt.text : '';

    const qty = num(lineQtyEl.value);
    const priceGross = num(linePriceEl.value);

    if (!itemId) { alert('اختر الصنف'); return; }
    if (qty <= 0) { alert('الكمية غير صحيحة'); return; }
    if (priceGross < 0) { alert('السعر غير صحيح'); return; }

    hideMsg();

    const emptyRow = document.getElementById('emptyRow');
    if (emptyRow) emptyRow.remove();

    const c = computeLine(qty, priceGross);
    const tr = document.createElement('tr');
    tr.dataset.row = '1';

    tr.innerHTML = `
      <td class="num cell-no">0</td>
      <td style="font-weight:900">
        ${escapeHtml(itemName)}
        <input type="hidden" name="items[${idx}][item_id]" value="${escapeAttr(itemId)}">
      </td>
      <td><input type="number" step="0.01" min="0.01" name="items[${idx}][qty]" value="${fmt(qty)}" class="qtyInput" style="width:120px"></td>
      <td><input type="number" step="0.01" min="0" name="items[${idx}][price_gross]" value="${fmt(priceGross)}" class="priceInput" style="width:140px"></td>
      <td class="num cell-sub">${fmt(c.sub)}</td>
      <td class="num cell-vat">${fmt(c.vat)}</td>
      <td class="num cell-total">${fmt(c.total)}</td>
      <td><button type="button" class="miniDanger">حذف</button></td>
    `;

    tr.querySelector('.miniDanger').addEventListener('click', ()=>{
      tr.remove();
      if (tbody.querySelectorAll('tr[data-row="1"]').length === 0) {
        tbody.innerHTML = `<tr id="emptyRow"><td colspan="8" style="text-align:center;color:var(--muted);font-weight:900;padding:16px">لم يتم إضافة أصناف بعد</td></tr>`;
        idx = 0;
      }
      renumber();
      recalcAll();
    });

    const qtyInput = tr.querySelector('.qtyInput');
    const priceInput = tr.querySelector('.priceInput');

    function recalcRow(){
      const q = num(qtyInput.value);
      const p = num(priceInput.value);
      const cc = computeLine(q, p);
      tr.querySelector('.cell-sub').textContent = fmt(cc.sub);
      tr.querySelector('.cell-vat').textContent = fmt(cc.vat);
      tr.querySelector('.cell-total').textContent = fmt(cc.total);
      recalcAll();
    }
    qtyInput.addEventListener('input', recalcRow);
    priceInput.addEventListener('input', recalcRow);

    tbody.appendChild(tr);
    idx++;
    renumber();
    recalcAll();

    lineItemEl.value = '';
    lineQtyEl.value = '1';
    linePriceEl.value = '';
  }

  lineItemEl.addEventListener('change', ()=>{
    const opt = lineItemEl.options[lineItemEl.selectedIndex];
    const def = opt ? num(opt.getAttribute('data-default-gross')) : 0;
    if (def > 0 && (linePriceEl.value === '' || num(linePriceEl.value) === 0)) {
      linePriceEl.value = def.toFixed(2);
    }
  });

  addLineBtn.addEventListener('click', addLine);

  clearLinesBtn.addEventListener('click', ()=>{
    if (!confirm('مسح كل الأصناف من الفاتورة؟')) return;
    tbody.innerHTML = `<tr id="emptyRow"><td colspan="8" style="text-align:center;color:var(--muted);font-weight:900;padding:16px">لم يتم إضافة أصناف بعد</td></tr>`;
    idx = 0;
    recalcAll();
  });

  vatRateEl.addEventListener('input', ()=>{
    const rows = tbody.querySelectorAll('tr[data-row="1"]');
    rows.forEach(tr=>{
      const q = num(tr.querySelector('.qtyInput').value);
      const p = num(tr.querySelector('.priceInput').value);
      const cc = computeLine(q, p);
      tr.querySelector('.cell-sub').textContent = fmt(cc.sub);
      tr.querySelector('.cell-vat').textContent = fmt(cc.vat);
      tr.querySelector('.cell-total').textContent = fmt(cc.total);
    });
    recalcAll();
  });

  form.addEventListener('submit', function(e){
    if (!hasLines()){
      e.preventDefault();
      showMsg();
    } else {
      hideMsg();
    }
  });

  function escapeHtml(str){
    return String(str ?? '')
      .replaceAll('&','&amp;')
      .replaceAll('<','&lt;')
      .replaceAll('>','&gt;')
      .replaceAll('"','&quot;')
      .replaceAll("'","&#039;");
  }
  function escapeAttr(str){ return escapeHtml(str).replaceAll('`','&#096;'); }

  recalcAll();
})();
</script>

</body>
</html>
<?php /**PATH /home/ksaad26/laravel/laundry-dashboard/resources/views/purchases.blade.php ENDPATH**/ ?>