PageSourceSearch

https://carregamento3bs.lovable.app/assets/CaminhoesPage-CRY2ouMj.js

js carregamento3bs.lovable.app collected 2026-10-03 05:35:28 UTC 12,481 bytes, 1 lines download raw bytes

1import{u as qa,r,j as a}from"./react-vendor-BorBU4Bs.js";import{u as Ta,a as H,b as v}from"./query-vendor-dlntIC_S.js";import{u as Ea,a as ka,b as Fa,s as t,ay as Ia,B as i,A as Ka,P as I,D as ha,i as ua,j as xa,k as ja,L as f,I as w,S as O,d as U,f as W,g as G,h as J,N as Aa,O as Qa,Q as S,U as l,V as Pa,W as d,n as Da,T as Va}from"./index-DkhxOMdR.js";import{S as za}from"./switch-FKqIeruD.js";import{g as X}from"./nextCode-BgBvFXGM.js";import{u as La,T as N}from"./TableColumnFilter-BcDRj-SX.js";import{u as Ba,S as y}from"./SortableTableHead-BQcfj6gO.js";import{A as Ra}from"./AuditHistoryDialog-DmwCV0IY.js";import{c as Ha}from"./cadastroAtivo-CMP99Sfl.js";import{P as Oa}from"./pencil-Cf5Ul2CU.js";import"./radix-vendor-CazrR58L.js";import"./charts-vendor-BxJanGhU.js";import"./supabase-vendor-BxTHC7hl.js";import"./xlsx-vendor-DOnNs5_d.js";import"./qr-vendor-Ca1-Dxvv.js";import"./arrow-up-wTT2lxSL.js";function ce(){const pa=qa(),{isAutoRefreshEnabled:ga}=Ea(),{isMaster:va}=ka(),[fa,M]=r.useState(!1),[u,Y]=r.useState(null),[Z,K]=r.useState(""),[$,A]=r.useState(""),[aa,Q]=r.useState(""),[ea,P]=r.useState(""),{toast:n}=Fa(),m=Ta(),{data:Na=[]}=H({queryKey:["transportadoras"],queryFn:async()=>{const{data:e,error:s}=await t.from("transportadoras").select("*").order("nome");if(s)throw s;return e}}),{data:sa=[]}=H({queryKey:["motoristas"],queryFn:async()=>{const{data:e,error:s}=await t.from("motoristas").select("id, codigo, nome, ativo").order("nome");if(s)throw s;return e}}),{data:oa=[],isLoading:ya}=H({queryKey:["caminhoes"],queryFn:async()=>{const{data:e,error:s}=await t.from("caminhoes").select("*, transportadoras(nome), caminhao_motoristas(motorista_id, motoristas(nome))").order("placa");if(s)throw s;return e},refetchOnWindowFocus:ga("caminhoes")}),ta=r.useMemo(()=>({codigo:e=>e.codigo||"",placa:e=>e.placa||"",transportadora:e=>{var s;return((s=e.transportadoras)==null?void 0:s.nome)||""},caixas:e=>String(e.qtd_caixas),capacidade:e=>String(e.capacidade_carga),motoristas:e=>(e.caminhao_motoristas||[]).map(s=>{var o;return((o=s.motoristas)==null?void 0:o.nome)||""}).join(", ")}),[]),{filters:x,setFilter:j,filtered:Ca}=La(oa,ta),{sort:p,toggleSort:g,sorted:ra}=Ba(Ca,ta),D=v({mutationFn:async()=>{const e={placa:Z,qtd_caixas:parseInt($)||0,capacidade_carga:parseFloat(aa)||0,transportadora_id:ea||null};if(u){const{error:s}=await t.from("caminhoes").update(e).eq("id",u);if(s)throw s}else{let s=V;if(ca){if(!b.trim())throw new Error("Informe o nome do motorista");const{data:k}=await t.from("motoristas").select("codigo"),F=X(k||[],"MT"),{data:_a,error:ma}=await t.from("motoristas").insert({nome:b.trim(),codigo:F}).select("id").single();if(ma)throw ma;s=_a.id}if(!s)throw new Error("Selecione ou cadastre um motorista");e.codigo=X(oa,"CM");const{data:o,error:c}=await t.from("caminhoes").insert(e).select("id").single();if(c)throw c;const{error:h}=await t.from("caminhao_motoristas").insert({caminhao_id:o.id,motorista_id:s});if(h)throw h}},onSuccess:()=>{m.invalidateQueries({queryKey:["caminhoes"]}),m.invalidateQueries({queryKey:["motoristas"]}),M(!1),ia(),n({title:u?"Atualizado!":"Cadastrado!"})},onError:e=>n({title:"Erro",description:e.message,variant:"destructive"})}),wa=v({mutationFn:async e=>{if(await Ha("caminhao",e)>0)throw new Error("Não é possível excluir: caminhão possui movimentações. Desative o cadastro.");await t.from("caminhao_motoristas").delete().eq("caminhao_id",e);const{error:o}=await t.from("caminhoes").delete().eq("id",e);if(o)throw o},onSuccess:()=>{m.invalidateQueries({queryKey:["caminhoes"]}),n({title:"Removido!"})},onError:e=>n({title:"Erro",description:e.message,variant:"destructive"})}),Sa=v({mutationFn:async({id:e,ativo:s})=>{const{error:o}=await t.from("caminhoes").update({ativo:s}).eq("id",e);if(o)throw o},onSuccess:()=>{m.invalidateQueries({queryKey:["caminhoes"]})},onError:e=>n({title:"Erro",description:e.message,variant:"destructive"})}),Ma=v({mutationFn:async({caminhaoId:e,motoristaId:s})=>{const{error:o}=await t.from("caminhao_motoristas").insert({caminhao_id:e,motorista_id:s});if(o)throw o},onSuccess:()=>
1{m.invalidateQueries({queryKey:["caminhoes"]}),n({title:"Motorista vinculado!"})},onError:e=>n({title:"Erro",description:e.message,variant:"destructive"})}),ba=v({mutationFn:async({caminhaoId:e,motoristaId:s})=>{const{error:o}=await t.from("caminhao_motoristas").delete().eq("caminhao_id",e).eq("motorista_id",s);if(o)throw o},onSuccess:()=>{m.invalidateQueries({queryKey:["caminhoes"]}),n({title:"Motorista desvinculado!"})},onError:e=>n({title:"Erro",description:e.message,variant:"destructive"})}),ia=()=>{Y(null),K(""),A(""),Q(""),P(""),na(""),z(!1),L("")},[V,na]=r.useState(""),[ca,z]=r.useState(!1),[b,L]=r.useState(""),[C,_]=r.useState(null),[q,la]=r.useState(""),[da,T]=r.useState(!1),[B,E]=r.useState(""),R=v({mutationFn:async({caminhaoId:e,nome:s})=>{const{data:o}=await t.from("motoristas").select("codigo"),c=X(o||[],"MT"),{data:h,error:k}=await t.from("motoristas").insert({nome:s,codigo:c}).select("id").single();if(k)throw k;const{error:F}=await t.from("caminhao_motoristas").insert({caminhao_id:e,motorista_id:h.id});if(F)throw F},onSuccess:()=>{m.invalidateQueries({queryKey:["caminhoes"]}),m.invalidateQueries({queryKey:["motoristas"]}),_(null),E(""),T(!1),n({title:"Motorista cadastrado e vinculado!"})},onError:e=>n({title:"Erro",description:e.message,variant:"destructive"})});return a.jsxs("div",{className:"animate-fade-in",children:[a.jsxs("div",{className:"flex flex-col items-center text-center gap-2 mb-6",children:[a.jsx("div",{className:"flex h-12 w-12 items-center justify-center rounded-xl bg-primary/20 text-primary",children:a.jsx(Ia,{className:"h-6 w-6"})}),a.jsx("h1",{className:"text-2xl font-bold text-foreground",children:"Caminhões"}),a.jsx("p",{className:"text-sm text-muted-foreground",children:"Gerencie os caminhões e seus motoristas"})]}),a.jsxs("div",{className:"flex items-center justify-between mb-6",children:[a.jsxs(i,{variant:"ghost",size:"sm",className:"-ml-2",onClick:()=>pa("/cadastro"),children:[a.jsx(Ka,{className:"h-4 w-4 mr-1"})," Voltar ao menu"]}),a.jsxs(i,{onClick:()=>{ia(),M(!0)},children:[a.jsx(I,{className:"h-4 w-4 mr-2"})," Novo Caminhão"]})]}),a.jsx(ha,{open:fa,onOpenChange:M,children:a.jsxs(ua,{children:[a.jsx(xa,{children:a.jsxs(ja,{children:[u?"Editar":"Novo"," Caminhão"]})}),a.jsxs("form",{onSubmit:e=>{e.preventDefault(),D.mutate()},className:"space-y-4",children:[a.jsxs("div",{className:"space-y-2",children:[a.jsx(f,{children:"Placa"}),a.jsx(w,{value:Z,onChange:e=>K(e.target.value.toUpperCase()),required:!0,placeholder:"ABC-1234"})]}),a.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[a.jsxs("div",{className:"space-y-2",children:[a.jsx(f,{children:"Qtd. Caixas"}),a.jsx(w,{type:"number",value:$,onChange:e=>A(e.target.value),placeholder:"0"})]}),a.jsxs("div",{className:"space-y-2",children:[a.jsx(f,{children:"Capacidade (kg)"}),a.jsx(w,{type:"number",step:"0.01",value:aa,onChange:e=>Q(e.target.value),placeholder:"0"})]})]}),a.jsxs("div",{className:"space-y-2",children:[a.jsx(f,{children:"Transportadora"}),a.jsxs(O,{value:ea,onValueChange:P,children:[a.jsx(U,{children:a.jsx(W,{placeholder:"Selecione (opcional)"})}),a.jsx(G,{children:Na.map(e=>a.jsx(J,{value:e.id,children:e.nome},e.id))})]})]}),!u&&a.jsxs("div",{className:"space-y-2",children:[a.jsxs(f,{children:["Motorista ",a.jsx("span",{className:"text-destructive",children:"*"})]}),ca?a.jsxs("div",{className:"space-y-2",children:[a.jsx(w,{value:b,onChange:e=>L(e.target.value),placeholder:"Nome do novo motorista"}),a.jsx(i,{type:"button",variant:"ghost",size:"sm",onClick:()=>{z(!1),L("")},children:"← Selecionar existente"})]}):a.jsxs("div",{className:"space-y-2",children:[a.jsxs(O,{value:V,onValueChange:na,children:[a.jsx(U,{children:a.jsx(W,{placeholder:"Selecione o motorista"})}),a.jsx(G,{children:sa.map(e=>a.jsx(J,{value:e.id,children:e.nome},e.id))})]}),a.jsxs(i,{type:"button",variant:"outline",size:"sm",className:"w-full",onClick:()=>z(!0),children:[a.jsx(I,{className:"h-3 w-3 mr-1"})," Cadastrar Novo Motorista"]})]})]}),a.jsx(i,{type:"submit",className:"w-full",disabled:D.isPending||!u&&!V&&!b.trim(),children:D.isPending?"Salvando...":"Salvar"})]})]})}),a.jsx(ha,{open:!!C,onOpenChange:e=>{e||(_(null),T(!1),E(""))}
1,children:a.jsxs(ua,{children:[a.jsx(xa,{children:a.jsx(ja,{children:da?"Novo Motorista":"Vincular Motorista"})}),a.jsx("div",{className:"space-y-4",children:da?a.jsxs(a.Fragment,{children:[a.jsxs("div",{className:"space-y-2",children:[a.jsx(f,{children:"Nome do Motorista"}),a.jsx(w,{value:B,onChange:e=>E(e.target.value),placeholder:"Digite o nome do motorista"})]}),a.jsx(i,{className:"w-full",disabled:!B.trim()||R.isPending,onClick:()=>{C&&R.mutate({caminhaoId:C,nome:B.trim()})},children:R.isPending?"Cadastrando...":"Cadastrar e Vincular"}),a.jsx(i,{variant:"ghost",className:"w-full",onClick:()=>{T(!1),E("")},children:"Voltar"})]}):a.jsxs(a.Fragment,{children:[a.jsxs(O,{value:q,onValueChange:la,children:[a.jsx(U,{children:a.jsx(W,{placeholder:"Selecione o motorista"})}),a.jsx(G,{children:sa.map(e=>a.jsx(J,{value:e.id,children:e.nome},e.id))})]}),a.jsx(i,{className:"w-full",disabled:!q,onClick:()=>{C&&q&&(Ma.mutate({caminhaoId:C,motoristaId:q}),_(null),la(""))},children:"Vincular"}),a.jsxs(i,{variant:"outline",className:"w-full",onClick:()=>T(!0),children:[a.jsx(I,{className:"h-4 w-4 mr-2"})," Cadastrar Novo Motorista"]})]})})]})}),a.jsx("div",{className:"bg-card rounded-lg border",children:a.jsxs(Aa,{children:[a.jsxs(Qa,{children:[a.jsxs(S,{className:"table-header",children:[a.jsx(y,{sortKey:"codigo",currentSort:p,onToggle:g,children:"Código"}),a.jsx(y,{sortKey:"placa",currentSort:p,onToggle:g,children:"Placa"}),a.jsx(y,{sortKey:"transportadora",currentSort:p,onToggle:g,children:"Transportadora"}),a.jsx(y,{sortKey:"caixas",currentSort:p,onToggle:g,children:"Caixas"}),a.jsx(y,{sortKey:"capacidade",currentSort:p,onToggle:g,children:"Capacidade"}),a.jsx(y,{sortKey:"motoristas",currentSort:p,onToggle:g,children:"Motoristas"}),a.jsx(l,{className:"w-20 text-center",children:"Ativo"}),a.jsx(l,{className:"w-24",children:"Ações"})]}),a.jsxs(S,{children:[a.jsx(l,{children:a.jsx(N,{value:x.codigo||"",onChange:e=>j("codigo",e)})}),a.jsx(l,{children:a.jsx(N,{value:x.placa||"",onChange:e=>j("placa",e)})}),a.jsx(l,{children:a.jsx(N,{value:x.transportadora||"",onChange:e=>j("transportadora",e)})}),a.jsx(l,{children:a.jsx(N,{value:x.caixas||"",onChange:e=>j("caixas",e)})}),a.jsx(l,{children:a.jsx(N,{value:x.capacidade||"",onChange:e=>j("capacidade",e)})}),a.jsx(l,{children:a.jsx(N,{value:x.motoristas||"",onChange:e=>j("motoristas",e)})}),a.jsx(l,{}),a.jsx(l,{})]})]}),a.jsx(Pa,{children:ya?a.jsx(S,{children:a.jsx(d,{colSpan:8,className:"text-center text-muted-foreground",children:"Carregando..."})}):ra.length===0?a.jsx(S,{children:a.jsx(d,{colSpan:8,className:"text-center text-muted-foreground",children:"Nenhum caminhão encontrado"})}):ra.map(e=>{var s,o;return a.jsxs(S,{className:e.ativo===!1?"opacity-50":void 0,children:[a.jsx(d,{className:"font-mono text-muted-foreground",children:e.codigo||"—"}),a.jsx(d,{className:"font-mono font-bold",children:e.placa}),a.jsx(d,{children:((s=e.transportadoras)==null?void 0:s.nome)??"—"}),a.jsx(d,{children:e.qtd_caixas}),a.jsxs(d,{children:[Number(e.capacidade_carga).toLocaleString("pt-BR")," kg"]}),a.jsx(d,{children:a.jsxs("div",{className:"flex flex-wrap gap-1",children:[(o=e.caminhao_motoristas)==null?void 0:o.map(c=>{var h;return a.jsxs(Da,{variant:"secondary",className:"cursor-pointer",onClick:()=>ba.mutate({caminhaoId:e.id,motoristaId:c.motorista_id}),children:[(h=c.motoristas)==null?void 0:h.nome," ✕"]},c.motorista_id)}),a.jsxs(i,{variant:"outline",size:"sm",className:"h-6 text-xs",onClick:()=>_(e.id),children:[a.jsx(I,{className:"h-3 w-3 mr-1"})," Motorista"]})]})}),a.jsx(d,{className:"text-center",children:a.jsx(za,{checked:e.ativo!==!1,onCheckedChange:c=>Sa.mutate({id:e.id,ativo:c})})}),a.jsx(d,{children:a.jsxs("div",{className:"flex gap-1",children:[a.jsx(i,{variant:"ghost",size:"icon",onClick:()=>{Y(e.id),K(e.placa),A(String(e.qtd_caixas)),Q(String(e.capacidade_carga)),P(e.transportadora_id??""),M(!0)},children:a.jsx(Oa,{className:"h-4 w-4"})}),a.jsx(i,{variant:"ghost",size:"icon",onClick:()=>wa.mutate(e.id),children:a.jsx(Va,{className:"h-4 w-4 text-destructive"})}),va&&a.jsx(Ra,{tableName:"caminhoes",recordId:e.id})]})})]},e.id)})})]})})]})}export{ce as default};

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.