import{r as m}from"./vendor-react-CWq2BoMo.js";import{T as p,g as d,A as u,J as t}from"./index-DVdSf46I.js";const T=a=>{const[v,y]=m.useState([]),[E,g]=m.useState(!0),_=async()=>{var e,n;if(console.log("🔍 [useEmployees] Starting fetch..."),console.log("🔍 [useEmployees] seller_id:",a),!a){console.warn("⚠️ [useEmployees] No sellerId provided, skipping fetch"),g(!1);return}try{const s=localStorage.getItem(p),o=JSON.stringify({seller_id:a}),c=`${d()}${u.GET_ALL("employees")}&filters=${encodeURIComponent(o)}&orderBy=employee_name&orderDir=ASC`;console.log("🔍 [useEmployees] Fetching URL:",c),console.log("🔍 [useEmployees] Filters:",o);const l=await fetch(c,{headers:s?{Authorization:`Bearer ${s}`}:{}});if(!l.ok)throw new Error("Failed to fetch employees");const i=await l.json();console.log("✅ [useEmployees] Received employees:",((e=i.data)==null?void 0:e.length)||0),console.log("✅ [useEmployees] Employee details:",(n=i.data)==null?void 0:n.map(r=>({id:r.id,name:r.employee_name,username:r.username,seller_id:r.seller_id,is_active:r.is_active}))),y(i.data||[])}catch(s){console.error("❌ [useEmployees] Error fetching employees:",s),t.error("فشل في تحميل الموظفين")}finally{g(!1)}};return m.useEffect(()=>{_()},[a]),{employees:v,loading:E,refetch:_,createEmployee:async e=>{if(a)try{const n=localStorage.getItem(p),s={...e,seller_id:a,is_active:e.is_active?1:0,can_access_pos:e.can_access_pos?1:0,can_view_dashboard:e.can_view_dashboard?1:0,can_create_invoices:e.can_create_invoices?1:0,can_edit_invoices:e.can_edit_invoices?1:0,can_delete_invoices:e.can_delete_invoices?1:0,can_view_customers:e.can_view_customers?1:0,can_approve_discounts:e.can_approve_discounts?1:0,can_approve_returns:e.can_approve_returns?1:0,can_manage_products:e.can_manage_products?1:0,can_manage_inventory:e.can_manage_inventory?1:0,can_view_reports:e.can_view_reports?1:0,can_view_administration:e.can_view_administration?1:0,can_manage_employees:e.can_manage_employees?1:0,can_manage_shifts:e.can_manage_shifts?1:0,can_view_settings:e.can_view_settings?1:0,can_view_expenses:e.can_view_expenses?1:0,can_create_expenses:e.can_create_expenses?1:0,can_approve_expenses:e.can_approve_expenses?1:0,can_edit_settings:e.can_edit_settings?1:0},o=await fetch(`${d()}${u.CREATE("employees")}`,{method:"POST",headers:{"Content-Type":"application/json",...n?{Authorization:`Bearer ${n}`}:{}},body:JSON.stringify(s)});if(!o.ok)throw new Error("Failed to create employee");const c=await o.json();return c.data?(t.success("تم إضافة الموظف بنجاح"),await _(),c.data):null}catch(n){return console.error("Error creating employee:",n),t.error("فشل في إضافة الموظف"),null}},updateEmployee:async(e,n)=>{try{const s=localStorage.getItem(p),o={...n};["is_active","is_owner","can_access_pos","can_view_dashboard","can_create_invoices","can_edit_invoices","can_delete_invoices","can_view_customers","can_approve_discounts","can_approve_returns","can_manage_products","can_manage_inventory","can_view_reports","can_view_administration","can_manage_employees","can_manage_shifts","can_view_settings","can_view_expenses","can_create_expenses","can_approve_expenses","can_edit_settings"].forEach(r=>{r in o&&(o[r]=o[r]?1:0)});const l=await fetch(`${d()}${u.UPDATE("employees",e)}`,{method:"POST",headers:{"Content-Type":"application/json",...s?{Authorization:`Bearer ${s}`}:{}},body:JSON.stringify(o)});if(!l.ok)throw new Error("Failed to update employee");const i=await l.json();return i.data?(t.success("تم تحديث بيانات الموظف"),await _(),i.data):null}catch(s){return console.error("Error updating employee:",s),t.error("فشل في تحديث الموظف"),null}},deleteEmployee:async e=>{try{const n=localStorage.getItem(p);if(!(await fetch(`${d()}${u.DELETE("employees",e)}`,{method:"POST",headers:n?{Authorization:`Bearer ${n}`}:{}})).ok)throw new Error("Failed to delete employee");return t.success("تم حذف الموظف"),await _(),!0}catch(n){return console.error("Error deleting employee:",n),t.error("فشل في حذف الموظف"),!1}}}};export{T as u};
