import{j as e}from"./vendor-query-BSpUEUJE.js";import{r as n,c as _s}from"./vendor-react-CWq2BoMo.js";import{C as $,d as O,a as Ne,f as ue,P as ze,b as Le}from"./card-Dd3vXi0j.js";import{c as de,a as P,I as pe,B as d,j as me,X as Is,R as Be,L as gs,J as je,b as fs,E as qs}from"./index-DVdSf46I.js";import{D as _e,a as Ie,c as qe,d as De,f as ke,F as Ns,m as oe,n as xe,o as ge,p as he,q as w,B as E,P as ne,Q as Re,a4 as Qe,r as Ds,i as He,a1 as Ue,s as Ts,t as ls,S as vs,V as Se,v as bs,w as ys,x as Ce,y as Y,z as ws,A as Z,E as Ms}from"./currencyConfig-Ch2cK6at.js";import{D as ns,a as cs,b as rs,c as be}from"./dropdown-menu-_tO9X-Op.js";import{M as Te,l as Me,t as Ps,v as As,L as ee,T as Ve,a as $e,N as is,O as Fs,Q as zs,R as Ls,V as Bs,X as Rs,A as Ge,c as Je,d as Ke,f as We,g as Xe,h as Ye,i as Ze,j as es,D as Vs,u as $s,b as ds,C as Os}from"./Index-CsHtvMhJ.js";import{u as Pe,C as Es,a as ve,S as Qs}from"./StocktakeStepIndicator-D7EjZMX_.js";import{C as os}from"./checkbox-be7Y7u6l.js";import{u as Hs}from"./usePhpSellerId-C6BnBLkv.js";import{F as Us}from"./folder-open-YbsM9hGS.js";import{P as Gs}from"./progress-DbdJg_K5.js";import{u as Ae}from"./useVarianceReasons-C1qsjLC9.js";import{A as ss}from"./alert-cV8dsncc.js";import{E as xs}from"./ellipsis-vertical-MEzxSsKu.js";import{f as hs,c as ms}from"./vendor-date-BDZqQjIb.js";import"./vendor-pdf-OPq5ZiHo.js";import"./vendor-ui-extra-CW6ezZiQ.js";import"./vendor-ui-core-BWFXPlYg.js";import"./vendor-charts-bmwX1u6y.js";import"./vendor-barcode-DhT-N8Id.js";/**
 * @license lucide-react v0.462.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const ks=de("ClipboardList",[["rect",{width:"8",height:"4",x:"8",y:"2",rx:"1",ry:"1",key:"tgr4d6"}],["path",{d:"M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2",key:"116196"}],["path",{d:"M12 11h4",key:"1jrz19"}],["path",{d:"M12 16h4",key:"n85exb"}],["path",{d:"M8 11h.01",key:"1dfujw"}],["path",{d:"M8 16h.01",key:"18s6g9"}]]);/**
 * @license lucide-react v0.462.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const Oe=de("FileCheck",[["path",{d:"M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z",key:"1rqfz7"}],["path",{d:"M14 2v4a2 2 0 0 0 2 2h4",key:"tnqrlb"}],["path",{d:"m9 15 2 2 4-4",key:"1grp1n"}]]);/**
 * @license lucide-react v0.462.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const Js=de("Layers",[["path",{d:"m12.83 2.18a2 2 0 0 0-1.66 0L2.6 6.08a1 1 0 0 0 0 1.83l8.58 3.91a2 2 0 0 0 1.66 0l8.58-3.9a1 1 0 0 0 0-1.83Z",key:"8b97xw"}],["path",{d:"m22 17.65-9.17 4.16a2 2 0 0 1-1.66 0L2 17.65",key:"dd6zsq"}],["path",{d:"m22 12.65-9.17 4.16a2 2 0 0 1-1.66 0L2 12.65",key:"ep9fru"}]]);/**
 * @license lucide-react v0.462.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const Ee=de("ListChecks",[["path",{d:"m3 17 2 2 4-4",key:"1jhpwq"}],["path",{d:"m3 7 2 2 4-4",key:"1obspn"}],["path",{d:"M13 6h8",key:"15sg57"}],["path",{d:"M13 12h8",key:"h98zly"}],["path",{d:"M13 18h8",key:"oe0vm4"}]]);/**
 * @license lucide-react v0.462.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const Ks=de("Pause",[["rect",{x:"14",y:"4",width:"4",height:"16",rx:"1",key:"zuxfzm"}],["rect",{x:"6",y:"4",width:"4",height:"16",rx:"1",key:"1okwgv"}]]);/**
 * @license lucide-react v0.462.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const Ws=de("SkipForward",[["polygon",{points:"5 4 15 12 5 20 5 4",key:"16p6eg"}],["line",{x1:"19",x2:"19",y1:"5",y2:"19",key:"futhcm"}]]);/**
 * @license lucide-react v0.462.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const us=de("SquareCheckBig",[["path",{d:"M21 10.5V19a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h12.5",key:"1uzm8b"}],["path",{d:"m9 11 3 3L22 4",key:"1pflzl"}]]);/**
 * @license lucide-react v0.462.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const js=de("Square",[["rect",{width:"18",height:"18",x:"3",y:"3",rx:"2",key:"afitv7"}]]);/**
 * @license lucide-react v0.462.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const Xs=de("WandSparkles",[["path",{d:"m21.64 3.64-1.28-1.28a1.21 1.21 0 0 0-1.72 0L2.36 18.64a1.21 1.21 0 0 0 0 1.72l1.28 1.28a1.2 1.2 0 0 0 1.72 0L21.64 5.36a1.2 1.2 0 0 0 0-1.72",key:"ul74o6"}],["path",{d:"m14 7 3 3",key:"1r5n42"}],["path",{d:"M5 6v4",key:"ilb8ba"}],["path",{d:"M19 14v4",key:"blhpug"}],["path",{d:"M10 2v2",key:"7u0qdc"}],["path",{d:"M7 8H3",key:"zfb6yr"}],["path",{d:"M21 16h-4",key:"1cnmox"}],["path",{d:"M11 3H9",key:"1obp7u"}]]),Ys=({open:c,onOpenChange:k,onSuccess:g})=>{const{createSession:h}=Pe(),{toast:S}=Te(),u=Me(),z=Hs(),{categories:o}=Ps(z),{products:x}=As(),[f,j]=n.useState(!1),[y,A]=n.useState(""),[C,N]=n.useState("full"),[r,F]=n.useState("all"),[m,L]=n.useState([]),[I,i]=n.useState([]),[M,D]=n.useState(""),[G,Q]=n.useState(""),ce=n.useMemo(()=>{const l={};return x.forEach(p=>{const q=p.category_id;q&&(l[q]=(l[q]||0)+1)}),l},[x]),J=n.useMemo(()=>{if(!x)return[];if(!M.trim())return x;const l=M.toLowerCase();return x.filter(p=>{var q,H,te;return((q=p.name)==null?void 0:q.toLowerCase().includes(l))||((H=p.barcode)==null?void 0:H.includes(l))||((te=p.sku)==null?void 0:te.toLowerCase().includes(l))})},[x,M]),B=n.useMemo(()=>r==="all"?x.length:r==="category"&&m.length>0?x.filter(l=>l.category_id&&m.includes(l.category_id)).length:r==="custom"?I.length:0,[r,m,I,x]);n.useEffect(()=>{c&&(A(""),N("full"),F("all"),L([]),i([]),D(""),Q(""))},[c]);const X=l=>{L(p=>p.includes(l)?p.filter(q=>q!==l):[...p,l])},K=()=>{L(o.map(l=>l.id))},_=()=>{L([])},re=l=>{i(p=>p.includes(l)?p.filter(q=>q!==l):[...p,l])},W=()=>{i(J.map(l=>l.id))},le=()=>{i([])},U=l=>{switch(l){case"full":return"جرد كامل";case"partial":return"جرد جزئي";case"cycle":return"جرد دوري";case"spot_check":return"فحص عشوائي";default:return l}},se=()=>{if(r==="all")return"جميع المنتجات";if(r==="category"){if(m.length===0)return"لم يتم اختيار فئات";if(m.length===1){const l=o.find(p=>p.id===m[0]);return(l==null?void 0:l.category_name)||"فئة واحدة"}return`${m.length} فئات`}return r==="custom"?I.length===0?"لم يتم اختيار منتجات":`${I.length} منتج`:r==="location"?"حسب الموقع":r},T=async l=>{if(r==="category"&&m.length===0){S({title:"يجب اختيار فئة واحدة على الأقل",variant:"destructive"});return}if(r==="custom"&&I.length===0){S({title:"يجب اختيار منتج واحد على الأقل",variant:"destructive"});return}j(!0);try{const p={name:y.trim()||void 0,type:C,scope_type:r,scope_categories:r==="category"?m:void 0,scope_products:r==="custom"?I:void 0,notes:G||void 0};await h(p)&&(S({title:l?"تم إنشاء وبدء الجلسة بنجاح":"تم إنشاء الجلسة بنجاح"}),g())}catch{S({title:"حدث خطأ أثناء إنشاء الجلسة",variant:"destructive"})}finally{j(!1)}};return e.jsx(_e,{open:c,onOpenChange:k,children:e.jsxs(Ie,{className:P("max-h-[90vh] flex flex-col",u?"w-[95vw] max-w-none":"max-w-2xl"),children:[e.jsx(qe,{children:e.jsxs(De,{className:"flex items-center gap-2",children:[e.jsx(ks,{className:"h-5 w-5"}),"إنشاء جلسة جرد جديدة"]})}),e.jsx(ke,{className:"flex-1 max-h-[60vh] px-1",children:e.jsxs("div",{className:"space-y-6 py-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsxs(ee,{className:"flex items-center gap-2",children:[e.jsx(Ns,{className:"h-4 w-4"}),"اسم الجرد"]}),e.jsx(pe,{placeholder:"مثال: جرد نهاية الشهر - ديسمبر 2025",value:y,onChange:l=>A(l.target.value)}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"إذا تركته فارغاً سيتم إنشاء اسم تلقائي"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs(ee,{className:"flex items-center gap-2",children:[e.jsx(Js,{className:"h-4 w-4"}),"نوع الجرد"]}),e.jsxs(oe,{value:C,onValueChange:l=>N(l),children:[e.jsx(xe,{children:e.jsx(ge,{})}),e.jsxs(he,{children:[e.jsx(w,{value:"full",children:"جرد كامل - جميع المنتجات"}),e.jsx(w,{value:"partial",children:"جرد جزئي - فئات محددة"}),e.jsx(w,{value:"cycle",children:"جرد دوري - مجدول"}),e.jsx(w,{value:"spot_check",children:"فحص عشوائي - تدقيق سريع"})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(ee,{children:"نطاق الجرد"}),e.jsxs(oe,{value:r||"all",onValueChange:l=>{F(l),l!=="category"&&L([]),l!=="custom"&&(i([]),D(""))},children:[e.jsx(xe,{children:e.jsx(ge,{})}),e.jsxs(he,{children:[e.jsx(w,{value:"all",children:"جميع المنتجات"}),e.jsx(w,{value:"category",children:"فئات محددة"}),e.jsx(w,{value:"custom",children:"منتجات محددة"}),e.jsx(w,{value:"location",children:"حسب الموقع"})]})]})]}),r==="category"&&e.jsxs("div",{className:"space-y-3 p-4 bg-muted/30 rounded-lg border border-dashed",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs(ee,{className:"flex items-center gap-2",children:[e.jsx(Us,{className:"h-4 w-4"}),"اختر الفئات للجرد"]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(d,{variant:"ghost",size:"sm",onClick:K,className:"h-7 text-xs gap-1",children:[e.jsx(us,{className:"h-3 w-3"}),"تحديد الكل"]}),e.jsxs(d,{variant:"ghost",size:"sm",onClick:_,className:"h-7 text-xs gap-1",children:[e.jsx(js,{className:"h-3 w-3"}),"إلغاء الكل"]})]})]}),e.jsx("div",{className:P("grid gap-2",u?"grid-cols-1":"grid-cols-2"),children:o.map(l=>{const p=ce[l.id]||0,q=m.includes(l.id);return e.jsxs("div",{onClick:()=>X(l.id),className:P("flex items-center gap-3 p-3 border rounded-lg cursor-pointer transition-colors",q?"border-primary bg-primary/10":"hover:bg-muted/50"),children:[e.jsx(os,{checked:q,className:"pointer-events-none"}),e.jsx("span",{className:"flex-1 font-medium truncate",children:l.category_name}),e.jsxs(E,{variant:"secondary",className:"shrink-0",children:[e.jsx(ne,{className:"h-3 w-3 ml-1"}),p]})]},l.id)})}),o.length===0&&e.jsx("p",{className:"text-sm text-muted-foreground text-center py-4",children:"لا توجد فئات متاحة"}),m.length>0&&e.jsxs("div",{className:"flex items-center justify-between pt-2 border-t",children:[e.jsxs("span",{className:"text-sm text-muted-foreground",children:["تم اختيار ",m.length," فئة"]}),e.jsxs(E,{variant:"default",className:"gap-1",children:[e.jsx(ne,{className:"h-3 w-3"}),B," منتج"]})]})]}),r==="custom"&&e.jsxs("div",{className:"space-y-3 p-4 bg-muted/30 rounded-lg border border-dashed",children:[e.jsxs("div",{className:"flex flex-col gap-2",children:[e.jsxs(ee,{className:"flex items-center gap-2",children:[e.jsx(ne,{className:"h-4 w-4"}),"اختر المنتجات للجرد"]}),e.jsx(pe,{placeholder:"ابحث بالاسم أو الباركود...",value:M,onChange:l=>D(l.target.value),className:"h-9"})]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("span",{className:"text-xs text-muted-foreground",children:[J.length," منتج متاح"]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(d,{variant:"ghost",size:"sm",onClick:W,className:"h-7 text-xs gap-1",children:[e.jsx(us,{className:"h-3 w-3"}),"تحديد الكل"]}),e.jsxs(d,{variant:"ghost",size:"sm",onClick:le,className:"h-7 text-xs gap-1",children:[e.jsx(js,{className:"h-3 w-3"}),"إلغاء الكل"]})]})]}),e.jsx(ke,{className:"h-48 border rounded-md",children:e.jsxs("div",{className:"p-2 space-y-1",children:[J.map(l=>{var H;const p=I.includes(l.id),q=(H=o.find(te=>te.id===l.category_id))==null?void 0:H.category_name;return e.jsxs("div",{onClick:()=>re(l.id),className:P("flex items-center gap-3 p-2 border rounded-lg cursor-pointer transition-colors",p?"border-primary bg-primary/10":"hover:bg-muted/50"),children:[e.jsx(os,{checked:p,className:"pointer-events-none shrink-0"}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:"font-medium text-sm truncate",children:l.name}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:[l.barcode||"بدون باركود"," | المخزون: ",l.stock_quantity??0]})]}),q&&e.jsx(E,{variant:"outline",className:"shrink-0 text-xs",children:q})]},l.id)}),J.length===0&&e.jsx("p",{className:"text-sm text-muted-foreground text-center py-4",children:"لا توجد منتجات مطابقة للبحث"})]})}),I.length>0&&e.jsxs("div",{className:"flex items-center justify-between pt-2 border-t",children:[e.jsxs("span",{className:"text-sm text-muted-foreground",children:["تم اختيار ",I.length," منتج"]}),e.jsxs(E,{variant:"default",className:"gap-1",children:[e.jsx(ne,{className:"h-3 w-3"}),I.length," منتج"]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(ee,{htmlFor:"notes",children:"ملاحظات (اختياري)"}),e.jsx(Re,{id:"notes",placeholder:"أي ملاحظات إضافية...",value:G,onChange:l=>Q(l.target.value),rows:3})]}),e.jsx($,{className:"bg-muted/30 border-dashed",children:e.jsxs(O,{className:"p-4",children:[e.jsxs("h4",{className:"font-medium mb-3 flex items-center gap-2",children:[e.jsx(Es,{className:"h-4 w-4"}),"ملخص الجرد"]}),e.jsxs("div",{className:"space-y-2 text-sm",children:[e.jsxs("div",{className:"flex justify-between",children:[e.jsx("span",{className:"text-muted-foreground",children:"الاسم:"}),e.jsx("span",{className:"font-medium",children:y||"سيتم إنشاء اسم تلقائي"})]}),e.jsxs("div",{className:"flex justify-between",children:[e.jsx("span",{className:"text-muted-foreground",children:"النوع:"}),e.jsx(E,{variant:"outline",children:U(C)})]}),e.jsxs("div",{className:"flex justify-between",children:[e.jsx("span",{className:"text-muted-foreground",children:"النطاق:"}),e.jsx("span",{children:se()})]}),e.jsxs("div",{className:"flex justify-between items-center pt-2 border-t",children:[e.jsx("span",{className:"text-muted-foreground",children:"المنتجات المتوقعة:"}),e.jsxs(E,{variant:"secondary",className:"gap-1",children:[e.jsx(ne,{className:"h-3 w-3"}),B," منتج"]})]})]})]})})]})}),e.jsxs(Qe,{className:P("gap-2 pt-4 border-t",u?"flex-col":"flex-row"),children:[e.jsx(d,{variant:"outline",onClick:()=>k(!1),disabled:f,className:u?"w-full":"",children:"إلغاء"}),e.jsxs(d,{variant:"secondary",onClick:()=>T(!1),disabled:f,className:P("gap-2",u&&"w-full"),children:[e.jsx(Ds,{className:"h-4 w-4"}),"حفظ كمسودة"]}),e.jsxs(d,{onClick:()=>T(!0),disabled:f,className:P("gap-2",u&&"w-full"),children:[e.jsx(He,{className:"h-4 w-4"}),"إنشاء وبدء فوراً"]})]})]})})},Zs=({countId:c,varianceType:k,sessionId:g,onReasonSelected:h})=>{const{reasons:S,getReasonsByType:u}=Ae(),{updateVarianceReason:z}=ve(g),{toast:o}=Te(),[x,f]=n.useState(""),[j,y]=n.useState(""),[A,C]=n.useState(!1),N=u(k),r=async()=>{if(!x){o({title:"يرجى اختيار سبب الفرق",variant:"destructive"});return}C(!0);try{await z(c,x,j||void 0)&&(o({title:"تم حفظ سبب الفرق"}),h==null||h())}finally{C(!1)}},F=()=>k==="surplus"?e.jsx(Ve,{className:"h-4 w-4 text-green-600"}):e.jsx($e,{className:"h-4 w-4 text-red-600"});return e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[F(),e.jsx("span",{className:"font-medium",children:k==="surplus"?"زيادة في المخزون":"نقص في المخزون"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(ee,{children:"سبب الفرق"}),e.jsxs(oe,{value:x,onValueChange:f,children:[e.jsx(xe,{children:e.jsx(ge,{placeholder:"اختر السبب..."})}),e.jsx(he,{children:N.length===0?e.jsx(w,{value:"none",disabled:!0,children:"لا توجد أسباب متاحة"}):N.map(m=>e.jsx(w,{value:m.id,children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(me,{className:"h-3 w-3 text-muted-foreground"}),m.name]})},m.id))})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(ee,{children:"ملاحظات إضافية (اختياري)"}),e.jsx(pe,{placeholder:"أي تفاصيل إضافية...",value:j,onChange:m=>y(m.target.value)})]}),e.jsxs(d,{onClick:r,disabled:!x||A,className:"w-full gap-2",children:[e.jsx(Ue,{className:"h-4 w-4"}),"حفظ السبب"]})]})},et=({sessionName:c,sessionNumber:k,products:g,showSystemQty:h=!1})=>{const[S,u]=_s.useState(!1),z=()=>{const x=o(),f=window.open("","_blank");f&&(f.document.write(x),f.document.close(),f.focus(),setTimeout(()=>{f.print(),f.close()},250))},o=()=>{const x=new Date().toLocaleString("ar-SA"),f=g.map((j,y)=>`
      <tr>
        <td style="text-align: center; padding: 8px; border: 1px solid #ddd;">${y+1}</td>
        <td style="padding: 8px; border: 1px solid #ddd;">${j.product_name}</td>
        <td style="text-align: center; padding: 8px; border: 1px solid #ddd; font-family: monospace;">${j.barcode||j.sku||"-"}</td>
        ${h?`<td style="text-align: center; padding: 8px; border: 1px solid #ddd;">${j.stock_quantity} ${j.unit||""}</td>`:""}
        <td style="text-align: center; padding: 8px; border: 1px solid #ddd; min-width: 80px;"></td>
        <td style="padding: 8px; border: 1px solid #ddd; min-width: 100px;"></td>
      </tr>
    `).join("");return`
      <!DOCTYPE html>
      <html dir="rtl" lang="ar">
      <head>
        <meta charset="UTF-8">
        <title>قائمة العد - ${c}</title>
        <style>
          body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            padding: 20px;
            direction: rtl;
          }
          .header {
            text-align: center;
            margin-bottom: 30px;
            border-bottom: 2px solid #333;
            padding-bottom: 15px;
          }
          .header h1 {
            margin: 0 0 10px 0;
            font-size: 24px;
          }
          .info {
            display: flex;
            justify-content: space-between;
            margin-bottom: 20px;
            font-size: 14px;
          }
          table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 30px;
          }
          th {
            background-color: #f5f5f5;
            padding: 10px;
            border: 1px solid #ddd;
            font-weight: bold;
          }
          .footer {
            margin-top: 40px;
            display: flex;
            justify-content: space-between;
          }
          .signature-box {
            border-top: 1px solid #333;
            width: 200px;
            text-align: center;
            padding-top: 5px;
          }
          @media print {
            body { print-color-adjust: exact; }
          }
        </style>
      </head>
      <body>
        <div class="header">
          <h1>📋 قائمة العد</h1>
          <h2>${c}</h2>
        </div>
        
        <div class="info">
          <div><strong>رقم الجلسة:</strong> ${k}</div>
          <div><strong>التاريخ:</strong> ${x}</div>
          <div><strong>عدد المنتجات:</strong> ${g.length}</div>
        </div>
        
        <table>
          <thead>
            <tr>
              <th style="width: 50px;">#</th>
              <th>اسم المنتج</th>
              <th style="width: 120px;">الباركود</th>
              ${h?'<th style="width: 100px;">كمية النظام</th>':""}
              <th style="width: 100px;">الكمية الفعلية</th>
              <th style="width: 120px;">ملاحظات</th>
            </tr>
          </thead>
          <tbody>
            ${f}
          </tbody>
        </table>
        
        <div class="footer">
          <div class="signature-box">
            <div>توقيع المُعِد</div>
          </div>
          <div class="signature-box">
            <div>توقيع المراجع</div>
          </div>
        </div>
      </body>
      </html>
    `};return e.jsxs(_e,{open:S,onOpenChange:u,children:[e.jsx(Ts,{asChild:!0,children:e.jsxs(d,{variant:"outline",size:"sm",className:"gap-2",children:[e.jsx(ls,{className:"h-4 w-4"}),"طباعة قائمة العد"]})}),e.jsxs(Ie,{className:"max-w-2xl max-h-[80vh] overflow-auto",children:[e.jsx(qe,{children:e.jsxs(De,{className:"flex items-center justify-between",children:[e.jsx("span",{children:"معاينة قائمة العد"}),e.jsx(d,{variant:"ghost",size:"icon",onClick:()=>u(!1),children:e.jsx(Is,{className:"h-4 w-4"})})]})}),e.jsxs("div",{className:"space-y-4",children:[e.jsx("div",{className:"bg-muted p-4 rounded-lg",children:e.jsxs("div",{className:"flex justify-between text-sm",children:[e.jsxs("span",{children:[e.jsx("strong",{children:"الجلسة:"})," ",c]}),e.jsxs("span",{children:[e.jsx("strong",{children:"الرقم:"})," ",k]}),e.jsxs("span",{children:[e.jsx("strong",{children:"المنتجات:"})," ",g.length]})]})}),e.jsx("div",{className:"border rounded-lg overflow-hidden max-h-[300px] overflow-y-auto",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{className:"bg-muted sticky top-0",children:e.jsxs("tr",{children:[e.jsx("th",{className:"p-2 text-center",children:"#"}),e.jsx("th",{className:"p-2 text-right",children:"المنتج"}),e.jsx("th",{className:"p-2 text-center",children:"الباركود"}),h&&e.jsx("th",{className:"p-2 text-center",children:"كمية النظام"})]})}),e.jsxs("tbody",{children:[g.slice(0,10).map((x,f)=>e.jsxs("tr",{className:"border-t",children:[e.jsx("td",{className:"p-2 text-center",children:f+1}),e.jsx("td",{className:"p-2",children:x.product_name}),e.jsx("td",{className:"p-2 text-center font-mono text-xs",children:x.barcode||"-"}),h&&e.jsx("td",{className:"p-2 text-center",children:x.stock_quantity})]},x.id)),g.length>10&&e.jsx("tr",{className:"border-t bg-muted/50",children:e.jsxs("td",{colSpan:h?4:3,className:"p-2 text-center text-muted-foreground",children:["... و ",g.length-10," منتج آخر"]})})]})]})}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("input",{type:"checkbox",id:"showSystemQty",checked:h,disabled:!0,className:"rounded"}),e.jsx("label",{htmlFor:"showSystemQty",className:"text-sm text-muted-foreground",children:"إظهار كمية النظام (للمقارنة أثناء العد)"})]}),e.jsxs("div",{className:"flex justify-end gap-2",children:[e.jsx(d,{variant:"outline",onClick:()=>u(!1),children:"إلغاء"}),e.jsxs(d,{onClick:z,className:"gap-2",children:[e.jsx(ls,{className:"h-4 w-4"}),"طباعة"]})]})]})]})]})},st=({session:c,onBack:k,onComplete:g})=>{const{countItems:h,isLoading:S,hasLoadedOnce:u,fetchCountItems:z,initializeCountItems:o,submitCount:x,searchByBarcode:f,getStatistics:j,getNextUncountedItem:y}=ve(c.id),{completeSession:A}=Pe(),{toast:C}=Te(),N=Me(),[r,F]=n.useState(""),[m,L]=n.useState(null),[I,i]=n.useState({}),[M,D]=n.useState(null),[G,Q]=n.useState("all"),[ce,J]=n.useState(!1),[B,X]=n.useState(!1),[K,_]=n.useState(!1),[re,W]=n.useState(null),le=n.useRef(null),U=n.useRef(),se=n.useRef(),T=`stocktake_autosave_${c.id}`;n.useEffect(()=>{const t=localStorage.getItem(T);if(t)try{const b=JSON.parse(t);b.quantities&&Object.keys(b.quantities).length>0&&(i(b.quantities),W(new Date(b.savedAt)),C({title:"تم استعادة البيانات المحفوظة تلقائياً"}))}catch(b){console.error("Error loading auto-saved data:",b)}},[c.id]),n.useEffect(()=>{u&&!S&&h.length===0&&c.status==="in_progress"&&(console.log("[Stocktake] No count items found after fetch, initializing products..."),o(c))},[u,S,h.length,c.status,c.id,o]),n.useEffect(()=>{const t=setTimeout(()=>{var b;(b=le.current)==null||b.focus()},100);return()=>clearTimeout(t)},[]),n.useEffect(()=>(se.current=setInterval(()=>{if(Object.keys(I).length>0){const t={quantities:I,savedAt:new Date().toISOString()};localStorage.setItem(T,JSON.stringify(t)),W(new Date)}},3e4),()=>{se.current&&clearInterval(se.current)}),[I,T]);const l=n.useCallback(()=>{localStorage.removeItem(T)},[T]),p=n.useCallback(t=>{t.key==="Enter"&&H(r)},[r]),q=t=>{F(t),D(null),U.current&&clearTimeout(U.current),/^\d{8,14}$/.test(t)&&(U.current=setTimeout(()=>{H(t)},300))},H=async t=>{if(!t.trim())return;const b=await f(t.trim());b?(is(),D("success"),L(b.id),F(""),setTimeout(()=>{var ae;(ae=document.getElementById(`qty-${b.id}`))==null||ae.focus()},100)):(Fs(),D("error"),C({title:"المنتج غير موجود في قائمة الجرد",variant:"destructive"})),setTimeout(()=>D(null),1500)},te=t=>{J(!1),H(t)},a=(t,b)=>{i(ae=>({...ae,[t]:Math.max(0,b)}))},s=async t=>{const b=I[t.id]??0;if(await x({count_id:t.id,counted_qty:b})){is(),C({title:`تم حفظ العد: ${t.product_name}`});const V=y();V&&V.id!==t.id&&(L(V.id),setTimeout(()=>{var as;(as=document.getElementById(`qty-${V.id}`))==null||as.focus()},100))}},v=()=>{const t=y();t&&L(t.id)},ie=async()=>{_(!0);try{if(await A(c.id))l(),C({title:"تم إنهاء العد وإرسال الجلسة للمراجعة"}),g&&g();else throw new Error("Failed to complete session")}catch(t){console.error("Error completing session:",t),C({title:"حدث خطأ أثناء إنهاء الجلسة",variant:"destructive"})}finally{_(!1),X(!1)}},Fe=()=>{let t=h;switch(G){case"counted":t=t.filter(b=>b.is_counted);break;case"uncounted":t=t.filter(b=>!b.is_counted);break;case"variance":t=t.filter(b=>b.variance_qty!==0);break}if(r&&!M){const b=r.toLowerCase();t=t.filter(ae=>{var V;return(V=ae.product_name)==null?void 0:V.toLowerCase().includes(b)})}return t},R=j(),fe=Fe(),ts=R.total>0?R.progress:0,Ss=ts>=90,Cs=h.map(t=>({id:t.product_id,product_name:t.product_name||"",barcode:t.product_barcode,sku:t.product_sku,stock_quantity:t.system_qty,unit:"",category_name:""}));return e.jsxs("div",{className:"space-y-4",children:[e.jsxs($,{children:[e.jsx(Ne,{className:"pb-3",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs(d,{variant:"ghost",onClick:k,className:"gap-2",children:[e.jsx(ss,{className:"h-4 w-4"}),"رجوع"]}),e.jsx("div",{className:"text-center",children:e.jsx("h2",{className:"font-semibold",children:c.name||c.session_number})}),e.jsx(et,{sessionName:c.name||c.session_number,sessionNumber:c.session_number,products:Cs,showSystemQty:!1})]})}),e.jsxs(O,{className:"pt-0",children:[e.jsxs("div",{className:"mb-4",children:[e.jsxs("div",{className:"flex justify-between text-sm mb-2",children:[e.jsx("span",{children:"التقدم"}),e.jsxs("span",{className:"font-medium",children:[R.counted," / ",R.total]})]}),e.jsx(Gs,{value:ts,className:"h-3"})]}),e.jsxs("div",{className:"grid grid-cols-3 gap-3 text-center",children:[e.jsxs("div",{className:"p-2 bg-muted/50 rounded-lg",children:[e.jsx(ue,{className:"h-5 w-5 mx-auto text-green-600 mb-1"}),e.jsx("p",{className:"text-xl font-bold",children:R.counted}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"تم عده"})]}),e.jsxs("div",{className:"p-2 bg-muted/50 rounded-lg",children:[e.jsx(ne,{className:"h-5 w-5 mx-auto text-blue-600 mb-1"}),e.jsx("p",{className:"text-xl font-bold",children:R.remaining}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"متبقي"})]}),e.jsxs("div",{className:"p-2 bg-muted/50 rounded-lg",children:[e.jsx(me,{className:"h-5 w-5 mx-auto text-orange-600 mb-1"}),e.jsx("p",{className:"text-xl font-bold",children:R.withVariance}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"فروقات"})]})]}),Ss&&e.jsxs(d,{onClick:()=>X(!0),className:"w-full gap-2 bg-green-600 hover:bg-green-700 mt-4",size:"lg",children:[e.jsx(zs,{className:"h-5 w-5"}),"إنهاء العد وإرسال للمراجعة (",R.counted,"/",R.total,")"]})]})]}),e.jsx($,{children:e.jsxs(O,{className:"p-4",children:[e.jsxs("div",{className:"flex gap-2",children:[e.jsxs("div",{className:"relative flex-1",children:[e.jsx(vs,{className:"absolute right-3 top-1/2 -translate-y-1/2 h-5 w-5 text-muted-foreground"}),e.jsx(pe,{ref:le,placeholder:"مسح الباركود أو البحث...",value:r,onChange:t=>q(t.target.value),onKeyDown:p,className:P("pr-12 h-12 text-lg font-mono transition-all",M==="success"&&"ring-2 ring-green-500 bg-green-50",M==="error"&&"ring-2 ring-red-500 bg-red-50")}),r&&e.jsx(d,{variant:"ghost",size:"icon",className:"absolute left-10 top-1/2 -translate-y-1/2 h-8 w-8",onClick:()=>{var t;F(""),(t=le.current)==null||t.focus()},children:e.jsx(Se,{className:"h-4 w-4"})})]}),e.jsx(d,{variant:"outline",size:"icon",className:"h-12 w-12",onClick:()=>J(!0),children:e.jsx(Ls,{className:"h-5 w-5"})})]}),e.jsx("div",{className:"flex gap-2 mt-3 overflow-x-auto pb-1",children:[{key:"all",label:"الكل",count:h.length},{key:"uncounted",label:"لم يعد",count:h.filter(t=>!t.is_counted).length},{key:"counted",label:"تم عده",count:h.filter(t=>t.is_counted).length},{key:"variance",label:"فروقات",count:h.filter(t=>t.variance_qty!==0).length}].map(t=>e.jsxs(d,{variant:G===t.key?"default":"outline",size:"sm",onClick:()=>Q(t.key),className:"whitespace-nowrap gap-1",children:[t.label,e.jsx(E,{variant:"secondary",className:"text-xs px-1.5",children:t.count})]},t.key))})]})}),e.jsx($,{children:e.jsx(O,{className:"p-4",children:!u||S?e.jsxs("div",{className:"text-center py-8 text-muted-foreground",children:[e.jsx(Be,{className:"h-8 w-8 mx-auto animate-spin mb-4"}),"جاري تحميل المنتجات..."]}):fe.length===0?e.jsxs("div",{className:"text-center py-8",children:[e.jsx(ne,{className:"h-12 w-12 mx-auto text-muted-foreground mb-4"}),e.jsx("p",{className:"text-muted-foreground mb-4",children:"لا توجد منتجات"}),e.jsxs(d,{variant:"outline",onClick:()=>z(),disabled:S,className:"gap-2",children:[e.jsx(Be,{className:P("h-4 w-4",S&&"animate-spin")}),"إعادة تحميل"]})]}):e.jsx(ke,{className:N?"h-[50vh]":"h-[60vh]",children:e.jsx("div",{className:"space-y-3",children:fe.map(t=>{const b=m===t.id,ae=I[t.id]??t.counted_qty??0;return e.jsxs("div",{className:P("border rounded-lg p-4 transition-all",b&&"ring-2 ring-primary bg-primary/5",t.is_counted&&"bg-green-50 dark:bg-green-950/20"),onClick:()=>L(t.id),children:[e.jsxs("div",{className:"flex items-start gap-3",children:[e.jsx("div",{className:"w-16 h-16 bg-muted rounded-lg flex items-center justify-center",children:e.jsx(ne,{className:"h-8 w-8 text-muted-foreground"})}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-start justify-between mb-1",children:[e.jsx("h3",{className:"font-medium truncate",children:t.product_name}),t.is_counted&&e.jsx(ue,{className:"h-5 w-5 text-green-600 flex-shrink-0"})]}),e.jsx("p",{className:"text-sm text-muted-foreground font-mono",children:t.product_barcode||t.product_sku||"-"}),e.jsx("div",{className:"flex items-center gap-3 mt-2 text-sm",children:e.jsxs("span",{className:"text-muted-foreground",children:["في النظام: ",e.jsx("strong",{children:t.system_qty})]})})]})]}),(b||!t.is_counted)&&e.jsxs("div",{className:"mt-4 pt-4 border-t",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(d,{variant:"outline",size:"icon",className:"h-12 w-12",onClick:V=>{V.stopPropagation(),a(t.id,ae-1)},children:e.jsx(Bs,{className:"h-5 w-5"})}),e.jsx(pe,{id:`qty-${t.id}`,type:"number",value:ae,onChange:V=>a(t.id,parseInt(V.target.value)||0),className:"h-12 text-center text-xl font-bold flex-1",min:0}),e.jsx(d,{variant:"outline",size:"icon",className:"h-12 w-12",onClick:V=>{V.stopPropagation(),a(t.id,ae+1)},children:e.jsx(ze,{className:"h-5 w-5"})})]}),e.jsxs("div",{className:"flex gap-2 mt-3",children:[e.jsxs(d,{className:"flex-1 h-12 gap-2",onClick:V=>{V.stopPropagation(),s(t)},children:[e.jsx(Ue,{className:"h-5 w-5"}),"حفظ"]}),!t.is_counted&&e.jsxs(d,{variant:"outline",className:"h-12 gap-2",onClick:V=>{V.stopPropagation(),v()},children:[e.jsx(Ws,{className:"h-5 w-5"}),"تخطي"]})]})]}),t.is_counted&&t.variance_qty!==0&&!t.variance_reason_id&&e.jsx("div",{className:"mt-3 pt-3 border-t",children:e.jsx(Zs,{countId:t.id,varianceType:(t.variance_qty||0)>0?"surplus":"shortage",sessionId:c.id,onReasonSelected:()=>z()})})]},t.id)})})})})}),ce&&e.jsx(Rs,{onDetected:te,onClose:()=>J(!1)}),e.jsx(Ge,{open:B,onOpenChange:X,children:e.jsxs(Je,{children:[e.jsxs(Ke,{children:[e.jsxs(We,{className:"flex items-center gap-2",children:[e.jsx(ue,{className:"h-5 w-5 text-green-600"}),"إنهاء جلسة العد"]}),e.jsxs(Xe,{className:"space-y-3",children:[e.jsx("p",{children:"هل أنت متأكد من إنهاء جلسة العد وإرسالها للمراجعة؟"}),e.jsxs("div",{className:"bg-muted p-3 rounded-lg space-y-1 text-sm",children:[e.jsxs("div",{className:"flex justify-between",children:[e.jsx("span",{children:"المنتجات المعدودة:"}),e.jsx("span",{className:"font-bold text-green-600",children:R.counted})]}),e.jsxs("div",{className:"flex justify-between",children:[e.jsx("span",{children:"المنتجات المتبقية:"}),e.jsx("span",{className:"font-bold text-amber-600",children:R.remaining})]}),e.jsxs("div",{className:"flex justify-between",children:[e.jsx("span",{children:"منتجات بها فروقات:"}),e.jsx("span",{className:"font-bold text-red-600",children:R.withVariance})]})]}),R.remaining>0&&e.jsxs("p",{className:"text-amber-600 text-sm",children:["⚠️ يوجد ",R.remaining," منتج لم يتم عده بعد"]})]})]}),e.jsxs(Ye,{children:[e.jsx(Ze,{disabled:K,children:"إلغاء"}),e.jsx(es,{onClick:ie,disabled:K,className:"bg-green-600 hover:bg-green-700",children:K?"جاري الإرسال...":"تأكيد وإرسال للمراجعة"})]})]})})]})},ps=({countId:c,varianceType:k,sessionId:g,currentReasonId:h,currentReasonName:S,onReasonSelected:u,compact:z=!1})=>{const{activeReasons:o,getReasonsByType:x}=Ae(),{updateVarianceReason:f}=ve(g),[j,y]=n.useState(!1),A=x(k),C=async N=>{if(N!==h){y(!0);try{await f(c,N)&&(je.success("تم تحديث السبب"),u==null||u())}finally{y(!1)}}};return j?e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(gs,{className:"h-4 w-4 animate-spin text-muted-foreground"}),e.jsx("span",{className:"text-xs text-muted-foreground",children:"جاري الحفظ..."})]}):h&&S?e.jsxs(oe,{value:h,onValueChange:C,children:[e.jsx(xe,{className:P("h-7 border-dashed",z?"w-24 text-xs":"w-32"),children:e.jsx(E,{variant:"outline",className:"text-xs truncate",children:S})}),e.jsx(he,{children:A.map(N=>e.jsx(w,{value:N.id,children:N.name},N.id))})]}):e.jsxs(oe,{value:"",onValueChange:C,children:[e.jsx(xe,{className:P("h-7 border-dashed border-orange-300 bg-orange-50 dark:bg-orange-950/20",z?"w-24 text-xs":"w-32"),children:e.jsx(ge,{placeholder:"اختر سبب..."})}),e.jsx(he,{children:A.length===0?e.jsx(w,{value:"none",disabled:!0,children:"لا توجد أسباب"}):A.map(N=>e.jsx(w,{value:N.id,children:N.name},N.id))})]})},tt=({open:c,onOpenChange:k,items:g,sessionId:h,onComplete:S})=>{const{activeReasons:u}=Ae(),{updateVarianceReason:z}=ve(h),[o,x]=n.useState(""),[f,j]=n.useState(""),[y,A]=n.useState(!1),[C,N]=n.useState(0),r=g.filter(i=>!i.variance_reason_id&&i.variance_qty!==0),F=r.filter(i=>i.variance_qty<0),m=r.filter(i=>i.variance_qty>0),L=u.filter(i=>F.length>0&&m.length>0?!0:F.length>0?i.type==="shortage"||i.type==="both":m.length>0?i.type==="surplus"||i.type==="both":!0),I=async()=>{if(!o){je.error("يرجى اختيار سبب للفرق");return}A(!0),N(0);let i=0;const M=r.length;for(let D=0;D<r.length;D++){const G=r[D];try{await z(G.id,o,f||void 0)&&i++}catch(Q){console.error("Error updating reason for item:",G.id,Q)}N(Math.round((D+1)/M*100))}A(!1),i===M?je.success(`تم تطبيق السبب على ${i} عنصر`):je.warning(`تم تطبيق السبب على ${i} من ${M} عنصر`),x(""),j(""),k(!1),S==null||S()};return e.jsx(_e,{open:c,onOpenChange:k,children:e.jsxs(Ie,{className:"max-w-md",children:[e.jsx(qe,{children:e.jsxs(De,{className:"flex items-center gap-2",children:[e.jsx(me,{className:"h-5 w-5 text-orange-500"}),"تطبيق سبب على جميع الفروقات"]})}),e.jsxs("div",{className:"space-y-4 py-4",children:[e.jsxs("div",{className:"p-3 bg-muted rounded-lg space-y-2 text-sm",children:[e.jsxs("div",{className:"flex justify-between",children:[e.jsx("span",{children:"عناصر بدون سبب:"}),e.jsx("span",{className:"font-bold text-orange-600",children:r.length})]}),F.length>0&&e.jsxs("div",{className:"flex justify-between text-red-600",children:[e.jsx("span",{children:"نقص:"}),e.jsx("span",{className:"font-medium",children:F.length})]}),m.length>0&&e.jsxs("div",{className:"flex justify-between text-green-600",children:[e.jsx("span",{children:"زيادة:"}),e.jsx("span",{className:"font-medium",children:m.length})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(ee,{children:"اختر السبب للتطبيق"}),e.jsxs(oe,{value:o,onValueChange:x,disabled:y,children:[e.jsx(xe,{children:e.jsx(ge,{placeholder:"اختر سبب الفرق..."})}),e.jsx(he,{children:L.map(i=>e.jsx(w,{value:i.id,children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:i.type==="shortage"?"text-red-600":i.type==="surplus"?"text-green-600":"text-muted-foreground",children:i.type==="shortage"?"↓":i.type==="surplus"?"↑":"↕"}),i.name]})},i.id))})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(ee,{children:"ملاحظات إضافية (اختياري)"}),e.jsx(pe,{placeholder:"ملاحظات تطبق على جميع العناصر...",value:f,onChange:i=>j(i.target.value),disabled:y})]}),y&&e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center justify-between text-sm",children:[e.jsx("span",{children:"جاري التطبيق..."}),e.jsxs("span",{children:[C,"%"]})]}),e.jsx("div",{className:"w-full h-2 bg-muted rounded-full overflow-hidden",children:e.jsx("div",{className:"h-full bg-primary transition-all duration-300",style:{width:`${C}%`}})})]})]}),e.jsxs(Qe,{children:[e.jsx(d,{variant:"outline",onClick:()=>k(!1),disabled:y,children:"إلغاء"}),e.jsx(d,{onClick:I,disabled:!o||y||r.length===0,className:"gap-2",children:y?e.jsxs(e.Fragment,{children:[e.jsx(gs,{className:"h-4 w-4 animate-spin"}),"جاري التطبيق..."]}):e.jsxs(e.Fragment,{children:[e.jsx(Ue,{className:"h-4 w-4"}),"تطبيق على ",r.length," عنصر"]})})]})]})})},at=({session:c,onBack:k,onSettled:g})=>{const{countItems:h,isLoading:S,fetchCountItems:u,getStatistics:z}=ve(c.id),{approveSession:o,settleSession:x}=Pe(),{reasons:f}=Ae(),{currentEmployee:j,isOwnerOrAdmin:y}=fs(),A=Me(),[C,N]=n.useState(!1),[r,F]=n.useState(!1),[m,L]=n.useState(!1),[I,i]=n.useState(""),[M,D]=n.useState(""),[G,Q]=n.useState(!1),[ce,J]=n.useState("all"),B=y(),X=B||!!(j!=null&&j.can_approve_stocktake),K=B||!!(j!=null&&j.can_settle_stocktake),_=n.useMemo(()=>h.filter(a=>a.variance_qty!==0),[h]),re=n.useMemo(()=>{switch(ce){case"shortage":return _.filter(a=>a.variance_qty<0);case"surplus":return _.filter(a=>a.variance_qty>0);default:return _}},[_,ce]),W=_.filter(a=>!a.variance_reason_id),le=z(),U=_.filter(a=>a.variance_qty<0).reduce((a,s)=>a+Math.abs(s.variance_value),0),se=_.filter(a=>a.variance_qty>0).reduce((a,s)=>a+s.variance_value,0),T=se-U,l={shortageCount:_.filter(a=>a.variance_qty<0).length,surplusCount:_.filter(a=>a.variance_qty>0).length,totalProducts:_.length},p=async()=>{if(W.length>0){je.error(`يوجد ${W.length} عنصر بدون سبب للفرق`);return}Q(!0);try{await o(c.id,I)&&(je.success("تم الموافقة على الجرد"),N(!1),u())}finally{Q(!1)}},q=async()=>{Q(!0);try{await x(c.id,M)&&(je.success("تم تسوية الجرد وتحديث المخزون"),F(!1),await u(),g==null||g())}finally{Q(!1)}},H=a=>{if(!a)return null;const s=f.find(v=>v.id===a);return(s==null?void 0:s.name)||"غير معروف"},te=a=>{const s=a.variance_qty<0,v=H(a.variance_reason_id);return e.jsx($,{className:"mb-3",children:e.jsxs(O,{className:"p-4",children:[e.jsxs("div",{className:"flex items-start justify-between mb-2",children:[e.jsxs("div",{className:"flex-1",children:[e.jsx("h3",{className:"font-medium",children:a.product_name}),e.jsx("p",{className:"text-sm text-muted-foreground font-mono",children:a.product_barcode||a.product_sku||"-"})]}),e.jsxs(E,{variant:"outline",className:P(s?"bg-red-50 text-red-700 border-red-200":"bg-green-50 text-green-700 border-green-200"),children:[s?e.jsx($e,{className:"h-3 w-3 ml-1"}):e.jsx(Ve,{className:"h-3 w-3 ml-1"}),s?"نقص":"زيادة"]})]}),e.jsxs("div",{className:"grid grid-cols-3 gap-3 text-sm mb-3",children:[e.jsxs("div",{className:"text-center p-2 bg-muted/50 rounded",children:[e.jsx("p",{className:"text-muted-foreground text-xs",children:"النظام"}),e.jsx("p",{className:"font-bold",children:a.system_qty})]}),e.jsxs("div",{className:"text-center p-2 bg-muted/50 rounded",children:[e.jsx("p",{className:"text-muted-foreground text-xs",children:"المعدود"}),e.jsx("p",{className:"font-bold",children:a.counted_qty})]}),e.jsxs("div",{className:P("text-center p-2 rounded",s?"bg-red-50":"bg-green-50"),children:[e.jsx("p",{className:"text-muted-foreground text-xs",children:"الفرق"}),e.jsxs("p",{className:P("font-bold",s?"text-red-700":"text-green-700"),children:[a.variance_qty>0?"+":"",a.variance_qty]})]})]}),e.jsxs("div",{className:"flex items-center justify-between text-sm",children:[e.jsx("span",{className:"text-muted-foreground",children:"قيمة الفرق:"}),e.jsxs("span",{className:P("font-bold",s?"text-red-700":"text-green-700"),children:[s?"-":"+",Math.abs(a.variance_value).toFixed(2)]})]}),e.jsxs("div",{className:"mt-3 pt-3 border-t",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("span",{className:"text-sm text-muted-foreground",children:"السبب:"}),e.jsx(ps,{countId:a.id,varianceType:s?"shortage":"surplus",sessionId:c.id,currentReasonId:a.variance_reason_id,currentReasonName:v||void 0,onReasonSelected:()=>u()})]}),a.variance_notes&&e.jsx("p",{className:"text-sm text-muted-foreground mt-1",children:a.variance_notes})]})]})},a.id)};return e.jsxs("div",{className:"space-y-4",children:[e.jsxs($,{children:[e.jsx(Ne,{className:"pb-3",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs(d,{variant:"ghost",onClick:k,className:"gap-2",children:[e.jsx(ss,{className:"h-4 w-4"}),"رجوع"]}),e.jsxs("div",{className:"text-center",children:[e.jsx("h2",{className:"font-semibold",children:c.name||c.session_number}),e.jsx(E,{variant:"outline",children:c.status==="pending_review"?"بانتظار المراجعة":c.status==="approved"?"معتمد":c.status==="settled"?"تمت التسوية":c.status})]}),e.jsx("div",{className:"w-20"})]})}),e.jsx(O,{className:"pt-0",children:e.jsx(Qs,{currentStatus:c.status})})]}),e.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-3",children:[e.jsx($,{children:e.jsxs(O,{className:"p-4 text-center",children:[e.jsx(ne,{className:"h-8 w-8 mx-auto text-blue-600 mb-2"}),e.jsx("p",{className:"text-2xl font-bold",children:le.total}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"إجمالي المنتجات"})]})}),e.jsx($,{children:e.jsxs(O,{className:"p-4 text-center",children:[e.jsx(me,{className:"h-8 w-8 mx-auto text-orange-600 mb-2"}),e.jsx("p",{className:"text-2xl font-bold",children:_.length}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"منتجات بها فروقات"})]})}),e.jsx($,{children:e.jsxs(O,{className:"p-4 text-center",children:[e.jsx($e,{className:"h-8 w-8 mx-auto text-red-600 mb-2"}),e.jsxs("p",{className:"text-2xl font-bold text-red-600",children:["-",U.toFixed(2)]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"قيمة النقص"})]})}),e.jsx($,{children:e.jsxs(O,{className:"p-4 text-center",children:[e.jsx(Ve,{className:"h-8 w-8 mx-auto text-green-600 mb-2"}),e.jsxs("p",{className:"text-2xl font-bold text-green-600",children:["+",se.toFixed(2)]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"قيمة الزيادة"})]})})]}),e.jsx($,{children:e.jsx(O,{className:"p-4",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(Vs,{className:"h-8 w-8 text-primary"}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"صافي الفرق"}),e.jsxs("p",{className:P("text-2xl font-bold",T<0?"text-red-600":T>0?"text-green-600":""),children:[T>0?"+":"",T.toFixed(2)]})]})]}),e.jsx("div",{className:"flex items-center gap-2",children:W.length>0&&e.jsxs(e.Fragment,{children:[e.jsxs(E,{variant:"destructive",className:"gap-1",children:[e.jsx(me,{className:"h-3 w-3"}),W.length," بدون سبب"]}),e.jsxs(d,{variant:"outline",size:"sm",onClick:()=>L(!0),className:"gap-1",children:[e.jsx(Xs,{className:"h-4 w-4"}),"تطبيق سبب للكل"]})]})})]})})}),c.status==="approved"&&K&&e.jsxs($,{className:"border-2 border-primary/30 bg-primary/5",children:[e.jsx(Ne,{className:"pb-2",children:e.jsxs(Le,{className:"text-lg flex items-center gap-2",children:[e.jsx(Ns,{className:"h-5 w-5"}),"ملخص التسوية المتوقعة"]})}),e.jsxs(O,{className:"space-y-4",children:[e.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4 text-center",children:[e.jsxs("div",{className:"bg-background p-3 rounded-lg",children:[e.jsx("div",{className:"text-2xl font-bold",children:l.totalProducts}),e.jsx("div",{className:"text-xs text-muted-foreground",children:"منتج سيتم تعديله"})]}),e.jsxs("div",{className:"bg-background p-3 rounded-lg",children:[e.jsx("div",{className:"text-2xl font-bold text-red-600",children:l.shortageCount}),e.jsx("div",{className:"text-xs text-muted-foreground",children:"نقص (خسارة)"})]}),e.jsxs("div",{className:"bg-background p-3 rounded-lg",children:[e.jsx("div",{className:"text-2xl font-bold text-green-600",children:l.surplusCount}),e.jsx("div",{className:"text-xs text-muted-foreground",children:"زيادة (ربح)"})]}),e.jsxs("div",{className:"bg-background p-3 rounded-lg",children:[e.jsx("div",{className:`text-2xl font-bold ${T>=0?"text-green-600":"text-red-600"}`,children:T.toFixed(2)}),e.jsx("div",{className:"text-xs text-muted-foreground",children:"صافي الفرق"})]})]}),e.jsx("div",{className:"bg-amber-50 dark:bg-amber-900/20 p-3 rounded-lg border border-amber-200 dark:border-amber-800",children:e.jsxs("p",{className:"text-sm text-amber-800 dark:text-amber-200 flex items-center gap-2",children:[e.jsx(me,{className:"h-4 w-4"}),"عند التسوية سيتم تحديث كميات ",l.totalProducts," منتج في المخزون تلقائياً"]})})]})]}),c.status==="pending_review"&&e.jsx($,{children:e.jsxs(O,{className:"p-4",children:[e.jsx("div",{className:"flex flex-col sm:flex-row gap-3",children:X&&e.jsxs(d,{className:"flex-1 gap-2",onClick:()=>N(!0),disabled:W.length>0,children:[e.jsx(Oe,{className:"h-5 w-5"}),"الموافقة على الجرد"]})}),W.length>0&&e.jsx("p",{className:"text-sm text-destructive mt-2 text-center",children:"يجب تحديد سبب لجميع الفروقات قبل الموافقة"})]})}),c.status==="approved"&&e.jsx($,{children:e.jsxs(O,{className:"p-4",children:[e.jsx("div",{className:"flex flex-col sm:flex-row gap-3",children:K&&e.jsxs(d,{className:"flex-1 gap-2",onClick:()=>F(!0),children:[e.jsx(Ee,{className:"h-5 w-5"}),"تسوية المخزون"]})}),e.jsx("p",{className:"text-sm text-muted-foreground mt-2 text-center",children:"سيتم تحديث كميات المخزون لتطابق الكميات المعدودة"})]})}),e.jsxs($,{children:[e.jsx(Ne,{className:"pb-3",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(Le,{className:"text-lg",children:"المنتجات بها فروقات"}),e.jsx("div",{className:"flex gap-2",children:["all","shortage","surplus"].map(a=>e.jsxs(d,{variant:ce===a?"default":"outline",size:"sm",onClick:()=>J(a),children:[a==="all"?"الكل":a==="shortage"?"نقص":"زيادة",e.jsx(E,{variant:"secondary",className:"mr-1 text-xs px-1",children:a==="all"?_.length:a==="shortage"?_.filter(s=>s.variance_qty<0).length:_.filter(s=>s.variance_qty>0).length})]},a))})]})}),e.jsx(O,{className:"pt-0",children:S?e.jsx("div",{className:"text-center py-8 text-muted-foreground",children:"جاري التحميل..."}):re.length===0?e.jsxs("div",{className:"text-center py-8",children:[e.jsx(ue,{className:"h-12 w-12 mx-auto text-green-600 mb-4"}),e.jsx("p",{className:"text-muted-foreground",children:"لا توجد فروقات"})]}):A?e.jsx(ke,{className:"h-[50vh]",children:re.map(te)}):e.jsx("div",{className:"rounded-md border",children:e.jsxs(bs,{children:[e.jsx(ys,{children:e.jsxs(Ce,{children:[e.jsx(Y,{children:"المنتج"}),e.jsx(Y,{className:"text-center",children:"النظام"}),e.jsx(Y,{className:"text-center",children:"المعدود"}),e.jsx(Y,{className:"text-center",children:"الفرق"}),e.jsx(Y,{className:"text-center",children:"القيمة"}),e.jsx(Y,{children:"السبب"})]})}),e.jsx(ws,{children:re.map(a=>{const s=a.variance_qty<0,v=H(a.variance_reason_id);return e.jsxs(Ce,{children:[e.jsx(Z,{children:e.jsxs("div",{children:[e.jsx("p",{className:"font-medium",children:a.product_name}),e.jsx("p",{className:"text-xs text-muted-foreground font-mono",children:a.product_barcode||a.product_sku||"-"})]})}),e.jsx(Z,{className:"text-center",children:a.system_qty}),e.jsx(Z,{className:"text-center",children:a.counted_qty}),e.jsx(Z,{className:"text-center",children:e.jsxs(E,{variant:"outline",className:P(s?"bg-red-50 text-red-700":"bg-green-50 text-green-700"),children:[a.variance_qty>0?"+":"",a.variance_qty]})}),e.jsxs(Z,{className:P("text-center font-medium",s?"text-red-600":"text-green-600"),children:[s?"-":"+",Math.abs(a.variance_value).toFixed(2)]}),e.jsx(Z,{children:e.jsx(ps,{countId:a.id,varianceType:s?"shortage":"surplus",sessionId:c.id,currentReasonId:a.variance_reason_id,currentReasonName:v||void 0,onReasonSelected:()=>u(),compact:!0})})]},a.id)})})]})})})]}),e.jsx(_e,{open:C,onOpenChange:N,children:e.jsxs(Ie,{className:A?"w-[95vw]":"max-w-md",children:[e.jsx(qe,{children:e.jsx(De,{children:"الموافقة على الجرد"})}),e.jsxs("div",{className:"space-y-4 py-4",children:[e.jsxs("div",{className:"p-4 bg-muted rounded-lg space-y-2",children:[e.jsxs("div",{className:"flex justify-between text-sm",children:[e.jsx("span",{children:"إجمالي المنتجات:"}),e.jsx("span",{className:"font-medium",children:le.total})]}),e.jsxs("div",{className:"flex justify-between text-sm",children:[e.jsx("span",{children:"منتجات بها فروقات:"}),e.jsx("span",{className:"font-medium",children:_.length})]}),e.jsxs("div",{className:"flex justify-between text-sm",children:[e.jsx("span",{children:"صافي الفرق:"}),e.jsx("span",{className:P("font-medium",T<0?"text-red-600":"text-green-600"),children:T.toFixed(2)})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(ee,{children:"ملاحظات الموافقة (اختياري)"}),e.jsx(Re,{placeholder:"أي ملاحظات على نتائج الجرد...",value:I,onChange:a=>i(a.target.value)})]})]}),e.jsxs(Qe,{children:[e.jsx(d,{variant:"outline",onClick:()=>N(!1),children:"إلغاء"}),e.jsxs(d,{onClick:p,disabled:G,children:[e.jsx(Oe,{className:"h-4 w-4 ml-2"}),"موافقة"]})]})]})}),e.jsx(Ge,{open:r,onOpenChange:F,children:e.jsxs(Je,{children:[e.jsxs(Ke,{children:[e.jsxs(We,{className:"flex items-center gap-2 text-amber-600",children:[e.jsx(me,{className:"h-5 w-5"}),"تأكيد تسوية الجرد"]}),e.jsxs(Xe,{className:"space-y-3",children:[e.jsx("p",{children:"هذه العملية ستقوم بـ:"}),e.jsxs("ul",{className:"list-disc list-inside space-y-1 text-sm",children:[e.jsxs("li",{children:["تحديث كميات ",e.jsx("strong",{children:l.totalProducts})," منتج في المخزون"]}),e.jsxs("li",{children:["تسجيل ",e.jsx("strong",{children:l.shortageCount})," حركة نقص بقيمة ",e.jsx("strong",{className:"text-red-600",children:U.toFixed(2)})]}),e.jsxs("li",{children:["تسجيل ",e.jsx("strong",{children:l.surplusCount})," حركة زيادة بقيمة ",e.jsx("strong",{className:"text-green-600",children:se.toFixed(2)})]})]}),e.jsxs("p",{className:"font-bold",children:["صافي الفرق: ",e.jsx("span",{className:T>=0?"text-green-600":"text-red-600",children:T.toFixed(2)})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(ee,{children:"ملاحظات التسوية (اختياري)"}),e.jsx(Re,{placeholder:"أي ملاحظات إضافية...",value:M,onChange:a=>D(a.target.value)})]}),e.jsx("p",{className:"text-amber-600 text-sm",children:"⚠️ لا يمكن التراجع عن هذه العملية"})]})]}),e.jsxs(Ye,{children:[e.jsx(Ze,{children:"إلغاء"}),e.jsxs(es,{onClick:q,disabled:G,className:"bg-primary",children:[e.jsx(Ee,{className:"h-4 w-4 ml-2"}),G?"جاري التسوية...":"تأكيد التسوية"]})]})]})}),e.jsx(tt,{open:m,onOpenChange:L,items:_,sessionId:c.id,onComplete:()=>u()})]})},ye={draft:{label:"مسودة",color:"bg-gray-500",icon:e.jsx(Ms,{className:"h-3 w-3"})},in_progress:{label:"جاري",color:"bg-blue-500",icon:e.jsx(He,{className:"h-3 w-3"})},pending_review:{label:"بانتظار المراجعة",color:"bg-yellow-500",icon:e.jsx(Ks,{className:"h-3 w-3"})},approved:{label:"معتمد",color:"bg-green-500",icon:e.jsx(ue,{className:"h-3 w-3"})},settled:{label:"مُسوّى",color:"bg-purple-500",icon:e.jsx(ue,{className:"h-3 w-3"})},cancelled:{label:"ملغى",color:"bg-red-500",icon:e.jsx(Se,{className:"h-3 w-3"})}},we={full:{label:"جرد كامل",color:"bg-indigo-100 text-indigo-800"},partial:{label:"جرد جزئي",color:"bg-teal-100 text-teal-800"},cycle:{label:"جرد دوري",color:"bg-orange-100 text-orange-800"},spot_check:{label:"فحص عشوائي",color:"bg-pink-100 text-pink-800"}},St=()=>{const{sessions:c,isLoading:k,fetchSessions:g,startSession:h,completeSession:S,cancelSession:u,deleteSession:z}=Pe(),{currentEmployee:o,isOwnerOrAdmin:x}=fs(),{checkPermission:f,silentCheck:j}=$s(),{toast:y}=Te(),A=Me(),[C,N]=n.useState(""),[r,F]=n.useState("all"),[m,L]=n.useState("all"),[I,i]=n.useState(!1),[M,D]=n.useState(null),[G,Q]=n.useState(!1),[ce,J]=n.useState(!1),[B,X]=n.useState(null),K=x(),_=K||!!(o!=null&&o.can_create_stocktake),re=K||!!(o!=null&&o.can_count_stocktake),W=K||!!(o!=null&&o.can_approve_stocktake),le=K||!!(o!=null&&o.can_settle_stocktake);n.useEffect(()=>{g()},[]);const U=c.filter(s=>{var R,fe;const v=((R=s.session_number)==null?void 0:R.toLowerCase().includes(C.toLowerCase()))||((fe=s.name)==null?void 0:fe.toLowerCase().includes(C.toLowerCase())),ie=r==="all"||s.status===r,Fe=m==="all"||s.type===m;return v&&ie&&Fe}),se=async s=>{await h(s.id)&&(y({title:"تم بدء الجلسة بنجاح"}),D({...s,status:"in_progress"}),Q(!0))},T=s=>{D(s),Q(!0)},l=async s=>{await S(s.id)&&y({title:"تم إنهاء الجلسة وإرسالها للمراجعة"})},p=async s=>{if(!f("can_create_stocktake","ليس لديك صلاحية إلغاء جلسات الجرد"))return;await u(s.id)&&y({title:"تم إلغاء الجلسة"})},q=async()=>{if(!B||!f("can_create_stocktake","ليس لديك صلاحية حذف جلسات الجرد"))return;await z(B.id)&&y({title:"تم حذف الجلسة"}),X(null)},H=s=>{D(s),J(!0)},te=s=>{const v=[];return s.status==="draft"&&re&&v.push(e.jsxs(d,{size:"sm",onClick:()=>se(s),className:"gap-1",children:[e.jsx(He,{className:"h-4 w-4"}),"بدء"]},"start")),s.status==="in_progress"&&re&&(v.push(e.jsxs(d,{size:"sm",variant:"outline",onClick:()=>T(s),className:"gap-1",children:[e.jsx(ss,{className:"h-4 w-4"}),"متابعة"]},"continue")),v.push(e.jsxs(d,{size:"sm",variant:"secondary",onClick:()=>l(s),className:"gap-1",children:[e.jsx(ue,{className:"h-4 w-4"}),"إنهاء"]},"complete"))),s.status==="pending_review"&&W&&v.push(e.jsxs(d,{size:"sm",variant:"default",onClick:()=>H(s),className:"gap-1",children:[e.jsx(Oe,{className:"h-4 w-4"}),"مراجعة"]},"review")),s.status==="approved"&&le&&v.push(e.jsxs(d,{size:"sm",variant:"default",onClick:()=>H(s),className:"gap-1 bg-purple-600 hover:bg-purple-700",children:[e.jsx(Ee,{className:"h-4 w-4"}),"تسوية"]},"settle")),s.status==="settled"&&v.push(e.jsxs(d,{size:"sm",variant:"outline",onClick:()=>H(s),className:"gap-1",children:[e.jsx(qs,{className:"h-4 w-4"}),"عرض"]},"view")),v};if(ce&&M)return e.jsx(at,{session:M,onBack:()=>{J(!1),D(null),g()},onSettled:()=>{J(!1),D(null),g()}});if(G&&M)return e.jsx(st,{session:M,onBack:()=>{Q(!1),D(null),g()}});const a=s=>{const v=ye[s.status]||ye.draft,ie=we[s.type]||we.full;return e.jsx($,{className:"mb-3",children:e.jsxs(O,{className:"p-4",children:[e.jsxs("div",{className:"flex items-start justify-between mb-3",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"font-semibold text-lg",children:s.name||s.session_number}),e.jsx("p",{className:"text-sm text-muted-foreground",children:s.session_number})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(E,{className:`${v.color} text-white gap-1`,children:[v.icon,v.label]}),e.jsxs(ns,{children:[e.jsx(cs,{asChild:!0,children:e.jsx(d,{variant:"ghost",size:"icon",className:"h-8 w-8",children:e.jsx(xs,{className:"h-4 w-4"})})}),e.jsxs(rs,{align:"end",children:[s.status==="draft"&&j("can_create_stocktake")&&e.jsxs(be,{onClick:()=>X(s),children:[e.jsx(ds,{className:"h-4 w-4 ml-2"}),"حذف"]}),(s.status==="draft"||s.status==="in_progress")&&j("can_create_stocktake")&&e.jsxs(be,{onClick:()=>p(s),children:[e.jsx(Se,{className:"h-4 w-4 ml-2"}),"إلغاء"]})]})]})]})]}),e.jsxs("div",{className:"flex flex-wrap gap-2 mb-3",children:[e.jsx(E,{variant:"outline",className:ie.color,children:ie.label}),e.jsxs("span",{className:"text-sm text-muted-foreground flex items-center gap-1",children:[e.jsx(Os,{className:"h-3 w-3"}),s.created_at?hs(new Date(s.created_at),"dd MMM yyyy",{locale:ms}):"-"]})]}),e.jsx("div",{className:"flex flex-wrap gap-2",children:te(s)})]})},s.id)};return e.jsxs("div",{className:"space-y-4",children:[e.jsxs($,{children:[e.jsx(Ne,{className:"pb-3",children:e.jsxs("div",{className:"flex flex-col sm:flex-row sm:items-center justify-between gap-3",children:[e.jsxs(Le,{className:"flex items-center gap-2",children:[e.jsx(ks,{className:"h-5 w-5"}),"جلسات الجرد"]}),_&&e.jsxs(d,{onClick:()=>i(!0),className:"gap-2",children:[e.jsx(ze,{className:"h-4 w-4"}),"جلسة جديدة"]})]})}),e.jsxs(O,{className:"pt-0",children:[e.jsxs("div",{className:"flex flex-col sm:flex-row gap-3 mb-4",children:[e.jsxs("div",{className:"relative flex-1",children:[e.jsx(vs,{className:"absolute right-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground"}),e.jsx(pe,{placeholder:"بحث...",value:C,onChange:s=>N(s.target.value),className:"pr-10"})]}),e.jsxs(oe,{value:r,onValueChange:F,children:[e.jsx(xe,{className:"w-full sm:w-[180px]",children:e.jsx(ge,{placeholder:"الحالة"})}),e.jsxs(he,{children:[e.jsx(w,{value:"all",children:"جميع الحالات"}),e.jsx(w,{value:"draft",children:"مسودة"}),e.jsx(w,{value:"in_progress",children:"جاري"}),e.jsx(w,{value:"pending_review",children:"بانتظار المراجعة"}),e.jsx(w,{value:"approved",children:"معتمد"}),e.jsx(w,{value:"settled",children:"مُسوّى"}),e.jsx(w,{value:"cancelled",children:"ملغى"})]})]}),e.jsxs(oe,{value:m,onValueChange:L,children:[e.jsx(xe,{className:"w-full sm:w-[180px]",children:e.jsx(ge,{placeholder:"النوع"})}),e.jsxs(he,{children:[e.jsx(w,{value:"all",children:"جميع الأنواع"}),e.jsx(w,{value:"full",children:"جرد كامل"}),e.jsx(w,{value:"partial",children:"جرد جزئي"}),e.jsx(w,{value:"cycle",children:"جرد دوري"}),e.jsx(w,{value:"spot_check",children:"فحص عشوائي"})]})]}),e.jsx(d,{variant:"outline",size:"icon",onClick:()=>g(),children:e.jsx(Be,{className:"h-4 w-4"})})]}),k&&e.jsx("div",{className:"text-center py-8 text-muted-foreground",children:"جاري التحميل..."}),!k&&U.length===0&&e.jsxs("div",{className:"text-center py-12",children:[e.jsx(ne,{className:"h-12 w-12 mx-auto text-muted-foreground mb-4"}),e.jsx("h3",{className:"font-semibold text-lg mb-2",children:"لا توجد جلسات جرد"}),e.jsx("p",{className:"text-muted-foreground mb-4",children:"ابدأ بإنشاء جلسة جرد جديدة"}),_&&e.jsxs(d,{onClick:()=>i(!0),className:"gap-2",children:[e.jsx(ze,{className:"h-4 w-4"}),"إنشاء جلسة"]})]}),!k&&A&&U.length>0&&e.jsx("div",{children:U.map(a)}),!k&&!A&&U.length>0&&e.jsx("div",{className:"rounded-md border",children:e.jsxs(bs,{children:[e.jsx(ys,{children:e.jsxs(Ce,{children:[e.jsx(Y,{children:"رقم الجلسة"}),e.jsx(Y,{children:"الاسم"}),e.jsx(Y,{children:"النوع"}),e.jsx(Y,{children:"الحالة"}),e.jsx(Y,{children:"التاريخ"}),e.jsx(Y,{children:"الإجراءات"})]})}),e.jsx(ws,{children:U.map(s=>{const v=ye[s.status]||ye.draft,ie=we[s.type]||we.full;return e.jsxs(Ce,{children:[e.jsx(Z,{className:"font-mono text-sm",children:s.session_number}),e.jsx(Z,{children:s.name||"-"}),e.jsx(Z,{children:e.jsx(E,{variant:"outline",className:ie.color,children:ie.label})}),e.jsx(Z,{children:e.jsxs(E,{className:`${v.color} text-white gap-1`,children:[v.icon,v.label]})}),e.jsx(Z,{className:"text-sm",children:s.created_at?hs(new Date(s.created_at),"dd/MM/yyyy",{locale:ms}):"-"}),e.jsx(Z,{children:e.jsxs("div",{className:"flex items-center gap-2",children:[te(s),e.jsxs(ns,{children:[e.jsx(cs,{asChild:!0,children:e.jsx(d,{variant:"ghost",size:"icon",className:"h-8 w-8",children:e.jsx(xs,{className:"h-4 w-4"})})}),e.jsxs(rs,{align:"end",children:[s.status==="draft"&&j("can_create_stocktake")&&e.jsxs(be,{onClick:()=>X(s),children:[e.jsx(ds,{className:"h-4 w-4 ml-2"}),"حذف"]}),(s.status==="draft"||s.status==="in_progress")&&j("can_create_stocktake")&&e.jsxs(be,{onClick:()=>p(s),children:[e.jsx(Se,{className:"h-4 w-4 ml-2"}),"إلغاء"]})]})]})]})})]},s.id)})})]})})]})]}),e.jsx(Ys,{open:I,onOpenChange:i,onSuccess:()=>{i(!1),g()}}),e.jsx(Ge,{open:!!B,onOpenChange:()=>X(null),children:e.jsxs(Je,{children:[e.jsxs(Ke,{children:[e.jsx(We,{children:"تأكيد الحذف"}),e.jsxs(Xe,{children:['هل أنت متأكد من حذف جلسة الجرد "',(B==null?void 0:B.name)||(B==null?void 0:B.session_number),'"؟']})]}),e.jsxs(Ye,{children:[e.jsx(Ze,{children:"إلغاء"}),e.jsx(es,{onClick:q,className:"bg-destructive text-destructive-foreground",children:"حذف"})]})]})})]})};export{St as StocktakeSessionsList};
