${typeBtn("Hybrid","add")}
`);
window._addType="Hybrid";
}
async function doAddStock(){
const name=document.getElementById("a-name").value;
const price=document.getElementById("a-price").value;
const qty=document.getElementById("a-qty").value;
const alert=document.getElementById("a-alert").value;
if(!name||!price||!qty)return;
await sb.from("stock").insert({name,type:window._addType||"Hybrid",price:+price,qty:+qty,alert:+alert||10});
addHist(`เพิ่มสินค้า ${name} (${qty}g)`,"action");
addLog("เพิ่มสินค้า",`${S.user.name} เพิ่ม ${name} ${qty}g ฿${price}`);
hideModal();
}
function showEditStock(id){
const item=S.stock.find(s=>s.id===id);if(!item)return;
window._editType=item.type;
showModal(``);
}
async function doEditStock(id){
const item=S.stock.find(s=>s.id===id);if(!item)return;
const name=document.getElementById("e-name").value;
const price=+document.getElementById("e-price").value;
const qty=+document.getElementById("e-qty").value;
const alert=+document.getElementById("e-alert").value;
await sb.from("stock").update({name,type:window._editType||item.type,price,qty,alert}).eq("id",id);
if(item.price!==price)addLog("แก้ไขราคา",`${S.user.name} เปลี่ยนราคา ${name} ฿${item.price}→฿${price}`);
if(item.qty!==qty)addLog("แก้ไขสต็อก",`${S.user.name} แก้ไขสต็อก ${name} ${item.qty}g→${qty}g`);
addHist(`แก้ไขสินค้า ${name}`,"action");
hideModal();
}
function showRestock(id){
const item=S.stock.find(s=>s.id===id);if(!item)return;
showModal(`
เติมสต็อก: ${esc(item.name)}
ปัจจุบัน: ${item.qty}g
`);
}
function updRsPrev(cur){const v=+document.getElementById("rs-qty").value;const p=document.getElementById("rs-prev");if(v>0){p.textContent=`สต็อกใหม่: ${cur+v}g`;p.style.display="block";}else p.style.display="none";}
async function doRestock(id,cur,name){
const qty=+document.getElementById("rs-qty").value;if(!qty||qty<=0)return;
await sb.from("stock").update({qty:cur+qty}).eq("id",id);
addHist(`เติมสต็อก ${name} +${qty}g`,"action");
addLog("เติมสต็อก",`${S.user.name} เติม ${name} +${qty}g`);
hideModal();
}
function confirmDel(id){
const item=S.stock.find(s=>s.id===id);if(!item)return;
showModal(`
ลบ "${esc(item.name)}"?
การกระทำนี้ไม่สามารถย้อนกลับได้
`);