PageSourceSearch

https://tsofit.org.il/assets/CommunityPubModule-DTD_UWe5.js

js tsofit.org.il collected 2026-09-25 02:14:13 UTC 115,101 bytes, 4 lines download raw bytes

1import{j as e,r as m,d as $,z as T,aJ as be,dy as fe,P as Z,_ as pe,x as de,aT as se,N as Me,ar as ge,y as je,B as F,aM as Xe,ad as Je,X as H,T as re,b as z,aA as I,aF as Qe,aU as Le,aH as Ze}from"./index-BuF13C9R.js";import{f as ze}from"./money-CmD9E_V-.js";import{K as et}from"./KibbutzModuleGuard-ervrt65z.js";import{P as tt}from"./PageTitleMenu-DlabIv_d.js";import{S as G}from"./switch-9kzbE2Dl.js";import{B as st}from"./BudgetItemSelect-D9zLjNZr.js";import{T as at}from"./trending-up-BxObsxzL.js";import{C as Ee}from"./calendar-clock-Dg6I_-A7.js";import{T as _e}from"./truck-Vc5srr9S.js";import{I as ve}from"./image-BzvPncEw.js";import{E as rt}from"./eye-off-lYYIvXNt.js";import{P as ae}from"./pencil-CoE4mwuD.js";import{C as lt}from"./copy-BClGV4nG.js";import{S as we}from"./share-2-BamByTGH.js";import{G as nt}from"./gift-BYx8vw-x.js";import{B as Ae}from"./boxes-DUYsLADb.js";import{R as it}from"./refresh-cw-DvPS6Mu1.js";import{C as qe}from"./chevron-left-D2IUBEPa.js";import{B as ke}from"./ban-BrNeMJoo.js";import{U as Ne}from"./upload-DeFZlL4S.js";import{K as dt}from"./key-round-BTTGfzC9.js";import"./useActionVisibility-DLgqAw2q.js";import"./index-BGaOrCq_.js";import"./formStyles-CzLsWpb8.js";const te=t=>t==null||t===""?"":String(t/100),W=t=>t===""||t==null?null:Math.round(Number(t)*100),B=t=>{if(t==null||t==="")return"—";const a=t/100;return ze(a)},k="w-full h-11 rounded-xl border border-border bg-white px-3 text-sm text-right focus:outline-none focus:border-amber-400",ee=t=>{if(!t.track_inventory)return{key:"untracked",label:"לא מנוהל",badge:"bg-slate-100 text-slate-500"};const a=Number(t.stock_quantity)||0;return a<=0?{key:"out",label:"אזל",badge:"bg-rose-50 text-rose-600"}:a<=(Number(t.minimum_stock)||0)?{key:"low",label:"מלאי נמוך",badge:"bg-amber-50 text-amber-700"}:{key:"in",label:"במלאי",badge:"bg-emerald-50 text-emerald-600"}},Q=t=>{if(!t)return!1;const a=new Date(t),b=new Date;return a.getFullYear()===b.getFullYear()&&a.getMonth()===b.getMonth()&&a.getDate()===b.getDate()},ie=t=>{if(!t)return!1;const a=new Date(t),b=new Date;return a.getFullYear()===b.getFullYear()&&a.getMonth()===b.getMonth()},ot={name:"",description:"",image_url:"",category_id:"",supplier_id:"",sku:"",barcode:"",sale_price:"",cost_price:"",track_inventory:!0,stock_quantity:"0",minimum_stock:"0",maximum_quantity_per_order:"",display_order:"0",is_active:!0,is_visible_in_menu:!0,is_featured:!1},ne={name:"",icon:"",image_url:"",description:"",display_order:"0",is_active:!0},Ce={type:"supplier",company_name:"",contact_name:"",phone:"",email:"",address:"",website:"",payment_terms:"",business_number:"",notes:"",is_active:!0};function ct({kibbutzId:t}){const[a,b]=m.useState([]),[j,C]=m.useState([]),[n,f]=m.useState([]),[r,P]=m.useState([]),[D,p]=m.useState([]),[l,d]=m.useState([]),[h,o]=m.useState([]),[S,A]=m.useState([]),[v,g]=m.useState(null),[s,_]=m.useState([]),[E,O]=m.useState(!0),[i,w]=m.useState(!1),[c,N]=m.useState("products"),[u,L]=m.useState(""),[M,U]=m.useState({category:"all",supplier:"all",status:"all",stock:"all",menu:"all"}),[X,K]=m.useState(null),[Fe,le]=m.useState(!1),[oe,ce]=m.useState(null),V=m.useCallback(async()=>{O(!0);try{const[y,x,q,Y,J,We,He,Ke,ye,Ie]=await Promise.all([$.entities.PubProduct.filter({kibbutz_id:t},"display_order",1e3).catch(()=>[]),$.entities.PubCategory.filter({kibbutz_id:t},"display_order",500).catch(()=>[]),$.entities.PubSupplier.filter({kibbutz_id:t},"-created_date",500).catch(()=>[]),$.entities.PubOrder.filter({kibbutz_id:t},"-created_date",1e3).catch(()=>[]),$.entities.PubPayment.filter({kibbutz_id:t},"-created_date",1e3).catch(()=>[]),$.entities.PubWallet.filter({kibbutz_id:t},"-created_date",1e3).catch(()=>[]),$.entities.PubShift.filter({kibbutz_id:t},"-start_at",500).catch(()=>[]),$.entities.PubCardPackage.filter({kibbutz_id:t},"display_order",200).catch(()=>[]),$.entities.PubSettings.filter({kibbutz_id:t},"-created_date",1).catch(()=>[]),$.entities.PubOpeningHours.filter({kibbutz_id:t},"day_of_week",20).catch(()=>[])]);b(y||[]),C(x||[]),f(q||[]),P(Y||[]),p(J||[]),d(We||[]),o(He||[]),A(Ke||[]),g(ye&&ye[0]||null),_(Ie||[])}catch(y){console.error("Pub load error:",y),T.error("שגיאה בטעינת נתוני הפאב")}finally{O(!1),w(!0)}},[t]);m.useEffect(()=>{V()},[V]);const me=m.useCallback(y=>{var x;return((x=j.find(q=>q.id===y))==null?void 0:x.name)||"—"},[j]),ue=m.useCallback(y=>{var x;return((x=n.find(q=>q.id===y))==null?void 0:x.company_name)||"—"},[n]),R=m.useMemo(()=>{const y=r.filter(x=>x.status==="supplied");return{orders:{newCount:r.filter(x=>x.status==="new").length,inPrep:r.filter(x=>x.status==="in_preparation").length,ready:r.filter(x=>x.status==="ready").length,suppliedToday:y.filter(x=>Q(x.supplied_at)).length,redemptionToday:y.filter(x=>Q(x.supplied_at)).reduce((x,q)=>x+(Number(q.final_amount_agorot)||0),0)},income:{collectToday:D.filter(x=>x.status==="completed"&&Q(x.processed_at||x.created_date)).reduce((x,q)=>x+(Number(q.amount_paid_agorot)||0),0),collectMonth:D.filter(x=>x.status==="completed"&&ie(x.processed_at||x.created_date)).reduce((x,q)=>x+(Number(q.amount_paid_agorot)||0),0),redeemToday:mt(r),txToday:D.filter(x=>x.status==="completed"&&Q(x.processed_at||x.created_date)).length,openBalance:l.reduce((x,q)=>x+(Number(q.balance_agorot)||0),0)},products:{active:a.filter(x=>x.is_active!==!1).length,low:a.filter(x=>ee(x).key==="low").length,out:a.filter(x=>ee(x).key==="out").length,categories:j.filter(x=>x.is_active!==!1).length},shifts:{active:h.filter(x=>x.start_at&&x.end_at&&new Date(x.start_at)<=new Date&&new Date(x.end_at)>=new Date).length,next:h.filter(x=>x.start_at&&new Date(x.start_at)>new Date).sort((x,q)=>String(x.start_at).localeCompare(String(q.start_at)))[0]||null,unstaffed:h.filter(x=>(x.status==="open"||x.status==="partially_staffed")&&ut(x.start_at)).length}}},[a,j,r,D,l,h]),Oe=async(y,x)=>
1{if(!y.name.trim())return T.error("יש להזין שם מוצר"),!1;if(!y.category_id)return T.error("יש לבחור קטגוריה"),!1;if(y.sale_price==="")return T.error("יש להזין מחיר מכירה"),!1;const q={kibbutz_id:t,name:y.name.trim(),description:y.description||void 0,image_url:y.image_url||void 0,category_id:y.category_id,supplier_id:y.supplier_id||void 0,sku:y.sku||void 0,barcode:y.barcode||void 0,sale_price_agorot:W(y.sale_price),cost_price_agorot:W(y.cost_price),track_inventory:y.track_inventory,stock_quantity:y.stock_quantity!==""?Number(y.stock_quantity):0,minimum_stock:y.minimum_stock!==""?Number(y.minimum_stock):0,maximum_quantity_per_order:y.maximum_quantity_per_order!==""?Number(y.maximum_quantity_per_order):void 0,display_order:y.display_order!==""?Number(y.display_order):0,is_active:y.is_active,is_visible_in_menu:y.is_visible_in_menu,is_featured:y.is_featured};try{return x?await $.entities.PubProduct.update(x,q):await $.entities.PubProduct.create({...q,created_by:"admin"}),T.success(x?"המוצר עודכן":"המוצר נוסף"),K(null),V(),!0}catch(Y){return console.error("Save product error:",Y),T.error("שגיאה בשמירת המוצר"),!1}},Re=async y=>{try{const{id:x,created_date:q,updated_date:Y,...J}=y;await $.entities.PubProduct.create({...J,kibbutz_id:t,name:`${y.name} - עותק`,is_visible_in_menu:!1}),T.success("המוצר שוכפל"),V()}catch{T.error("שגיאה בשכפול")}},Ue=async(y,x)=>{try{await $.entities.PubProduct.update(y.id,{[x]:y[x]===!1?!0:!y[x]}),b(q=>q.map(Y=>Y.id===y.id?{...Y,[x]:Y[x]===!1?!0:!Y[x]}:Y))}catch{T.error("שגיאה בעדכון")}},Ge=async y=>{try{await $.entities.PubProduct.update(y.id,{is_active:!1,is_visible_in_menu:!1}),T.success("המוצר הוסר (סומן כלא פעיל)"),V()}catch{T.error("שגיאה בהסרה")}},Ye=m.useMemo(()=>{const y=u.trim().toLowerCase();return a.filter(x=>{var q,Y,J;return!(y&&!((q=x.name)!=null&&q.toLowerCase().includes(y)||me(x.category_id).toLowerCase().includes(y)||ue(x.supplier_id).toLowerCase().includes(y)||(Y=x.sku)!=null&&Y.toLowerCase().includes(y)||(J=x.barcode)!=null&&J.toLowerCase().includes(y))||M.category!=="all"&&x.category_id!==M.category||M.supplier!=="all"&&x.supplier_id!==M.supplier||M.status==="active"&&x.is_active===!1||M.status==="inactive"&&x.is_active!==!1||M.stock!=="all"&&ee(x).key!==M.stock||M.menu==="in"&&x.is_visible_in_menu===!1||M.menu==="out"&&x.is_visible_in_menu!==!1)})},[a,u,M,me,ue]);if(E&&!i)return e.jsx("div",{className:"flex items-center justify-center h-64",children:e.jsx("div",{className:"w-8 h-8 border-4 border-amber-200 border-t-amber-600 rounded-full animate-spin"})});const Ve=[{key:"orders",label:"הזמנות",icon:be,color:"text-amber-600",bg:"bg-amber-50",ring:"ring-amber-300",lines:[`${R.orders.newCount} חדשות`,`${R.orders.inPrep} בהכנה`,`${R.orders.ready} מוכנות`,`${R.orders.suppliedToday} סופקו היום`],foot:`מומש היום: ${B(R.orders.redemptionToday)}`},{key:"income",label:"הכנסות",icon:at,color:"text-emerald-600",bg:"bg-emerald-50",ring:"ring-emerald-300",lines:[`גבייה היום: ${B(R.income.collectToday)}`,`גבייה החודש: ${B(R.income.collectMonth)}`,`${R.income.txToday} עסקאות היום`],foot:`יתרות פתוחות: ${B(R.income.openBalance)}`},{key:"products",label:"מוצרים",icon:fe,color:"text-blue-600",bg:"bg-blue-50",ring:"ring-blue-300",lines:[`${R.products.active} פעילים`,`${R.products.low} מלאי נמוך`,`${R.products.out} אזלו`],foot:`${R.products.categories} קטגוריות פעילות`},{key:"shifts",label:"משמרות",icon:Ee,color:"text-purple-600",bg:"bg-purple-50",ring:"ring-purple-300",lines:[R.shifts.active?"משמרת פעילה כעת":"אין משמרת פעילה",R.shifts.next?`הבאה: ${new Date(R.shifts.next.start_at).toLocaleDateString("he-IL")}`:"אין משמרת מתוכננת"],foot:`${R.shifts.unstaffed} לא מאוישות השבוע`}];return e.jsxs("div",{dir:"rtl",children:[e.jsx("div",{className:"flex mb-6",children:e.jsx(tt,{title:"פאב קהילתי",items:[{label:"הוספת מוצר",icon:Z,iconBg:"bg-amber-50",iconColor:"text-amber-600",onClick:()=>{N("products"),K({mode:"create"})}},{label:"הוספת קטגוריה",icon:pe,iconBg:"bg-blue-50",iconColor:"text-blue-600",onClick:()=>le(!0)},{label:"ספקים ויצרנים",icon:_e,iconBg:"bg-slate-100",iconColor:"text-slate-600",separatorBefore:!0,onClick:()=>N("suppliers")},{label:"ניהול ברמנים",icon:de,iconBg:"bg-purple-50",iconColor:"text-purple-600",onClick:()=>N("staff")},{label:"מחירון כרטסיות",icon:se,iconBg:"bg-emerald-50",iconColor:"text-emerald-600",onClick:()=>N("card_pricing")},{label:"הגדרות הפאב",icon:Me,iconBg:"bg-slate-100",iconColor:"text-slate-600",onClick:()=>N("settings")}]})}),e.jsx("div",{className:"grid grid-cols-2 lg:grid-cols-4 gap-3 mb-6",children:Ve.map(y=>{const x=y.icon,q=c===y.key;return e.jsxs("button",{onClick:()=>N(y.key),className:`bg-white rounded-2xl border shadow-sm p-4 text-right transition-all hover:shadow-md ${q?`ring-2 ${y.ring} border-transparent`:"border-border hover:border-slate-300"}`,children:[e.jsxs("div",{className:"flex items-center justify-between mb-2",children:[e.jsx("div",{className:`w-9 h-9 rounded-lg ${y.bg} flex items-center justify-center`,children:e.jsx(x,{className:`w-5 h-5 ${y.color}`})}),e.jsx("span",{className:"text-sm font-bold text-foreground",children:y.label})]}),e.jsx("div",{className:"space-y-0.5",children:y.lines.map((Y,J)=>e.jsx("p",{className:"text-xs text-muted-foreground",children:Y},J))}),e.jsx("p",{className:"text-xs font-semibold text-slate-700 mt-2 pt-2 border-t border-slate-100",children:y.foot})]},y.key)})}),c==="products"&&e.jsxs(e.Fragment,{children:[e.jsx(Ot,{products:a,settings:v}),e.jsx(xt,{products:Ye,allCount:a.length,categories:j,suppliers:n,search:u,setSearch:L,filters:M,setFilters:U,catName:me,supName:ue,onAdd:()=>K({mode:"create"}),onManageCategories:()=>le(!0),onEdit:y=>K({mode:"edit",product:y}),onDuplicate:Re,onToggle:Ue,onDelete:Ge})]}),c==="suppliers"&&e.jsx(ft,{suppliers:n,products:a,onAdd:()=>ce({mode:"create"}),onEdit:y=>ce({mode:"edit",supplier:y}),onRefresh:V}),c==="staff"&&e.jsx($t,{kibbutzId:t,onBack:()=>N("products")}),c==="card_pricing"&&e.jsx(jt,{packages:S,kibbutzId:t,onRefresh:V,onBack:()=>N("products")}),c==="settings"&&e.jsx(Tt,{settings:v,hours:s,kibbutzId:t,onRefresh:V,onBack:()=>N("products")}),c==="orders"&&e.jsx(Rt,{orders:r,kibbutzId:t,onRefresh:V}),c==="income"&&e.jsx(Lt,{payments:D,orders:r,wallets:l,kibbutzId:t}),c==="shifts"&&e.jsx(qt,{kibbutzId:t}),X&&e.jsx(ht,{mode:X.mode,product:X.product,categories:j,suppliers:n,onClose:()=>K(null),onSave:Oe,onNeedCategory:()=>le(!0)}),Fe&&e.jsx(bt,{categories:j,products:a,kibbutzId:t,onClose:()=>le(!1),onRefresh:V}),oe&&e.jsx(pt,{mode:oe.mode,supplier:oe.supplier,products:a,kibbutzId:t,onClose:()=>ce(null),onRefresh:V})]})}function mt(t){return t.filter(a=>a.status==="supplied"&&Q(a.supplied_at)).reduce((a,b)=>a+(Number(b.final_amount_agorot)||0),0)}function ut(t){if(!t)return!1;const a=new Date(t),b=new Date,j=new Date(b);j.setDate(b.getDate()-b.getDay()),j.setHours(0,0,0,0);const C=new Date(j);return C.setDate(j.getDate()+7),a>=j&&a<C}function xt({products:t,allCount:a,categories:b,suppliers:j,search:C,setSearch:n,filters:f,setFilters:r,catName:P,supName:D,onAdd:p,onManageCategories:l,onEdit:d,onDuplicate:h,onToggle:o,onDelete:S}){const[A,v]=m.useState(null),g="h-10 rounded-xl border border-border bg-white px-2 text-sm focus:outline-none focus:border-amber-400";return e.jsxs("div",{children:[e.jsxs("div",{className:"flex flex-wrap items-center gap-2 mb-4",children:[e.jsxs("div",{className:"relative flex-1 min-w-[220px] max-w-md",children:[e.jsx(je,{className:"absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-slate-400"}),e.jsx("input",{value:C,onChange:s=>n(s.target.value),placeholder:"חיפוש שם / קטגוריה / ספק / מק״ט / ברקוד…",className:"w-full h-10 rounded-full border border-slate-200 bg-slate-50 pr-10 pl-4 text-sm focus:outline-none focus:ring-2 focus:ring-amber-100 focus:border-amber-300 focus:bg-white transition-all"})]}),e.jsxs("select",{className:g,value:f.category,onChange:s=>r(_=>({..._,category:s.target.value})),children:[e.jsx("option",{value:"all",children:"כל הקטגוריות"}),b.map(s=>e.jsx("option",{value:s.id,children:s.name},s.id))]}
1),e.jsxs("select",{className:g,value:f.supplier,onChange:s=>r(_=>({..._,supplier:s.target.value})),children:[e.jsx("option",{value:"all",children:"כל הספקים"}),j.map(s=>e.jsx("option",{value:s.id,children:s.company_name},s.id))]}),e.jsxs("select",{className:g,value:f.stock,onChange:s=>r(_=>({..._,stock:s.target.value})),children:[e.jsx("option",{value:"all",children:"כל המלאי"}),e.jsx("option",{value:"in",children:"במלאי"}),e.jsx("option",{value:"low",children:"מלאי נמוך"}),e.jsx("option",{value:"out",children:"אזל"})]}),e.jsxs("select",{className:g,value:f.status,onChange:s=>r(_=>({..._,status:s.target.value})),children:[e.jsx("option",{value:"all",children:"הכל"}),e.jsx("option",{value:"active",children:"פעילים"}),e.jsx("option",{value:"inactive",children:"לא פעילים"})]}),e.jsxs("select",{className:g,value:f.menu,onChange:s=>r(_=>({..._,menu:s.target.value})),children:[e.jsx("option",{value:"all",children:"תפריט (הכל)"}),e.jsx("option",{value:"in",children:"מוצגים בתפריט"}),e.jsx("option",{value:"out",children:"לא בתפריט"})]}),e.jsx("div",{className:"flex items-center gap-2 mr-auto",children:e.jsxs(F,{variant:"outline",onClick:l,className:"rounded-full gap-1.5 h-10",children:[e.jsx(pe,{className:"w-4 h-4"})," קטגוריות"]})})]}),a===0?e.jsxs("div",{className:"bg-white rounded-2xl border border-border shadow-sm p-12 flex flex-col items-center justify-center text-center",children:[e.jsx("div",{className:"w-16 h-16 rounded-full bg-amber-50 flex items-center justify-center mb-3",children:e.jsx(fe,{className:"w-8 h-8 text-amber-300"})}),e.jsx("p",{className:"text-sm text-muted-foreground mb-4",children:"עדיין לא נוספו מוצרים לפאב"}),e.jsxs(F,{onClick:p,className:"rounded-full gap-1.5 bg-amber-600 hover:bg-amber-700",children:[e.jsx(Z,{className:"w-4 h-4"})," הוספת מוצר ראשון"]})]}):t.length===0?e.jsx("div",{className:"bg-white rounded-2xl border border-border shadow-sm p-12 text-center text-sm text-muted-foreground",children:"לא נמצאו מוצרים התואמים לסינון"}):e.jsx("div",{className:"bg-white rounded-2xl border border-border shadow-sm overflow-hidden",children:e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"text-xs text-muted-foreground bg-slate-50 border-b border-border",children:[e.jsx("th",{className:"text-right font-medium px-3 py-2.5",children:"מוצר"}),e.jsx("th",{className:"text-right font-medium px-3 py-2.5",children:"קטגוריה"}),e.jsx("th",{className:"text-right font-medium px-3 py-2.5",children:"ספק"}),e.jsx("th",{className:"text-right font-medium px-3 py-2.5",children:"מכירה"}),e.jsx("th",{className:"text-right font-medium px-3 py-2.5",children:"עלות"}),e.jsx("th",{className:"text-right font-medium px-3 py-2.5",children:"מלאי"}),e.jsx("th",{className:"text-right font-medium px-3 py-2.5",children:"סטטוס"}),e.jsx("th",{className:"text-right font-medium px-3 py-2.5",children:"תפריט"}),e.jsx("th",{className:"text-right font-medium px-3 py-2.5",children:"פעולות"})]})}),e.jsx("tbody",{children:t.map(s=>{const _=ee(s);return e.jsxs("tr",{className:`border-b border-slate-50 hover:bg-slate-50/60 ${s.is_active===!1?"opacity-60":""}`,children:[e.jsx("td",{className:"px-3 py-2.5",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("div",{className:"w-9 h-9 rounded-lg bg-slate-100 overflow-hidden flex items-center justify-center flex-shrink-0",children:s.image_url?e.jsx("img",{src:s.image_url,alt:"",className:"w-full h-full object-cover"}):e.jsx(ve,{className:"w-4 h-4 text-slate-300"})}),e.jsxs("div",{className:"min-w-0",children:[e.jsxs("p",{className:"font-medium text-foreground truncate flex items-center gap-1",children:[s.name,s.is_featured&&e.jsx(Xe,{className:"w-3 h-3 text-amber-500"})]}),s.sku&&e.jsxs("p",{className:"text-[11px] text-slate-400",children:["מק״ט ",s.sku]})]})]})}),e.jsx("td",{className:"px-3 py-2.5 text-muted-foreground",children:P(s.category_id)}),e.jsx("td",{className:"px-3 py-2.5 text-muted-foreground",children:D(s.supplier_id)}),e.jsx("td",{className:"px-3 py-2.5 font-semibold text-emerald-700",children:B(s.sale_price_agorot)}),e.jsx("td",{className:"px-3 py-2.5 text-muted-foreground",children:B(s.cost_price_agorot)}),e.jsx("td",{className:"px-3 py-2.5",children:s.track_inventory?Number(s.stock_quantity)||0:"∞"}),e.jsx("td",{className:"px-3 py-2.5",children:e.jsx("span",{className:`inline-flex px-2 py-0.5 rounded-full text-xs font-medium ${_.badge}`,children:_.label})}),e.jsx("td",{className:"px-3 py-2.5",children:e.jsx("button",{onClick:()=>o(s,"is_visible_in_menu"),title:"הצג/הסתר בתפריט",className:`w-8 h-8 rounded-lg flex items-center justify-center ${s.is_visible_in_menu===!1?"text-slate-400 hover:bg-slate-100":"text-emerald-600 hover:bg-emerald-50"}`,children:s.is_visible_in_menu===!1?e.jsx(rt,{className:"w-4 h-4"}):e.jsx(Je,{className:"w-4 h-4"})})}),e.jsx("td",{className:"px-3 py-2.5",children:e.jsxs("div",{className:"flex items-center gap-0.5",children:[e.jsx("button",{onClick:()=>d(s),title:"עריכה",className:"w-8 h-8 rounded-lg flex items-center justify-center text-slate-500 hover:bg-slate-100",children:e.jsx(ae,{className:"w-4 h-4"})}),e.jsx("button",{onClick:()=>h(s),title:"שכפול",className:"w-8 h-8 rounded-lg flex items-center justify-center text-slate-500 hover:bg-slate-100",children:e.jsx(lt,{className:"w-4 h-4"})}),e.jsx("span",{title:"הפעלה/השבתה",className:"px-1",children:e.jsx(G,{checked:s.is_active!==!1,onCheckedChange:()=>o(s,"is_active")})}),A===s.id?e.jsxs(e.Fragment,{children:[e.jsx("button",{onClick:()=>{S(s),v(null)},className:"h-8 px-2 rounded-lg text-xs bg-red-600 text-white hover:bg-red-700",children:"הסר"}),e.jsx("button",{onClick:()=>v(null),className:"w-8 h-8 rounded-lg flex items-center justify-center text-slate-500 hover:bg-slate-100",children:e.jsx(H,{className:"w-4 h-4"})})]}):e.jsx("button",{onClick:()=>v(s.id),title:"הסרה",className:"w-8 h-8 rounded-lg flex items-center justify-center text-red-500 hover:bg-red-50",children:e.jsx(re,{className:"w-4 h-4"})})]})})]},s.id)})})]})})})]})}function ht({mode:t,product:a,categories:b,suppliers:j,onClose:C,onSave:n,onNeedCategory:f}){const[r,P]=m.useState(()=>a?{name:a.name||"",description:a.description||"",image_url:a.image_url||"",category_id:a.category_id||"",supplier_id:a.supplier_id||"",sku:a.sku||"",barcode:a.barcode||"",sale_price:te(a.sale_price_agorot),cost_price:te(a.cost_price_agorot),track_inventory:a.track_inventory!==!1,stock_quantity:String(a.stock_quantity??0),minimum_stock:String(a.minimum_stock??0),maximum_quantity_per_order:a.maximum_quantity_per_order??"",display_order:String(a.display_order??0),is_active:a.is_active!==!1,is_visible_in_menu:a.is_visible_in_menu!==!1,is_featured:!!a.is_featured}:{...ot}),[D,p]=m.useState(!1),[l,d]=m.useState(!1),[h,o]=m.useState("general"),S=(s,_)=>P(E=>({...E,[s]:_})),A=async s=>{var E;const _=(E=s.target.files)==null?void 0:E[0];if(_){d(!0);try{const{file_url:O}=await $.integrations.Core.UploadFile({file:_});S("image_url",O)}catch{T.error("שגיאה בהעלאת התמונה")}finally{d(!1),s.target.value=""}}},v=(()=>{const s=W(r.sale_price),_=W(r.cost_price);return!s||!_||_<=0?null:Math.round((s-_)/s*100)})(),g=async()=>{p(!0);const s=await n(r,t==="edit"?a.id:null);p(!1)};return e.jsx("div",{className:"fixed inset-0 z-50 flex items-center justify-center bg-black/40 p-4",onClick:C,children:e.jsxs("div",{className:"bg-white rounded-2xl shadow-xl w-full max-w-3xl max-h-[92vh] flex flex-col",dir:"rtl",onClick:s=>s.stopPropagation(),children:[e.jsxs("div",{className:"flex items-center justify-between p-5 border-b border-border flex-shrink-0",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"w-10 h-10 rounded-xl bg-amber-50 flex items-center justify-center",children:e.jsx(fe,{className:"w-5 h-5 text-amber-600"})}),e.jsx("h3",{className:"text-base font-bold",children:t==="edit"?"עריכת מוצר":"מוצר חדש"})]}),e.jsx("button",{onClick:C,className:"w-8 h-8 rounded-lg flex items-center justify-center text-slate-400 hover:bg-slate-100",children:e.jsx(H,{className:"w-5 h-5"})})]}),e.jsx("div",{className:"flex items-center gap-2 px-5 pt-4 flex-shrink-0",children:[["general","פרטים ומחיר"],["stock","מלאי ותצוגה"]].map(([s,_])=>e.jsx("button",{type:"button",onClick:()=>
1o(s),className:`h-9 px-4 rounded-full text-sm font-bold transition-all ${h===s?"bg-amber-500 text-white shadow-sm":"bg-slate-100 text-slate-600 hover:bg-slate-200"}`,children:_},s))}),e.jsxs("div",{className:"p-5 space-y-5 overflow-y-auto",children:[e.jsxs("section",{className:`rounded-2xl bg-slate-50 border border-slate-200/70 p-4 ${h==="general"?"":"hidden"}`,children:[e.jsx("p",{className:"text-xs font-bold text-slate-500 mb-3",children:"פרטים כלליים"}),e.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-3",children:[e.jsxs("div",{className:"sm:col-span-2 lg:col-span-1",children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"שם המוצר *"}),e.jsx("input",{className:k,value:r.name,onChange:s=>S("name",s.target.value),placeholder:"לדוגמה: בירה מהחבית"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"קטגוריה *"}),e.jsxs("div",{className:"flex gap-1.5",children:[e.jsxs("select",{className:k,value:r.category_id,onChange:s=>S("category_id",s.target.value),children:[e.jsx("option",{value:"",children:"בחר/י קטגוריה"}),b.map(s=>e.jsx("option",{value:s.id,children:s.name},s.id))]}),e.jsx("button",{type:"button",onClick:f,title:"ניהול קטגוריות",className:"w-11 h-11 flex-shrink-0 rounded-xl border border-border flex items-center justify-center text-slate-500 hover:bg-slate-100",children:e.jsx(Z,{className:"w-4 h-4"})})]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"ספק ראשי"}),e.jsxs("select",{className:k,value:r.supplier_id,onChange:s=>S("supplier_id",s.target.value),children:[e.jsx("option",{value:"",children:"ללא"}),j.map(s=>e.jsx("option",{value:s.id,children:s.company_name},s.id))]})]}),e.jsxs("div",{className:"sm:col-span-2 lg:col-span-3",children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"תיאור"}),e.jsx("input",{className:k,value:r.description,onChange:s=>S("description",s.target.value),placeholder:"תיאור קצר"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"מק״ט"}),e.jsx("input",{className:k,value:r.sku,onChange:s=>S("sku",s.target.value)})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"ברקוד"}),e.jsx("input",{className:k,value:r.barcode,onChange:s=>S("barcode",s.target.value)})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"תמונה"}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("div",{className:"w-11 h-11 rounded-xl bg-slate-100 overflow-hidden flex items-center justify-center flex-shrink-0",children:r.image_url?e.jsx("img",{src:r.image_url,alt:"",className:"w-full h-full object-cover"}):e.jsx(ve,{className:"w-5 h-5 text-slate-300"})}),e.jsxs("label",{className:"flex-1 inline-flex items-center justify-center gap-2 h-11 rounded-xl border border-dashed border-slate-300 text-sm font-medium text-slate-600 hover:border-amber-400 hover:bg-amber-50 cursor-pointer transition-colors",children:[e.jsx(Ne,{className:"w-4 h-4"})," ",l?"מעלה…":r.image_url?"החלף":"העלה",e.jsx("input",{type:"file",accept:"image/*",className:"hidden",onChange:A,disabled:l})]})]})]})]})]}),e.jsxs("section",{className:`rounded-2xl bg-slate-50 border border-slate-200/70 p-4 ${h==="general"?"":"hidden"}`,children:[e.jsx("p",{className:"text-xs font-bold text-slate-500 mb-3",children:"מחירים"}),e.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-3 gap-3",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"מחיר מכירה (₪) *"}),e.jsx("input",{type:"number",min:"0",step:"0.5",className:k,value:r.sale_price,onChange:s=>S("sale_price",s.target.value)})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"מחיר עלות (₪)"}),e.jsx("input",{type:"number",min:"0",step:"0.5",className:k,value:r.cost_price,onChange:s=>S("cost_price",s.target.value)})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"רווח משוער"}),e.jsx("div",{className:"h-11 rounded-xl border border-border bg-white px-3 flex items-center text-sm text-slate-600",children:v==null?"—":`${v}%`})]})]})]}),e.jsxs("section",{className:`rounded-2xl bg-slate-50 border border-slate-200/70 p-4 ${h==="stock"?"":"hidden"}`,children:[e.jsxs("div",{className:"flex items-center justify-between mb-3",children:[e.jsx("p",{className:"text-xs font-bold text-slate-500",children:"מלאי"}),e.jsxs("label",{className:"flex items-center gap-2 text-sm cursor-pointer",children:[e.jsx(G,{checked:r.track_inventory,onCheckedChange:s=>S("track_inventory",s)})," ניהול מלאי"]})]}),r.track_inventory&&e.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-3 gap-3",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"כמות נוכחית"}),e.jsx("input",{type:"number",className:k,value:r.stock_quantity,onChange:s=>S("stock_quantity",s.target.value)})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"מלאי מינימום (התראה)"}),e.jsx("input",{type:"number",min:"0",className:k,value:r.minimum_stock,onChange:s=>S("minimum_stock",s.target.value)})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"הגבלת כמות להזמנה"}),e.jsx("input",{type:"number",min:"0",className:k,value:r.maximum_quantity_per_order,onChange:s=>S("maximum_quantity_per_order",s.target.value),placeholder:"ללא הגבלה"})]})]})]}),e.jsxs("section",{className:`rounded-2xl bg-slate-50 border border-slate-200/70 p-4 ${h==="stock"?"":"hidden"}`,children:[e.jsx("p",{className:"text-xs font-bold text-slate-500 mb-3",children:"תצוגה"}),e.jsxs("div",{className:"flex flex-wrap items-center gap-4",children:[e.jsxs("label",{className:"flex items-center gap-2 text-sm cursor-pointer",children:[e.jsx(G,{checked:r.is_active,onCheckedChange:s=>S("is_active",s)})," מוצר פעיל"]}),e.jsxs("label",{className:"flex items-center gap-2 text-sm cursor-pointer",children:[e.jsx(G,{checked:r.is_visible_in_menu,onCheckedChange:s=>S("is_visible_in_menu",s)})," הצגה בתפריט"]}),e.jsxs("label",{className:"flex items-center gap-2 text-sm cursor-pointer",children:[e.jsx(G,{checked:r.is_featured,onCheckedChange:s=>S("is_featured",s)})," מוצר מומלץ"]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("label",{className:"text-xs text-muted-foreground",children:"סדר הופעה"}),e.jsx("input",{type:"number",className:"w-20 h-9 rounded-lg border border-border bg-white px-2 text-sm",value:r.display_order,onChange:s=>S("display_order",s.target.value)})]})]})]})]}),e.jsxs("div",{className:"flex items-center justify-between gap-2 p-5 border-t border-border flex-shrink-0",children:[e.jsx(F,{variant:"outline",onClick:C,className:"rounded-full",children:"ביטול"}),e.jsxs(F,{onClick:g,disabled:D,className:"rounded-full gap-1.5 bg-amber-600 hover:bg-amber-700",children:[e.jsx(I,{className:"w-4 h-4"})," ",D?"שומר…":"שמירת מוצר"]})]})]})})}function bt({categories:t,products:a,kibbutzId:b,onClose:j,onRefresh:C}){const[n,f]=m.useState({...ne}),[r,P]=m.useState(null),[D,p]=m.useState(!1),l=(v,g)=>f(s=>({...s,[v]:g})),d=v=>a.filter(g=>g.category_id===v).length,h=async()=>
1{if(!n.name.trim()){T.error("יש להזין שם קטגוריה");return}p(!0);const v={kibbutz_id:b,name:n.name.trim(),icon:n.icon||void 0,image_url:n.image_url||void 0,description:n.description||void 0,display_order:n.display_order!==""?Number(n.display_order):0,is_active:n.is_active};try{r?await $.entities.PubCategory.update(r,v):await $.entities.PubCategory.create(v),T.success(r?"הקטגוריה עודכנה":"הקטגוריה נוספה"),f({...ne}),P(null),C()}catch{T.error("שגיאה בשמירה")}finally{p(!1)}},o=v=>{P(v.id),f({name:v.name||"",icon:v.icon||"",image_url:v.image_url||"",description:v.description||"",display_order:String(v.display_order??0),is_active:v.is_active!==!1})},S=async v=>{if(d(v.id)>0){T.error("לא ניתן למחוק קטגוריה עם מוצרים — יש להעביר את המוצרים לקטגוריה אחרת תחילה");return}try{await $.entities.PubCategory.delete(v.id),T.success("הקטגוריה נמחקה"),r===v.id&&(P(null),f({...ne})),C()}catch{T.error("שגיאה במחיקה")}},A=[...t].sort((v,g)=>v.display_order-g.display_order||String(v.name).localeCompare(g.name));return e.jsx("div",{className:"fixed inset-0 z-50 flex items-start justify-center bg-black/40 p-4 overflow-y-auto",onClick:j,children:e.jsxs("div",{className:"bg-white rounded-2xl shadow-xl w-full max-w-2xl my-8",dir:"rtl",onClick:v=>v.stopPropagation(),children:[e.jsxs("div",{className:"flex items-center justify-between p-5 border-b border-border",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"w-10 h-10 rounded-xl bg-blue-50 flex items-center justify-center",children:e.jsx(pe,{className:"w-5 h-5 text-blue-600"})}),e.jsx("h3",{className:"text-base font-bold",children:"ניהול קטגוריות"})]}),e.jsx("button",{onClick:j,className:"w-8 h-8 rounded-lg flex items-center justify-center text-slate-400 hover:bg-slate-100",children:e.jsx(H,{className:"w-5 h-5"})})]}),e.jsxs("div",{className:"p-5",children:[e.jsxs("div",{className:"rounded-2xl bg-slate-50 border border-slate-200/70 p-4 mb-4",children:[e.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-4 gap-3",children:[e.jsxs("div",{className:"sm:col-span-2",children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"שם קטגוריה *"}),e.jsx("input",{className:k,value:n.name,onChange:v=>l("name",v.target.value),placeholder:"לדוגמה: בירות, שתייה קלה"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"אייקון/אימוג׳י"}),e.jsx("input",{className:k,value:n.icon,onChange:v=>l("icon",v.target.value),placeholder:"🍺"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"סדר"}),e.jsx("input",{type:"number",className:k,value:n.display_order,onChange:v=>l("display_order",v.target.value)})]}),e.jsxs("div",{className:"sm:col-span-3",children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"תיאור קצר"}),e.jsx("input",{className:k,value:n.description,onChange:v=>l("description",v.target.value)})]}),e.jsx("div",{className:"flex items-end",children:e.jsxs("label",{className:"flex items-center gap-2 text-sm cursor-pointer h-11",children:[e.jsx(G,{checked:n.is_active,onCheckedChange:v=>l("is_active",v)})," פעילה"]})})]}),e.jsxs("div",{className:"flex items-center gap-2 mt-3",children:[e.jsxs(F,{onClick:h,disabled:D,className:"rounded-full gap-1.5 bg-blue-600 hover:bg-blue-700",children:[e.jsx(I,{className:"w-4 h-4"})," ",r?"עדכון":"הוספה"]}),r&&e.jsx(F,{variant:"outline",onClick:()=>{P(null),f({...ne})},className:"rounded-full",children:"ביטול עריכה"})]})]}),A.length===0?e.jsx("p",{className:"text-sm text-muted-foreground text-center py-6",children:"אין קטגוריות עדיין"}):e.jsx("div",{className:"space-y-2",children:A.map(v=>e.jsxs("div",{className:`flex items-center gap-3 rounded-xl border px-3 py-2.5 ${v.is_active===!1?"opacity-60 border-border":"border-border"}`,children:[e.jsx("span",{className:"text-lg w-7 text-center",children:v.icon||"📦"}),e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsx("p",{className:"text-sm font-medium text-foreground truncate",children:v.name}),e.jsxs("p",{className:"text-[11px] text-slate-400",children:[d(v.id)," מוצרים · סדר ",v.display_order??0,v.is_active===!1?" · לא פעילה"
1:""]})]}),e.jsx("button",{onClick:()=>o(v),className:"w-8 h-8 rounded-lg flex items-center justify-center text-slate-500 hover:bg-slate-100",children:e.jsx(ae,{className:"w-4 h-4"})}),e.jsx("button",{onClick:()=>S(v),className:"w-8 h-8 rounded-lg flex items-center justify-center text-red-500 hover:bg-red-50",children:e.jsx(re,{className:"w-4 h-4"})})]},v.id))})]})]})})}function ft({suppliers:t,products:a,onAdd:b,onEdit:j,onRefresh:C}){const[n,f]=m.useState(""),[r,P]=m.useState(!1),[D,p]=m.useState(null),l=o=>a.filter(S=>S.supplier_id===o).length,d=t.filter(o=>{var A,v,g;const S=n.trim().toLowerCase();return!(r&&o.is_active===!1||S&&!((A=o.company_name)!=null&&A.toLowerCase().includes(S)||(v=o.contact_name)!=null&&v.toLowerCase().includes(S)||(g=o.phone)!=null&&g.includes(S)))}),h=async o=>{if(l(o.id)>0){T.error("לא ניתן למחוק ספק עם מוצרים מקושרים"),p(null);return}try{await $.entities.PubSupplier.delete(o.id),T.success("הספק נמחק"),C()}catch{T.error("שגיאה במחיקה")}finally{p(null)}};return e.jsxs("div",{children:[e.jsxs("div",{className:"flex flex-wrap items-center gap-2 mb-4",children:[e.jsxs("div",{className:"relative flex-1 min-w-[220px] max-w-md",children:[e.jsx(je,{className:"absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-slate-400"}),e.jsx("input",{value:n,onChange:o=>f(o.target.value),placeholder:"חיפוש ספק…",className:"w-full h-10 rounded-full border border-slate-200 bg-slate-50 pr-10 pl-4 text-sm focus:outline-none focus:ring-2 focus:ring-amber-100 focus:border-amber-300 focus:bg-white transition-all"})]}),e.jsxs("label",{className:"flex items-center gap-2 text-sm text-muted-foreground cursor-pointer",children:[e.jsx(G,{checked:r,onCheckedChange:P})," פעילים בלבד"]}),e.jsxs(F,{onClick:b,className:"rounded-full gap-1.5 h-10 bg-amber-600 hover:bg-amber-700 mr-auto",children:[e.jsx(Z,{className:"w-4 h-4"})," הוספת ספק"]})]}),d.length===0?e.jsxs("div",{className:"bg-white rounded-2xl border border-border shadow-sm p-12 flex flex-col items-center justify-center text-center",children:[e.jsx("div",{className:"w-16 h-16 rounded-full bg-slate-100 flex items-center justify-center mb-3",children:e.jsx(_e,{className:"w-8 h-8 text-slate-300"})}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"אין ספקים להצגה"})]}):e.jsx("div",{className:"bg-white rounded-2xl border border-border shadow-sm overflow-hidden",children:e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"text-xs text-muted-foreground bg-slate-50 border-b border-border",children:[e.jsx("th",{className:"text-right font-medium px-3 py-2.5",children:"שם החברה"}),e.jsx("th",{className:"text-right font-medium px-3 py-2.5",children:"סוג"}),e.jsx("th",{className:"text-right font-medium px-3 py-2.5",children:"איש קשר"}),e.jsx("th",{className:"text-right font-medium px-3 py-2.5",children:"טלפון"}),e.jsx("th",{className:"text-right font-medium px-3 py-2.5",children:"תנאי תשלום"}),e.jsx("th",{className:"text-right font-medium px-3 py-2.5",children:"מוצרים"}),e.jsx("th",{className:"text-right font-medium px-3 py-2.5",children:"סטטוס"}),e.jsx("th",{className:"text-right font-medium px-3 py-2.5",children:"פעולות"})]})}),e.jsx("tbody",{children:d.map(o=>e.jsxs("tr",{className:`border-b border-slate-50 hover:bg-slate-50/60 ${o.is_active===!1?"opacity-60":""}`,children:[e.jsx("td",{className:"px-3 py-2.5 font-medium text-foreground",children:o.company_name}),e.jsx("td",{className:"px-3 py-2.5 text-muted-foreground",children:o.type==="manufacturer"?"יצרן":"ספק"}),e.jsx("td",{className:"px-3 py-2.5 text-muted-foreground",children:o.contact_name||"—"}),e.jsx("td",{className:"px-3 py-2.5 text-muted-foreground",dir:"ltr",children:o.phone||"—"}),e.jsx("td",{className:"px-3 py-2.5 text-muted-foreground",children:o.payment_terms||"—"}),e.jsx("td",{className:"px-3 py-2.5",children:l(o.id)}),e.jsx("td",{className:"px-3 py-2.5",children:e.jsx("span",{className:`inline-flex px-2 py-0.5 rounded-full text-xs font-medium ${o.is_active===!1?"bg-slate-100 text-slate-500":"bg-emerald-50 text-emerald-600"}`,children:o.is_active===!1?"לא פעיל":"פעיל"})}),e.jsx("td",{className:"px-3 py-2.5",children:e.jsxs("div",{className:"flex items-center gap-0.5",children:[e.jsx("button",{onClick:()=>j(o),className:"w-8 h-8 rounded-lg flex items-center justify-center text-slate-500 hover:bg-slate-100",children:e.jsx(ae,{className:"w-4 h-4"})}),D===o.id?e.jsxs(e.Fragment,{children:[e.jsx("button",{onClick:()=>h(o),className:"h-8 px-2 rounded-lg text-xs bg-red-600 text-white hover:bg-red-700",children:"מחק"}),e.jsx("button",{onClick:()=>p(null),className:"w-8 h-8 rounded-lg flex items-center justify-center text-slate-500 hover:bg-slate-100",children:e.jsx(H,{className:"w-4 h-4"})})]}):e.jsx("button",{onClick:()=>p(o.id),className:"w-8 h-8 rounded-lg flex items-center justify-center text-red-500 hover:bg-red-50",children:e.jsx(re,{className:"w-4 h-4"})})]})})]},o.id))})]})})})]})}function pt({mode:t,supplier:a,products:b,kibbutzId:j,onClose:C,onRefresh:n}){const[f,r]=m.useState(()=>a?{...Ce,...a,is_active:a.is_active!==!1}:{...Ce}),[P,D]=m.useState(!1),p=(h,o)=>r(S=>({...S,[h]:o})),l=a?b.filter(h=>h.supplier_id===a.id):[],d=async()=>
1{if(!f.company_name.trim()){T.error("יש להזין שם חברה");return}D(!0);const h={kibbutz_id:j,type:f.type,company_name:f.company_name.trim(),contact_name:f.contact_name||void 0,phone:f.phone||void 0,email:f.email||void 0,address:f.address||void 0,website:f.website||void 0,payment_terms:f.payment_terms||void 0,business_number:f.business_number||void 0,notes:f.notes||void 0,is_active:f.is_active};try{t==="edit"?await $.entities.PubSupplier.update(a.id,h):await $.entities.PubSupplier.create(h),T.success(t==="edit"?"הספק עודכן":"הספק נוסף"),C(),n()}catch{T.error("שגיאה בשמירה")}finally{D(!1)}};return e.jsx("div",{className:"fixed inset-0 z-50 flex items-start justify-center bg-black/40 p-4 overflow-y-auto",onClick:C,children:e.jsxs("div",{className:"bg-white rounded-2xl shadow-xl w-full max-w-2xl my-8",dir:"rtl",onClick:h=>h.stopPropagation(),children:[e.jsxs("div",{className:"flex items-center justify-between p-5 border-b border-border",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"w-10 h-10 rounded-xl bg-slate-100 flex items-center justify-center",children:e.jsx(_e,{className:"w-5 h-5 text-slate-600"})}),e.jsx("h3",{className:"text-base font-bold",children:t==="edit"?"עריכת ספק":"ספק חדש"})]}),e.jsx("button",{onClick:C,className:"w-8 h-8 rounded-lg flex items-center justify-center text-slate-400 hover:bg-slate-100",children:e.jsx(H,{className:"w-5 h-5"})})]}),e.jsxs("div",{className:"p-5 space-y-4",children:[e.jsxs("div",{className:"rounded-2xl bg-slate-50 border border-slate-200/70 p-4 grid grid-cols-1 sm:grid-cols-2 gap-3",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"שם חברה *"}),e.jsx("input",{className:k,value:f.company_name,onChange:h=>p("company_name",h.target.value)})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"סוג"}),e.jsxs("select",{className:k,value:f.type,onChange:h=>p("type",h.target.value),children:[e.jsx("option",{value:"supplier",children:"ספק"}),e.jsx("option",{value:"manufacturer",children:"יצרן"})]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"איש קשר"}),e.jsx("input",{className:k,value:f.contact_name,onChange:h=>p("contact_name",h.target.value)})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"טלפון"}),e.jsx("input",{className:k,value:f.phone,onChange:h=>p("phone",h.target.value),dir:"ltr"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"אימייל"}),e.jsx("input",{className:k,value:f.email,onChange:h=>p("email",h.target.value),dir:"ltr"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"תנאי תשלום"}),e.jsx("input",{className:k,value:f.payment_terms,onChange:h=>p("payment_terms",h.target.value),placeholder:"שוטף +30"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"כתובת"}),e.jsx("input",{className:k,value:f.address,onChange:h=>p("address",h.target.value)})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"אתר אינטרנט"}),e.jsx("input",{className:k,value:f.website,onChange:h=>p("website",h.target.value),dir:"ltr"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"ח.פ / עוסק"}),e.jsx("input",{className:k,value:f.business_number,onChange:h=>p("business_number",h.target.value)})]}),e.jsx("div",{className:"flex items-end",children:e.jsxs("label",{className:"flex items-center gap-2 text-sm cursor-pointer h-11",children:[e.jsx(G,{checked:f.is_active,onCheckedChange:h=>p("is_active",h)})," ספק פעיל"]})}),e.jsxs("div",{className:"sm:col-span-2",children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"הערות"}),e.jsx("input",{className:k,value:f.notes,onChange:h=>p("notes",h.target.value)})]})]}),t==="edit"&&e.jsxs("div",{children:[e.jsxs("p",{className:"text-xs font-bold text-slate-500 mb-2 flex items-center gap-1.5",children:[e.jsx(Ae,{className:"w-4 h-4"})," מוצרים מקושרים (",l.length,")"]}),l.length===0?e.jsx("p",{className:"text-sm text-muted-foreground",children:"אין מוצרים מקושרים. שייכו מוצר לספק דרך עריכת המוצר."}):e.jsx("div",{className:"space-y-1.5",children:l.map(h=>e.jsxs("div",{className:"flex items-center justify-between rounded-xl border border-border/60 px-3 py-2 text-sm",children:[e.jsx("span",{className:"font-medium",children:h.name}),e.jsxs("span",{className:"text-muted-foreground",children:["מלאי ",h.track_inventory?Number(h.stock_quantity)||0:"∞"," · עלות ",B(h.cost_price_agorot)]})]}
1,h.id))})]})]}),e.jsxs("div",{className:"flex items-center justify-between gap-2 p-5 border-t border-border",children:[e.jsx(F,{variant:"outline",onClick:C,className:"rounded-full",children:"ביטול"}),e.jsxs(F,{onClick:d,disabled:P,className:"rounded-full gap-1.5 bg-amber-600 hover:bg-amber-700",children:[e.jsx(I,{className:"w-4 h-4"})," ",P?"שומר…":"שמירה"]})]})]})})}const gt={name:"",payment_amount:"",wallet_credit:"",description:"",benefit_text:"",display_order:"0",is_active:!0,start_date:"",end_date:"",max_purchases:""};function jt({packages:t,kibbutzId:a,onRefresh:b,onBack:j}){const[C,n]=m.useState(null),[f,r]=m.useState(null),P=[...t].sort((l,d)=>l.display_order-d.display_order||String(l.name).localeCompare(d.name)),D=async l=>{try{await $.entities.PubCardPackage.delete(l.id),T.success("הכרטסייה נמחקה"),b()}catch{T.error("שגיאה במחיקה")}finally{r(null)}},p=async l=>{try{await $.entities.PubCardPackage.update(l.id,{is_active:l.is_active===!1}),b()}catch{T.error("שגיאה בעדכון")}};return e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center justify-between mb-4",children:[e.jsx("button",{onClick:j,className:"text-sm text-muted-foreground hover:text-foreground",children:"← חזרה למוצרים"}),e.jsxs(F,{onClick:()=>n({mode:"create"}),className:"rounded-full gap-1.5 bg-emerald-600 hover:bg-emerald-700",children:[e.jsx(Z,{className:"w-4 h-4"})," הוספת כרטסייה"]})]}),e.jsx("p",{className:"text-xs font-bold text-slate-500 mb-2",children:"תצוגה מקדימה (כפי שהתושב רואה)"}),P.filter(l=>l.is_active!==!1).length===0?e.jsx("div",{className:"bg-white rounded-2xl border border-border shadow-sm p-8 text-center text-sm text-muted-foreground mb-6",children:"עדיין לא הוגדרו כרטסיות פעילות לרכישה"}):e.jsx("div",{className:"grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4 mb-6",children:P.filter(l=>l.is_active!==!1).map(l=>e.jsxs("div",{className:"rounded-2xl border-2 border-emerald-200 bg-gradient-to-br from-emerald-50 to-white p-5 shadow-sm",children:[e.jsxs("div",{className:"flex items-center justify-between mb-1",children:[e.jsx("h3",{className:"text-base font-bold text-slate-900",children:l.name}),Number(l.bonus_amount_agorot)>0&&e.jsxs("span",{className:"inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-xs font-bold bg-amber-100 text-amber-700",children:[e.jsx(nt,{className:"w-3 h-3"})," בונוס ",B(l.bonus_amount_agorot)]})]}),l.description&&e.jsx("p",{className:"text-xs text-slate-500 mb-3",children:l.description}),e.jsxs("div",{className:"flex items-end gap-2 mb-1",children:[e.jsx("span",{className:"text-3xl font-black text-emerald-700",children:B(l.payment_amount_agorot)}),e.jsx("span",{className:"text-sm text-slate-400 mb-1",children:"תשלום"})]}),e.jsxs("p",{className:"text-sm font-semibold text-slate-700",children:["מקבלים יתרה של ",B(l.wallet_credit_agorot)]}),l.benefit_text&&e.jsx("p",{className:"text-xs text-emerald-600 mt-2",children:l.benefit_text})]},l.id))}),e.jsx("p",{className:"text-xs font-bold text-slate-500 mb-2",children:"כל הכרטסיות"}),P.length===0?e.jsx("div",{className:"bg-white rounded-2xl border border-border shadow-sm p-8 text-center text-sm text-muted-foreground",children:"אין כרטסיות"}):e.jsx("div",{className:"space-y-2",children:P.map(l=>e.jsxs("div",{className:`flex items-center gap-3 rounded-xl border bg-white px-4 py-3 ${l.is_active===!1?"opacity-60 border-border":"border-border"}`,children:[e.jsx("div",{className:"w-9 h-9 rounded-lg bg-emerald-50 flex items-center justify-center flex-shrink-0",children:e.jsx(se,{className:"w-4.5 h-4.5 text-emerald-600"})}),e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsx("p",{className:"text-sm font-bold text-foreground truncate",children:l.name}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:["תשלום ",B(l.payment_amount_agorot)," · יתרה ",B(l.wallet_credit_agorot)," · בונוס ",B(l.bonus_amount_agorot)]})]}),e.jsx(G,{checked:l.is_active!==!1,onCheckedChange:()=>p(l)}),e.jsx("button",{onClick:()=>n({mode:"edit",pkg:l}),className:"w-8 h-8 rounded-lg flex items-center justify-center text-slate-500 hover:bg-slate-100",children:e.jsx(ae,{className:"w-4 h-4"})}),f===l.id?e.jsxs(e.Fragment,{children:[e.jsx("button",{onClick:()=>D(l),className:"h-8 px-2 rounded-lg text-xs bg-red-600 text-white hover:bg-red-700",children:"מחק"}),e.jsx("button",{onClick:()=>r(null),className:"w-8 h-8 rounded-lg flex items-center justify-center text-slate-500 hover:bg-slate-100",children:e.jsx(H,{className:"w-4 h-4"})})]}):e.jsx("button",{onClick:()=>r(l.id),className:"w-8 h-8 rounded-lg flex items-center justify-center text-red-500 hover:bg-red-50",children:e.jsx(re,{className:"w-4 h-4"})})]},l.id))}),C&&e.jsx(_t,{mode:C.mode,pkg:C.pkg,kibbutzId:a,onClose:()=>n(null),onRefresh:b})]})}function _t({mode:t,pkg:a,kibbutzId:b,onClose:j,onRefresh:C}){const[n,f]=m.useState(()=>a?{name:a.name||"",payment_amount:te(a.payment_amount_agorot),wallet_credit:te(a.wallet_credit_agorot),description:a.description||"",benefit_text:a.benefit_text||"",display_order:String(a.display_order??0),is_active:a.is_active!==!1,start_date:a.start_date||"",end_date:a.end_date||"",max_purchases:a.max_purchases??""}:{...gt}),[r,P]=m.useState(!1),D=(d,h)=>f(o=>({...o,[d]:h})),p=(()=>{const d=W(n.payment_amount),h=W(n.wallet_credit);return d!=null&&h!=null?h-d:null})(),l=async()=>
1{if(!n.name.trim()){T.error("יש להזין שם כרטסייה");return}if(n.payment_amount===""||n.wallet_credit===""){T.error("יש להזין סכום תשלום ויתרה");return}P(!0);const d={kibbutz_id:b,name:n.name.trim(),payment_amount_agorot:W(n.payment_amount),wallet_credit_agorot:W(n.wallet_credit),bonus_amount_agorot:p,description:n.description||void 0,benefit_text:n.benefit_text||void 0,display_order:n.display_order!==""?Number(n.display_order):0,is_active:n.is_active,start_date:n.start_date||void 0,end_date:n.end_date||void 0,max_purchases:n.max_purchases!==""?Number(n.max_purchases):void 0};try{t==="edit"?await $.entities.PubCardPackage.update(a.id,d):await $.entities.PubCardPackage.create(d),T.success(t==="edit"?"הכרטסייה עודכנה":"הכרטסייה נוספה"),j(),C()}catch{T.error("שגיאה בשמירה")}finally{P(!1)}};return e.jsx("div",{className:"fixed inset-0 z-50 flex items-start justify-center bg-black/40 p-4 overflow-y-auto",onClick:j,children:e.jsxs("div",{className:"bg-white rounded-2xl shadow-xl w-full max-w-lg my-8",dir:"rtl",onClick:d=>d.stopPropagation(),children:[e.jsxs("div",{className:"flex items-center justify-between p-5 border-b border-border",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"w-10 h-10 rounded-xl bg-emerald-50 flex items-center justify-center",children:e.jsx(se,{className:"w-5 h-5 text-emerald-600"})}),e.jsx("h3",{className:"text-base font-bold",children:t==="edit"?"עריכת כרטסייה":"כרטסייה חדשה"})]}),e.jsx("button",{onClick:j,className:"w-8 h-8 rounded-lg flex items-center justify-center text-slate-400 hover:bg-slate-100",children:e.jsx(H,{className:"w-5 h-5"})})]}),e.jsxs("div",{className:"p-5 space-y-3",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"שם הכרטסייה *"}),e.jsx("input",{className:k,value:n.name,onChange:d=>D("name",d.target.value),placeholder:"לדוגמה: כרטסיית 100"})]}),e.jsxs("div",{className:"grid grid-cols-3 gap-3",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"תשלום (₪) *"}),e.jsx("input",{type:"number",min:"0",step:"1",className:k,value:n.payment_amount,onChange:d=>D("payment_amount",d.target.value)})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"יתרה מתקבלת (₪) *"}),e.jsx("input",{type:"number",min:"0",step:"1",className:k,value:n.wallet_credit,onChange:d=>D("wallet_credit",d.target.value)})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"בונוס"}),e.jsx("div",{className:`h-11 rounded-xl border px-3 flex items-center text-sm font-bold ${p>0?"border-amber-200 bg-amber-50 text-amber-700":"border-border bg-white text-slate-500"}`,children:p==null?"—":B(p)})]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"תיאור קצר"}),e.jsx("input",{className:k,value:n.description,onChange:d=>D("description",d.target.value)})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"טקסט הטבה"}),e.jsx("input",{className:k,value:n.benefit_text,onChange:d=>D("benefit_text",d.target.value),placeholder:"לדוגמה: 10% בונוס מתנה"})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"תאריך התחלה"}),e.jsx("input",{type:"date",className:k,value:n.start_date,onChange:d=>D("start_date",d.target.value)})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"תאריך סיום"}),e.jsx("input",{type:"date",className:k,value:n.end_date,onChange:d=>D("end_date",d.target.value)})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"מגבלת רכישות לתושב"}),e.jsx("input",{type:"number",min:"0",className:k,value:n.max_purchases,onChange:d=>D("max_purchases",d.target.value),placeholder:"ללא הגבלה"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"סדר הופעה"}),e.jsx("input",{type:"number",className:k,value:n.display_order,onChange:d=>D("display_order",d.target.value)})]})]}),e.jsxs("label",{className:"flex items-center gap-2 text-sm cursor-pointer pt-1",children:[e.jsx(G,{checked:n.is_active,onCheckedChange:d=>D("is_active",d)})," כרטסייה פעילה (מוצגת לרכישה)"]})]}),e.jsxs("div",{className:"flex items-center justify-between gap-2 p-5 border-t border-border",children:[e.jsx(F,{variant:"outline",onClick:j,className:"rounded-full",children:"ביטול"}),e.jsxs(F,{onClick:l,disabled:r,className:"rounded-full gap-1.5 bg-emerald-600 hover:bg-emerald-700",children:[e.jsx(I,{className:"w-4 h-4"})," ",r?"שומר…":"שמירה"]})]})]})})}const Se=["ראשון","שני","שלישי","רביעי","חמישי","שישי","שבת"],vt={Sun:0,Mon:1,Tue:2,Wed:3,Thu:4,Fri:5,Sat:6};function Nt(t,a){if(!t)return{open:!1,reason:"not_configured"};if(t.is_active===!1)return{open:!1,reason:"inactive"};if(t.ordering_enabled===!1)return{open:!1,reason:"ordering_disabled"};const b=a||[];if(b.length===0)return{open:!0,reason:"open"};const j=new Date,C=new Intl.DateTimeFormat("en-US",{timeZone:"Asia/Jerusalem",weekday:"short"}).format(j),n=new Intl.DateTimeFormat("en-GB",{timeZone:"Asia/Jerusalem",hour:"2-digit",minute:"2-digit",hour12:!1}).format(j),f=b.find(D=>Number(D.day_of_week)===vt[C]);if(!f)return{open:!0,reason:"open"};if(f.is_open===!1)return{open:!1,reason:"closed_today"};const r=String(f.order_start_time||f.open_time||"").slice(0,5),P=String(f.order_end_time||f.close_time||"").slice(0,5);return r&&n<r?{open:!1,reason:"before_hours",start:r,end:P}:P&&n>P?{open:!1,reason:"after_hours",start:r,end:P}:{open:!0,reason:"open",start:r,end:P}}const yt={not_configured:"הפאב עדיין לא הוגדר",inactive:'הפאב מסומן כלא פעיל — הפעילו את "הפאב פעיל" בהגדרות → כללי',ordering_disabled:'ההזמנות כבויות — הדליקו את "אפשר הזמנות" למעלה',closed_today:"היום מוגדר כסגור — הדליקו את המתג של היום למטה"};function wt({settings:t,hours:a}){const b=Nt(t,a);if(b.open)return e.jsxs("div",{className:"rounded-xl bg-emerald-50 border border-emerald-200 px-4 py-3 flex items-center gap-2 text-sm text-emerald-800",children:[e.jsx("span",{className:"w-2.5 h-2.5 rounded-full bg-emerald-500 animate-pulse"})," הפאב ",e.jsx("b",{children:"פתוח להזמנות"})," כרגע",b.start&&b.end?` (עד ${b.end})`:"","."]});const j=b.reason==="before_hours"?`ההזמנות להיום נפתחות בשעה ${b.start}`:b.reason==="after_hours"?`ההזמנות להיום נסגרו (עד ${b.end})`:yt[b.reason]||"הפאב סגור להזמנות כרגע";return e.jsxs("div",{className:"rounded-xl bg-amber-50 border border-amber-200 px-4 py-3 flex items-start gap-2 text-sm text-amber-800",children:[e.jsx(ge,{className:"w-4 h-4 flex-shrink-0 mt-0.5"}),e.jsxs("span",{children:["הפאב ",e.jsx("b",{children:"סגור להזמנות"})," כרגע — ",j,"."]})]})}const kt=[{key:"general",label:"כללי",icon:Me},{key:"hours",label:"שעות פעילות",icon:Qe},{key:"orders",label:"הזמנות",icon:be},{key:"cards",label:"כרטסיות ויתרות",icon:se},{key:"inventory",label:"מלאי",icon:Ae},{key:"notifications",label:"התראות",icon:ge}],Ct=[["allow_notes","אפשר הערת תושב"],["pickup_only","איסוף עצמי בלבד"]],St=[["cards_enabled","הפעלת רכישת כרטסיות"],["show_balance","הצגת יתרה באזור האישי"],["allow_manual_credit","אפשר זיכוי ידני"],["allow_manual_charge","אפשר חיוב ידני"]],Pt=[["low_stock_alert","התראת מלאי נמוך בניהול"],["show_out_of_stock","הצגת מוצר שאזל בתפריט"]],Dt=[["in_prep_resident","הזמנה בהכנה לתושב"],["ready_resident","הזמנה מוכנה לתושב"],["supplied_resident","הזמנה סופקה"],["cancel_resident","ביטול הזמנה"],["credit_resident","זיכוי / חיוב"]];function Tt({settings:t,hours:a,kibbutzId:b,onRefresh:j,onBack:C}){const[n,f]=m.useState("general"),[r,P]=m.useState(!1),[D,p]=m.useState(""),l=(t==null?void 0:t.settings)||{},[d,h]=m.useState({pub_name:(t==null?void 0:t.pub_name)||"",description:(t==null?void 0:t.description)||"",full_description:(t==null?void 0:t.full_description)||"",logo_url:(t==null?void 0:t.logo_url)||"",cover_image_url:(t==null?void 0:t.cover_image_url)||"",address:(t==null?void 0:t.address)||"",location:(t==null?void 0:t.location)||"",phone:(t==null?void 0:t.phone)||"",contact_name:(t==null?void 0:t.contact_name)||"",is_active:(t==null?void 0:t.is_active)===!0,is_visible_in_personal_area:(t==null?void 0:t.is_visible_in_personal_area)!==!1,ordering_enabled:(t==null?void 0:t.ordering_enabled)!==!1,resident_message:(t==null?void 0:t.resident_message)||"",closed_message:(t==null?void 0:t.closed_message)||"",estimated_preparation_minutes:String((t==null?void 0:t.estimated_preparation_minutes)??15),minimum_order_amount:te(t==null?void 0:t.minimum_order_amount),maximum_order_amount:te(t==null?void 0:t.maximum_order_amount),sumit_budget_item_id:(t==null?void 0:t.sumit_budget_item_id)??void 0}),[o,S]=m.useState({orders:l.orders||{},cards:l.cards||{},inventory:l.inventory||{},notifications:l.notifications||{}}),[A,v]=m.useState(()=>Se.map((c,N)=>{const u=(a||[]).find(L=>L.day_of_week===N)||{};
1return{id:u.id,day_of_week:N,is_open:!!u.is_open,open_time:u.open_time||"17:00",close_time:u.close_time||"23:00",order_start_time:u.order_start_time||"17:00",order_end_time:u.order_end_time||"22:30"}})),g=(c,N)=>h(u=>({...u,[c]:N})),s=(c,N)=>S(u=>({...u,[c]:{...u[c],[N]:u[c][N]===!1}})),_=(c,N,u)=>v(L=>L.map((M,U)=>U===c?{...M,[N]:u}:M)),E=async(c,N)=>{var L;const u=(L=N.target.files)==null?void 0:L[0];if(u){p(c);try{const{file_url:M}=await $.integrations.Core.UploadFile({file:u});g(c,M)}catch{T.error("שגיאה בהעלאה")}finally{p(""),N.target.value=""}}},O=async()=>{P(!0);const c={kibbutz_id:b,pub_name:d.pub_name||void 0,description:d.description||void 0,full_description:d.full_description||void 0,logo_url:d.logo_url||void 0,cover_image_url:d.cover_image_url||void 0,address:d.address||void 0,location:d.location||void 0,phone:d.phone||void 0,contact_name:d.contact_name||void 0,is_active:d.is_active,is_visible_in_personal_area:d.is_visible_in_personal_area,ordering_enabled:d.ordering_enabled,resident_message:d.resident_message||void 0,closed_message:d.closed_message||void 0,estimated_preparation_minutes:d.estimated_preparation_minutes!==""?Number(d.estimated_preparation_minutes):15,minimum_order_amount:W(d.minimum_order_amount),maximum_order_amount:W(d.maximum_order_amount),...d.sumit_budget_item_id!==void 0?{sumit_budget_item_id:d.sumit_budget_item_id}:{},settings:o};try{t!=null&&t.id?await $.entities.PubSettings.update(t.id,c):await $.entities.PubSettings.create(c);for(const N of A){const u={kibbutz_id:b,day_of_week:N.day_of_week,is_open:N.is_open,open_time:N.open_time,close_time:N.close_time,order_start_time:N.order_start_time,order_end_time:N.order_end_time};N.id?await $.entities.PubOpeningHours.update(N.id,u):await $.entities.PubOpeningHours.create(u)}T.success("ההגדרות נשמרו"),j()}catch(N){console.error("Save settings error:",N),T.error("שגיאה בשמירת ההגדרות")}finally{P(!1)}},i=({field:c,label:N})=>e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:N}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("div",{className:"w-12 h-11 rounded-xl bg-slate-100 overflow-hidden flex items-center justify-center flex-shrink-0",children:d[c]?e.jsx("img",{src:d[c],alt:"",className:"w-full h-full object-cover"}):e.jsx(ve,{className:"w-5 h-5 text-slate-300"})}),e.jsxs("label",{className:"flex-1 inline-flex items-center justify-center gap-2 h-11 rounded-xl border border-dashed border-slate-300 text-sm font-medium text-slate-600 hover:border-amber-400 hover:bg-amber-50 cursor-pointer",children:[e.jsx(Ne,{className:"w-4 h-4"})," ",D===c?"מעלה…":d[c]?"החלף":"העלה",e.jsx("input",{type:"file",accept:"image/*",className:"hidden",onChange:u=>E(c,u),disabled:D===c})]})]})]}),w=({group:c,items:N})=>e.jsx("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-2",children:N.map(([u,L])=>e.jsxs("label",{className:"flex items-center justify-between gap-2 rounded-xl border border-border bg-white px-4 py-3 cursor-pointer",children:[e.jsx("span",{className:"text-sm text-foreground",children:L}),e.jsx(G,{checked:o[c][u]!==!1,onCheckedChange:()=>s(c,u)})]},u))});return e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center justify-between mb-4",children:[e.jsx("button",{onClick:C,className:"text-sm text-muted-foreground hover:text-foreground",children:"← חזרה למוצרים"}),e.jsxs(F,{onClick:O,disabled:r,className:"rounded-full gap-1.5 bg-amber-600 hover:bg-amber-700",children:[e.jsx(I,{className:"w-4 h-4"})," ",r?"שומר…":"שמירת הגדרות"]})]}),e.jsx("div",{className:"flex flex-wrap gap-2 mb-4",children:kt.map(c=>{const N=c.icon,u=n===c.key;return e.jsxs("button",{onClick:()=>f(c.key),className:`inline-flex items-center gap-1.5 h-9 px-4 rounded-full text-sm font-medium border transition-all ${u?"bg-amber-600 text-white border-transparent":"bg-white text-foreground border-border hover:bg-accent"}`,children:[e.jsx(N,{className:"w-4 h-4"})," ",c.label]},c.key)})}),e.jsxs("div",{className:"bg-white rounded-2xl border border-border shadow-sm p-5",children:[n==="general"&&e.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-3",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"שם הפאב"}),e.jsx("input",{className:k,value:d.pub_name,onChange:c=>g("pub_name",c.target.value)})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"טלפון"}),e.jsx("input",{className:k,value:d.phone,onChange:c=>g("phone",c.target.value),dir:"ltr"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"איש קשר"}),e.jsx("input",{className:k,value:d.contact_name,onChange:c=>g("contact_name",c.target.value)})]}),e.jsx(i,{field:"logo_url",label:"לוגו"}),e.jsx(i,{field:"cover_image_url",label:"תמונת שער"}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"מיקום"}),e.jsx("input",{className:k,value:d.location,onChange:c=>g("location",c.target.value)})]}),e.jsxs("div",{className:"sm:col-span-2 lg:col-span-3",children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"תיאור קצר"}),e.jsx("input",{className:k,value:d.description,onChange:c=>g("description",c.target.value)})]}),e.jsxs("div",{className:"sm:col-span-2 lg:col-span-3",children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"תיאור מלא"}),e.jsx("input",{className:k,value:d.full_description,onChange:c=>g("full_description",c.target.value)})]}),e.jsxs("div",{className:"sm:col-span-2 lg:col-span-3",children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"כתובת"}),e.jsx("input",{className:k,value:d.address,onChange:c=>g("address",c.target.value)})]}),e.jsxs("div",{className:"sm:col-span-2 lg:col-span-3",children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"הודעה לתושב"}),e.jsx("input",{className:k,value:d.resident_message,onChange:c=>g("resident_message",c.target.value)})]}),e.jsxs("div",{className:"sm:col-span-2 lg:col-span-3 flex flex-wrap items-center gap-4 pt-1",children:[e.jsxs("label",{className:"flex items-center gap-2 text-sm cursor-pointer",children:[e.jsx(G,{checked:d.is_active,onCheckedChange:c=>g("is_active",c)})," הפאב פעיל"]}),e.jsxs("label",{className:"flex items-center gap-2 text-sm cursor-pointer",children:[e.jsx(G,{checked:d.is_visible_in_personal_area,onCheckedChange:c=>g("is_visible_in_personal_area",c)})," הצגה באזור האישי"]})]})]}),n==="hours"&&e.jsxs("div",{className:"space-y-3",children:[e.jsx(wt,{settings:t,hours:a}),e.jsxs("label",{className:"flex items-center gap-2 text-sm cursor-pointer mb-2",children:[e.jsx(G,{checked:d.ordering_enabled,onCheckedChange:c=>g("ordering_enabled",c)})," אפשר הזמנות"]}),A.map((c,N)=>e.jsxs("div",{className:"flex flex-wrap items-center gap-3 rounded-xl border border-border px-3 py-2.5",children:[e.jsxs("label",{className:"flex items-center gap-2 w-24 flex-shrink-0",children:[e.jsx(G,{checked:c.is_o
1pen,onCheckedChange:u=>_(N,"is_open",u)})," ",e.jsx("span",{className:"text-sm font-medium",children:Se[N]})]}),c.is_open?e.jsxs("div",{className:"flex flex-wrap items-center gap-2 text-sm",children:[e.jsx("span",{className:"text-xs text-muted-foreground",children:"פתוח"}),e.jsx("input",{type:"time",className:"h-9 rounded-lg border border-border bg-white px-2",value:c.open_time,onChange:u=>_(N,"open_time",u.target.value)}),e.jsx("input",{type:"time",className:"h-9 rounded-lg border border-border bg-white px-2",value:c.close_time,onChange:u=>_(N,"close_time",u.target.value)}),e.jsx("span",{className:"text-xs text-muted-foreground mr-2",children:"הזמנות"}),e.jsx("input",{type:"time",className:"h-9 rounded-lg border border-border bg-white px-2",value:c.order_start_time,onChange:u=>_(N,"order_start_time",u.target.value)}),e.jsx("input",{type:"time",className:"h-9 rounded-lg border border-border bg-white px-2",value:c.order_end_time,onChange:u=>_(N,"order_end_time",u.target.value)})]}):e.jsx("span",{className:"text-sm text-muted-foreground",children:"סגור"})]},N)),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"הודעה כשהפאב סגור"}),e.jsx("input",{className:k,value:d.closed_message,onChange:c=>g("closed_message",c.target.value)})]})]}),n==="orders"&&e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-3 gap-3",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"מינימום להזמנה (₪)"}),e.jsx("input",{type:"number",min:"0",className:k,value:d.minimum_order_amount,onChange:c=>g("minimum_order_amount",c.target.value)})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"מקסימום להזמנה (₪)"}),e.jsx("input",{type:"number",min:"0",className:k,value:d.maximum_order_amount,onChange:c=>g("maximum_order_amount",c.target.value)})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"זמן הכנה משוער (דקות)"}),e.jsx("input",{type:"number",min:"0",className:k,value:d.estimated_preparation_minutes,onChange:c=>g("estimated_preparation_minutes",c.target.value)})]})]}),e.jsx(w,{group:"orders",items:Ct})]}),n==="cards"&&e.jsxs("div",{className:"space-y-4",children:[e.jsx(w,{group:"cards",items:St}),e.jsx(st,{communityId:b,value:d.sumit_budget_item_id,onChange:c=>g("sumit_budget_item_id",c),hint:"הסעיף התקציבי שאליו ייזקפו רכישות הכרטסיות בסאמיט."})]}),n==="inventory"&&e.jsx(w,{group:"inventory",items:Pt}),n==="notifications"&&e.jsx(w,{group:"notifications",items:Dt})]})]})}const Be={bartender:"ברמן",volunteer:"מתנדב",shift_lead:"אחראי משמרת",pub_manager:"מנהל פאב"};function $t({kibbutzId:t,onBack:a}){const[b,j]=m.useState(null),[C,n]=m.useState(null),f=m.useCallback(async()=>{var p;try{const l=await $.functions.invoke("pubStaffList",{community_id:t});j(((p=l==null?void 0:l.data)==null?void 0:p.staff)||[])}catch{j([]),T.error("שגיאה בטעינת הצוות")}},[]);m.useEffect(()=>{f()},[f]);const r=`${window.location.origin}/pub-bar/${t}`,P=p=>{const l=`שלום ${(p==null?void 0:p.name)||""}, קישור הכניסה למסך הברמנים של הפאב:
2${r}
3מתחברים עם מספר הטלפון וקוד ה-PIN שהוגדר עבורכם.`;window.open(`https://wa.me/?text=${encodeURIComponent(l)}`,"_blank","noopener")},D=()=>{var p;(p=navigator.clipboard)==null||p.writeText(r),T.success("הקישור הועתק")};return e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center justify-between mb-4 flex-wrap gap-2",children:[e.jsx("button",{onClick:a,className:"text-sm text-muted-foreground hover:text-foreground",children:"← חזרה למוצרים"}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(F,{variant:"outline",onClick:D,className:"rounded-full gap-1.5 h-10",children:[e.jsx(we,{className:"w-4 h-4"})," קישור מסך הברמנים"]}),e.jsxs(F,{onClick:()=>n({mode:"create"}),className:"rounded-full gap-1.5 h-10 bg-amber-600 hover:bg-amber-700",children:[e.jsx(Z,{className:"w-4 h-4"})," הוספת ברמן"]})]})]}),b==null?e.jsx("div",{className:"flex justify-center py-10",children:e.jsx(z,{className:"w-6 h-6 animate-spin text-amber-500"})}):b.length===0?e.jsxs("div",{className:"bg-white rounded-2xl border border-border shadow-sm p-12 flex flex-col items-center justify-center text-center",children:[e.jsx("div",{className:"w-16 h-16 rounded-full bg-purple-50 flex items-center justify-center mb-3",children:e.jsx(de,{className:"w-8 h-8 text-purple-300"})}),e.jsx("p",{className:"text-sm text-muted-foreground mb-4",children:"עדיין לא הוגדרו ברמנים או מתנדבים"}),e.jsxs(F,{onClick:()=>n({mode:"create"}),className:"rounded-full gap-1.5 bg-amber-600 hover:bg-amber-700",children:[e.jsx(Z,{className:"w-4 h-4"})," הוספת ברמן ראשון"]})]}):e.jsx("div",{className:"space-y-2",children:b.map(p=>e.jsxs("div",{className:`flex items-center gap-3 rounded-xl border bg-white px-4 py-3 ${p.is_active===!1?"opacity-60 border-border":"border-border"}`,children:[e.jsx("div",{className:"w-9 h-9 rounded-lg bg-purple-50 flex items-center justify-center flex-shrink-0",children:e.jsx(de,{className:"w-4.5 h-4.5 text-purple-600"})}),e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsxs("p",{className:"text-sm font-bold text-foreground truncate",children:[p.name," ",e.jsxs("span",{className:"text-xs font-normal text-muted-foreground",children:["· ",Be[p.role]||p.role]})]}),e.jsxs("p",{className:"text-xs text-muted-foreground",dir:"ltr",children:[p.phone,p.has_pin?"":" · ללא PIN",p.locked?" · נעול":""]})]}),e.jsx("button",{onClick:()=>P(p),title:"שלח קישור בוואטסאפ",className:"w-9 h-9 rounded-lg flex items-center justify-center text-emerald-600 hover:bg-emerald-50",children:e.jsx(we,{className:"w-4 h-4"})}),e.jsx("button",{onClick:()=>n({mode:"edit",member:p}),className:"w-9 h-9 rounded-lg flex items-center justify-center text-slate-500 hover:bg-slate-100",children:e.jsx(ae,{className:"w-4 h-4"})})]}
3,p.id))}),C&&e.jsx(Mt,{mode:C.mode,member:C.member,kibbutzId:t,onClose:()=>n(null),onSaved:()=>{n(null),f()}})]})}function Mt({mode:t,member:a,kibbutzId:b,onClose:j,onSaved:C}){const[n,f]=m.useState(()=>a?{name:a.name||"",phone:a.phone||"",role:a.role||"bartender",is_active:a.is_active!==!1,pin:""}:{name:"",phone:"",role:"bartender",is_active:!0,pin:""}),[r,P]=m.useState(!1),D=(l,d)=>f(h=>({...h,[l]:d})),p=async()=>{if(!n.name.trim()||!n.phone.trim()){T.error("שם וטלפון נדרשים");return}if(t==="create"&&n.pin.length<4){T.error("יש להגדיר קוד PIN בן 4 ספרות");return}P(!0);try{await $.functions.invoke("pubSaveStaff",{community_id:b,staff_id:a==null?void 0:a.id,name:n.name.trim(),phone:n.phone.trim(),role:n.role,is_active:n.is_active,...n.pin.length>=4?{pin:n.pin}:{}}),T.success(t==="edit"?"הפרטים עודכנו":"הברמן נוסף"),C()}catch(l){T.error((l==null?void 0:l.message)||"שגיאה בשמירה")}finally{P(!1)}};return e.jsx("div",{className:"fixed inset-0 z-50 flex items-start justify-center bg-black/40 p-4 overflow-y-auto",onClick:j,children:e.jsxs("div",{className:"bg-white rounded-2xl shadow-xl w-full max-w-md my-8",dir:"rtl",onClick:l=>l.stopPropagation(),children:[e.jsxs("div",{className:"flex items-center justify-between p-5 border-b border-border",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"w-10 h-10 rounded-xl bg-purple-50 flex items-center justify-center",children:e.jsx(de,{className:"w-5 h-5 text-purple-600"})}),e.jsx("h3",{className:"text-base font-bold",children:t==="edit"?"עריכת ברמן":"ברמן / מתנדב חדש"})]}),e.jsx("button",{onClick:j,className:"w-8 h-8 rounded-lg flex items-center justify-center text-slate-400 hover:bg-slate-100",children:e.jsx(H,{className:"w-5 h-5"})})]}),e.jsxs("div",{className:"p-5 space-y-3",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"שם *"}),e.jsx("input",{className:k,value:n.name,onChange:l=>D("name",l.target.value)})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"טלפון *"}),e.jsx("input",{className:k,value:n.phone,onChange:l=>D("phone",l.target.value),dir:"ltr",placeholder:"050-0000000"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"הרשאה"}),e.jsx("select",{className:k,value:n.role,onChange:l=>D("role",l.target.value),children:Object.entries(Be).map(([l,d])=>e.jsx("option",{value:l,children:d},l))})]}),e.jsxs("div",{children:[e.jsxs("label",{className:"block text-xs font-medium text-muted-foreground mb-1 flex items-center gap-1",children:[e.jsx(dt,{className:"w-3.5 h-3.5"})," קוד PIN (4 ספרות)",t==="edit"?" — השאירו ריק כדי לא לשנות":" *"]}),e.jsx("input",{className:`${k} tracking-[0.3em] text-center`,value:n.pin,onChange:l=>D("pin",l.target.value.replace(/\D/g,"").slice(0,4)),inputMode:"numeric",maxLength:4,placeholder:"••••"})]}),e.jsxs("label",{className:"flex items-center gap-2 text-sm cursor-pointer pt-1",children:[e.jsx(G,{checked:n.is_active,onCheckedChange:l=>D("is_active",l)})," פעיל"]})]}),e.jsxs("div",{className:"flex items-center justify-between gap-2 p-5 border-t border-border",children:[e.jsx(F,{variant:"outline",onClick:j,className:"rounded-full",children:"ביטול"}),e.jsxs(F,{onClick:p,disabled:r,className:"rounded-full gap-1.5 bg-amber-600 hover:bg-amber-700",children:[e.jsx(I,{className:"w-4 h-4"})," ",r?"שומר…":"שמירה"]})]})]})})}const Pe={card_purchase:"רכישת כרטסייה",bonus:"בונוס כרטסייה",order_charge:"חיוב הזמנה",order_refund:"החזר הזמנה",partial_refund:"החזר חלקי",admin_credit:"זיכוי מנהל",manual_charge:"חיוב ידני",balance_fix:"תיקון יתרה",purchase_cancel:"ביטול רכישה"};function Lt({payments:t,orders:a,wallets:b,kibbutzId:j}){const[C,n]=m.useState(null),[f,r]=m.useState("month"),[P,D]=m.useState("all"),[p,l]=m.useState(!1);m.useEffect(()=>{let s=!0;return $.entities.PubWalletTransaction.filter({kibbutz_id:j},"-created_date",2e3).then(_=>{s&&n(_||[])}).catch(()=>{s&&n([])}),()=>{s=!1}},[j]);const d=m.useMemo(()=>Object.fromEntries((a||[]).map(s=>[s.id,s])),[a]),h=m.useCallback(s=>{if(f==="all")return!0;
3if(!s)return!1;const _=new Date(s),E=new Date;if(f==="today")return Q(s);if(f==="month")return ie(s);if(f==="week"){const O=new Date(E);return O.setDate(E.getDate()-E.getDay()),O.setHours(0,0,0,0),_>=O}return!0},[f]),o=m.useMemo(()=>{const s=(t||[]).filter(i=>i.status==="completed"&&Q(i.processed_at||i.created_date)),_=(t||[]).filter(i=>i.status==="completed"&&ie(i.processed_at||i.created_date)),E=(C||[]).filter(i=>i.type==="order_charge"),O=(C||[]).filter(i=>["order_refund","partial_refund"].includes(i.type));return{collectToday:s.reduce((i,w)=>i+(Number(w.amount_paid_agorot)||0),0),collectMonth:_.reduce((i,w)=>i+(Number(w.amount_paid_agorot)||0),0),redeemToday:E.filter(i=>Q(i.created_date)).reduce((i,w)=>i+Math.abs(Number(w.amount_agorot)||0),0),redeemMonth:E.filter(i=>ie(i.created_date)).reduce((i,w)=>i+Math.abs(Number(w.amount_agorot)||0),0),refunds:O.reduce((i,w)=>i+Math.abs(Number(w.amount_agorot)||0),0),openBalance:(b||[]).reduce((i,w)=>i+(Number(w.balance_agorot)||0),0),orderCount:(a||[]).filter(i=>i.status==="supplied").length}},[t,C,b,a]),S=o.orderCount?Math.round(o.redeemMonth/o.orderCount):0,A=m.useMemo(()=>{const s=(C||[]).filter(E=>h(E.created_date));return P==="all"?s:P==="collection"?s.filter(E=>["card_purchase","bonus"].includes(E.type)):P==="redemption"?s.filter(E=>E.type==="order_charge"):P==="refunds"?s.filter(E=>["order_refund","partial_refund"].includes(E.type)):s},[C,h,P]),v=()=>{const s=["תאריך","סוג","סכום (₪)","יתרה לפני","יתרה אחרי","הזמנה","תושב","הערה"],_=A.map(w=>{const c=w.order_id?d[w.order_id]:null;return[new Date(w.created_date).toLocaleString("he-IL"),Pe[w.type]||w.type,(Number(w.amount_agorot)||0)/100,(Number(w.balance_before_agorot)||0)/100,(Number(w.balance_after_agorot)||0)/100,(c==null?void 0:c.order_number)||"",(c==null?void 0:c.resident_name)||"",(w.note||"").replace(/[\n,]/g," ")].map(N=>`"${String(N).replace(/"/g,'""')}"`).join(",")}),E="\uFEFF"+[s.join(","),..._].join(`
4`),O=URL.createObjectURL(new Blob([E],{type:"text/csv;charset=utf-8;"})),i=document.createElement("a");i.href=O,i.download=`pub-income-${new Date().toISOString().slice(0,10)}.csv`,i.click(),URL.revokeObjectURL(O)},g=[{label:"גבייה היום",value:o.collectToday,hint:"רכישת כרטסיות",color:"text-emerald-700",bg:"bg-emerald-50"},{label:"גבייה החודש",value:o.collectMonth,hint:"רכישת כרטסיות",color:"text-emerald-700",bg:"bg-emerald-50"},{label:"מימושים היום",value:o.redeemToday,hint:"חיובי הזמנות (לא הכנסה חדשה)",color:"text-blue-700",bg:"bg-blue-50"},{label:"מימושים החודש",value:o.redeemMonth,hint:"חיובי הזמנות",color:"text-blue-700",bg:"bg-blue-50"},{label:"זיכויים",value:o.refunds,hint:"החזרים לתושבים",color:"text-rose-700",bg:"bg-rose-50"},{label:"יתרות פתוחות",value:o.openBalance,hint:"יתרות התושבים בפאב",color:"text-amber-700",bg:"bg-amber-50"}];return e.jsxs("div",{children:[e.jsx("div",{className:"grid grid-cols-2 md:grid-cols-3 lg:grid-cols-6 gap-2.5 mb-4",children:g.map((s,_)=>e.jsxs("div",{className:"rounded-2xl border border-emerald-500 bg-white p-3",children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:s.label}),e.jsx("p",{className:`text-xl font-black ${s.color} mt-1`,children:B(s.value)}),e.jsx("p",{className:"text-[11px] text-slate-500 mt-0.5 leading-tight",children:s.hint})]},_))}),e.jsxs("div",{className:"flex items-center justify-between gap-2 mb-3 flex-wrap",children:[e.jsxs("div",{className:"text-sm text-muted-foreground",children:[o.orderCount," הזמנות סופקו · ממוצע ",B(S)," להזמנה"]}),e.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[e.jsxs("select",{className:"h-9 rounded-xl border border-border bg-white px-2 text-sm",value:f,onChange:s=>r(s.target.value),children:[e.jsx("option",{value:"today",children:"היום"}),e.jsx("option",{value:"week",children:"השבוע"}),e.jsx("option",{value:"month",children:"החודש"}),e.jsx("option",{value:"all",children:"הכל"})]}),e.jsxs("select",{className:"h-9 rounded-xl border border-border bg-white px-2 text-sm",value:P,onChange:s=>D(s.target.value),children:[e.jsx("option",{value:"all",children:"כל התנועות"}),e.jsx("option",{value:"collection",children:"רכישות כרטסיות"}),e.jsx("option",{value:"redemption",children:"מימושי הזמנות"}),e.jsx("option",{value:"refunds",children:"זיכויים"})]}),e.jsxs(F,{variant:"outline",onClick:v,className:"rounded-full gap-1.5 h-9",children:[e.jsx(Ne,{className:"w-4 h-4"})," ייצוא לאקסל"]}),e.jsxs(F,{onClick:()=>l(!0),className:"rounded-full gap-1.5 h-9 bg-emerald-600 hover:bg-emerald-700",children:[e.jsx(se,{className:"w-4 h-4"})," זיכוי ידני"]})]})]}),p&&e.jsx(Ft,{kibbutzId:j,onClose:()=>l(!1),onDone:()=>l(!1)}),C==null?e.jsx("div",{className:"flex justify-center py-10",children:e.jsx(z,{className:"w-6 h-6 animate-spin text-amber-500"})}):A.length===0?e.jsx("div",{className:"bg-white rounded-2xl border border-border shadow-sm p-10 text-center text-sm text-muted-foreground",children:"אין תנועות לתקופה שנבחרה"}):e.jsx("div",{className:"bg-white rounded-2xl border border-border shadow-sm overflow-hidden",children:e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"text-xs text-muted-foreground bg-slate-50 border-b border-border",children:[e.jsx("th",{className:"text-right font-medium px-3 py-2.5",children:"תאריך"}),e.jsx("th",{className:"text-right font-medium px-3 py-2.5",children:"סוג"}),e.jsx("th",{className:"text-right font-medium px-3 py-2.5",children:"סכום"}),e.jsx("th",{className:"text-right font-medium px-3 py-2.5",children:"יתרה אחרי"}),e.jsx("th",{className:"text-right font-medium px-3 py-2.5",children:"הזמנה"}),e.jsx("th",{className:"text-right font-medium px-3 py-2.5",children:"תושב"})]})}),e.jsx("tbody",{children:A.slice(0,500).map(s=>{const _=s.order_id?d[s.order_id]:null,E=(Number(s.amount_agorot)||0)<0;return e.jsxs("tr",{className:"border-b border-slate-50 hover:bg-slate-50/60",children:[e.jsx("td",{className:"px-3 py-2 text-muted-foreground whitespace-nowrap",children:new Date(s.created_date).toLocaleDateString("he-IL")}),e.jsx("td",{className:"px-3 py-2",children:Pe[s.type]||s.type}),e.jsxs("td",{className:`px-3 py-2 font-semibold ${E?"text-rose-600":"text-emerald-700"}`,children:[E?"−":"+",B(Math.abs(Number(s.amount_agorot)||0))]}),e.jsx("td",{className:"px-3 py-2 text-muted-foreground",children:B(s.balance_after_agorot)}),e.jsx("td",{className:"px-3 py-2 text-muted-foreground",children:_!=null&&_.order_number?`#${_.order_number}`:"—"}),e.jsx("td",{className:"px-3 py-2 text-muted-foreground",children:(_==null?void 0:_.resident_name)||"—"})]},s.id)})})]})})})]})}const Et=["א","ב","ג","ד","ה","ו","ש"],he={planned:{label:"מתוכננת",badge:"bg-slate-100 text-slate-600"},open:{label:"פתוחה לשיבוץ",badge:"bg-amber-50 text-amber-700"},partially_staffed:{label:"מאוישת חלקית",badge:"bg-amber-50 text-amber-700"},staffed:{label:"מאוישת",badge:"bg-emerald-50 text-emerald-600"},active:{label:"פעילה",badge:"bg-blue-50 text-blue-700"},completed:{label:"הושלמה",badge:"bg-slate-100 text-slate-500"},cancelled:{label:"בוטלה",badge:"bg-rose-50 text-rose-600"}},At=(t,a,b)=>`${t}-${String(a+1).padStart(2,"0")}-${String(b).padStart(2,"0")}`;function qt({kibbutzId:t}){const a=new Date,[b,j]=m.useState({y:a.getFullYear(),m:a.getMonth()}),[C,n]=m.useState(null),[f,r]=m.useState([]),[P,D]=m.useState([]),[p,l]=m.useState(null),[d,h]=m.useState(null),o=m.useCallback(async()=>{try{const[u,L,M]=await Promise.all([$.entities.PubShift.filter({kibbutz_id:t},"-start_at",500).catch(()=>[]),$.entities.PubShiftAssignment.filter({kibbutz_id:t}
4,"-created_date",2e3).catch(()=>[]),$.functions.invoke("pubStaffList",{community_id:t}).then(U=>{var X;return((X=U==null?void 0:U.data)==null?void 0:X.staff)||[]}).catch(()=>[])]);n(u||[]),r(L||[]),D(M||[])}catch{n([])}},[t]);m.useEffect(()=>{o()},[o]);const S=m.useCallback(u=>f.filter(L=>L.shift_id===u),[f]),A=m.useCallback(u=>{var L;return((L=P.find(M=>M.id===u))==null?void 0:L.name)||"—"},[P]),v=new Date(b.y,b.m,1),g=v.getDay(),s=new Date(b.y,b.m+1,0).getDate(),_=[...Array(g).fill(null),...Array.from({length:s},(u,L)=>L+1)],E=new Intl.DateTimeFormat("he-IL",{month:"long",year:"numeric"}).format(v),O=m.useMemo(()=>{const u={};return(C||[]).forEach(L=>{const M=String(L.start_at||"").slice(0,10);M&&(u[M]=u[M]||[]).push(L)}),u},[C]),i=(C||[]).filter(u=>String(u.start_at||"").slice(0,7)===`${b.y}-${String(b.m+1).padStart(2,"0")}`).sort((u,L)=>String(u.start_at).localeCompare(String(L.start_at))),w=u=>j(L=>{const M=new Date(L.y,L.m+u,1);return{y:M.getFullYear(),m:M.getMonth()}}),c=async u=>{try{for(const L of S(u.id))await $.entities.PubShiftAssignment.delete(L.id);await $.entities.PubShift.delete(u.id),T.success("המשמרת נמחקה"),o()}catch{T.error("שגיאה במחיקה")}finally{h(null)}},N=u=>String(u||"").slice(11,16);return e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center justify-between mb-4",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("button",{onClick:()=>w(-1),className:"w-9 h-9 rounded-lg flex items-center justify-center text-slate-500 hover:bg-slate-100",children:e.jsx(Le,{className:"w-4 h-4"})}),e.jsx("span",{className:"text-sm font-bold w-32 text-center",children:E}),e.jsx("button",{onClick:()=>w(1),className:"w-9 h-9 rounded-lg flex items-center justify-center text-slate-500 hover:bg-slate-100",children:e.jsx(qe,{className:"w-4 h-4"})})]}),e.jsxs(F,{onClick:()=>l({mode:"create"}),className:"rounded-full gap-1.5 bg-amber-600 hover:bg-amber-700 h-10",children:[e.jsx(Z,{className:"w-4 h-4"})," הוספת משמרת"]})]}),C==null?e.jsx("div",{className:"flex justify-center py-10",children:e.jsx(z,{className:"w-6 h-6 animate-spin text-amber-500"})}):e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"bg-white rounded-2xl border border-border shadow-sm p-4 mb-4",children:e.jsxs("div",{className:"grid grid-cols-7 gap-1 text-center",children:[Et.map((u,L)=>e.jsx("div",{className:"text-[11px] font-medium text-slate-400 py-1",children:u},L)),_.map((u,L)=>{if(u==null)return e.jsx("div",{},L);const M=At(b.y,b.m,u),U=O[M]||[],X=M===new Date().toISOString().slice(0,10);return e.jsxs("div",{className:`min-h-[52px] rounded-lg border p-1 text-right ${X?"ring-2 ring-amber-300 border-transparent":"border-slate-100"}`,children:[e.jsx("span",{className:"text-xs text-slate-500",children:u}),U.slice(0,2).map(K=>e.jsxs("button",{onClick:()=>l({mode:"edit",shift:K}),className:"block w-full mt-0.5 truncate text-[10px] px-1 py-0.5 rounded bg-amber-100 text-amber-800 text-right",children:[N(K.start_at)," ",K.title||"משמרת"]},K.id)),U.length>2&&e.jsxs("span",{className:"text-[10px] text-slate-400",children:["+",U.length-2]})]},L)})]})}),e.jsxs("p",{className:"text-xs font-bold text-slate-500 mb-2",children:["משמרות ב",E]}),i.length===0?e.jsx("div",{className:"bg-white rounded-2xl border border-border shadow-sm p-8 text-center text-sm text-muted-foreground",children:"אין משמרות בחודש זה"}):e.jsx("div",{className:"space-y-2",children:i.map(u=>{const L=S(u.id),M=he[u.status]||he.planned;return e.jsxs("div",{className:"flex items-center gap-3 rounded-xl border border-border bg-white px-4 py-3",children:[e.jsxs("div",{className:"w-11 h-11 rounded-xl bg-amber-50 flex flex-col items-center justify-center flex-shrink-0",children:[e.jsx("span",{className:"text-[10px] text-amber-700",children:new Date(u.start_at).toLocaleDateString("he-IL",{month:"short"})}),e.jsx("span",{className:"text-sm font-black text-amber-700",children:new Date(u.start_at).getDate()})]}),e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsxs("p",{className:"text-sm font-bold text-foreground truncate",children:[u.title||"משמרת"," ",e.jsxs("span",{className:"text-xs font-normal text-muted-foreground",children:["· ",N(u.start_at),"–",N(u.end_at)]})]}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:[L.length,"/",u.required_staff_count||L.length," משובצים",L.length?` · ${L.map(U=>
4A(U.staff_id)).slice(0,3).join(", ")}`:""]})]}),e.jsx("span",{className:`inline-flex px-2 py-0.5 rounded-full text-xs font-medium ${M.badge}`,children:M.label}),e.jsx("button",{onClick:()=>l({mode:"edit",shift:u}),className:"w-8 h-8 rounded-lg flex items-center justify-center text-slate-500 hover:bg-slate-100",children:e.jsx(ae,{className:"w-4 h-4"})}),d===u.id?e.jsxs(e.Fragment,{children:[e.jsx("button",{onClick:()=>c(u),className:"h-8 px-2 rounded-lg text-xs bg-red-600 text-white",children:"מחק"}),e.jsx("button",{onClick:()=>h(null),className:"w-8 h-8 rounded-lg flex items-center justify-center text-slate-500 hover:bg-slate-100",children:e.jsx(H,{className:"w-4 h-4"})})]}):e.jsx("button",{onClick:()=>h(u.id),className:"w-8 h-8 rounded-lg flex items-center justify-center text-red-500 hover:bg-red-50",children:e.jsx(re,{className:"w-4 h-4"})})]},u.id)})})]}),p&&e.jsx(Bt,{mode:p.mode,shift:p.shift,staff:P,assignments:p.shift?S(p.shift.id):[],kibbutzId:t,onClose:()=>l(null),onSaved:()=>{l(null),o()}})]})}function Bt({mode:t,shift:a,staff:b,assignments:j,kibbutzId:C,onClose:n,onSaved:f}){const[r,P]=m.useState(()=>a?{date:String(a.start_at||"").slice(0,10),start_time:String(a.start_at||"").slice(11,16)||"18:00",end_time:String(a.end_at||"").slice(11,16)||"23:00",title:a.title||"",manager_staff_id:a.manager_staff_id||"",required_staff_count:String(a.required_staff_count??2),notes:a.notes||"",status:a.status||"planned",assigned:j.map(o=>o.staff_id)}:{date:new Date().toISOString().slice(0,10),start_time:"18:00",end_time:"23:00",title:"",manager_staff_id:"",required_staff_count:"2",notes:"",status:"planned",assigned:[]}),[D,p]=m.useState(!1),l=(o,S)=>P(A=>({...A,[o]:S})),d=o=>P(S=>({...S,assigned:S.assigned.includes(o)?S.assigned.filter(A=>A!==o):[...S.assigned,o]})),h=async()=>{if(!r.date){T.error("יש לבחור תאריך");return}p(!0);const o=`${r.date}T${r.start_time}:00`,S=`${r.date}T${r.end_time}:00`,A=r.required_staff_count!==""?Number(r.required_staff_count):null,v=r.assigned.length===0?"open":A&&r.assigned.length>=A?"staffed":"partially_staffed",g={kibbutz_id:C,title:r.title||void 0,start_at:o,end_at:S,manager_staff_id:r.manager_staff_id||void 0,required_staff_count:A??void 0,notes:r.notes||void 0,status:r.status==="planned"?v:r.status};try{let s=a==null?void 0:a.id;s?await $.entities.PubShift.update(s,g):s=(await $.entities.PubShift.create(g)).id;for(const _ of j)await $.entities.PubShiftAssignment.delete(_.id);for(const _ of r.assigned)await $.entities.PubShiftAssignment.create({kibbutz_id:C,shift_id:s,staff_id:_,status:"assigned"});T.success(t==="edit"?"המשמרת עודכנה":"המשמרת נוספה"),f()}catch{T.error("שגיאה בשמירה")}finally{p(!1)}};return e.jsx("div",{className:"fixed inset-0 z-50 flex items-start justify-center bg-black/40 p-4 overflow-y-auto",onClick:n,children:e.jsxs("div",{className:"bg-white rounded-2xl shadow-xl w-full max-w-lg my-8",dir:"rtl",onClick:o=>o.stopPropagation(),children:[e.jsxs("div",{className:"flex items-center justify-between p-5 border-b border-border",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"w-10 h-10 rounded-xl bg-amber-50 flex items-center justify-center",children:e.jsx(Ee,{className:"w-5 h-5 text-amber-600"})}),e.jsx("h3",{className:"text-base font-bold",children:t==="edit"?"עריכת משמרת":"משמרת חדשה"})]}),e.jsx("button",{onClick:n,className:"w-8 h-8 rounded-lg flex items-center justify-center text-slate-400 hover:bg-slate-100",children:e.jsx(H,{className:"w-5 h-5"})})]}),e.jsxs("div",{className:"p-5 space-y-3",children:[e.jsxs("div",{className:"grid grid-cols-3 gap-3",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"תאריך *"}),e.jsx("input",{type:"date",className:k,value:r.date,onChange:o=>l("date",o.target.value)})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"משעה"}),e.jsx("input",{type:"time",className:k,value:r.start_time,onChange:o=>l("start_time",o.target.value)})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"עד שעה"}),e.jsx("input",{type:"time",className:k,value:r.end_time,onChange:o=>l("end_time",o.target.value)})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"שם המשמרת"}),e.jsx("input",{className:k,value:r.title,onChange:o=>l("title",o.target.value),placeholder:"לדוגמה: משמרת ערב"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"מספר עובדים נדרש"}),e.jsx("input",{type:"number",min:"0",className:k,value:r.required_staff_count,onChange:o=>l("required_staff_count",o.target.value)})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"אחראי משמרת"}),e.jsxs("select",{className:k,value:r.manager_staff_id,onChange:o=>l("manager_staff_id",o.target.value),children:[e.jsx("option",{value:"",children:"ללא"}),b.map(o=>e.jsx("option",{value:o.id,children:o.name},o.id))]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"סטטוס"}),e.jsx("select",{className:k,value:r.status,onChange:o=>l("status",o.target.value),children:Object.entries(he).map(([o,S])=>e.jsx("option",{value:o,children:S.label},o))})]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1.5",children:"שיבוץ ברמנים ומתנדבים"}),b.length===0?e.jsx("p",{className:"text-xs text-muted-foreground",children:'אין ברמנים — הוסיפו דרך "ניהול ברמנים".'}):e.jsx("div",{className:"flex flex-wrap gap-1.5",children:b.map(o=>e.jsx("button",{type:"button",onClick:()=>d(o.id),className:`px-3 py-1.5 rounded-full text-xs font-medium border transition-all ${r.assigned.includes(o.id)?"bg-amber-100 text-amber-700 border-amber-300":"bg-white text-slate-600 border-border hover:bg-accent"}`,children:o.name},o.id))})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"הערות"}),e.jsx("input",{className:k,value:r.notes,onChange:o=>l("notes",o.target.value)})]})]}),e.jsxs("div",{className:"flex items-center justify-between gap-2 p-5 border-t border-border",children:[e.jsx(F,{variant:"outline",onClick:n,className:"rounded-full",children:"ביטול"}),e.jsxs(F,{onClick:h,disabled:D,className:"rounded-full gap-1.5 bg-amber-600 hover:bg-amber-700",children:[e.jsx(I,{className:"w-4 h-4"})," ",D?"שומר…":"שמירה"]})]})]})})}function Ft({kibbutzId:t,onClose:a,onDone:b}){const[j,C]=m.useState(""),[n,f]=m.useState([]),[r,P]=m.useState(null),[D,p]=m.useState(""),[l,d]=m.useState("admin_credit"),[h,o]=m.useState(""),[S,A]=m.useState(!1);m.useEffect(()=>{if(r||j.trim().length<2){f([]);return}let g=!0;const s=setTimeout(()=>{$.functions.invoke("pubResidentSearch",{q:j,community_id:t}).then(_=>{var E;g&&f(((E=_==null?void 0:_.data)==null?void 0:E.residents)||[])}).catch(()=>{})},250);return()=>{g=!1,clearTimeout(s)}},[j,r]);const v=async()=>{var s;if(!r){T.error("בחרו תושב");return}const g=W(D);if(!g||g<=0){T.error("יש להזין סכום");return}A(!0);try{const _=await $.functions.invoke("pubWalletAdjust",{resident_phone:r.phone,amount_agorot:g,type:l,note:h,community_id:t});T.success(`בוצע · יתרה חדשה: ${B((s=_==null?void 0:_.data)==null?void 0:s.balance_agorot)}`),b()}catch(_){T.error((_==null?void 0:_.message)||"שגיאה בפעולה")}finally{A(!1)}};return e.jsx("div",{className:"fixed inset-0 z-50 flex items-start justify-center bg-black/40 p-4 overflow-y-auto",onClick:a,children:e.jsxs("div",{className:"bg-white rounded-2xl shadow-xl w-full max-w-md my-8",dir:"rtl",onClick:g=>g.stopPropagation(),children:[e.jsxs("div",{className:"flex items-center justify-between p-5 border-b border-border",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"w-10 h-10 rounded-xl bg-emerald-50 flex items-center justify-center",children:e.jsx(se,{className:"w-5 h-5 text-emerald-600"})}),e.jsx("h3",{className:"text-base font-bold",children:"זיכוי / חיוב ידני ליתרת פאב"})]}),e.jsx("button",{onClick:a,className:"w-8 h-8 rounded-lg flex items-center justify-center text-slate-400 hover:bg-slate-100",children:e.jsx(H,{className:"w-5 h-5"})})]}),e.jsxs("div",{className:"p-5 space-y-3",children:[r?e.jsxs("div",{className:"flex items-center justify-between rounded-xl bg-slate-50 px-3 py-2",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-bold",children:r.full_name}),e.jsx("p",{className:"text-xs text-slate-500",dir:"ltr",children:r.phone})]}),e.jsx("button",{onClick:()=>{P(null),C("")},className:"text-xs text-rose-600 hover:underline",children:"שינוי"})]}):e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"חיפוש תושב (שם או טלפון)"}),e.jsx("input",{className:k,value:j,onChange:g=>C(g.target.value),placeholder:"לפחות 2 תווים…"}),n.length>0&&e.jsx("div",{className:"mt-1 border border-border rounded-xl divide-y divide-slate-100 max-h-48 overflow-y-auto",children:n.map(g=>e.jsxs("button",{onClick:()=>P(g),className:"w-full text-right px-3 py-2 text-sm hover:bg-slate-50 flex items-center justify-between",children:[e.jsx("span",{className:"font-medium",children:g.full_name}),e.jsx("span",{className:"text-xs text-slate-400",dir:"ltr",children:g.phone})]},g.id))})]}),e.jsx("div",{className:"flex gap-2",children:[{v:"admin_credit",l:"זיכוי (הוספה)"},{v:"manual_charge",l:"חיוב (הפחתה)"}].map(g=>e.jsx("button",{type:"button",onClick:()=>d(g.v),className:`flex-1 h-10 rounded-xl border text-sm font-medium transition-all ${l===g.v?g.v==="manual_charge"?"bg-rose-600 text-white border-transparent":"bg-emerald-600 text-white border-transparent":"bg-white border-border text-foreground hover:bg-accent"}`,children:g.l},g.v))}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"סכום (₪)"}),e.jsx("input",{type:"number",min:"0",step:"1",className:k,value:D,onChange:g=>p(g.target.value)})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-muted-foreground mb-1",children:"הערה"}),e.jsx("input",{className:k,value:h,onChange:g=>o(g.target.value),placeholder:"לדוגמה: טעינת בדיקה"})]}),e.jsx("p",{className:"text-[11px] text-slate-400",children:"הפעולה נרשמת בתנועות הארנק ושולחת התראה לתושב."})]}),e.jsxs("div",{className:"flex items-center justify-between gap-2 p-5 border-t border-border",children:[e.jsx(F,{variant:"outline",onClick:a,className:"rounded-full",children:"ביטול"}),e.jsxs(F,{onClick:v,disabled:S||!r,className:"rounded-full gap-1.5 bg-emerald-600 hover:bg-emerald-700",children:[e.jsx(I,{className:"w-4 h-4"})," ",S?"מבצע…":"ביצוע"]})]})]})})}function Ot({products:t,settings:a}){var n,f;if(((f=(n=a==null?void 0:a.settings)==null?void 0:n.inventory)==null?void 0:f.low_stock_alert)===!1)return null;const b=(t||[]).filter(r=>r.is_active!==!1&&ee(r).key==="out"),j=(t||[]).filter(r=>r.is_active!==!1&&ee(r).key==="low");
4if(b.length===0&&j.length===0)return null;const C=[...b,...j].slice(0,10).map(r=>r.name).filter(Boolean).join(" · ");return e.jsxs("div",{className:"mb-4 rounded-2xl border border-amber-200 bg-amber-50 p-4 flex items-start gap-3",children:[e.jsx("span",{className:"w-9 h-9 rounded-lg bg-amber-100 flex items-center justify-center flex-shrink-0",children:e.jsx(ge,{className:"w-5 h-5 text-amber-600"})}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("p",{className:"text-sm font-bold text-amber-800",children:["התראת מלאי",b.length>0?` · ${b.length} אזלו`:"",j.length>0?` · ${j.length} במלאי נמוך`:""]}),e.jsxs("p",{className:"text-xs text-amber-700 mt-0.5 line-clamp-2",children:[C,b.length+j.length>10?" …":""]})]})]})}const xe={new:{label:"חדשה",badge:"bg-blue-100 text-blue-700",dot:"bg-blue-500"},in_preparation:{label:"בהכנה",badge:"bg-amber-100 text-amber-700",dot:"bg-amber-500"},ready:{label:"מוכנה",badge:"bg-emerald-100 text-emerald-700",dot:"bg-emerald-500"},supplied:{label:"סופקה",badge:"bg-slate-100 text-slate-600",dot:"bg-slate-400"},cancelled:{label:"בוטלה",badge:"bg-rose-100 text-rose-700",dot:"bg-rose-500"},partially_cancelled:{label:"זוכתה חלקית",badge:"bg-orange-100 text-orange-700",dot:"bg-orange-500"}},De={new:"התחל הכנה",in_preparation:"סמן כמוכנה",ready:"סמן כסופקה"},Te=[{key:"all",label:"הכל"},{key:"new",label:"חדשות"},{key:"in_preparation",label:"בהכנה"},{key:"ready",label:"מוכנות"},{key:"supplied",label:"סופקו"},{key:"cancelled",label:"בוטלו"}],$e=t=>t?new Date(t).toLocaleString("he-IL",{day:"2-digit",month:"2-digit",hour:"2-digit",minute:"2-digit"}):"";function Rt({orders:t,kibbutzId:a,onRefresh:b}){const[j,C]=m.useState("all"),[n,f]=m.useState(""),[r,P]=m.useState(null),[D,p]=m.useState({}),[l,d]=m.useState(null),[h,o]=m.useState(null),[S,A]=m.useState(""),v=m.useMemo(()=>{const i={all:t.length};for(const w of Te)w.key!=="all"&&(i[w.key]=t.filter(c=>c.status===w.key).length);return i},[t]),g=m.useMemo(()=>{const i=n.trim().toLowerCase();return t.filter(w=>{if(j==="cancelled"){if(!["cancelled","partially_cancelled"].includes(w.status))return!1}else if(j!=="all"&&w.status!==j)return!1;return!(i&&!(`${w.order_number||""}`.toLowerCase().includes(i)||(w.resident_name||"").toLowerCase().includes(i)||(w.resident_phone||"").includes(i)))})},[t,j,n]),s=async i=>{var N;if((N=D[i.id])!=null&&N.items)return;p(u=>({...u,[i.id]:{loading:!0}}));const[w,c]=await Promise.all([$.entities.PubOrderItem.filter({kibbutz_id:a,order_id:i.id},"created_date",100).catch(()=>[]),$.entities.PubOrderStatusHistory.filter({kibbutz_id:a,order_id:i.id},"created_date",50).catch(()=>[])]);p(u=>({...u,[i.id]:{items:w||[],history:c||[],loading:!1}}))},_=i=>
4{if(r===i.id){P(null);return}P(i.id),s(i)},E=async i=>{d(i.id);try{await $.functions.invoke("pubManagerOrderAction",{community_id:a,order_id:i.id,action:"advance",expected_status:i.status}),T.success("ההזמנה עודכנה"),await b()}catch(w){T.error((w==null?void 0:w.message)||"שגיאה בעדכון ההזמנה")}finally{d(null)}},O=async()=>{var i;if(h){if(!S.trim()){T.error("יש להזין סיבת ביטול");return}d(h.id);try{const w=await $.functions.invoke("pubManagerOrderAction",{community_id:a,order_id:h.id,action:"cancel",reason:S.trim()}),c=Number((i=w==null?void 0:w.data)==null?void 0:i.refund_agorot)||0;T.success(`ההזמנה בוטלה${c>0?` · זוכה ${B(c)}`:""}`),o(null),A(""),await b()}catch(w){T.error((w==null?void 0:w.message)||"שגיאה בביטול ההזמנה")}finally{d(null)}}};return e.jsxs("div",{className:"bg-white rounded-2xl border border-border shadow-sm",children:[e.jsxs("div",{className:"p-4 border-b border-border flex items-center gap-3 flex-wrap",children:[e.jsx("span",{className:"w-9 h-9 rounded-lg bg-sky-50 flex items-center justify-center",children:e.jsx(be,{className:"w-5 h-5 text-sky-600"})}),e.jsx("h2",{className:"text-base font-bold text-foreground flex-1",children:"ניהול הזמנות"}),e.jsxs("div",{className:"relative",children:[e.jsx(je,{className:"absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-slate-400"}),e.jsx("input",{value:n,onChange:i=>f(i.target.value),placeholder:"מספר הזמנה / שם / טלפון…",className:"w-64 h-9 rounded-full border border-slate-200 bg-slate-50 pr-9 pl-3 text-sm focus:outline-none focus:border-amber-300 focus:bg-white"})]}),e.jsx("button",{onClick:b,className:"w-9 h-9 rounded-lg border border-slate-200 flex items-center justify-center text-slate-500 hover:bg-slate-50",title:"רענון",children:e.jsx(it,{className:"w-4 h-4"})})]}),e.jsx("div",{className:"px-4 pt-3 flex items-center gap-1.5 flex-wrap",children:Te.map(i=>{const w=j===i.key;return e.jsxs("button",{onClick:()=>C(i.key),className:`inline-flex items-center gap-1.5 h-8 px-3.5 rounded-full text-sm font-semibold transition-all ${w?"bg-amber-500 text-white":"bg-slate-100 text-slate-600 hover:bg-slate-200"}`,children:[i.label,e.jsx("span",{className:`text-xs ${w?"text-white/80":"text-slate-400"}`,children:v[i.key]??0})]},i.key)})}),e.jsx("div",{className:"p-4",children:g.length===0?e.jsx("div",{className:"py-14 text-center text-sm text-muted-foreground",children:"אין הזמנות להצגה"}):e.jsx("div",{className:"space-y-2",children:g.map(i=>{const w=xe[i.status]||xe.new,c=r===i.id,N=D[i.id],u=De[i.status],L=["new","in_preparation","ready"].includes(i.status);return e.jsxs("div",{className:`rounded-xl border transition-all ${c?"border-amber-300 bg-amber-50/30":"border-slate-200 bg-white"}`,children:[e.jsxs("button",{onClick:()=>_(i),className:"w-full flex items-center gap-3 p-3 text-right",children:[e.jsx("span",{className:`w-2.5 h-2.5 rounded-full flex-shrink-0 ${w.dot}`}),e.jsxs("span",{className:"font-bold text-slate-800 w-16",children:["#",i.order_number||"—"]}),e.jsxs("span",{className:"flex-1 min-w-0",children:[e.jsx("span",{className:"block text-sm font-semibold text-slate-800 truncate",children:i.resident_name||"תושב/ת"}),e.jsx("span",{className:"block text-xs text-slate-400",children:$e(i.created_date)})]}),e.jsx("span",{className:"text-sm font-black text-emerald-700",children:B(i.final_amount_agorot??i.subtotal_agorot)}),e.jsx("span",{className:`inline-flex items-center px-2.5 py-1 rounded-full text-xs font-bold ${w.badge}`,children:w.label}),c?e.jsx(qe,{className:"w-4 h-4 text-slate-400 -rotate-90"}):e.jsx(Le,{className:"w-4 h-4 text-slate-400"})]}),c&&e.jsx("div",{className:"px-4 pb-4 border-t border-amber-100 pt-3",children:N!=null&&N.loading?e.jsx("div",{className:"py-6 flex justify-center",children:e.jsx(z,{className:"w-5 h-5 animate-spin text-slate-400"})}):e.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-3 gap-4",children:[e.jsxs("div",{className:"lg:col-span-2",children:[e.jsx("p",{className:"text-xs font-bold text-slate-500 mb-2",children:"פריטים"}),e.jsx("div",{className:"rounded-lg border border-slate-200 overflow-hidden",children:e.jsx("table",{className:"w-full text-sm",children:e.jsx("tbody",{children:((N==null?void 0:N.items)||[]).map(M=>{const U=Number(M.refunded_quantity)||0;return e.jsxs("tr",{className:"border-b border-slate-100 last:border-0",children:[e.jsxs("td",{className:"px-3 py-2 text-slate-800",children:[M.product_name_snapshot,U>0&&e.jsxs("span",{className:"text-xs text-rose-500 mr-1",children:["(זוכה ",U,")"]})]}),e.jsxs("td",{className:"px-3 py-2 text-slate-500 text-center w-16",children:["×",M.quantity]}),e.jsx("td",{className:"px-3 py-2 font-semibold text-slate-700 text-left w-24",children:B(M.line_total_agorot)})]},M.id)})})})}),i.resident_note&&e.jsxs("p",{className:"mt-2 text-sm text-slate-600 bg-slate-50 rounded-lg px-3 py-2",children:[e.jsx("span",{className:"font-bold",children:"הערה: "}),i.resident_note]}),i.cancellation_reason&&e.jsxs("p",{className:"mt-2 text-sm text-rose-600 bg-rose-50 rounded-lg px-3 py-2",children:[e.jsx("span",{className:"font-bold",children:"סיבת ביטול: "}),i.cancellation_reason]})]}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"rounded-lg bg-slate-50 p-3 text-sm",children:[e.jsxs("div",{className:"flex justify-between text-slate-500",children:[e.jsx("span",{children:"סכום"}),e.jsx("span",{children:B(i.subtotal_agorot)})]}),Number(i.refund_amount_agorot)>0&&e.jsxs("div",{className:"flex justify-between text-rose-600",children:[e.jsx("span",{children:"זוכה"}),e.jsxs("span",{children:["−",B(i.refund_amount_agorot)]})]}),e.jsxs("div",{className:"flex justify-between font-black text-slate-800 mt-1 pt-1 border-t border-slate-200",children:[e.jsx("span",{children:"שולם"}),e.jsx("span",{children:B(i.final_amount_agorot??i.subtotal_agorot)})]})]}),i.resident_phone&&e.jsxs("a",{href:`tel:${i.resident_phone}`,className:"flex items-center gap-2 text-sm text-slate-600 hover:text-slate-900",children:[e.jsx(Ze,{className:"w-4 h-4",dir:"ltr"}),i.resident_phone]}),((N==null?void 0:N.history)||[]).length>0&&e.jsx("div",{className:"text-xs text-slate-400 space-y-0.5",children:N.history.map(M=>e.jsxs("div",{children:[$e(M.created_date)," · ",(xe[M.new_status]||{}).label||M.new_status]},M.id))}),e.jsxs("div",{className:"flex flex-col gap-2 pt-1",children:[u&&e.jsxs("button",{onClick:()=>E(i),disabled:l===i.id,className:"inline-flex items-center justify-center gap-2 h-10 rounded-xl bg-amber-500 text-white text-sm font-bold hover:bg-amber-600 disabled:opacity-50",children:[l===i.id?e.jsx(z,{className:"w-4 h-4 animate-spin"}):e.jsx(I,{className:"w-4 h-4"})," ",De[i.status]]}),L&&e.jsxs("button",{onClick:()=>{o(i),A("")},disabled:l===i.id,className:"inline-flex items-center justify-center gap-2 h-10 rounded-xl border border-rose-200 text-rose-600 text-sm font-bold hover:bg-rose-50 disabled:opacity-50",children:[e.jsx(ke,{className:"w-4 h-4"})," ביטול הזמנה"]})]})]})]})})]},i.id)})})}),h&&e.jsx("div",{className:"fixed inset-0 z-50 bg-black/40 flex items-center justify-center p-4",onClick:()=>!l&&o(null),children:e.jsxs("div",{className:"bg-white rounded-2xl shadow-xl w-full max-w-md p-5",dir:"rtl",onClick:i=>i.stopPropagation(),children:[e.jsxs("h3",{className:"text-base font-bold text-slate-900 mb-1",children:["ביטול הזמנה #",h.order_number]}),e.jsxs("p",{className:"text-sm text-muted-foreground mb-3",children:["ההזמנה תבוטל והתשלום (",B(h.final_amount_agorot??h.subtotal_agorot),") יזוכה ליתרת הפאב של התושב."]}),e.jsx("label",{className:"block text-xs font-bold text-slate-500 mb-1",children:"סיבת הביטול *"}),e.jsx("textarea",{value:S,onChange:i=>A(i.target.value),rows:3,className:"w-full rounded-xl border border-border bg-white px-3 py-2 text-sm focus:outline-none focus:border-amber-400",placeholder:"לדוגמה: המוצר אזל, ביטול לבקשת התושב"}),e.jsxs("div",{className:"flex gap-2 mt-4",children:[e.jsxs("button",{onClick:O,disabled:l===h.id,className:"flex-1 h-11 rounded-xl bg-rose-600 text-white font-bold hover:bg-rose-700 disabled:opacity-50 inline-flex items-center justify-center gap-2",children:[l===h.id?e.jsx(z,{className:"w-4 h-4 animate-spin"}):e.jsx(ke,{className:"w-4 h-4"})," בטל וזכה"]}),e.jsx("button",{onClick:()=>o(null),disabled:l===h.id,className:"h-11 px-5 rounded-xl border border-border text-slate-600 font-medium hover:bg-slate-50",children:"חזרה"})]})]})})]})}function ms({kibbutzId:t}){return e.jsx(et,{moduleKey:"community_pub",children:e.jsx(ct,{kibbutzId:t})})}
4export{ms as default};

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.