import{j as e}from"./vendor-query-BSpUEUJE.js";import{r as l}from"./vendor-react-CWq2BoMo.js";import{C as y,d as j,a as Z,b as ee}from"./card-Dd3vXi0j.js";import{m as _,n as v,a0 as se,o as M,p as C,q as a,S as te,v as ae,w as re,x as S,y as c,z as le,A as n,B as g}from"./currencyConfig-Ch2cK6at.js";import{c as O,e as ne,T as ce,g as F,U as ie,I as de}from"./index-DVdSf46I.js";import{B as A}from"./Index-CsHtvMhJ.js";import{F as oe}from"./filter-CrT8avJX.js";import{f as r,s as B,e as $,b as me,c as xe}from"./vendor-date-BDZqQjIb.js";import"./vendor-ui-core-BWFXPlYg.js";import"./vendor-ui-extra-CW6ezZiQ.js";import"./vendor-pdf-OPq5ZiHo.js";import"./vendor-charts-bmwX1u6y.js";import"./alert-cV8dsncc.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 H=O("CircleArrowDown",[["circle",{cx:"12",cy:"12",r:"10",key:"1mglay"}],["path",{d:"M12 8v8",key:"napkw2"}],["path",{d:"m8 12 4 4 4-4",key:"k98ssh"}]]);/**
 * @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 L=O("CircleArrowUp",[["circle",{cx:"12",cy:"12",r:"10",key:"1mglay"}],["path",{d:"m16 12-4-4-4 4",key:"177agl"}],["path",{d:"M12 16V8",key:"1sbj14"}]]),ke=()=>{const i=ne(),[R,U]=l.useState([]),[k,V]=l.useState([]),[z,T]=l.useState(!0),[f,q]=l.useState("month"),[N,J]=l.useState("all"),[w,K]=l.useState("all"),[b,P]=l.useState(""),Q=()=>{const s=new Date;switch(f){case"today":return{start:r(s,"yyyy-MM-dd"),end:r(s,"yyyy-MM-dd")};case"week":return{start:r(me(s,7),"yyyy-MM-dd"),end:r(s,"yyyy-MM-dd")};case"month":return{start:r(B(s),"yyyy-MM-dd"),end:r($(s),"yyyy-MM-dd")};default:return{start:r(B(s),"yyyy-MM-dd"),end:r($(s),"yyyy-MM-dd")}}};l.useEffect(()=>{i&&Y()},[i,f]);const Y=async()=>{if(i){T(!0);try{const s=localStorage.getItem(ce),{start:t,end:x}=Q(),d=await fetch(`${F()}/auto-crud.php?action=get&table=employees&filters=${encodeURIComponent(JSON.stringify({seller_id:i}))}`,{headers:s?{Authorization:`Bearer ${s}`}:{}});if(d.ok){const o=await d.json();V(o.data||[])}const h=await fetch(`${F()}/auto-crud.php?action=get&table=cash_drawer_transactions&filters=${encodeURIComponent(JSON.stringify({seller_id:i}))}`,{headers:s?{Authorization:`Bearer ${s}`}:{}});if(!h.ok)throw new Error("Failed to fetch transactions");let u=(await h.json()).data||[];const X=new Date(t),I=new Date(x);I.setHours(23,59,59,999),u=u.filter(o=>{const p=new Date(o.created_at);return p>=X&&p<=I}),u.sort((o,p)=>new Date(p.created_at).getTime()-new Date(o.created_at).getTime()),U(u)}catch(s){console.error("Error fetching transactions:",s)}finally{T(!1)}}},G=s=>{var x;if(s.employee_id){const d=k.find(h=>String(h.id)===String(s.employee_id));if(d)return d.employee_name}const t=(x=s.notes)==null?void 0:x.match(/بواسطة\s+(.+?)$/);return t?t[1]:s.employee_name?s.employee_name:"غير محدد"},W=s=>{switch(s){case"cash_in":return"إضافة نقد";case"cash_out":return"سحب نقد";case"opening":return"رصيد افتتاحي";case"closing":return"رصيد إغلاق";default:return s}},m=R.filter(s=>{var t;return!(N!=="all"&&s.employee_id!==N||w!=="all"&&s.transaction_type!==w||b&&!((t=s.reason)!=null&&t.toLowerCase().includes(b.toLowerCase())))}),D=m.filter(s=>s.transaction_type==="cash_in"||s.transaction_type==="opening").reduce((s,t)=>s+Number(t.amount||0),0),E=m.filter(s=>s.transaction_type==="cash_out").reduce((s,t)=>s+Number(t.amount||0),0);return z?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-primary"})}):e.jsxs("div",{className:"space-y-6",dir:"rtl",children:[e.jsxs("div",{className:"grid md:grid-cols-3 gap-4",children:[e.jsx(y,{className:"bg-green-50 dark:bg-green-900/20 border-green-200",children:e.jsx(j,{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:[D.toFixed(2)," د.أ"]})]}),e.jsx(L,{className:"h-10 w-10 text-green-500"})]})})}),e.jsx(y,{className:"bg-red-50 dark:bg-red-900/20 border-red-200",children:e.jsx(j,{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-red-600",children:[E.toFixed(2)," د.أ"]})]}),e.jsx(H,{className:"h-10 w-10 text-red-500"})]})})}),e.jsx(y,{className:"bg-blue-50 dark:bg-blue-900/20 border-blue-200",children:e.jsx(j,{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-blue-600",children:[(D-E).toFixed(2)," د.أ"]})]}),e.jsx(A,{className:"h-10 w-10 text-blue-500"})]})})})]}),e.jsxs(y,{children:[e.jsx(Z,{children:e.jsxs(ee,{className:"flex items-center gap-2",children:[e.jsx(A,{className:"h-6 w-6"}),"حركات الدرج النقدي"]})}),e.jsxs(j,{children:[e.jsxs("div",{className:"flex flex-wrap gap-4 mb-6",children:[e.jsxs(_,{value:f,onValueChange:q,children:[e.jsxs(v,{className:"w-40",children:[e.jsx(se,{className:"h-4 w-4 ml-2"}),e.jsx(M,{})]}),e.jsxs(C,{children:[e.jsx(a,{value:"today",children:"اليوم"}),e.jsx(a,{value:"week",children:"آخر 7 أيام"}),e.jsx(a,{value:"month",children:"هذا الشهر"})]})]}),e.jsxs(_,{value:N,onValueChange:J,children:[e.jsxs(v,{className:"w-48",children:[e.jsx(ie,{className:"h-4 w-4 ml-2"}),e.jsx(M,{placeholder:"الموظف"})]}),e.jsxs(C,{children:[e.jsx(a,{value:"all",children:"كل الموظفين"}),k.map(s=>e.jsx(a,{value:s.id,children:s.employee_name},s.id))]})]}),e.jsxs(_,{value:w,onValueChange:K,children:[e.jsxs(v,{className:"w-40",children:[e.jsx(oe,{className:"h-4 w-4 ml-2"}),e.jsx(M,{placeholder:"النوع"})]}),e.jsxs(C,{children:[e.jsx(a,{value:"all",children:"كل الأنواع"}),e.jsx(a,{value:"cash_in",children:"إضافة نقد"}),e.jsx(a,{value:"cash_out",children:"سحب نقد"}),e.jsx(a,{value:"opening",children:"رصيد افتتاحي"}),e.jsx(a,{value:"closing",children:"رصيد إغلاق"})]})]}),e.jsxs("div",{className:"relative flex-1 min-w-[200px]",children:[e.jsx(te,{className:"absolute right-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground"}),e.jsx(de,{placeholder:"بحث في السبب...",value:b,onChange:s=>P(s.target.value),className:"pr-10"})]})]}),e.jsx("div",{className:"rounded-lg border overflow-hidden",children:e.jsxs(ae,{children:[e.jsx(re,{children:e.jsxs(S,{children:[e.jsx(c,{className:"text-right",children:"التاريخ والوقت"}),e.jsx(c,{className:"text-right",children:"الموظف"}),e.jsx(c,{className:"text-right",children:"النوع"}),e.jsx(c,{className:"text-right",children:"المبلغ"}),e.jsx(c,{className:"text-right",children:"السبب"}),e.jsx(c,{className:"text-right",children:"الحالة"})]})}),e.jsx(le,{children:m.length===0?e.jsx(S,{children:e.jsx(n,{colSpan:6,className:"text-center text-muted-foreground py-8",children:"لا توجد حركات نقدية"})}):m.map(s=>e.jsxs(S,{children:[e.jsx(n,{className:"text-sm",children:r(new Date(s.created_at),"dd/MM/yyyy HH:mm",{locale:xe})}),e.jsx(n,{className:"font-medium",children:G(s)}),e.jsx(n,{children:e.jsxs(g,{variant:"outline",className:s.transaction_type==="cash_in"||s.transaction_type==="opening"?"bg-green-50 text-green-700 border-green-200":"bg-red-50 text-red-700 border-red-200",children:[s.transaction_type==="cash_in"||s.transaction_type==="opening"?e.jsx(L,{className:"h-3 w-3 ml-1"}):e.jsx(H,{className:"h-3 w-3 ml-1"}),W(s.transaction_type)]})}),e.jsxs(n,{className:`font-bold ${s.transaction_type==="cash_in"||s.transaction_type==="opening"?"text-green-600":"text-red-600"}`,children:[s.transaction_type==="cash_out"?"-":"+",Number(s.amount||0).toFixed(2)," د.أ"]}),e.jsx(n,{className:"text-sm text-muted-foreground max-w-[200px] truncate",children:s.reason||"-"}),e.jsx(n,{children:s.requires_approval?s.approved_by?e.jsx(g,{className:"bg-green-100 text-green-800",children:"معتمد"}):e.jsx(g,{className:"bg-yellow-100 text-yellow-800",children:"بانتظار الاعتماد"}):e.jsx(g,{variant:"secondary",children:"تلقائي"})})]},s.id))})]})})]})]})]})};export{ke as CashDrawerMovements};
