import{a as e}from"./rolldown-runtime-B0Z9INg1.js";import{$ as t}from"./vendor-charts-iXlq6Rne.js";import{c as n}from"./api-CqQWIBIg.js";import{t as r}from"./DataTable-Dc5x7VxX.js";import{t as i}from"./plus-BV0zuql8.js";import{_ as a}from"./vendor-flow-IzlrcDx6.js";import{a as o,i as s,t as c}from"./index-HkVQGc9M.js";import{t as l}from"./Input-DAV_OJ97.js";import{t as u}from"./Textarea-U2S-asBb.js";import{t as d}from"./ConfirmDialog-BTjlqE-B.js";import{t as f}from"./extractErrorMessage-DBWk03ld.js";import{W as p,kt as m,rn as h,v as g}from"./mastersRtkApi-BlaMikAe.js";import{t as _}from"./StatusBadge-D6eiAqxN.js";import{t as v}from"./useErpSyncEnabled-FXpCMy0J.js";import{a as y,i as b,n as x,r as S,t as C}from"./_shared-BuOngqr2.js";import{t as w}from"./useMasterTable-CIy11KIN.js";import{t as T}from"./ImageUploader-CGsrgHwG.js";import{t as E}from"./erp-DYgUcJAQ.js";var D=e(t(),1),O=a();function k(){let{canEdit:e,canDelete:t}=c(`category`),a=w(),{data:o,isLoading:l,isFetching:u}=m(a.params),h=o?.count??0,[g,v]=(0,D.useState)(void 0),[x,S]=(0,D.useState)(null),[C,{isLoading:T}]=p(),E=async()=>{if(x)try{await C(x.id).unwrap(),n.success(`Category deleted`),S(null)}catch(e){n.error(f(e))}},k=[{accessorKey:`name`,header:`Category`,cell:({row:e})=>(0,O.jsxs)(`div`,{className:`flex items-center gap-2.5`,children:[(0,O.jsx)(b,{url:e.original.image_url}),(0,O.jsxs)(`div`,{children:[(0,O.jsx)(`p`,{className:`font-medium text-[var(--color-text-primary)]`,children:e.original.name}),(0,O.jsx)(`p`,{className:`text-[11px] text-[var(--color-text-muted)] font-mono`,children:e.original.code})]})]})},{accessorKey:`subcategory_count`,header:`Subcategories`},{accessorKey:`product_count`,header:`Products`},{accessorKey:`sort_order`,header:`Sort`},{accessorKey:`is_active`,header:`Status`,cell:({getValue:e})=>{let t=e();return(0,O.jsx)(_,{label:t?`Active`:`Inactive`,variant:t?`success`:`default`})}}];return e&&k.push({id:`actions`,header:``,enableSorting:!1,cell:({row:e})=>(0,O.jsx)(y,{onEdit:()=>v(e.original),onDelete:t?()=>S(e.original):void 0})}),(0,O.jsxs)(O.Fragment,{children:[(0,O.jsx)(r,{columns:k,data:o?.results??[],isLoading:l,isFetching:u,searchable:!0,searchPlaceholder:`Search categories...`,onSearch:a.onSearch,totalItems:h,currentPage:a.page,pageSize:a.pageSize,totalPages:a.totalPages(h),onPageChange:a.onPageChange,onPageSizeChange:a.onPageSizeChange,toolbar:e?(0,O.jsxs)(s,{onClick:()=>v(null),children:[(0,O.jsx)(i,{className:`w-4 h-4`}),`Add Category`]}):void 0,emptyTitle:`No categories yet`,emptyDescription:`Create your first product category to get started.`,emptyActionLabel:e?`Add Category`:void 0,onEmptyAction:e?()=>v(null):void 0,onRowClick:e?e=>v(e):void 0}),g!==void 0&&(0,O.jsx)(A,{category:g,onClose:()=>v(void 0)}),(0,O.jsx)(d,{isOpen:!!x,title:`Delete category`,message:(0,O.jsxs)(O.Fragment,{children:[`This will permanently delete `,(0,O.jsx)(`strong`,{children:x?.name}),`. Categories in use by products cannot be deleted.`]}),isLoading:T,onConfirm:E,onCancel:()=>S(null)})]})}function A({category:e,onClose:t}){let{erpEnabled:r}=v(),i=!!e,[a,c]=(0,D.useState)({name:e?.name??``,erp_code:e?.erp_code??``,description:e?.description??``,image_url:e?.image_url??``,sort_order:e?.sort_order??0,is_active:e?.is_active??!0}),[d,{isLoading:p}]=g(),[m,{isLoading:_}]=h(),y=p||_;return(0,O.jsx)(o,{isOpen:!0,onClose:t,title:i?`Edit Category`:`Add Category`,size:`md`,footer:(0,O.jsxs)(O.Fragment,{children:[(0,O.jsx)(s,{variant:`outline`,onClick:t,disabled:y,children:`Cancel`}),(0,O.jsx)(s,{onClick:async()=>{if(!a.name.trim()){n.error(`Name is required.`);return}try{let o=E(a,r);i?await m({id:e.id,body:o}).unwrap():await d(o).unwrap(),n.success(i?`Category updated`:`Category created`),t()}catch(e){n.error(f(e))}},isLoading:y,children:i?`Save changes`:`Create`})]}),children:(0,O.jsxs)(`div`,{className:`space-y-4`,children:[(0,O.jsxs)(`div`,{className:`grid grid-cols-2 gap-4`,children:[(0,O.jsx)(l,{label:`Name`,value:a.name,onChange:e=>c({...a,name:e.target.value}),placeholder:`e.g. Beverages`}),(0,O.jsx)(x,{code:e?.code})]}),r&&(0,O.jsx)(l,{label:`ERP code`,value:a.erp_code??``,onChange:e=>c({...a,erp_code:e.target.value}),placeholder:`External ERP code`,hint:`For ERP sync mapping.`}),(0,O.jsx)(u,{label:`Description`,value:a.description,onChange:e=>c({...a,description:e.target.value}),placeholder:`Optional description`}),(0,O.jsx)(T,{folder:`categories`,label:`Category image`,value:a.image_url?[{url:a.image_url}]:[],onChange:e=>c({...a,image_url:e[0]?.url??``})}),(0,O.jsxs)(`div`,{className:`grid grid-cols-2 gap-4`,children:[(0,O.jsx)(l,{label:`Sort order`,type:`number`,value:String(a.sort_order),onChange:e=>c({...a,sort_order:Number(e.target.value)||0})}),(0,O.jsx)(`div`,{className:`flex items-end`,children:(0,O.jsx)(C,{checked:!!a.is_active,onChange:e=>c({...a,is_active:e})})})]})]})})}function j(){return(0,O.jsx)(S,{title:`Categories`,description:`Top-level product categories.`,children:(0,O.jsx)(k,{})})}export{j as default};