import{a as e,t}from"./react-D0JuimcS.js";import{n,o as r}from"./utils-Dac180Gn.js";import{r as i}from"./api-NMlTtVre.js";import{t as a}from"./list-table-M8Qu8ozW.js";import{t as o}from"./funnel-BDsPorMA.js";import{t as s}from"./plus-VCCEyniC.js";import{n as c,t as l}from"./filter-sheet-ChUwrDC4.js";import{Fo as u,Mr as d,ga as f,kr as p}from"./index-8M5AnKdQ.js";import{t as m}from"./page-content-DDtdeMxM.js";import{t as h}from"./badge-Crg76WQE.js";import{a as g,i as _,n as v,o as y,t as b}from"./dialog-CILF_XUh.js";import{t as x}from"./list-page-header-DTmo84Zp.js";import{t as S}from"./auth-status-badge-pVqOsTb5.js";import{t as C}from"./row-actions-CXQ090e_.js";import{C as w,n as T,p as E}from"./serviceApi-BlxU9Cj1.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=u(),[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=[]}=d(),{data:G=[]}=p(),[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)(f,{onClick:()=>e(`/contract-price-books/new`),children:[(0,O.jsx)(s,{className:`size-4 mr-2`}),` Add Contract`]}),filter:(0,O.jsxs)(f,{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)(f,{variant:`outline`,size:`sm`,onClick:()=>z(null),children:`Cancel`}),(0,O.jsx)(f,{variant:`destructive`,size:`sm`,onClick:J,children:`Delete`})]})]})})]})}export{A as default};