import{j as e}from"./vendor-query-BSpUEUJE.js";import{r as d}from"./vendor-react-CWq2BoMo.js";import{C as m,d as x,a as R,b as P}from"./card-Dd3vXi0j.js";import{e as De,b as Ce,T as ne,g as B,j as E,B as g,L as D,U as Me,E as re,X as z,S as Fe,A as Te,F as Ae,J as _,G as Oe}from"./index-DVdSf46I.js";import{a0 as ce,F as de,m as L,n as V,o as J,p as U,q as f,B as S,t as ie,v as He,w as Ie,x as q,y as u,z as Re,A as h,D as Pe,a as Be,c as Ee,d as ze,M as Le}from"./currencyConfig-Ch2cK6at.js";import{b as Ve,D as oe,C as Je,A as Ue,c as qe,d as Ke,f as We,g as Ge,h as Qe,i as Xe,j as Ye,W as Ze,k as es,B as ss}from"./Index-CsHtvMhJ.js";import{F as as}from"./filter-CrT8avJX.js";import{C as ts}from"./chart-pie-oUDzrogX.js";import{R as ls,a as ns,b as rs,C as cs,T as ds}from"./vendor-charts-bmwX1u6y.js";import{f as i,s as me,e as xe,b as is,c as C}from"./vendor-date-BDZqQjIb.js";import"./vendor-pdf-OPq5ZiHo.js";import"./vendor-ui-extra-CW6ezZiQ.js";import"./vendor-ui-core-BWFXPlYg.js";import"./alert-cV8dsncc.js";import"./vendor-barcode-DhT-N8Id.js";const he=["#22c55e","#3b82f6","#a855f7","#f97316","#64748b","#ec4899","#06b6d4"],os=n=>{switch(n==null?void 0:n.toLowerCase()){case"cash":return ss;case"card":return es;case"cliq":return Fe;case"bank_transfer":return Le;default:return Ze}},ms=n=>{switch(n==null?void 0:n.toLowerCase()){case"cash":return"bg-green-500";case"card":return"bg-blue-500";case"cliq":return"bg-purple-500";case"bank_transfer":return"bg-gray-500";case"credit":return"bg-orange-500";default:return"bg-slate-500"}},ks=()=>{const n=De(),{currentEmployee:o}=Ce(),[K,pe]=d.useState([]),[W,ue]=d.useState([]),[je,G]=d.useState(!0),[l,ge]=d.useState(null),[fe,Q]=d.useState(!1),[v,X]=d.useState(null),[Y,Z]=d.useState(!1),[p,$]=d.useState(null),ee=K.filter(s=>s.status==="open").length,[M,Ne]=d.useState("month"),[F,be]=d.useState("all"),[T,ve]=d.useState("all"),[A,ye]=d.useState(!1),O=(o==null?void 0:o.role)&&["owner","admin","manager"].includes(o.role.toLowerCase()),we=()=>{const s=new Date;switch(M){case"today":return{start:i(s,"yyyy-MM-dd"),end:i(s,"yyyy-MM-dd")};case"week":return{start:i(is(s,7),"yyyy-MM-dd"),end:i(s,"yyyy-MM-dd")};case"month":return{start:i(me(s),"yyyy-MM-dd"),end:i(xe(s),"yyyy-MM-dd")};default:return{start:i(me(s),"yyyy-MM-dd"),end:i(xe(s),"yyyy-MM-dd")}}};d.useEffect(()=>{n&&H()},[n,M]);const H=async()=>{if(n){G(!0);try{const s=localStorage.getItem(ne),{start:t,end:r}=we(),a=await fetch(`${B()}/auto-crud.php?action=get&table=employees&filters=${encodeURIComponent(JSON.stringify({seller_id:n}))}`,{headers:s?{Authorization:`Bearer ${s}`}:{}});if(a.ok){const w=await a.json();ue(w.data||[])}const c=await fetch(`${B()}/auto-crud.php?action=get&table=shifts&filters=${encodeURIComponent(JSON.stringify({seller_id:n}))}`,{headers:s?{Authorization:`Bearer ${s}`}:{}});if(!c.ok)throw new Error("Failed to fetch shifts");let b=(await c.json()).data||[];const I=new Date(t),le=new Date(r);le.setHours(23,59,59,999),b=b.filter(w=>{const k=new Date(w.opened_at);return k>=I&&k<=le}),b.sort((w,k)=>new Date(k.opened_at).getTime()-new Date(w.opened_at).getTime()),pe(b)}catch(s){console.error("Error fetching shifts:",s)}finally{G(!1)}}},se=s=>{if(s.employee_name)return s.employee_name;if(!s.employee_id)return"غير محدد";const t=W.find(r=>String(r.id)===String(s.employee_id));return(t==null?void 0:t.employee_name)||"غير معروف"},N=K.filter(s=>!(F!=="all"&&s.employee_id!==F||T!=="all"&&s.status!==T||A&&(s.status!=="closed"||Math.abs(Number(s.cash_difference||0))<.01))),_e=N.reduce((s,t)=>s+Number(t.total_sales||0),0),y=N.filter(s=>s.status==="closed").reduce((s,t)=>s+Number(t.cash_difference||0),0),Se=s=>{switch(s==null?void 0:s.toLowerCase()){case"cash":return"💵";case"card":return"💳";case"cliq":return"📱";case"bank_transfer":return"🏦";case"credit":return"📋";default:return"💰"}},ae=s=>{const t=window.open("","_blank","width=800,height=600");if(!t)return;const r=s.closed_at?Math.round((new Date(s.closed_at).getTime()-new Date(s.opened_at).getTime())/1e3/60):0,a=typeof s.payment_breakdown=="string"?JSON.parse(s.payment_breakdown||"[]"):s.payment_breakdown||[],c=a.length>0?a.map(j=>`
          <div class="row">
            <span class="label">
              ${Se(j.method_code)} ${j.method_name}:
            </span>
            <span class="value">
              ${Number(j.total||0).toFixed(2)} د.أ (${j.count} فاتورة)
              ${j.affects_cash_drawer?'<span class="cash-tag">يؤثر على الدرج</span>':""}
            </span>
          </div>
        `).join(""):`
          <div class="row"><span class="label">💵 المبيعات النقدية:</span><span class="value">${Number(s.total_cash_sales||0).toFixed(2)} د.أ</span></div>
          <div class="row"><span class="label">💳 المبيعات بالبطاقة:</span><span class="value">${Number(s.total_card_sales||0).toFixed(2)} د.أ</span></div>
          <div class="row"><span class="label">📋 المبيعات الآجلة:</span><span class="value">${Number(s.total_credit_sales||0).toFixed(2)} د.أ</span></div>
        `;t.document.write(`
      <html dir="rtl">
        <head>
          <title>تقرير الشفت - ${s.shift_number}</title>
          <style>
            body { font-family: 'Cairo', Arial, sans-serif; padding: 20px; direction: rtl; }
            .header { text-align: center; border-bottom: 2px solid #333; padding-bottom: 10px; margin-bottom: 20px; }
            .section { margin: 20px 0; }
            .row { display: flex; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid #eee; }
            .label { font-weight: bold; }
            .value { color: #2563eb; }
            .total-row { background: #f3f4f6; padding: 12px; margin: 10px 0; border-radius: 8px; font-size: 18px; font-weight: bold; }
            .difference { padding: 15px; border-radius: 8px; margin: 10px 0; }
            .difference.positive { background: #dcfce7; color: #16a34a; }
            .difference.negative { background: #fee2e2; color: #dc2626; }
            .cash-tag { 
              background: #dbeafe; 
              color: #1d4ed8; 
              padding: 2px 8px; 
              border-radius: 4px; 
              font-size: 11px; 
              margin-right: 8px; 
            }
            @media print { button { display: none; } }
          </style>
        </head>
        <body>
          <div class="header"><h1>تقرير الشفت</h1><h2>${s.shift_number}</h2></div>
          <div class="section">
            <h3>معلومات الشفت</h3>
            <div class="row"><span class="label">وقت الفتح:</span><span class="value">${i(new Date(s.opened_at),"dd/MM/yyyy HH:mm",{locale:C})}</span></div>
            ${s.closed_at?`<div class="row"><span class="label">وقت الإغلاق:</span><span class="value">${i(new Date(s.closed_at),"dd/MM/yyyy HH:mm",{locale:C})}</span></div><div class="row"><span class="label">مدة الشفت:</span><span class="value">${r} دقيقة</span></div>`:""}
            ${s.employee_name?`<div class="row"><span class="label">الموظف:</span><span class="value">${s.employee_name}</span></div>`:""}
          </div>
          <div class="section">
            <h3>المبيعات حسب طريقة الدفع</h3>
            <div class="row"><span class="label">عدد الفواتير:</span><span class="value">${s.total_invoices}</span></div>
            ${c}
            <div class="total-row"><span>📊 إجمالي المبيعات: ${Number(s.total_sales||0).toFixed(2)} د.أ</span></div>
          </div>
          <div class="section">
            <h3>الدرج النقدي</h3>
            <div class="row"><span class="label">الرصيد الافتتاحي:</span><span class="value">${Number(s.opening_cash||0).toFixed(2)} د.أ</span></div>
            ${s.closed_at?`<div class="row"><span class="label">الرصيد المتوقع:</span><span class="value">${Number(s.expected_cash||0).toFixed(2)} د.أ</span></div><div class="row"><span class="label">الرصيد الفعلي:</span><span class="value">${Number(s.closing_cash||0).toFixed(2)} د.أ</span></div><div class="difference ${Number(s.cash_difference||0)>=0?"positive":"negative"}"><strong>الفرق: ${Number(s.cash_difference||0).toFixed(2)} د.أ</strong> ${Number(s.cash_difference||0)>=0?"(زيادة)":"(نقص)"}</div>`:""}
          </div>
          ${s.notes?`<div class="section"><h3>ملاحظات</h3><p>${s.notes}</p></div>`:""}
          <script>window.onload = () => { setTimeout(() => { window.print(); window.close(); }, 500); };<\/script>
        </body>
      </html>
    `),t.document.close()},te=s=>{ge(s),Q(!0)},$e=async s=>{if(O){X(s.id);try{const t=localStorage.getItem(ne);if(!(await fetch(`${B()}${Te.UPDATE("shifts",s.id)}`,{method:"POST",headers:{"Content-Type":"application/json",...t?{Authorization:`Bearer ${t}`}:{}},body:JSON.stringify({status:"closed",closed_at:Ae(new Date),closed_by:(o==null?void 0:o.id)||null,notes:(s.notes||"")+` - تم إغلاقه عن بُعد بواسطة المدير (${(o==null?void 0:o.employee_name)||"غير محدد"})`})})).ok)throw new Error("فشل إغلاق الشفت");_.success(`تم إغلاق الشفت ${s.shift_number} عن بُعد`),H(),$(null)}catch{_.error("فشل إغلاق الشفت عن بُعد")}finally{X(null)}}},ke=async()=>{if(n){Z(!0);try{const{closedCount:s}=await Oe(n);s>0?(_.success(`تم إغلاق ${s} شفتات يتيمة`),H()):_.info("لا توجد شفتات يتيمة للتنظيف")}catch{_.error("فشل تنظيف الشفتات اليتيمة")}finally{Z(!1)}}};return je?e.jsx("div",{className:"flex items-center justify-center h-64",children:e.jsx("div",{className:"animate-spin rounded-full h-12 w-12 border-b-2 border-blue-600"})}):e.jsxs("div",{className:"space-y-6",dir:"rtl",children:[ee>1&&e.jsx(m,{className:"border-2 border-red-400 bg-red-50 dark:bg-red-900/20",children:e.jsx(x,{className:"py-4",children:e.jsxs("div",{className:"flex flex-col sm:flex-row items-start sm:items-center justify-between gap-3",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(E,{className:"h-8 w-8 text-red-600 flex-shrink-0"}),e.jsxs("div",{children:[e.jsxs("p",{className:"font-bold text-red-800 dark:text-red-200 font-cairo text-lg",children:["تحذير: ",ee," شفتات مفتوحة!"]}),e.jsx("p",{className:"text-sm text-red-700 dark:text-red-300 font-cairo",children:'يجب أن يكون هناك شفت واحد فقط مفتوح. اضغط على "تنظيف" لإغلاق الشفتات الزائدة.'})]})]}),e.jsxs(g,{variant:"destructive",onClick:ke,disabled:Y,className:"min-h-[44px] gap-2 font-cairo",children:[Y?e.jsx(D,{className:"h-4 w-4 animate-spin"}):e.jsx(Ve,{className:"h-4 w-4"}),"تنظيف الشفتات اليتيمة"]})]})})}),e.jsxs("div",{className:"grid md:grid-cols-3 gap-4",children:[e.jsx(m,{children:e.jsx(x,{className:"p-4",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"عدد الشفتات"}),e.jsx("p",{className:"text-2xl font-bold",children:N.length})]}),e.jsx(ce,{className:"h-10 w-10 text-primary"})]})})}),e.jsx(m,{children:e.jsx(x,{className:"p-4",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"إجمالي المبيعات"}),e.jsxs("p",{className:"text-2xl font-bold text-green-600",children:[_e.toFixed(2)," د.أ"]})]}),e.jsx(oe,{className:"h-10 w-10 text-green-500"})]})})}),e.jsx(m,{className:y<0?"bg-red-50 dark:bg-red-900/20":"",children:e.jsx(x,{className:"p-4",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"إجمالي الفروقات"}),e.jsxs("p",{className:`text-2xl font-bold ${y>=0?"text-green-600":"text-red-600"}`,children:[y>=0?"+":"",y.toFixed(2)," د.أ"]})]}),e.jsx(E,{className:`h-10 w-10 ${y>=0?"text-green-500":"text-red-500"}`})]})})})]}),e.jsxs(m,{children:[e.jsx(R,{children:e.jsxs(P,{className:"flex items-center gap-2",children:[e.jsx(de,{className:"h-6 w-6"}),"تقارير الشفتات"]})}),e.jsxs(x,{children:[e.jsxs("div",{className:"flex flex-col sm:flex-row flex-wrap gap-3 mb-6",children:[e.jsxs("div",{className:"grid grid-cols-2 sm:flex gap-2 flex-1",children:[e.jsxs(L,{value:M,onValueChange:Ne,children:[e.jsxs(V,{className:"w-full sm:w-40 h-11",children:[e.jsx(Je,{className:"h-4 w-4 ml-2"}),e.jsx(J,{})]}),e.jsxs(U,{children:[e.jsx(f,{value:"today",children:"اليوم"}),e.jsx(f,{value:"week",children:"آخر 7 أيام"}),e.jsx(f,{value:"month",children:"هذا الشهر"})]})]}),e.jsxs(L,{value:F,onValueChange:be,children:[e.jsxs(V,{className:"w-full sm:w-48 h-11",children:[e.jsx(Me,{className:"h-4 w-4 ml-2"}),e.jsx(J,{placeholder:"الموظف"})]}),e.jsxs(U,{children:[e.jsx(f,{value:"all",children:"كل الموظفين"}),W.map(s=>e.jsx(f,{value:s.id,children:s.employee_name},s.id))]})]})]}),e.jsxs("div",{className:"grid grid-cols-2 sm:flex gap-2",children:[e.jsxs(L,{value:T,onValueChange:ve,children:[e.jsxs(V,{className:"w-full sm:w-36 h-11",children:[e.jsx(as,{className:"h-4 w-4 ml-2"}),e.jsx(J,{placeholder:"الحالة"})]}),e.jsxs(U,{children:[e.jsx(f,{value:"all",children:"الكل"}),e.jsx(f,{value:"open",children:"مفتوح"}),e.jsx(f,{value:"closed",children:"مغلق"})]})]}),e.jsxs(g,{variant:A?"default":"outline",onClick:()=>ye(!A),className:"gap-2 h-11 min-h-[44px]",children:[e.jsx(E,{className:"h-4 w-4"}),e.jsx("span",{className:"hidden sm:inline",children:"الفروقات فقط"}),e.jsx("span",{className:"sm:hidden",children:"فروقات"})]})]})]}),e.jsx("div",{className:"md:hidden space-y-3",children:N.length===0?e.jsx("div",{className:"text-center text-muted-foreground py-8",children:"لا توجد شفتات"}):N.map(s=>{const t=s.closed_at?Math.round((new Date(s.closed_at).getTime()-new Date(s.opened_at).getTime())/1e3/60):0,r=s.status==="closed"&&Math.abs(Number(s.cash_difference||0))>0;return e.jsx(m,{className:`border-2 ${r&&Number(s.cash_difference)<0?"border-red-300 bg-red-50 dark:bg-red-900/10":"border-border"}`,children:e.jsxs(x,{className:"p-4 space-y-3",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(ce,{className:"h-5 w-5 text-primary"}),e.jsx("span",{className:"font-bold",children:s.shift_number})]}),e.jsx(S,{variant:s.status==="open"?"default":"secondary",children:s.status==="open"?"مفتوح":"مغلق"})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-3 text-sm",children:[e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground",children:"التاريخ:"}),e.jsx("p",{className:"font-medium",children:i(new Date(s.opened_at),"dd/MM/yyyy HH:mm",{locale:C})})]}),e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground",children:"الموظف:"}),e.jsx("p",{className:"font-medium",children:se(s)})]}),e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground",children:"المدة:"}),e.jsx("p",{className:"font-medium",children:s.status==="closed"?`${t} دقيقة`:"جاري"})]}),e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground",children:"الفواتير:"}),e.jsx("p",{className:"font-bold",children:s.total_invoices})]})]}),e.jsxs("div",{className:"flex items-center justify-between pt-2 border-t",children:[e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground text-sm",children:"المبيعات:"}),e.jsxs("p",{className:"font-bold text-green-600",children:[Number(s.total_sales||0).toFixed(2)," د.أ"]})]}),s.status==="closed"&&s.cash_difference!==null&&e.jsxs(S,{className:`text-sm ${Number(s.cash_difference)>=0?"bg-green-100 text-green-800":"bg-red-100 text-red-800"}`,children:["الفرق: ",Number(s.cash_difference)>=0?"+":"",Number(s.cash_difference).toFixed(2)," د.أ"]})]}),e.jsxs("div",{className:"flex gap-2 pt-2",children:[e.jsxs(g,{size:"sm",variant:"outline",onClick:()=>te(s),className:"flex-1 min-h-[44px]",children:[e.jsx(re,{className:"h-4 w-4 ml-2"}),"عرض"]}),s.status==="closed"&&e.jsxs(g,{size:"sm",variant:"outline",onClick:()=>ae(s),className:"flex-1 min-h-[44px]",children:[e.jsx(ie,{className:"h-4 w-4 ml-2"}),"طباعة"]}),O&&s.status==="open"&&e.jsxs(g,{size:"sm",variant:"destructive",onClick:()=>$(s),disabled:v===s.id,className:"flex-1 min-h-[44px]",children:[v===s.id?e.jsx(D,{className:"h-4 w-4 animate-spin ml-2"}):e.jsx(z,{className:"h-4 w-4 ml-2"}),"إنهاء"]})]})]})},s.id)})}),e.jsx("div",{className:"hidden md:block rounded-lg border overflow-hidden",children:e.jsxs(He,{children:[e.jsx(Ie,{children:e.jsxs(q,{children:[e.jsx(u,{className:"text-right",children:"رقم الشفت"}),e.jsx(u,{className:"text-right",children:"التاريخ"}),e.jsx(u,{className:"text-right",children:"الموظف"}),e.jsx(u,{className:"text-right",children:"المدة"}),e.jsx(u,{className:"text-right",children:"الفواتير"}),e.jsx(u,{className:"text-right",children:"المبيعات"}),e.jsx(u,{className:"text-right",children:"الفرق النقدي"}),e.jsx(u,{className:"text-right",children:"الحالة"}),e.jsx(u,{className:"text-center",children:"الإجراءات"})]})}),e.jsx(Re,{children:N.length===0?e.jsx(q,{children:e.jsx(h,{colSpan:9,className:"text-center text-muted-foreground py-8",children:"لا توجد شفتات"})}):N.map(s=>{const t=s.closed_at?Math.round((new Date(s.closed_at).getTime()-new Date(s.opened_at).getTime())/1e3/60):0,r=s.status==="closed"&&Math.abs(Number(s.cash_difference||0))>0;return e.jsxs(q,{className:r&&Number(s.cash_difference)<0?"bg-red-50 dark:bg-red-900/10":"",children:[e.jsx(h,{className:"font-semibold",children:s.shift_number}),e.jsx(h,{className:"text-sm",children:i(new Date(s.opened_at),"dd/MM/yyyy HH:mm",{locale:C})}),e.jsx(h,{className:"font-medium",children:se(s)}),e.jsx(h,{children:s.status==="closed"?e.jsxs("span",{className:"text-sm",children:[t," دقيقة"]}):e.jsx(S,{variant:"outline",className:"bg-green-50 text-green-700",children:"مفتوح"})}),e.jsx(h,{className:"font-bold text-center",children:s.total_invoices}),e.jsxs(h,{className:"font-bold text-green-600",children:[Number(s.total_sales||0).toFixed(2)," د.أ"]}),e.jsx(h,{children:s.status==="closed"&&s.cash_difference!==null?e.jsxs(S,{className:Number(s.cash_difference)>=0?"bg-green-100 text-green-800":"bg-red-100 text-red-800",children:[Number(s.cash_difference)>=0?"+":"",Number(s.cash_difference).toFixed(2)," د.أ"]}):e.jsx("span",{className:"text-muted-foreground",children:"-"})}),e.jsx(h,{children:e.jsx(S,{variant:s.status==="open"?"default":"secondary",children:s.status==="open"?"مفتوح":"مغلق"})}),e.jsx(h,{children:e.jsxs("div",{className:"flex gap-2 justify-center",children:[e.jsx(g,{size:"sm",variant:"outline",onClick:()=>te(s),className:"min-h-[36px]",title:"عرض التفاصيل",children:e.jsx(re,{className:"h-4 w-4"})}),s.status==="closed"&&e.jsx(g,{size:"sm",variant:"outline",onClick:()=>ae(s),className:"min-h-[36px]",title:"طباعة",children:e.jsx(ie,{className:"h-4 w-4"})}),O&&s.status==="open"&&e.jsx(g,{size:"sm",variant:"destructive",onClick:()=>$(s),disabled:v===s.id,className:"min-h-[36px]",title:"إنهاء الشفت عن بُعد",children:v===s.id?e.jsx(D,{className:"h-4 w-4 animate-spin"}):e.jsx(z,{className:"h-4 w-4"})})]})})]},s.id)})})]})})]})]}),l&&e.jsx(Pe,{open:fe,onOpenChange:Q,children:e.jsxs(Be,{dir:"rtl",className:"max-w-[95vw] sm:max-w-2xl max-h-[90vh] overflow-y-auto",children:[e.jsx(Ee,{children:e.jsxs(ze,{children:["تفاصيل الشفت - ",l.shift_number]})}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-3",children:[e.jsx(m,{children:e.jsx(x,{className:"p-4",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"عدد الفواتير"}),e.jsx("p",{className:"text-2xl font-bold",children:l.total_invoices})]}),e.jsx(de,{className:"h-8 w-8 text-blue-500"})]})})}),e.jsx(m,{children:e.jsx(x,{className:"p-4",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"إجمالي المبيعات"}),e.jsxs("p",{className:"text-2xl font-bold text-green-600",children:[Number(l.total_sales||0).toFixed(2)," د.أ"]})]}),e.jsx(oe,{className:"h-8 w-8 text-green-500"})]})})})]}),(()=>{let s=[];if(l.payment_breakdown)try{s=typeof l.payment_breakdown=="string"?JSON.parse(l.payment_breakdown):l.payment_breakdown,Array.isArray(s)||(s=[])}catch{s=[]}s.length===0&&(s=[{method_code:"cash",method_name:"نقدي",total:Number(l.total_cash_sales||0),count:0,affects_cash_drawer:!0},{method_code:"card",method_name:"بطاقة",total:Number(l.total_card_sales||0),count:0,affects_cash_drawer:!1},{method_code:"credit",method_name:"آجل",total:Number(l.total_credit_sales||0),count:0,affects_cash_drawer:!1}].filter(a=>a.total>0));const t=s.reduce((a,c)=>a+c.total,0)||Number(l.total_sales||0),r=s.map((a,c)=>({name:a.method_name,value:a.total,percentage:t>0?(a.total/t*100).toFixed(1):"0",color:he[c%he.length]}));return e.jsxs(m,{children:[e.jsx(R,{className:"pb-2",children:e.jsxs(P,{className:"text-base flex items-center gap-2",children:[e.jsx(ts,{className:"h-4 w-4"}),"تحليل المبيعات حسب طريقة الدفع"]})}),e.jsx(x,{children:s.length>0?e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4",children:[e.jsx("div",{className:"h-48",children:e.jsx(ls,{width:"100%",height:"100%",children:e.jsxs(ns,{children:[e.jsx(rs,{data:r,dataKey:"value",nameKey:"name",cx:"50%",cy:"50%",innerRadius:40,outerRadius:70,paddingAngle:2,label:({name:a,percentage:c})=>`${c}%`,labelLine:!1,children:r.map((a,c)=>e.jsx(cs,{fill:a.color},`cell-${c}`))}),e.jsx(ds,{formatter:a=>[`${a.toFixed(2)} د.أ`,"المبلغ"],contentStyle:{direction:"rtl"}})]})})}),e.jsx("div",{className:"space-y-2",children:s.map((a,c)=>{const j=os(a.method_code),b=ms(a.method_code),I=t>0?(a.total/t*100).toFixed(1):"0";return e.jsxs("div",{className:"flex items-center gap-3 p-2 rounded-lg bg-muted/50",children:[e.jsx("div",{className:`p-1.5 rounded ${b}`,children:e.jsx(j,{className:"h-4 w-4 text-white"})}),e.jsxs("div",{className:"flex-1",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("span",{className:"font-medium text-sm",children:a.method_name}),e.jsxs("span",{className:"font-bold",children:[a.total.toFixed(2)," د.أ"]})]}),e.jsxs("div",{className:"flex items-center justify-between text-xs text-muted-foreground",children:[e.jsxs("span",{children:[I,"% من الإجمالي"]}),a.count>0&&e.jsxs("span",{children:[a.count," فاتورة"]})]})]})]},a.method_code)})})]}):e.jsx("div",{className:"text-center text-muted-foreground py-4",children:"لا توجد بيانات طرق الدفع"})})]})})(),l.status==="closed"&&e.jsxs(m,{children:[e.jsx(R,{className:"pb-2",children:e.jsx(P,{className:"text-base",children:"الدرج النقدي"})}),e.jsxs(x,{className:"space-y-2",children:[e.jsxs("div",{className:"flex justify-between",children:[e.jsx("span",{children:"الرصيد الافتتاحي:"}),e.jsxs("span",{className:"font-bold",children:[Number(l.opening_cash||0).toFixed(2)," د.أ"]})]}),e.jsxs("div",{className:"flex justify-between",children:[e.jsx("span",{children:"الرصيد المتوقع:"}),e.jsxs("span",{className:"font-bold",children:[Number(l.expected_cash||0).toFixed(2)," د.أ"]})]}),e.jsxs("div",{className:"flex justify-between",children:[e.jsx("span",{children:"الرصيد الفعلي:"}),e.jsxs("span",{className:"font-bold",children:[Number(l.closing_cash||0).toFixed(2)," د.أ"]})]}),e.jsxs("div",{className:`flex justify-between p-3 rounded-lg ${Number(l.cash_difference||0)>=0?"bg-green-50 dark:bg-green-900/20":"bg-red-50 dark:bg-red-900/20"}`,children:[e.jsx("span",{className:"font-bold",children:"الفرق:"}),e.jsxs("span",{className:`font-bold ${Number(l.cash_difference||0)>=0?"text-green-600":"text-red-600"}`,children:[Number(l.cash_difference||0)>=0?"+":"",Number(l.cash_difference||0).toFixed(2)," د.أ"]})]})]})]})]})]})}),e.jsx(Ue,{open:!!p,onOpenChange:s=>!s&&$(null),children:e.jsxs(qe,{dir:"rtl",children:[e.jsxs(Ke,{children:[e.jsx(We,{className:"font-cairo",children:"إنهاء الشفت عن بُعد"}),e.jsxs(Ge,{className:"font-cairo",children:["هل أنت متأكد من إنهاء الشفت ",e.jsx("span",{className:"font-bold",children:p==null?void 0:p.shift_number})," للموظف ",e.jsx("span",{className:"font-bold",children:(p==null?void 0:p.employee_name)||"غير محدد"}),"؟",e.jsx("br",{}),e.jsx("span",{className:"text-amber-600",children:"سيتم إغلاق الشفت بدون إدخال رصيد الإغلاق."})]})]}),e.jsxs(Qe,{className:"flex-row-reverse gap-2",children:[e.jsx(Xe,{className:"font-cairo",children:"إلغاء"}),e.jsxs(Ye,{onClick:()=>p&&$e(p),className:"bg-destructive text-destructive-foreground hover:bg-destructive/90 font-cairo",children:[v?e.jsx(D,{className:"h-4 w-4 animate-spin ml-2"}):e.jsx(z,{className:"h-4 w-4 ml-2"}),"إنهاء الشفت"]})]})]})})]})};export{ks as ShiftReport};
