import{j as e}from"./vendor-query-BSpUEUJE.js";import{r as D}from"./vendor-react-CWq2BoMo.js";import{C as P,d as A,a as we,b as ye,P as re}from"./card-Dd3vXi0j.js";import{c as me,I as k,a as p,B as y,u as Ce,R as Q,b as Se}from"./index-DVdSf46I.js";import{m as ke,n as pe,o as De,p as Me,q as R,ab as Te,Q as Fe,J as Pe,M as he,B as Z,F as Ae,E as Ie,Z as F,t as ze,v as je,w as ge,x as K,y as j,z as ue,A as g,S as Le,D as ne,a as ce,c as le,d as ie}from"./currencyConfig-Ch2cK6at.js";import{u as $e}from"./usePhpSuppliers-dMSWU_Bv.js";import{S as Y,n as fe,a5 as Be,b as He,a4 as Re,C as Ee,u as Ve,M as Oe,l as Ue,a6 as de}from"./Index-CsHtvMhJ.js";import{z as d,u as qe}from"./vendor-forms-Bkp1rCGP.js";import{t as Ge,F as Je,a as f,b,c as N,d as v,e as w,f as oe}from"./form-B68-jcLg.js";import{D as be,a as Ne,b as ve,c as U}from"./dropdown-menu-_tO9X-Op.js";import{M as _e}from"./mail-D3z08Jo4.js";import{E as Qe}from"./ellipsis-vertical-MEzxSsKu.js";import{D as xe}from"./date-picker-mgFhUy-U.js";import{f as Ze,c as Ke}from"./vendor-date-BDZqQjIb.js";import{F as We}from"./filter-CrT8avJX.js";import"./vendor-pdf-OPq5ZiHo.js";import"./vendor-ui-extra-CW6ezZiQ.js";import"./vendor-ui-core-BWFXPlYg.js";import"./vendor-charts-bmwX1u6y.js";import"./alert-cV8dsncc.js";import"./vendor-barcode-DhT-N8Id.js";import"./calendar-BjYdwRhe.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 Xe=me("ArrowDownRight",[["path",{d:"m7 7 10 10",key:"1fmybs"}],["path",{d:"M17 7v10H7",key:"6fjiku"}]]);/**
 * @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 Ye=me("ArrowUpRight",[["path",{d:"M7 7h10v10",key:"1tivn9"}],["path",{d:"M7 17 17 7",key:"1vkiza"}]]),es=d.object({supplier_name:d.string().min(2,"اسم المورد مطلوب (حد أدنى حرفين)"),supplier_number:d.string().optional(),contact_person:d.string().optional(),phone_number:d.string().optional(),email:d.string().email("البريد الإلكتروني غير صحيح").optional().or(d.literal("")),address:d.string().optional(),city:d.string().optional(),tax_number:d.string().optional(),payment_terms:d.string().optional(),credit_limit:d.number().min(0).optional(),rating:d.number().min(0).max(5).optional(),is_active:d.boolean().default(!0),notes:d.string().optional()}),ss=({supplier:s,onSubmit:o,onCancel:L})=>{const n=qe({resolver:Ge(es),defaultValues:{supplier_name:(s==null?void 0:s.supplier_name)||"",supplier_number:(s==null?void 0:s.supplier_number)||"",contact_person:(s==null?void 0:s.contact_person)||"",phone_number:(s==null?void 0:s.phone_number)||"",email:(s==null?void 0:s.email)||"",address:(s==null?void 0:s.address)||"",city:(s==null?void 0:s.city)||"",tax_number:(s==null?void 0:s.tax_number)||"",payment_terms:(s==null?void 0:s.payment_terms)||"net_30",credit_limit:(s==null?void 0:s.credit_limit)||0,rating:(s==null?void 0:s.rating)||0,is_active:(s==null?void 0:s.is_active)==1||(s==null?void 0:s.is_active)===!0||!s,notes:(s==null?void 0:s.notes)||""}}),x=n.watch("rating"),M=a=>{const u={...a,is_active:a.is_active?1:0};o(u)};return e.jsx(Je,{...n,children:e.jsxs("form",{onSubmit:n.handleSubmit(M),className:"space-y-6",dir:"rtl",children:[e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4",children:[e.jsx(f,{control:n.control,name:"supplier_name",render:({field:a})=>e.jsxs(b,{children:[e.jsx(N,{children:"اسم المورد *"}),e.jsx(v,{children:e.jsx(k,{placeholder:"مثال: شركة التوريدات المتحدة",...a})}),e.jsx(w,{})]})}),e.jsx(f,{control:n.control,name:"supplier_number",render:({field:a})=>e.jsxs(b,{children:[e.jsx(N,{children:"رقم المورد"}),e.jsx(v,{children:e.jsx(k,{placeholder:"رقم تعريفي داخلي",...a})}),e.jsx(w,{})]})}),e.jsx(f,{control:n.control,name:"contact_person",render:({field:a})=>e.jsxs(b,{children:[e.jsx(N,{children:"جهة الاتصال"}),e.jsx(v,{children:e.jsx(k,{placeholder:"اسم الشخص المسؤول",...a})}),e.jsx(w,{})]})}),e.jsx(f,{control:n.control,name:"phone_number",render:({field:a})=>e.jsxs(b,{children:[e.jsx(N,{children:"رقم الهاتف"}),e.jsx(v,{children:e.jsx(k,{placeholder:"05xxxxxxxx",...a})}),e.jsx(w,{})]})}),e.jsx(f,{control:n.control,name:"email",render:({field:a})=>e.jsxs(b,{children:[e.jsx(N,{children:"البريد الإلكتروني"}),e.jsx(v,{children:e.jsx(k,{type:"email",placeholder:"supplier@example.com",...a})}),e.jsx(w,{})]})}),e.jsx(f,{control:n.control,name:"tax_number",render:({field:a})=>e.jsxs(b,{children:[e.jsx(N,{children:"الرقم الضريبي"}),e.jsx(v,{children:e.jsx(k,{placeholder:"رقم التسجيل الضريبي",...a})}),e.jsx(w,{})]})})]}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4",children:[e.jsx(f,{control:n.control,name:"city",render:({field:a})=>e.jsxs(b,{children:[e.jsx(N,{children:"المدينة"}),e.jsx(v,{children:e.jsx(k,{placeholder:"الرياض",...a})}),e.jsx(w,{})]})}),e.jsx(f,{control:n.control,name:"address",render:({field:a})=>e.jsxs(b,{children:[e.jsx(N,{children:"العنوان"}),e.jsx(v,{children:e.jsx(k,{placeholder:"العنوان التفصيلي",...a})}),e.jsx(w,{})]})})]}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4",children:[e.jsx(f,{control:n.control,name:"payment_terms",render:({field:a})=>e.jsxs(b,{children:[e.jsx(N,{children:"شروط الدفع"}),e.jsxs(ke,{onValueChange:a.onChange,defaultValue:a.value,children:[e.jsx(v,{children:e.jsx(pe,{children:e.jsx(De,{placeholder:"اختر شروط الدفع"})})}),e.jsxs(Me,{children:[e.jsx(R,{value:"cash",children:"نقدي"}),e.jsx(R,{value:"net_7",children:"صافي 7 أيام"}),e.jsx(R,{value:"net_15",children:"صافي 15 يوم"}),e.jsx(R,{value:"net_30",children:"صافي 30 يوم"}),e.jsx(R,{value:"net_45",children:"صافي 45 يوم"}),e.jsx(R,{value:"net_60",children:"صافي 60 يوم"}),e.jsx(R,{value:"net_90",children:"صافي 90 يوم"})]})]}),e.jsx(w,{})]})}),e.jsx(f,{control:n.control,name:"credit_limit",render:({field:a})=>e.jsxs(b,{children:[e.jsxs(N,{children:["الحد الائتماني (",Te().symbol,")"]}),e.jsx(v,{children:e.jsx(k,{type:"number",placeholder:"0",...a,onChange:u=>a.onChange(Number(u.target.value))})}),e.jsx(oe,{children:"الحد الأقصى للمبالغ المستحقة"}),e.jsx(w,{})]})})]}),e.jsx(f,{control:n.control,name:"rating",render:({field:a})=>e.jsxs(b,{children:[e.jsx(N,{children:"التقييم"}),e.jsx(v,{children:e.jsxs("div",{className:"flex items-center gap-2",children:[[1,2,3,4,5].map(u=>e.jsx("button",{type:"button",onClick:()=>a.onChange(u),className:"focus:outline-none",children:e.jsx(Y,{className:p("h-6 w-6 transition-colors",u<=(x||0)?"fill-amber-400 text-amber-400":"text-muted-foreground/30 hover:text-amber-400")})},u)),e.jsxs("span",{className:"text-sm text-muted-foreground mr-2",children:[x||0," من 5"]})]})}),e.jsx(w,{})]})}),e.jsx(f,{control:n.control,name:"notes",render:({field:a})=>e.jsxs(b,{children:[e.jsx(N,{children:"ملاحظات"}),e.jsx(v,{children:e.jsx(Fe,{placeholder:"أي ملاحظات إضافية عن المورد...",className:"resize-none",rows:3,...a})}),e.jsx(w,{})]})}),e.jsx(f,{control:n.control,name:"is_active",render:({field:a})=>e.jsxs(b,{className:"flex flex-row items-center justify-between rounded-lg border p-4",children:[e.jsxs("div",{className:"space-y-0.5",children:[e.jsx(N,{className:"text-base",children:"المورد نشط"}),e.jsx(oe,{children:"إلغاء التفعيل يخفي المورد من القوائم"})]}),e.jsx(v,{children:e.jsx(Pe,{checked:a.value,onCheckedChange:a.onChange})})]})}),e.jsxs("div",{className:"flex justify-end gap-3 pt-4 border-t",children:[e.jsx(y,{type:"button",variant:"outline",onClick:L,children:"إلغاء"}),e.jsx(y,{type:"submit",children:s?"حفظ التعديلات":"إضافة المورد"})]})]})})},ts=({supplier:s,onEdit:o,onDelete:L,onViewStatement:n,canManage:x})=>{const M=l=>Array.from({length:5},(q,_)=>e.jsx(Y,{className:p("h-3 w-3",_<l?"fill-amber-400 text-amber-400":"text-muted-foreground/30")},_)),a=l=>l>0?"text-red-600 dark:text-red-400":l<0?"text-green-600 dark:text-green-400":"text-muted-foreground",u=l=>({cash:"نقدي",net_7:"صافي 7 أيام",net_15:"صافي 15 يوم",net_30:"صافي 30 يوم",net_45:"صافي 45 يوم",net_60:"صافي 60 يوم",net_90:"صافي 90 يوم"})[l]||l;return e.jsx(P,{className:"overflow-hidden transition-all hover:shadow-md",children:e.jsxs(A,{className:"p-4",children:[e.jsxs("div",{className:"flex items-start justify-between gap-3",children:[e.jsxs("div",{className:"flex items-start gap-3 flex-1",children:[e.jsx("div",{className:"h-12 w-12 rounded-xl bg-amber-100 dark:bg-amber-900/30 flex items-center justify-center flex-shrink-0",children:e.jsx(he,{className:"h-6 w-6 text-amber-600 dark:text-amber-400"})}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[e.jsx("h3",{className:"font-semibold text-base truncate",children:s.supplier_name}),e.jsx(Z,{variant:s.is_active==1?"default":"secondary",className:"text-[10px] px-1.5 py-0",children:s.is_active==1?"نشط":"غير نشط"})]}),s.supplier_number&&e.jsxs("p",{className:"text-xs text-muted-foreground mb-2",children:["رقم: ",s.supplier_number]}),e.jsxs("div",{className:"space-y-1.5",children:[s.phone_number&&e.jsxs("div",{className:"flex items-center gap-1.5 text-sm",children:[e.jsx(fe,{className:"h-3.5 w-3.5 text-muted-foreground"}),e.jsx("a",{href:`tel:${s.phone_number}`,className:"text-primary hover:underline",children:s.phone_number})]}),s.email&&e.jsxs("div",{className:"flex items-center gap-1.5 text-sm",children:[e.jsx(_e,{className:"h-3.5 w-3.5 text-muted-foreground"}),e.jsx("a",{href:`mailto:${s.email}`,className:"text-primary hover:underline truncate",children:s.email})]}),s.city&&e.jsxs("div",{className:"flex items-center gap-1.5 text-sm text-muted-foreground",children:[e.jsx(Be,{className:"h-3.5 w-3.5"}),e.jsx("span",{children:s.city})]})]})]})]}),e.jsxs(be,{children:[e.jsx(Ne,{asChild:!0,children:e.jsx(y,{variant:"ghost",size:"icon",className:"h-8 w-8",children:e.jsx(Qe,{className:"h-4 w-4"})})}),e.jsxs(ve,{align:"end",children:[e.jsxs(U,{onClick:n,children:[e.jsx(Ae,{className:"h-4 w-4 ml-2"}),"كشف حساب"]}),x&&e.jsxs(e.Fragment,{children:[e.jsxs(U,{onClick:o,children:[e.jsx(Ie,{className:"h-4 w-4 ml-2"}),"تعديل"]}),e.jsxs(U,{onClick:L,className:"text-red-600",children:[e.jsx(He,{className:"h-4 w-4 ml-2"}),"حذف"]})]})]})]})]}),e.jsxs("div",{className:"mt-4 pt-3 border-t flex flex-wrap items-center justify-between gap-3",children:[e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsx("div",{className:"flex items-center gap-1",children:M(s.rating||0)}),s.payment_terms&&e.jsx(Z,{variant:"outline",className:"text-xs",children:u(s.payment_terms)})]}),e.jsxs("div",{className:"text-left",children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:"الرصيد"}),e.jsx("p",{className:p("font-semibold",a(s.current_balance||0)),children:F(s.current_balance||0)})]})]})]})})},as=({supplier:s})=>{const{user:o}=Ce(),{get:L}=Re(),n=(o==null?void 0:o.seller_id)||(o==null?void 0:o.id),[x,M]=D.useState([]),[a,u]=D.useState(!1),[l,q]=D.useState(new Date(new Date().setMonth(new Date().getMonth()-3))),[_,E]=D.useState(new Date),C=async()=>{if(!(!n||!(s!=null&&s.id))){u(!0);try{const r=await L(`auto-crud.php?action=get&table=purchase_orders&filter[seller_id]=${n}&filter[supplier_id]=${s.id}`),m=(r==null?void 0:r.data)||[];console.log("📦 Purchase Orders:",m.length,m);const $=await L(`auto-crud.php?action=get&table=supplier_payments&filter[seller_id]=${n}&filter[supplier_id]=${s.id}`),z=($==null?void 0:$.data)||[];console.log("💰 Payments:",z.length,z),console.log("🔍 Supplier ID used:",s.id);const S=[];let T=0;m.forEach(c=>{const h=Number(c.total_amount)||0;T+=h,S.push({id:c.id,date:c.order_date,type:"purchase_order",reference:c.po_number,description:`أمر شراء - ${c.status==="received"?"مستلم":c.status==="partial"?"جزئي":"معلق"}`,debit:h,credit:0,balance:T})}),z.forEach(c=>{const h=Number(c.amount)||0;T-=h,S.push({id:c.id,date:c.payment_date,type:"payment",reference:c.reference_number||"-",description:`دفعة - ${c.payment_method==="cash"?"نقدي":c.payment_method==="bank_transfer"?"تحويل":c.payment_method==="check"?"شيك":"أخرى"}`,debit:0,credit:h,balance:T})}),S.sort((c,h)=>new Date(c.date).getTime()-new Date(h.date).getTime());let B=0;S.forEach(c=>{B+=c.debit-c.credit,c.balance=B});const G=S.filter(c=>{const h=new Date(c.date);h.setHours(12,0,0,0);const V=l?new Date(l):null;V&&V.setHours(0,0,0,0);const O=_?new Date(_):null;return O&&O.setHours(23,59,59,999),(!V||h>=V)&&(!O||h<=O)});console.log("📊 Total transactions before filter:",S.length),console.log("📅 Date range:",l==null?void 0:l.toISOString(),"to",_==null?void 0:_.toISOString()),console.log("✅ Filtered transactions:",G.length),M(G)}catch(r){console.error("Error fetching statement:",r)}finally{u(!1)}}};D.useEffect(()=>{C()},[s==null?void 0:s.id,n,l,_]);const W=x.reduce((r,m)=>r+m.debit,0),X=x.reduce((r,m)=>r+m.credit,0),i=x.length>0?x[x.length-1].balance:0,I=()=>{window.print()};return e.jsxs("div",{className:"space-y-4",dir:"rtl",children:[e.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-3 gap-4",children:[e.jsx(P,{className:"border-red-200 dark:border-red-800",children:e.jsx(A,{className:"pt-4",children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"p-2 bg-red-100 dark:bg-red-900/30 rounded-lg",children:e.jsx(Ye,{className:"h-5 w-5 text-red-600"})}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"إجمالي المشتريات"}),e.jsx("p",{className:"text-xl font-bold text-red-600",children:F(W)})]})]})})}),e.jsx(P,{className:"border-green-200 dark:border-green-800",children:e.jsx(A,{className:"pt-4",children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"p-2 bg-green-100 dark:bg-green-900/30 rounded-lg",children:e.jsx(Xe,{className:"h-5 w-5 text-green-600"})}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"إجمالي المدفوعات"}),e.jsx("p",{className:"text-xl font-bold text-green-600",children:F(X)})]})]})})}),e.jsx(P,{className:p("border-2",i>0?"border-red-300":i<0?"border-green-300":"border-gray-300"),children:e.jsx(A,{className:"pt-4",children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:p("p-2 rounded-lg",i>0?"bg-red-100 dark:bg-red-900/30":i<0?"bg-green-100 dark:bg-green-900/30":"bg-gray-100 dark:bg-gray-900/30"),children:e.jsx(Ee,{className:p("h-5 w-5",i>0?"text-red-600":i<0?"text-green-600":"text-gray-600")})}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"الرصيد الحالي"}),e.jsxs("p",{className:p("text-xl font-bold",i>0?"text-red-600":i<0?"text-green-600":"text-gray-600"),children:[F(Math.abs(i)),i>0?" (مستحق)":i<0?" (لصالحك)":""]})]})]})})})]}),e.jsx(P,{children:e.jsx(A,{className:"py-4",children:e.jsxs("div",{className:"flex flex-wrap items-center gap-4",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"text-sm text-muted-foreground",children:"من:"}),e.jsx(xe,{value:l,onChange:r=>r&&q(r)})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"text-sm text-muted-foreground",children:"إلى:"}),e.jsx(xe,{value:_,onChange:r=>r&&E(r)})]}),e.jsx("div",{className:"flex-1"}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(y,{variant:"outline",size:"sm",onClick:C,children:[e.jsx(Q,{className:p("h-4 w-4 ml-2",a&&"animate-spin")}),"تحديث"]}),e.jsxs(y,{variant:"outline",size:"sm",onClick:I,children:[e.jsx(ze,{className:"h-4 w-4 ml-2"}),"طباعة"]})]})]})})}),e.jsx(P,{children:e.jsx(A,{className:"p-0",children:a?e.jsx("div",{className:"flex items-center justify-center py-12",children:e.jsx(Q,{className:"h-8 w-8 animate-spin text-primary"})}):x.length===0?e.jsx("div",{className:"text-center py-12 text-muted-foreground",children:"لا توجد حركات في الفترة المحددة"}):e.jsxs(je,{children:[e.jsx(ge,{children:e.jsxs(K,{children:[e.jsx(j,{className:"text-right",children:"التاريخ"}),e.jsx(j,{className:"text-right",children:"النوع"}),e.jsx(j,{className:"text-right",children:"المرجع"}),e.jsx(j,{className:"text-right",children:"البيان"}),e.jsx(j,{className:"text-right",children:"مدين"}),e.jsx(j,{className:"text-right",children:"دائن"}),e.jsx(j,{className:"text-right",children:"الرصيد"})]})}),e.jsx(ue,{children:x.map(r=>e.jsxs(K,{children:[e.jsx(g,{children:Ze(new Date(r.date),"dd/MM/yyyy",{locale:Ke})}),e.jsx(g,{children:e.jsx(Z,{variant:r.type==="purchase_order"?"default":"secondary",children:r.type==="purchase_order"?"مشتريات":"دفعة"})}),e.jsx(g,{children:r.reference}),e.jsx(g,{children:r.description}),e.jsx(g,{className:"text-red-600",children:r.debit>0?F(r.debit):"-"}),e.jsx(g,{className:"text-green-600",children:r.credit>0?F(r.credit):"-"}),e.jsx(g,{className:p("font-medium",r.balance>0?"text-red-600":r.balance<0?"text-green-600":""),children:F(Math.abs(r.balance))})]},r.id))})]})})})]})},ks=()=>{const{currentEmployee:s}=Se(),{checkPermission:o,silentCheck:L}=Ve(),{toast:n}=Oe(),x=Ue(),{suppliers:M,isLoading:a,error:u,createSupplier:l,updateSupplier:q,deleteSupplier:_,refetchSuppliers:E}=$e(),[C,W]=D.useState(""),[X,i]=D.useState(!1),[I,r]=D.useState(null),[m,$]=D.useState(null),[z,S]=D.useState(null),T=(s==null?void 0:s.can_manage_suppliers)===!0||(s==null?void 0:s.can_manage_suppliers)==1||(s==null?void 0:s.role)==="admin",B=(M==null?void 0:M.filter(t=>{var ee,se,te,ae;const H=((ee=t.supplier_name)==null?void 0:ee.toLowerCase().includes(C.toLowerCase()))||((se=t.supplier_number)==null?void 0:se.toLowerCase().includes(C.toLowerCase()))||((te=t.contact_person)==null?void 0:te.toLowerCase().includes(C.toLowerCase()))||((ae=t.phone_number)==null?void 0:ae.includes(C)),J=z===null||t.is_active==1===z;return H&&J}))||[],G=async t=>{if(o("can_manage_suppliers","ليس لديك صلاحية إضافة الموردين"))try{const H=s==null?void 0:s.seller_id;await l({...t,seller_id:H}),await E(),n({title:"تم إضافة المورد",description:`تم إضافة المورد "${t.supplier_name}" بنجاح`}),i(!1)}catch{n({title:"خطأ",description:"فشل في إضافة المورد",variant:"destructive"})}},c=async t=>{if(I&&o("can_manage_suppliers","ليس لديك صلاحية تعديل الموردين"))try{await q(I.id,t),await E(),n({title:"تم التحديث",description:`تم تحديث بيانات المورد "${t.supplier_name}" بنجاح`}),r(null)}catch{n({title:"خطأ",description:"فشل في تحديث المورد",variant:"destructive"})}},h=async t=>{if(o("can_manage_suppliers","ليس لديك صلاحية حذف الموردين")&&confirm(`هل أنت متأكد من حذف المورد "${t.supplier_name}"؟`))try{await _(t.id),await E(),n({title:"تم الحذف",description:`تم حذف المورد "${t.supplier_name}" بنجاح`})}catch{n({title:"خطأ",description:"فشل في حذف المورد",variant:"destructive"})}},V=t=>Array.from({length:5},(H,J)=>e.jsx(Y,{className:p("h-3 w-3",J<t?"fill-amber-400 text-amber-400":"text-muted-foreground/30")},J)),O=t=>t>0?"text-red-600 dark:text-red-400":t<0?"text-green-600 dark:text-green-400":"text-muted-foreground";return a?e.jsx("div",{className:"flex items-center justify-center h-64",children:e.jsx(Q,{className:"h-8 w-8 animate-spin text-primary"})}):e.jsxs("div",{className:"space-y-4",dir:"rtl",children:[e.jsxs(P,{className:"border-amber-200 dark:border-amber-800",children:[e.jsx(we,{className:"pb-3",children:e.jsxs("div",{className:"flex flex-col sm:flex-row sm:items-center justify-between gap-4",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"p-2 bg-amber-100 dark:bg-amber-900/30 rounded-lg",children:e.jsx(de,{className:"h-6 w-6 text-amber-600 dark:text-amber-400"})}),e.jsxs("div",{children:[e.jsx(ye,{className:"text-xl",children:"إدارة الموردين"}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:[B.length," مورد"]})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(y,{variant:"outline",size:"sm",onClick:()=>E(),children:[e.jsx(Q,{className:"h-4 w-4 ml-2"}),"تحديث"]}),T&&e.jsxs(y,{onClick:()=>i(!0),children:[e.jsx(re,{className:"h-4 w-4 ml-2"}),"إضافة مورد"]})]})]})}),e.jsx(A,{children:e.jsxs("div",{className:"flex flex-col sm:flex-row gap-3",children:[e.jsxs("div",{className:"relative flex-1",children:[e.jsx(Le,{className:"absolute right-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground"}),e.jsx(k,{placeholder:"بحث بالاسم أو الرقم أو الهاتف...",value:C,onChange:t=>W(t.target.value),className:"pr-10"})]}),e.jsxs(be,{children:[e.jsx(Ne,{asChild:!0,children:e.jsxs(y,{variant:"outline",size:"sm",children:[e.jsx(We,{className:"h-4 w-4 ml-2"}),z===null?"الكل":z?"نشط":"غير نشط"]})}),e.jsxs(ve,{align:"end",children:[e.jsx(U,{onClick:()=>S(null),children:"الكل"}),e.jsx(U,{onClick:()=>S(!0),children:"نشط"}),e.jsx(U,{onClick:()=>S(!1),children:"غير نشط"})]})]})]})})]}),B.length===0?e.jsx(P,{children:e.jsxs(A,{className:"flex flex-col items-center justify-center py-12",children:[e.jsx(de,{className:"h-12 w-12 text-muted-foreground/50 mb-4"}),e.jsx("p",{className:"text-muted-foreground text-center",children:C?"لا توجد نتائج للبحث":"لا يوجد موردين حالياً"}),T&&!C&&e.jsxs(y,{variant:"outline",className:"mt-4",onClick:()=>i(!0),children:[e.jsx(re,{className:"h-4 w-4 ml-2"}),"إضافة أول مورد"]})]})}):x?e.jsx("div",{className:"grid gap-3",children:B.map(t=>e.jsx(ts,{supplier:t,onEdit:()=>r(t),onDelete:()=>h(t),onViewStatement:()=>$(t),canManage:T},t.id))}):e.jsx(P,{children:e.jsx(A,{className:"p-0",children:e.jsxs(je,{children:[e.jsx(ge,{children:e.jsxs(K,{children:[e.jsx(j,{className:"text-right",children:"المورد"}),e.jsx(j,{className:"text-right",children:"التواصل"}),e.jsx(j,{className:"text-right",children:"التقييم"}),e.jsx(j,{className:"text-right",children:"الحد الائتماني"}),e.jsx(j,{className:"text-right",children:"الرصيد الحالي"}),e.jsx(j,{className:"text-right",children:"الحالة"}),e.jsx(j,{className:"text-right",children:"الإجراءات"})]})}),e.jsx(ue,{children:B.map(t=>e.jsxs(K,{children:[e.jsx(g,{children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"h-10 w-10 rounded-full bg-amber-100 dark:bg-amber-900/30 flex items-center justify-center",children:e.jsx(he,{className:"h-5 w-5 text-amber-600"})}),e.jsxs("div",{children:[e.jsx("p",{className:"font-medium",children:t.supplier_name}),e.jsx("p",{className:"text-xs text-muted-foreground",children:t.supplier_number})]})]})}),e.jsx(g,{children:e.jsxs("div",{className:"space-y-1",children:[t.phone_number&&e.jsxs("div",{className:"flex items-center gap-1 text-sm",children:[e.jsx(fe,{className:"h-3 w-3"}),t.phone_number]}),t.email&&e.jsxs("div",{className:"flex items-center gap-1 text-sm text-muted-foreground",children:[e.jsx(_e,{className:"h-3 w-3"}),t.email]})]})}),e.jsx(g,{children:e.jsx("div",{className:"flex gap-0.5",children:V(t.rating||0)})}),e.jsx(g,{children:t.credit_limit?F(Number(t.credit_limit)):"غير محدد"}),e.jsx(g,{children:e.jsx("span",{className:O(t.current_balance||0),children:F(Number(t.current_balance||0))})}),e.jsx(g,{children:e.jsx(Z,{variant:t.is_active==1?"default":"secondary",children:t.is_active==1?"نشط":"غير نشط"})}),e.jsx(g,{children:e.jsxs("div",{className:"flex gap-2",children:[e.jsx(y,{variant:"outline",size:"sm",onClick:()=>$(t),children:"كشف حساب"}),T&&e.jsxs(e.Fragment,{children:[e.jsx(y,{variant:"ghost",size:"sm",onClick:()=>r(t),children:"تعديل"}),e.jsx(y,{variant:"ghost",size:"sm",className:"text-red-600",onClick:()=>h(t),children:"حذف"})]})]})})]},t.id))})]})})}),e.jsx(ne,{open:X||!!I,onOpenChange:t=>{t||(i(!1),r(null))},children:e.jsxs(ce,{className:"max-w-2xl max-h-[90vh] overflow-y-auto",children:[e.jsx(le,{children:e.jsx(ie,{children:I?"تعديل بيانات المورد":"إضافة مورد جديد"})}),e.jsx(ss,{supplier:I,onSubmit:I?c:G,onCancel:()=>{i(!1),r(null)}})]})}),e.jsx(ne,{open:!!m,onOpenChange:t=>{t||$(null)},children:e.jsxs(ce,{className:"max-w-4xl max-h-[90vh] overflow-y-auto",children:[e.jsx(le,{children:e.jsxs(ie,{children:["كشف حساب المورد: ",m==null?void 0:m.supplier_name]})}),m&&e.jsx(as,{supplier:m})]})})]})};export{ks as SupplierManagement};
