1import{r as f,j as e,L as T}from"./react-vendor-BTQ2CC_i.js";import{B as p,s as c,u as o}from"./index-6nKlOOAn.js";import{C as v}from"./card-OrUbVgmn.js";import{I as k}from"./input-BsZNoCRD.js";import{L as A}from"./label-Zqk89_mh.js";import{B as M}from"./badge-BEJFaJxh.js";import{u as G,a as O,D as $,d as V,S as F,v as Y,P as H,e as J,f as K,C as Q}from"./dnd-DHu9w9nM.js";import{u as W}from"./useLeadPipelines-FsI_8RoW.js";import{p as X,a9 as B,au as D,ab as q,av as Z}from"./icons-ChNyLqzK.js";import"./query-C_DoptXT.js";import"./ui-vendor-BvkQ8K72.js";import"./supabase-DmwCYaEW.js";import"./motion-jxCd4ad2.js";import"./carousel-D4nW_QRy.js";const U=[{value:"open",label:"Aberto"},{value:"won",label:"Ganho"},{value:"lost",label:"Perdido"}],z=["#94a3b8","#64748b","#3b82f6","#06b6d4","#10b981","#84cc16","#f59e0b","#f97316","#ef4444","#ec4899","#8b5cf6"];function he(){const{pipelines:a,stages:h,loading:S,refetch:n,stagesByPipeline:m}=W(),[u,g]=f.useState(null);f.useEffect(()=>{!u&&a.length&&g((a.find(s=>s.is_default)??a[0]).id)},[a,u]);const t=a.find(s=>s.id===u)??null,C=async()=>{const s=prompt("Nome do novo pipeline:");if(!s)return;const{error:r}=await c.from("lead_pipelines").insert({name:s,sort_order:a.length});if(r)return o.error(r.message);o.success("Pipeline criado"),n()},_=async s=>{const r=prompt("Novo nome:",s.name);if(!r||r===s.name)return;const{error:l}=await c.from("lead_pipelines").update({name:r}).eq("id",s.id);if(l)return o.error(l.message);n()},x=async s=>{const{error:r}=await c.from("lead_pipelines").update({is_default:!0}).eq("id",s.id);if(r)return o.error(r.message);o.success("Pipeline padrão atualizado"),n()},N=async s=>{if(!confirm(`Apagar pipeline "${s.name}" e todos os seus estados? Os leads ficarão sem pipeline.`))return;const{error:r}=await c.from("lead_pipelines").delete().eq("id",s.id);if(r)return o.error(r.message);o.success("Pipeline apagado"),u===s.id&&g(null),n()},j=async()=>{if(!t)return;const s=m(t.id),{error:r}=await c.from("lead_stages").insert({pipeline_id:t.id,name:"Novo estado",color:z[s.length%z.length],sort_order:s.length,type:"open",probability:10});if(r)return o.error(r.message);n()},y=async(s,r)=>{const{error:l}=await c.from("lead_stages").update(r).eq("id",s.id);if(l)return o.error(l.message);n()},b=async s=>{if(!confirm(`Apagar estado "${s.name}"? Leads neste estado ficarão sem estado.`))return;const{error:r}=await c.from("lead_stages").delete().eq("id",s.id);if(r)return o.error(r.message);n()},w=G(O(H,{activationConstraint:{distance:4}})),i=async s=>{if(!t)return;const{active:r,over:l}=s;if(!l||r.id===l.id)return;const P=m(t.id),E=P.findIndex(d=>d.id===r.id),L=P.findIndex(d=>d.id===l.id);if(E<0||L<0)return;const I=J(P,E,L).map((d,R)=>({id:d.id,sort_order:R}));for(const d of I)await c.from("lead_stages").update({sort_order:d.sort_order}).eq("id",d.id);n()};return e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center justify-between mb-6",children:[e.jsxs("div",{children:[e.jsxs(T,{to:"/admin/leads",className:"text-sm text-muted-foreground hover:text-foreground inline-flex items-center gap-1 mb-2",children:[e.jsx(X,{className:"h-3 w-3"})," Voltar a Leads"]}),e.jsx("h1",{className:"text-3xl font-semibold",children:"Pipelines"}),e.jsx("p",{className:"text-muted-foreground",children:"Cria pipelines e estados personalizados para gerir os teus leads."})]}),e.jsxs(p,{onClick:C,children:[e.jsx(B,{className:"h-4 w-4 mr-1"})," Novo pipeline"]})]}),S?e.jsx("div",{className:"text-muted-foreground",children:"A carregarâ¦"}):e.jsxs("div",{className:"grid lg:grid-cols-[280px_1fr] gap-6",children:[e.jsx(v,{className:"p-3 h-fit",children:e.jsxs("div",{className:"space-y-1",children:[a.map(s=>e.jsxs("button",{onClick:()=>g(s.id),className:`w-full text-left px-3 py-2 rounded-md hover:bg-accent text-sm flex items-center justify-between ${u===s.id?"bg-accent":""}`,children:[e.jsx("span",{className:"truncate",children:s.name}),s.is_default&&e.jsx(D,{className:"h-3 w-3 fill-current text-yellow-500"})]},s.id)),a.length===0&&e.jsx("p",{className:"text-sm text-muted-foreground px-3 py-2",children:"Sem pipelines."})]})}),e.jsx("div",{className:"space-y-4",children:t?e.jsxs(e.Fragment,{children:[e.jsx(v,{className:"p-4",children:e.jsxs("div",{className:"flex items-center justify-between gap-3 flex-wrap",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("h2",{className:"text-xl font-semibold",children:t.name}),t.is_default&&e.jsx(M,{variant:"secondary",children:"Padrão"})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(p,{size:"sm",variant:"outline",onClick:()=>_(t),children:"Renomear"}),!t.is_default&&e.jsxs(p,{size:"sm",variant:"outline",onClick:()=>x(t),children:[e.jsx(D,{className:"h-3 w-3 mr-1"})," Marcar como padrão"]}),e.jsxs(p,{size:"sm",variant:"outline",onClick:()=>N(t),className:"text-destructive",children:[e.jsx(q,{className:"h-3 w-3 mr-1"})," Apagar"]})]})]})}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("h3",{className:"font-medium",children:"Estados"}),e.jsxs(p,{size:"sm",onClick:j,children:[e.jsx(B,{className:"h-4 w-4 mr-1"})," Adicionar estado"]})]}),e.jsx($,{sensors:w,collisionDetection:V,onDragEnd:i,children:e.jsx(F,{items:m(t.i
1d).map(s=>s.id),strategy:Y,children:e.jsxs("div",{className:"space-y-2",children:[m(t.id).map(s=>e.jsx(ee,{stage:s,onUpdate:r=>y(s,r),onDelete:()=>b(s)},s.id)),m(t.id).length===0&&e.jsx(v,{className:"p-6 text-center text-muted-foreground text-sm",children:"Sem estados. Adiciona o primeiro."})]})})})]}):e.jsx(v,{className:"p-6 text-muted-foreground",children:"Seleciona um pipeline."})})]})]})}function ee({stage:a,onUpdate:h,onDelete:S}){const{attributes:n,listeners:m,setNodeRef:u,transform:g,transition:t,isDragging:C}=K({id:a.id}),_={transform:Q.Transform.toString(g),transition:t,opacity:C?.5:1},[x,N]=f.useState(a.name),[j,y]=f.useState(a.color),[b,w]=f.useState(a.probability);return f.useEffect(()=>{N(a.name),y(a.color),w(a.probability)},[a.id,a.name,a.color,a.probability]),e.jsx(v,{ref:u,style:_,className:"p-3",children:e.jsxs("div",{className:"flex items-center gap-3 flex-wrap",children:[e.jsx("button",{...n,...m,className:"cursor-grab active:cursor-grabbing text-muted-foreground",children:e.jsx(Z,{className:"h-4 w-4"})}),e.jsx("div",{className:"flex items-center gap-2",children:e.jsx("input",{type:"color",value:j,onChange:i=>y(i.target.value),onBlur:()=>j!==a.color&&h({color:j}),className:"h-8 w-10 rounded border bg-background cursor-pointer","aria-label":"Cor"})}),e.jsx("div",{className:"flex-1 min-w-[160px]",children:e.jsx(k,{value:x,onChange:i=>N(i.target.value),onBlur:()=>x!==a.name&&x.trim()&&h({name:x.trim()})})}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(A,{className:"text-xs text-muted-foreground",children:"Tipo"}),e.jsx("select",{value:a.type,onChange:i=>h({type:i.target.value}),className:"h-9 rounded-md border bg-background px-2 text-sm",children:U.map(i=>e.jsx("option",{value:i.value,children:i.label},i.value))})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(A,{className:"text-xs text-muted-foreground",children:"Prob."}),e.jsx(k,{type:"number",min:0,max:100,value:b,onChange:i=>w(Number(i.target.value)),onBlur:()=>b!==a.probability&&h({probability:Math.max(0,Math.min(100,b))}),className:"w-20"}),e.jsx("span",{className:"text-xs text-muted-foreground",children:"%"})]}),e.jsx(p,{size:"sm",variant:"ghost",onClick:S,className:"text-destructive",children:e.jsx(q,{className:"h-4 w-4"})})]})})}export{he 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.