| 1 |
- import{i as e}from"./rolldown-runtime-aKtaBQYM.js";import{T as t,a as n,k as r,t as i,w as a}from"./client-Wixdakxn.js";import{n as o}from"./QueryClientProvider-DEK0HFZS.js";import{t as s}from"./useQuery-DkJQ4hAK.js";import{t as ee}from"./useMutation-C6DF_nPh.js";import{t as te}from"./arrow-left-CB7yV5kj.js";import{n as ne}from"./ToastContext-FG4Q6YJB.js";import{t as c}from"./cpu-DTZ4uimA.js";import{t as l}from"./loader-circle-HWxuHTYP.js";import{t as u}from"./map-pin-DJ53b4cG.js";import{t as d}from"./qr-code-sm0XMy71.js";import{t as re}from"./x-vFoBjOZn.js";import{t as f}from"./Button-Bj3LhBE2.js";import{h as p,p as m}from"./amsHelpers-DDsrDUnz.js";import{a as h}from"./colors-BBBvvSKR.js";import{n as ie}from"./useUiSettings-BC4O5ZgX.js";import{i as g,s as ae}from"./index-DvOZfyJ0.js";import{a as oe,i as se,o as ce,r as _,s as v}from"./inventorySearch-D36VsGbN.js";var y=e(r(),1),b=a();function x({tray:e}){let{t:n}=t(),r=p(e);return r===`physical`?(0,b.jsx)(`span`,{className:`text-[10px] font-normal text-bambu-gray`,children:n(`ams.slotEmpty`)}):r===`reset`||!e?(0,b.jsx)(`span`,{className:`text-xs font-normal text-bambu-gray`,children:n(`ams.slotUnconfigured`)}):(0,b.jsxs)(`span`,{className:`flex flex-col items-center gap-0.5 max-w-full`,children:[(0,b.jsxs)(`span`,{className:`flex items-center gap-1 max-w-full`,children:[e.tray_color&&(0,b.jsx)(`span`,{className:`w-2.5 h-2.5 shrink-0 rounded-full border border-bambu-gray/50`,style:h(e.tray_color)}),(0,b.jsx)(`span`,{className:`text-[10px] font-normal text-white truncate`,children:e.tray_type})]}),e.remain>=0&&(0,b.jsxs)(b.Fragment,{children:[` `,(0,b.jsxs)(`span`,{className:`text-[10px] font-normal text-bambu-gray`,children:[e.remain,`%`]})]})]})}var le=[{key:`ams`,Icon:c,labelKey:`inventory.qrAssign.tabAms`},{key:`storage`,Icon:u,labelKey:`inventory.qrAssign.tabStorage`}];function S(e){return e.kind===`ams`?e.printerName?`${e.printerName} · ${e.label}`:e.label:e.locationName}function ue(e,t,n){return e(t>1?n===0?`printers.extL`:`printers.extR`:`printers.external`)}function C({isOpen:e,onClose:r,spoolmanMode:a=!1}){let{t:c}=t(),{showToast:u}=ne(),p=o(),[h,C]=(0,y.useState)(`target`),[w,de]=(0,y.useState)(`ams`),[T,E]=(0,y.useState)(null),[D,O]=(0,y.useState)(null),[k,A]=(0,y.useState)(null),[j,M]=(0,y.useState)(``),[N,P]=(0,y.useState)(!1),[F,I]=(0,y.useState)(null),[L,R]=(0,y.useState)(null),[z,B]=(0,y.useState)(!1),[V,H]=(0,y.useState)(null),U=(0,y.useRef)(!1),W=(0,y.useRef)(0);(0,y.useEffect)(()=>(e&&(C(`target`),O(null),A(null),M(``),P(!1),I(null),R(null),B(!1),H(null),U.current=!1),()=>{W.current+=1}),[e]);let{data:fe}=ie({enabled:e}),{data:G,isLoading:pe}=s({queryKey:[`printers`],queryFn:()=>n.getPrinters(),enabled:e}),{data:K=[],isLoading:q}=s({queryKey:ae,queryFn:n.getLocations,enabled:e&&h===`target`}),J=T??G?.[0]?.id??null,{data:Y,isLoading:me}=s({queryKey:[`printerStatus`,J],queryFn:()=>n.getPrinterStatus(J),enabled:e&&h===`target`&&w===`ams`&&J!==null}),X=(0,y.useMemo)(()=>{if(!Y)return[];let e=[];for(let t of Y.ams??[])for(let n of t.tray??[])e.push({amsId:t.id,trayId:n.id,isHt:t.is_ams_ht,isExternal:!1,label:m(t.id,n.id,t.is_ams_ht,!1),tray:n});let t=Y.vt_tray??[];for(let n of t){let r=(n.id??254)-254;e.push({amsId:255,trayId:r,isHt:!1,isExternal:!0,label:ue(c,t.length,r),tray:n})}return e},[Y,c]),Z=ee({mutationFn:async({spoolId:e,tg:t})=>t.kind===`ams`?a?(await n.assignSpoolmanSlot({spoolman_spool_id:e,printer_id:t.printerId,ams_id:t.amsId,tray_id:t.trayId,move:!0}),{pendingConfig:!1}):{pendingConfig:!!(await n.assignSpool({spool_id:e,printer_id:t.printerId,ams_id:t.amsId,tray_id:t.trayId,move:!0})).pending_config}:(a?await n.updateSpoolmanInventorySpool(e,{location_id:t.locationId}):await n.updateSpool(e,{location_id:t.locationId}),{pendingConfig:!1}),onSuccess:({pendingConfig:e},{tg:t})=>{u(e?c(`inventory.assignPendingInsert`):c(t.kind===`ams`?`inventory.qrAssign.assignSuccess`:`inventory.qrAssign.storageSuccess`,{location:S(t)}),`success`),p.invalidateQueries({queryKey:[`spool-assignments`]}),p.invalidateQueries({queryKey:a?[`spoolman-inventory-spools`]:[`inventory-spools`]}),p.invalidateQueries({queryKey:[`spoolman-slot-assignments`]}),t.kind===`ams`?(n.refreshPrinterStatus(t.printerId).catch(()=>{}),p.invalidateQueries({queryKey:[`printerStatus`,t.printerId]}),a&&p.invalidateQueries({queryKey:[`linked-spools`]})):g(p),r()},onError:(e,{tg:t})=>{U.current=!1,R(`${c(`inventory.qrAssign.assignFailed`,{location:S(t)})}: ${e.message}`)}});if(!e)return null;let he=G?.find(e=>e.id===J)?.name??``,ge=w===`ams`?D!==null&&J!==null:k!==null,Q=Z.isPending||z||V!==null,$=k&&!K.some(e=>e.id===k.id)?[...K,k]:K,_e=async()=>{let e=j.trim();if(!(!e||N)){P(!0);try{let t=await n.createLocation({name:e});g(p),A({id:t.id,name:t.name}),M(``)}catch(e){u(e instanceof Error?e.message:c(`locations.saveFailed`),`error`)}finally{P(!1)}}},ve=()=>{let e;if(w===`ams`){if(!D||J===null)return;e={kind:`ams`,printerId:J,printerName:he,amsId:D.amsId,trayId:D.trayId,isExternal:D.isExternal,label:D.label,filament:{type:D.tray?.tray_type||``,profile:D.tray?.tray_sub_brands||void 0}}}else{if(!k)return;e={kind:`storage`,locationId:k.id,locationName:k.name}}I(e),R(null),C(`scan`)},ye=async(e,t)=>{let r=W.current;B(!0);try{let i=await n.getSpool(e);if(W.current!==r)return;if(i.archived_at){R(c(`inventory.spoolIdArchived`,{id:e})),U.current=!1;return}let a=fe?.disable_filament_warnings?null:v(i,t.filament);if(a){H({spoolId:e,tg:t,details:a});return}Z.mutate({spoolId:e,tg:t})}catch(t){if(W.current!==r)return;R(t instanceof i&&t.status===404?c(`inventory.spoolIdNotFound`,{id:e}):t instanceof Error?t.message:c(`inventory.assignFailed`)),U.current=!1}finally{B(!1)}};return(0,b.jsxs)(b.Fragment,{children:[(0,b.jsxs)(`div`,{className:`fixed inset-0 z-[100] flex items-start sm:items-center justify-center p-4 overflow-y-auto`,children:[(0,b.jsx)(`div`,{className:`absolute inset-0 bg-black/60 backdrop-blur-sm`,onClick:r}),(0,b.jsxs)(`div`,{className:`relative w-full max-w-lg bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-xl shadow-2xl max-h-[90vh] overflow-hidden flex flex-col my-auto`,children:[(0,b.jsxs)(`div`,{className:`flex items-center justify-between p-4 border-b border-bambu-dark-tertiary`,children:[(0,b.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,b.jsx)(d,{className:`w-5 h-5 text-bambu-green`}),(0,b.jsx)(`h2`,{className:`text-lg font-semibold text-white`,children:c(h===`target`?`inventory.qrAssign.title`:`inventory.qrAssign.scanTitle`)})]}),(0,b.jsx)(`button`,{onClick:r,className:`p-1 text-bambu-gray hover:text-white rounded transition-colors`,children:(0,b.jsx)(re,{className:`w-5 h-5`})})]}),(0,b.jsx)(`div`,{className:`p-4 space-y-4 overflow-y-auto`,children:h===`target`?(0,b.jsxs)(b.Fragment,{children:[(0,b.jsx)(`p`,{className:`text-sm text-bambu-gray`,children:c(`inventory.qrAssign.description`)}),(0,b.jsx)(`div`,{className:`flex gap-2`,children:le.map(({key:e,Icon:t,labelKey:n})=>(0,b.jsxs)(`button`,{onClick:()=>de(e),className:`flex-1 flex items-center justify-center gap-2 py-2 rounded-lg text-sm font-medium border transition-colors ${w===e?`bg-bambu-green/20 border-bambu-green text-bambu-green`:`bg-bambu-dark border-bambu-dark-tertiary text-bambu-gray hover:border-bambu-gray`}`,children:[(0,b.jsx)(t,{className:`w-4 h-4`}),c(n)]},e))}),w===`ams`?(0,b.jsxs)(`div`,{className:`space-y-3`,children:[(0,b.jsxs)(`div`,{children:[(0,b.jsx)(`label`,{htmlFor:`qr-printer-select`,className:`block text-xs font-medium text-bambu-gray uppercase tracking-wide mb-1`,children:c(`inventory.qrAssign.printer`)}),(0,b.jsx)(`select`,{id:`qr-printer-select`,value:J??``,onChange:e=>{E(Number(e.target.value)),O(null)},disabled:pe||!G?.length,className:`w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:outline-none focus:border-bambu-green`,children:G?.map(e=>(0,b.jsx)(`option`,{value:e.id,children:e.name},e.id))})]}),(0,b.jsxs)(`div`,{children:[(0,b.jsx)(`span`,{className:`block text-xs font-medium text-bambu-gray uppercase tracking-wide mb-1`,children:c(`inventory.qrAssign.selectSlot`)}),me?(0,b.jsx)(`div`,{className:`flex justify-center py-6`,children:(0,b.jsx)(l,{className:`w-6 h-6 text-bambu-green animate-spin`})}):X.length>0?(0,b.jsx)(`div`,{className:`grid grid-cols-4 gap-2`,children:X.map(e=>(0,b.jsxs)(`button`,{onClick:()=>O(e),className:`aspect-square min-w-0 px-1 flex flex-col items-center justify-center gap-1 rounded-lg border text-sm font-semibold transition-colors ${D?.amsId===e.amsId&&D?.trayId===e.trayId?`bg-bambu-green/20 border-bambu-green text-white`:`bg-bambu-dark border-bambu-dark-tertiary text-bambu-gray hover:border-bambu-gray`}`,children:[(0,b.jsx)(`span`,{children:e.label}),` `,(0,b.jsx)(x,{tray:e.tray})]},`${e.amsId}-${e.trayId}`))}):(0,b.jsx)(`p`,{className:`text-center py-6 text-sm text-bambu-gray`,children:c(`inventory.qrAssign.noSlots`)})]})]}):(0,b.jsxs)(`div`,{children:[(0,b.jsx)(`label`,{htmlFor:`qr-storage-select`,className:`block text-xs font-medium text-bambu-gray uppercase tracking-wide mb-1`,children:c(`inventory.qrAssign.storageLabel`)}),q?(0,b.jsx)(`div`,{className:`flex justify-center py-6`,children:(0,b.jsx)(l,{className:`w-6 h-6 text-bambu-green animate-spin`})}):(0,b.jsxs)(`select`,{id:`qr-storage-select`,value:k?.id??``,onChange:e=>{let t=e.target.value?Number(e.target.value):null,n=t===null?null:$.find(e=>e.id===t);A(n?{id:n.id,name:n.name}:null)},className:`w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:outline-none focus:border-bambu-green`,children:[(0,b.jsx)(`option`,{value:``,children:c(`inventory.qrAssign.storagePick`)}),$.map(e=>(0,b.jsx)(`option`,{value:e.id,children:e.name},e.id))]}),!q&&$.length===0&&(0,b.jsx)(`p`,{className:`mt-2 text-xs text-bambu-gray`,children:c(`locations.empty`)}),(0,b.jsxs)(`div`,{className:`mt-2 flex gap-2`,children:[(0,b.jsx)(`input`,{type:`text`,maxLength:255,value:j,onChange:e=>M(e.target.value),placeholder:c(`locations.createPlaceholder`),className:`flex-1 px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green`}),(0,b.jsx)(`button`,{type:`button`,onClick:_e,disabled:!j.trim()||N,className:`px-3 py-2 text-sm rounded-lg bg-bambu-dark-tertiary text-white hover:bg-bambu-gray-dark disabled:opacity-50`,children:N?(0,b.jsx)(l,{className:`w-4 h-4 animate-spin`}):c(`locations.addShort`)})]})]})]}):(0,b.jsx)(oe,{onDecode:e=>{if(U.current||Q||!F)return;let t=_(e);if(!t){R(c(`inventory.qrAssign.invalidQr`));return}let n=se(t,a);if(n===null){R(c(`inventory.qrAssign.spoolmanLabel`));return}if(R(null),U.current=!0,F.kind===`ams`&&!a){ye(n,F);return}Z.mutate({spoolId:n,tg:F})},busy:Q,error:L,targetLabel:F?S(F):void 0})}),(0,b.jsxs)(`div`,{className:`flex justify-between gap-2 p-4 border-t border-bambu-dark-tertiary`,children:[h===`scan`?(0,b.jsxs)(f,{variant:`secondary`,onClick:()=>C(`target`),disabled:Q,children:[(0,b.jsx)(te,{className:`w-4 h-4`}),c(`inventory.qrAssign.back`)]}):(0,b.jsx)(`span`,{}),(0,b.jsxs)(`div`,{className:`flex gap-2`,children:[(0,b.jsx)(f,{variant:`secondary`,onClick:r,children:c(`common.cancel`)}),h===`target`&&(0,b.jsxs)(f,{onClick:ve,disabled:!ge,children:[(0,b.jsx)(d,{className:`w-4 h-4`}),c(`inventory.qrAssign.startScan`)]})]})]})]})]}),V&&(0,b.jsx)(ce,{mismatch:V.details,location:S(V.tg),onConfirm:()=>{V&&(H(null),Z.mutate({spoolId:V.spoolId,tg:V.tg}))},onCancel:()=>{H(null),U.current=!1}})]})}function w(e){let t=e.cost_per_kg;if(t!=null&&t>0)return{rate:t,fromFilament:!1};let n=e.effective_cost_per_kg;return n==null?{rate:t??null,fromFilament:!1}:{rate:n,fromFilament:!0}}export{C as n,w as t};
|