import{a as e,t}from"./react-DK-MTtD8.js";import{r as n,s as r}from"./utils-CXcsdpX-.js";import{r as i}from"./api-DcvYaav1.js";import{t as a}from"./list-table-CQD2C7IB.js";import{t as o}from"./funnel-BT4pjjO7.js";import{t as s}from"./plus-CuMPbIyn.js";import{n as c,t as l}from"./filter-sheet-omdfG0qY.js";import{Gr as u,Hr as d,rs as f,za as p}from"./index-Ct5uU080.js";import{t as m}from"./page-content-Bh5Zxb_Y.js";import{t as h}from"./badge-CL1e8lO_.js";import{a as g,i as _,n as v,o as y,t as b}from"./dialog-DuXDR687.js";import{t as x}from"./list-page-header-CePoWpko.js";import{t as S}from"./auth-status-badge-BlJK5R24.js";import{t as C}from"./row-actions-bhl00K6X.js";import{C as w,n as T,p as E}from"./serviceApi-Bn3V4hja.js";var D=e(t(),1),O=r(),k=[{value:`draft`,label:`Draft`},{value:`active`,label:`Active`},{value:`expired`,label:`Expired`}];function A(){let e=f(),[t,r]=(0,D.useState)(``),[A,j]=(0,D.useState)(`all`),[M,N]=(0,D.useState)(`all`),[P,F]=(0,D.useState)(`all`),[I,L]=(0,D.useState)(!1),[R,z]=(0,D.useState)(null),B={};A!==`all`&&(B.status=A),M!==`all`&&(B.client=M),P!==`all`&&(B.branch=P),t&&(B.search=t);let{data:V=[],isLoading:H,refetch:U}=w(B),{data:W=[]}=u(),{data:G=[]}=d(),[K]=E(),[q]=T(),J=async()=>{R&&(await K(R).unwrap().catch(()=>{}),z(null),U())},Y=async(e,t)=>{try{await q({id:e,status:t}).unwrap(),U()}catch{}},X=()=>{j(`all`),N(`all`),F(`all`)},Z=[A,M,P].filter(e=>e!==`all`).length;return(0,O.jsxs)(m,{children:[(0,O.jsx)(x,{title:`Contract Price Books`,description:`Manage customer-specific pricing agreements`,searchPlaceholder:`Search by code, contract name, customer...`,searchValue:t,onSearchChange:r,action:(0,O.jsxs)(p,{onClick:()=>e(`/contract-price-books/new`),children:[(0,O.jsx)(s,{className:`size-4 mr-2`}),` Add Contract`]}),filter:(0,O.jsxs)(p,{variant:`outline`,size:`sm`,onClick:()=>L(!0),className:`relative`,children:[(0,O.jsx)(o,{className:`mr-2 h-4 w-4`}),` Filters`,Z>0&&(0,O.jsx)(h,{variant:`destructive`,className:`ml-2 size-5 p-0 flex items-center justify-center text-[10px]`,children:Z})]})}),(0,O.jsx)(a,{data:V,columns:[{key:`code`,label:`Contract ID`,render:e=>(0,O.jsx)(`span`,{className:`font-mono`,children:e.code})},{key:`client_name`,label:`Customer`,render:e=>e.client_name},{key:`contract_name`,label:`Contract Name`},{key:`start_date`,label:`Start Date`,render:e=>n(e.start_date)},{key:`end_date`,label:`End Date`,render:e=>n(e.end_date)},{key:`status`,label:`Status`,render:e=>(0,O.jsx)(`select`,{value:e.status,onClick:e=>e.stopPropagation(),onChange:t=>{t.stopPropagation(),Y(e.id,t.target.value)},className:`border rounded px-2 py-1 text-xs bg-background cursor-pointer focus:outline-none focus:ring-1 focus:ring-ring`,children:k.map(e=>(0,O.jsx)(`option`,{value:e.value,children:e.label},e.value))})},{key:`version`,label:`Version`,render:e=>`v${e.version}`},{key:`authorized_status`,label:`Approval Status`,render:e=>(0,O.jsx)(S,{status:e.authorized_status})},{key:`actions`,label:`Actions`,sortable:!1,render:t=>(0,O.jsx)(C,{onView:()=>e(`/contract-price-books/${t.id}`),onEdit:()=>e(`/contract-price-books/${t.id}/edit`),onDelete:()=>z(t.id),approval:{entityType:`ContractPriceBook`,objectId:t.id,contentTypeId:i.CONTRACT_PRICE_BOOK,authorizedStatus:t.authorized_status??1,onApprovalChange:()=>U()}})}],isLoading:H,rowKey:e=>e.id,emptyMessage:`No contract price books found`}),(0,O.jsxs)(c,{open:I,onOpenChange:L,onClear:X,children:[(0,O.jsx)(l,{label:`Status`,value:A,onChange:j,placeholder:`All Status`,options:k}),(0,O.jsx)(l,{label:`Customer`,value:M,onChange:N,placeholder:`All Customers`,options:W.map(e=>({value:String(e.id),label:e.name}))}),(0,O.jsx)(l,{label:`Branch`,value:P,onChange:F,placeholder:`All Branches`,options:G.map(e=>({value:String(e.id),label:e.name}))})]}),(0,O.jsx)(b,{open:!!R,onOpenChange:e=>{e||z(null)},children:(0,O.jsxs)(v,{className:`sm:max-w-md`,children:[(0,O.jsx)(g,{children:(0,O.jsx)(y,{children:`Delete Contract Price Book`})}),(0,O.jsx)(`p`,{className:`text-sm text-muted-foreground`,children:`Are you sure you want to delete this contract price book? This action cannot be undone.`}),(0,O.jsxs)(_,{children:[(0,O.jsx)(p,{variant:`outline`,size:`sm`,onClick:()=>z(null),children:`Cancel`}),(0,O.jsx)(p,{variant:`destructive`,size:`sm`,onClick:J,children:`Delete`})]})]})})]})}export{A as default};