import{a as e,t}from"./react-DK-MTtD8.js";import{s as n}from"./utils-CXcsdpX-.js";import{t as r}from"./list-table-CQD2C7IB.js";import{t as i}from"./plus-CuMPbIyn.js";import{t as a}from"./save-DstqMto4.js";import{Ra as o,aa as s,za as c}from"./index-Ct5uU080.js";import{t as l}from"./page-content-Bh5Zxb_Y.js";import{t as u}from"./badge-CL1e8lO_.js";import{a as d,i as f,n as p,o as m,t as h}from"./dialog-DuXDR687.js";import{t as g}from"./record-actions-CTlg0qBJ.js";import{t as _}from"./list-page-header-CePoWpko.js";import{t as v}from"./use-server-pagination-CR_EVf1w.js";import{t as y}from"./auth-status-badge-BlJK5R24.js";var b=e(t(),1),x=n(),S={name:``,status:`active`};function C({title:e,label:t,placeholder:n,contentTypeId:C,useGetQuery:w,useCreateMutation:T,useUpdateMutation:E,useDeleteMutation:D}){let[O,k]=(0,b.useState)(``),[A,j]=(0,b.useState)(!1),[M,N]=(0,b.useState)(null),[P,F]=(0,b.useState)(S),[I,L]=(0,b.useState)(!1),R=v(),{data:z,isLoading:B,refetch:V}=w(R.buildParams(O?{search:O}:void 0)),H=R.getItems(z),[U,{isLoading:W}]=T(),[G,{isLoading:K}]=E(),[q]=D(),J=()=>{N(null),F(S),L(!1),j(!0)},Y=e=>{N(e.id),F({name:e.name,status:e.status}),L(!1),j(!0)},X=e=>{N(e.id),F({name:e.name,status:e.status}),L(!0),j(!0)},Z=async e=>{e.preventDefault(),M?await G({id:M,data:P}):await U(P),j(!1),V()},Q=(e,t)=>F(n=>({...n,[e]:t}));return(0,x.jsxs)(l,{children:[(0,x.jsx)(_,{title:e,description:`Manage ${e.toLowerCase()} master data`,searchPlaceholder:`Search ${e.toLowerCase()}...`,searchValue:O,onSearchChange:e=>{k(e),R.setPage(1)},action:(0,x.jsxs)(c,{onClick:J,size:`sm`,children:[(0,x.jsx)(i,{className:`mr-2 h-4 w-4`}),` Add `,t]})}),(0,x.jsx)(r,{data:H,columns:[{key:`code`,label:`Code`,render:e=>(0,x.jsx)(`span`,{className:`font-mono`,children:e.code})},{key:`name`,label:`Name`,render:e=>(0,x.jsx)(`span`,{className:`font-medium`,children:e.name})},{key:`status`,label:`Status`,render:e=>(0,x.jsx)(u,{variant:e.status===`active`?`default`:`secondary`,children:e.status})},{key:`authorized_status`,label:`Auth`,render:e=>(0,x.jsx)(y,{status:e.authorized_status})},{key:`actions`,label:`Actions`,sortable:!1,render:e=>(0,x.jsx)(g,{approval:C?{entityType:t,objectId:e.id,contentTypeId:C,authorizedStatus:e.authorized_status??1,onApprovalChange:()=>V()}:void 0,onView:()=>X(e),onEdit:()=>Y(e),onDelete:()=>q(e.id)})}],isLoading:B,rowKey:e=>e.id,emptyMessage:`No ${e.toLowerCase()} found`,...R.tableProps(z)}),(0,x.jsx)(h,{open:A,onOpenChange:j,children:(0,x.jsxs)(p,{className:`sm:max-w-md`,children:[(0,x.jsx)(d,{children:(0,x.jsx)(m,{children:I?`View ${t}`:M?`Edit ${t}`:`New ${t}`})}),(0,x.jsxs)(`form`,{onSubmit:Z,className:`space-y-4`,children:[(0,x.jsxs)(`div`,{className:`space-y-2`,children:[(0,x.jsxs)(s,{children:[t,` Name `,(0,x.jsx)(`span`,{className:`text-destructive`,children:`*`})]}),(0,x.jsx)(o,{placeholder:n,value:P.name,onChange:e=>Q(`name`,e.target.value),required:!0,disabled:I})]}),(0,x.jsxs)(`div`,{className:`space-y-2`,children:[(0,x.jsx)(s,{children:`Status`}),(0,x.jsxs)(`select`,{value:P.status,onChange:e=>Q(`status`,e.target.value),className:`w-full border rounded-md px-3 py-2 text-sm h-9`,disabled:I,children:[(0,x.jsx)(`option`,{value:`active`,children:`Active`}),(0,x.jsx)(`option`,{value:`inactive`,children:`Inactive`})]})]}),(0,x.jsxs)(f,{children:[(0,x.jsx)(c,{type:`button`,variant:`outline`,size:`sm`,onClick:()=>j(!1),children:`Cancel`}),!I&&(0,x.jsxs)(c,{type:`submit`,size:`sm`,disabled:W||K,children:[(0,x.jsx)(a,{className:`mr-2 h-4 w-4`}),W||K?`Saving...`:`Save`]})]})]})]})})]})}export{C as t};