<?php $androidProducts=[]; $productRs=$conn->query("SELECT id,name,price FROM shop_products ORDER BY name ASC"); if($productRs) while($product=$productRs->fetch_assoc()) $androidProducts[]=$product; ?>
<div class="content-wrapper android-sales-new"><main class="asn-main"><header class="asn-heading"><div><span>New Sale</span><h1>عملية بيع جديدة</h1><p>اختر المنتجات والكميات ثم راجع الإجمالي قبل إنشاء الفاتورة.</p></div><a href="sales.php"><i class="fa fa-arrow-right"></i><b>رجوع</b></a></header><nav class="asn-nav"><a href="sales.php"><i class="fa fa-home"></i><span>الرئيسية</span></a><a class="is-active" href="sales_new.php"><i class="fa fa-cart-plus"></i><span>بيع جديد</span></a><a href="sales_products.php"><i class="fa fa-cubes"></i><span>الأصناف</span></a><a href="sales_summary.php"><i class="fa fa-bar-chart"></i><span>الملخص</span></a></nav><?php if($err): ?><div class="asn-message"><i class="fa fa-exclamation-triangle"></i><?= h($err) ?></div><?php endif; ?><form method="post" class="asn-form" data-ua-android-native-layout><section class="asn-section"><div class="asn-head"><div><span>Customer</span><h2>بيانات العميل</h2></div><i class="fa fa-user"></i></div><div class="asn-pair"><label><span>اسم العميل</span><input name="customer_name"></label><label><span>هاتف العميل</span><input name="customer_phone" inputmode="tel"></label></div></section><section class="asn-section"><div class="asn-head"><div><span>Sale Items</span><h2>عناصر الفاتورة</h2></div><button type="button" id="asnAdd"><i class="fa fa-plus"></i> عنصر</button></div><div id="asnItems" class="asn-items"></div></section><section class="asn-total"><span>الإجمالي</span><strong id="asnGrand" dir="ltr">0.00</strong></section><button class="asn-submit" name="create" value="1"><i class="fa fa-save"></i> إنشاء فاتورة المبيعات</button></form></main></div>
<template id="asnTemplate"><article class="asn-item"><header><span>عنصر فاتورة</span><button type="button" class="asnRemove"><i class="fa fa-times"></i></button></header><label><span>المنتج</span><select class="asnProduct"><option value="0" data-price="0">اختر منتجاً</option><?php foreach($androidProducts as $product): ?><option value="<?= (int)$product['id'] ?>" data-price="<?= h(number_format((float)$product['price'],2,'.','')) ?>"><?= h($product['name']) ?></option><?php endforeach; ?></select></label><div class="asn-item-data"><label><span>الكمية</span><input class="asnQty" type="number" value="1" min="1" inputmode="numeric"></label><div><span>السعر</span><strong class="asnUnit" dir="ltr">0.00</strong></div><div><span>الإجمالي</span><strong class="asnLine" dir="ltr">0.00</strong></div></div></article></template>
<script>(function(){const host=document.getElementById('asnItems'),template=document.getElementById('asnTemplate'),grand=document.getElementById('asnGrand');function reindex(){host.querySelectorAll('.asn-item').forEach((row,index)=>{row.querySelector('.asnProduct').name=`item[${index}][pid]`;row.querySelector('.asnQty').name=`item[${index}][qty]`;});}function calc(){let total=0;host.querySelectorAll('.asn-item').forEach(row=>{const select=row.querySelector('.asnProduct'),price=parseFloat(select.selectedOptions[0]?.dataset.price||0),qty=parseInt(row.querySelector('.asnQty').value||0,10)||0,line=price*qty;row.querySelector('.asnUnit').textContent=price.toFixed(2);row.querySelector('.asnLine').textContent=line.toFixed(2);total+=line;});grand.textContent=total.toFixed(2);}function add(){const row=template.content.firstElementChild.cloneNode(true);row.querySelector('.asnRemove').addEventListener('click',()=>{if(host.children.length===1){row.querySelector('.asnProduct').value='0';row.querySelector('.asnQty').value='1';}else row.remove();reindex();calc();});row.querySelector('.asnProduct').addEventListener('change',calc);row.querySelector('.asnQty').addEventListener('input',calc);host.appendChild(row);reindex();calc();}document.getElementById('asnAdd').addEventListener('click',add);add();})();</script>
