import{r as i,a as Le,t as Me,j as s}from"./app-BBZbvTSO.js";import{c as K,B as N}from"./button-CZmOQP7j.js";import{I as ze}from"./input-dmj4QjAp.js";import{T as Ve,a as qe,b as F,c as ee,d as Ue,e as A}from"./table-BDtwArHp.js";import{A as se,P as Be,a as He,b as j,c as Je,d as T,e as te,f as Xe}from"./pagination-tuDq0xKj.js";import{F as Ge,S as ae,a as re,b as le,c as ie,d as $}from"./select-KQeGfQXO.js";import{S as Qe}from"./search-bar-FrEZMnbC.js";import{C as ce}from"./checkbox-B0_9jnY6.js";import{d as ne,a as oe,b as de,c as he}from"./tooltip-CRkL5R4V.js";import{P as fe,a as pe,b as xe,D as We}from"./date-picker-D96g6NFs.js";import{L as me}from"./label-BPDCCRHl.js";import{S as je}from"./scroll-area-hNGxDuJk.js";import{S as Ye}from"./switch-9bH0NbTy.js";import{X as ue}from"./index-C0VDnRYN.js";import{L as Ze}from"./loader-circle-BqAFTH1R.js";/**
 * @license lucide-react v0.475.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const es=[["path",{d:"M12 5v14",key:"s699le"}],["path",{d:"m19 12-7 7-7-7",key:"1idqje"}]],ss=K("ArrowDown",es);/**
 * @license lucide-react v0.475.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const ts=[["path",{d:"m5 12 7-7 7 7",key:"hav0vg"}],["path",{d:"M12 19V5",key:"x0mq9r"}]],as=K("ArrowUp",ts);/**
 * @license lucide-react v0.475.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const rs=[["path",{d:"M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8",key:"1357e3"}],["path",{d:"M3 3v5h5",key:"1xhq8a"}]],ls=K("RotateCcw",rs);/**
 * @license lucide-react v0.475.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const is=[["path",{d:"M20 7h-9",key:"3s1dr2"}],["path",{d:"M14 17H5",key:"gfn3mx"}],["circle",{cx:"17",cy:"17",r:"3",key:"18b49y"}],["circle",{cx:"7",cy:"7",r:"3",key:"dfmy0x"}]],cs=K("Settings2",is),bs=i.forwardRef(({columns:ge,searchPlaceholder:ve="Search...",searchRoute:Ne,actions:J=[],onRowClick:k,selectable:I=!1,onSelectionChange:E,selectedItems:X,itemKey:L="id",pageName:O},_e)=>{var Z;const[f,M]=i.useState(""),[u,z]=i.useState("10"),[we,be]=i.useState([]),[d,V]=i.useState(null),[p,q]=i.useState("asc"),[n,S]=i.useState({}),[h,P]=i.useState({}),[o,x]=i.useState([]),[_,U]=i.useState(ge.map(e=>({...e,visible:e.visible!==!1}))),[r,ye]=i.useState({data:[],current_page:1,last_page:1,from:0,to:0,total:0}),[Se,G]=i.useState(!1),[C,Q]=i.useState(!0),[w,m]=i.useState(1);i.useRef(null);const b=i.useRef(null),B=i.useRef(!1);i.useImperativeHandle(_e,()=>({fetchData:()=>{b.current&&clearTimeout(b.current);const e={page:w,search:f,per_page:u,sort:d,direction:p,...n,...Object.fromEntries(Object.entries(h).flatMap(([a,t])=>{const l=[];return t!=null&&t.from&&(l.push([`filter_${a}_from`,t.from.toISOString().split("T")[0]]),t.to&&l.push([`filter_${a}_to`,t.to.toISOString().split("T")[0]])),l}))};H(e)}})),i.useEffect(()=>{const e=localStorage.getItem(`table_settings:${O}`);if(e)try{let a;try{a=JSON.parse(e)}catch{try{a=JSON.parse(decodeURIComponent(atob(e)))}catch(c){console.error("Failed to decode table settings:",c);return}}const t=a;t.columnVisibility&&U(l=>l.map(c=>({...c,visible:t.columnVisibility[c.accessorKey]??c.visible}))),t.filters&&(S(t.filters),x(Object.keys(t.filters).map(l=>l.replace("filter_","")))),t.dateRanges&&(P(t.dateRanges),x(l=>[...l,...Object.keys(t.dateRanges)])),t.sortColumn&&(V(t.sortColumn),q(t.sortDirection)),t.itemsPerPage&&z(t.itemsPerPage),t.search&&M(t.search),t.currentPage&&m(t.currentPage)}catch(a){console.error("Failed to load table settings:",a)}Q(!1)},[O]),i.useEffect(()=>{if(!C){if(b.current&&clearTimeout(b.current),!B.current)return;b.current=setTimeout(()=>{const e={page:w,search:f,per_page:u,sort:d,direction:p,...n,...Object.fromEntries(Object.entries(h).flatMap(([a,t])=>{const l=[];return t!=null&&t.from&&(l.push([`filter_${a}_from`,t.from.toISOString().split("T")[0]]),t.to&&l.push([`filter_${a}_to`,t.to.toISOString().split("T")[0]])),l}))};H(e)},100)}},[C,w,f,u,d,p,n,h]),i.useEffect(()=>{if(!C&&!B.current){const e={page:w,search:f,per_page:u,sort:d,direction:p,...n,...Object.fromEntries(Object.entries(h).flatMap(([a,t])=>{const l=[];return t!=null&&t.from&&(l.push([`filter_${a}_from`,t.from.toISOString().split("T")[0]]),t.to&&l.push([`filter_${a}_to`,t.to.toISOString().split("T")[0]])),l}))};H(e),B.current=!0}},[C]),i.useEffect(()=>{if(!C){const e={columnVisibility:Object.fromEntries(_.map(a=>[a.accessorKey,a.visible])),filters:n,dateRanges:h,sortColumn:d,sortDirection:p,itemsPerPage:u,search:f,currentPage:w};try{const a=btoa(encodeURIComponent(JSON.stringify(e)));localStorage.setItem(`table_settings:${O}`,a)}catch(a){console.error("Failed to save table settings:",a)}}},[_,n,h,d,p,u,f,w,C,O]);const H=async(e={})=>{try{G(!0);const a={page:e.page||w,per_page:e.per_page||u,sort:e.sort||d,direction:e.direction||p,search:e.search!==void 0?e.search:f,...e};Object.keys(a).forEach(l=>{(a[l]===void 0||l!=="search"&&a[l]==="")&&delete a[l]});const t=await Le.get(Ne,{params:a});ye(t.data),Q(!1)}catch{Me.error("Failed to fetch data")}finally{G(!1)}},g=e=>e==null||typeof e!="object"?e:typeof L=="function"?L(e):e[L],v=X??we,R=e=>{X===void 0&&be(e),E==null||E(e)},Ce=e=>{M(e),m(1)},y=e=>{m(e)},$e=e=>{z(e),m(1)},Ie=e=>{const a=d===e&&p==="asc"?"desc":"asc";V(e),q(a)},W=(e,a)=>{const t={...n};a?(t[`filter_${e}`]=a,o.includes(e)||x([...o,e])):(delete t[`filter_${e}`],x(o.filter(l=>l!==e))),S(t),m(1)},Pe=(e,a)=>{const t={...n},l={...h};a!=null&&a.from?(t[`filter_${e}_from`]=a.from.toISOString().split("T")[0],a.to&&(t[`filter_${e}_to`]=a.to.toISOString().split("T")[0]),l[e]=a,o.includes(e)||x([...o,e])):(delete t[`filter_${e}_from`],delete t[`filter_${e}_to`],delete l[e],x(o.filter(c=>c!==e))),S(t),P(l),m(1)},De=e=>{const a={...n},t={...h};delete a[`filter_${e}`],delete a[`filter_${e}_from`],delete a[`filter_${e}_to`],delete t[e],S(a),P(t),x(o.filter(l=>l!==e)),m(1)},Te=()=>{S({}),P({}),x([]),m(1)},ke=e=>{R(e?r.data:[])},Oe=(e,a)=>{let t;a?t=v.some(c=>g(c)===g(e))?v:[...v,e]:t=v.filter(l=>g(l)!==g(e)),R(t)},Re=((Z=r==null?void 0:r.data)==null?void 0:Z.length)>0&&r.data.every(e=>v.some(a=>g(a)===g(e))),Fe=e=>d!==e?s.jsx(se,{className:"h-4 w-4"}):p==="asc"?s.jsx(as,{className:"h-4 w-4"}):s.jsx(ss,{className:"h-4 w-4"}),Ae=e=>{const a=_.map(t=>t.accessorKey===e?{...t,visible:!t.visible}:t);U(a)},D=_.filter(e=>e.visible),Y=D.filter(e=>e.filter),Ke=e=>{if(!(e!=null&&e.from))return"";const a=e.from.toLocaleDateString(),t=e.to?e.to.toLocaleDateString():"";return t?`${a} - ${t}`:a},Ee=()=>{M(""),m(1),z("10"),V(null),q("asc"),S({}),P({}),x([])};return i.useEffect(()=>()=>{b.current&&clearTimeout(b.current)},[]),s.jsxs("div",{className:"space-y-5",children:[s.jsxs("div",{className:"flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4",children:[s.jsxs("div",{className:"w-full sm:max-w-md flex flex-col sm:flex-row items-start sm:items-center gap-2",children:[s.jsx(Qe,{value:f,onChange:Ce,placeholder:ve,className:"w-full"}),s.jsxs("div",{className:"flex items-center gap-2 w-full sm:w-auto",children:[Y.length>0&&s.jsx("div",{className:"flex items-center gap-2 whitespace-nowrap",children:s.jsxs(fe,{children:[s.jsx(pe,{asChild:!0,children:s.jsx(N,{variant:"outline",size:"icon",className:`${o.length>0?"text-primary":""}`,children:s.jsx(Ge,{className:"h-4 w-4"})})}),s.jsx(xe,{className:"w-[calc(100vw-2rem)] sm:w-80",children:s.jsxs("div",{className:"space-y-4",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("h4",{className:"font-medium px-2",children:"Filters"}),o.length>0&&s.jsx(N,{variant:"ghost",size:"sm",onClick:Te,className:"h-8",children:"Clear filters"})]}),s.jsx(je,{className:"h-[300px] pr-4",children:s.jsx("div",{className:"space-y-4 px-2",children:Y.map(e=>{var a,t,l;return s.jsxs("div",{className:"space-y-2",children:[s.jsx(me,{children:e.header}),((a=e.filter)==null?void 0:a.type)==="select"&&e.filter.options&&s.jsxs(ae,{value:n[`filter_${e.accessorKey}`]||"all",onValueChange:c=>W(e.accessorKey,c==="all"?"":c),children:[s.jsx(re,{children:s.jsx(le,{placeholder:e.filter.placeholder||"Select..."})}),s.jsxs(ie,{children:[s.jsx($,{value:"all",children:"All"}),e.filter.options.map(c=>s.jsx($,{value:c.value,children:c.label},c.value))]})]}),((t=e.filter)==null?void 0:t.type)==="text"&&s.jsx(ze,{value:n[`filter_${e.accessorKey}`]||"",onChange:c=>W(e.accessorKey,c.target.value),placeholder:e.filter.placeholder||"Filter..."}),((l=e.filter)==null?void 0:l.type)==="date"&&s.jsx(We,{date:h[e.accessorKey],onSelect:c=>Pe(e.accessorKey,c),mode:"range",placeholder:e.filter.placeholder||"Select date range...",numberOfMonths:2})]},e.accessorKey)})})})]})})]})}),s.jsx("div",{className:"flex items-center gap-2 whitespace-nowrap",children:s.jsxs(fe,{children:[s.jsx(pe,{asChild:!0,children:s.jsxs(N,{variant:"outline",size:"icon",className:"relative",children:[s.jsx(cs,{className:"h-4 w-4"}),_.some(e=>!e.visible)&&s.jsx("span",{className:"absolute -top-1 -right-1 h-2 w-2 rounded-full bg-primary"})]})}),s.jsx(xe,{className:"w-[calc(100vw-2rem)] sm:w-72",children:s.jsxs("div",{className:"space-y-4",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("h4",{className:"font-medium",children:"Column Settings"}),s.jsx(N,{variant:"ghost",size:"sm",onClick:()=>U(e=>e.map(a=>({...a,visible:!0}))),className:"h-8 text-xs",children:"Show all"})]}),s.jsx(je,{className:"h-[300px] pr-4",children:s.jsx("div",{className:"space-y-1",children:_.map(e=>s.jsxs("div",{className:"flex items-center justify-between py-2 px-1 rounded-md hover:bg-muted/50",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[e.sortable&&s.jsx(se,{className:"h-3 w-3 text-muted-foreground"}),s.jsx(me,{className:"text-sm font-normal cursor-pointer",children:e.header})]}),s.jsx(Ye,{checked:e.visible,onCheckedChange:()=>Ae(e.accessorKey)})]},e.accessorKey))})})]})})]})}),(o.length>0||f||d)&&s.jsx("div",{className:"flex items-center gap-2 whitespace-nowrap",children:s.jsx(ne,{children:s.jsxs(oe,{children:[s.jsx(de,{asChild:!0,children:s.jsxs(N,{variant:"outline",size:"icon",onClick:Ee,className:"relative",children:[s.jsx(ls,{className:"h-4 w-4"}),s.jsx("span",{className:"sr-only",children:"Reset table"})]})}),s.jsx(he,{children:"Reset table to initial state"})]})})}),I&&v.length>0&&s.jsxs("div",{className:"flex items-center gap-2 whitespace-nowrap",children:[s.jsxs("span",{className:"text-sm text-muted-foreground",children:[v.length," selected"]}),s.jsx(ne,{children:s.jsxs(oe,{children:[s.jsx(de,{asChild:!0,children:s.jsx(N,{variant:"outline",size:"icon",onClick:()=>{R([])},children:s.jsx(ue,{className:"h-4 w-4"})})}),s.jsx(he,{children:"Clear selection"})]})})]})]})]}),J.length>0&&s.jsx("div",{className:"flex gap-2 w-full sm:w-auto justify-end",children:J.filter(e=>e.show!==!1).map((e,a)=>s.jsxs(N,{onClick:e.onClick,variant:e.variant||"default",className:"w-full sm:w-auto",children:[e.icon&&s.jsx("span",{children:e.icon}),s.jsx("span",{className:"hidden sm:inline",children:e.label}),!e.icon&&s.jsx("span",{className:"sm:hidden",children:e.label.charAt(0)})]},a))})]}),o.length>0&&s.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[s.jsx("span",{className:"text-sm text-muted-foreground",children:"Active filters:"}),o.map(e=>{var l;const a=_.find(c=>c.accessorKey===e),t=((l=a==null?void 0:a.filter)==null?void 0:l.type)==="date";return s.jsxs("div",{className:"flex items-center gap-1 bg-muted px-2 py-1 rounded-md",children:[s.jsxs("span",{className:"text-sm",children:[a==null?void 0:a.header,": ",t?Ke(h[e]):n[`filter_${e}`]]}),s.jsx(N,{variant:"ghost",size:"icon",className:"h-4 w-4",onClick:()=>De(e),children:s.jsx(ue,{className:"h-3 w-3"})})]},e)})]}),s.jsx("div",{className:"rounded-md overflow-x-auto",children:s.jsxs(Ve,{children:[s.jsx(qe,{className:"bg-muted/50",children:s.jsxs(F,{className:"hover:bg-muted/50",children:[I&&s.jsx(ee,{className:"w-px",children:s.jsx(ce,{checked:Re,onCheckedChange:ke,"aria-label":"Select all"})}),D.map(e=>s.jsx(ee,{className:`align-middle ${e.sortable?"cursor-pointer hover:bg-muted":""} ${e.padding?e.padding:""} ${e.width?e.width:""}`,children:s.jsxs("div",{className:`flex items-center gap-2 ${e.align?`justify-${e.align}`:""}`,onClick:()=>e.sortable&&Ie(e.accessorKey),children:[e.header,e.sortable&&s.jsx("span",{className:"text-muted-foreground",children:Fe(e.accessorKey)})]})},e.accessorKey))]})}),s.jsx(Ue,{children:Se?s.jsx(F,{children:s.jsx(A,{colSpan:D.length+(I?1:0),className:"text-center h-24",children:s.jsx(Ze,{className:"h-5 w-5 mx-auto animate-spin text-muted-foreground"})})}):!(r!=null&&r.data)||r.data.length===0?s.jsx(F,{children:s.jsx(A,{colSpan:D.length+(I?1:0),className:"text-center h-24 text-muted-foreground",children:"No items found"})}):r.data.map((e,a)=>s.jsxs(F,{className:k?"cursor-pointer hover:bg-muted/50":"",onClick:()=>k==null?void 0:k(e),children:[I&&s.jsx(A,{className:"w-px",children:s.jsx(ce,{checked:v.some(t=>g(t)===g(e)),onCheckedChange:t=>Oe(e,t),onClick:t=>t.stopPropagation(),"aria-label":`Select ${e.name||`item ${a+1}`}`})}),D.map(t=>s.jsx(A,{className:`py-2 align-middle ${t.align?`text-${t.align}`:""}`,children:t.cell?t.cell(e):e[t.accessorKey]},t.accessorKey))]},a))})]})}),(r==null?void 0:r.data)&&r.data.length>0&&s.jsxs("div",{className:"flex flex-col sm:flex-row justify-between gap-4 mt-4",children:[s.jsx("div",{className:"w-full sm:w-1/2",children:s.jsx(Be,{className:"justify-start",children:s.jsxs(He,{children:[s.jsx(j,{children:s.jsx(Je,{href:"#",onClick:e=>{e.preventDefault(),r.current_page>1&&y(r.current_page-1)},className:r.current_page===1?"pointer-events-none opacity-50":""})}),s.jsx(j,{children:s.jsx(T,{href:"#",onClick:e=>{e.preventDefault(),y(1)},isActive:r.current_page===1,children:"1"})}),r.current_page>3&&s.jsx(j,{children:s.jsx(te,{})}),r.current_page>2&&s.jsx(j,{children:s.jsx(T,{href:"#",onClick:e=>{e.preventDefault(),y(r.current_page-1)},children:r.current_page-1})}),r.current_page!==1&&r.current_page!==r.last_page&&s.jsx(j,{children:s.jsx(T,{href:"#",onClick:e=>{e.preventDefault(),y(r.current_page)},isActive:!0,children:r.current_page})}),r.current_page<r.last_page-1&&s.jsx(j,{children:s.jsx(T,{href:"#",onClick:e=>{e.preventDefault(),y(r.current_page+1)},children:r.current_page+1})}),r.current_page<r.last_page-2&&s.jsx(j,{children:s.jsx(te,{})}),r.last_page>1&&s.jsx(j,{children:s.jsx(T,{href:"#",onClick:e=>{e.preventDefault(),y(r.last_page)},isActive:r.current_page===r.last_page,children:r.last_page})}),s.jsx(j,{children:s.jsx(Xe,{href:"#",onClick:e=>{e.preventDefault(),r.current_page<r.last_page&&y(r.current_page+1)},className:r.current_page===r.last_page?"pointer-events-none opacity-50":""})})]})})}),s.jsxs("div",{className:"flex flex-col sm:flex-row justify-end items-start sm:items-center gap-4 w-full sm:w-1/2",children:[s.jsxs("div",{className:"text-sm text-muted-foreground",children:["Showing ",s.jsx("span",{className:"font-semibold",children:r.from})," to"," ",s.jsx("span",{className:"font-semibold",children:r.to})," of"," ",s.jsx("span",{className:"font-semibold",children:r.total})," items"]}),s.jsxs("div",{className:"flex items-center",children:[s.jsx("span",{className:"text-sm text-muted-foreground mr-2",children:"Per page:"}),s.jsxs(ae,{value:u,onValueChange:$e,children:[s.jsx(re,{className:"w-[80px] h-8",children:s.jsx(le,{})}),s.jsxs(ie,{children:[s.jsx($,{value:"10",children:"10"}),s.jsx($,{value:"25",children:"25"}),s.jsx($,{value:"50",children:"50"}),s.jsx($,{value:"100",children:"100"})]})]})]})]})]})]})});export{bs as D,ls as R};
