1import{a as $}from"./rolldown-runtime-DS2seoW7.js";import{y as G}from"./vendor-charts-BLlC71g6.js";import{d as H}from"./vendor-markdown-BnDz3YsI.js";import{b as V,n as E,t as k}from"./vendor-query-BSXLthyd.js";import{t as w}from"./button-BYAM3dpJ.js";import{t as O}from"./createLucideIcon-CTJyoloH.js";import{t as U}from"./AdminLayout-ysxYvXS3.js";import{t as J}from"./MarkdownEditor-jIKM_83j.js";import{t as W}from"./info-BB_duDX1.js";import{t as X}from"./plus-gfEYZoE2.js";import{t as Y}from"./trash-2-DFpJjJ09.js";import{t as p}from"./client-G18cNx3L.js";import{M as g,P as S,T as Z,h as C,n as ee,t as se,w as re,z as ae}from"./index-C3yrWq7m.js";import{a as ne,i as te,n as B,r as ie,t as I}from"./card-CgHkyPTd.js";import{a as L,c as oe,l as N,n as z,r as K}from"./types-CKg1_h9T.js";import{t as ce}from"./badge-TysjFtoF.js";import{a as l,c as le,i as d,l as de,n as u,o as m,s as x,t as ue}from"./form-BFTK4h-d.js";import{a as F,i as P,n as T,r as _,t as A}from"./select-5da7RMI5.js";import{t as Q}from"./checkbox-BJVRrZlb.js";import{t as me}from"./useAdminCheck-CVicxlRQ.js";import{t as he}from"./ImageUpload-By_NbNQO.js";var q=O("Gift",[["rect",{x:"3",y:"8",width:"18",height:"4",rx:"1",key:"bkv52"}],["path",{d:"M12 8v13",key:"1c76mn"}],["path",{d:"M19 12v7a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2v-7",key:"6wjy6b"}],["path",{d:"M7.5 8a2.5 2.5 0 0 1 0-5A4.8 8 0 0 1 12 8a4.8 8 0 0 1 4.5-5 2.5 2.5 0 0 1 0 5",key:"1ihvrl"}]]),D=$(G(),1),e=H(),xe=({parentCourseId:o,parentCourseTitle:i})=>{const j=V(),[f,y]=(0,D.useState)(""),{data:n,isLoading:a}=E({queryKey:["course-bundle",o],queryFn:async()=>{const{data:r,error:c}=await p.from("course_bundle_items").select(` 2 id, 3 included_course_id, 4 created_at, 5 included_course:courses!course_bundle_items_included_course_id_fkey(id, title, slug, price_cents) 6 `).eq("parent_course_id",o).order("created_at",{ascending:!0});if(c)throw c;return r||[]},enabled:!!o}),{data:h}=E({queryKey:["available-bundle-courses",o,n],queryFn:async()=>{const r=[o,...n?.map(R=>R.included_course_id)||[]],{data:c,error:M}=await p.from("courses").select("id, title, price_cents, is_free, is_published").eq("is_free",!1).gt("price_cents",0).eq("is_published",!0).not("id","in",`(${r.join(",")})`).order("title",{ascending:!0});if(M)throw M;return c||[]},enabled:!!o&&n!==void 0}),v=k({mutationFn:async r=>{const{error:c}=await p.from("course_bundle_items").insert({parent_course_id:o,included_course_id:r});if(c)throw c},onSuccess:()=>{j.invalidateQueries({queryKey:["course-bundle",o]}),j.invalidateQueries({queryKey:["available-bundle-courses"]}),g.success("Curso bônus adicionado!"),y("")},onError:r=>{console.error("Error adding bundle item:",r),r.message?.includes("unique")||r.message?.includes("duplicate")?g.error("Este curso já está vinculado"):g.error("Erro ao adicionar curso bônus")}}),b=k({mutationFn:async r=>{const{error:c}=await p.from("course_bundle_items").delete().eq("id",r);if(c)throw c},onSuccess:()=>{j.invalidateQueries({queryKey:["course-bundle",o]}),j.invalidateQueries({queryKey:["available-bundle-courses"]}),g.success("Curso bônus removido!")},onError:r=>{console.error("Error removing bundle item:",r),g.error("Erro ao remover curso bônus")}}),s=()=>{if(!f){g.error("Selecione um curso");return}v.mutate(f)},t=r=>new Intl.NumberFormat("pt-BR",{style:"currency",currency:"BRL"}).format(r/100);return a?(0,e.jsx)(I,{children:(0,e.jsx)(B,{className:"py-8",children:(0,e.jsx)("div",{className:"flex items-center justify-center",children:(0,e.jsx)(S,{className:"h-6 w-6 animate-spin"})})})}):(0,e.jsxs)(I,{className:"border-2 border-dashed border-[#0E0E70]/30",children:[(0,e.jsxs)(te,{className:"pb-3",children:[(0,e.jsxs)("div",{className:"flex items-center gap-2",children:[(0,e.jsx)(q,{className:"h-5 w-5 text-[#0E0E70]"}),(0,e.jsx)(ne,{className:"text-lg text-[#0E0E70]",children:"Cursos Bônus IncluÃdos"})]}),(0,e.jsx)(ie,{children:"Clientes autorizados terão acesso automático a estes cursos pagos"})]}),(0,e.jsxs)(B,{className:"space-y-4",children:[(0,e.jsxs)(se,{children:[(0,e.jsx)(W,{className:"h-4 w-4"}),(0,e.jsxs)(ee,{children:["Apenas cursos ",(0,e.jsx)("strong",{children:"pagos e publicados"})," podem ser vinculados como bônus. Ao autorizar um cliente no curso exclusivo, ele receberá matrÃcula automática nestes curs
6os."]})]}),n&&n.length>0?(0,e.jsx)("div",{className:"space-y-2",children:n.map(r=>(0,e.jsxs)("div",{className:"flex items-center justify-between p-3 bg-muted/50 rounded-lg border",children:[(0,e.jsxs)("div",{className:"flex items-center gap-3",children:[(0,e.jsx)(q,{className:"h-4 w-4 text-green-600"}),(0,e.jsxs)("div",{children:[(0,e.jsx)("span",{className:"font-medium",children:r.included_course?.title||"Curso não encontrado"}),r.included_course?.price_cents&&(0,e.jsx)(ce,{variant:"secondary",className:"ml-2",children:t(r.included_course.price_cents)})]})]}),(0,e.jsx)(w,{variant:"ghost",size:"sm",onClick:()=>b.mutate(r.id),disabled:b.isPending,children:b.isPending?(0,e.jsx)(S,{className:"h-4 w-4 animate-spin"}):(0,e.jsx)(Y,{className:"h-4 w-4 text-destructive"})})]},r.id))}):(0,e.jsxs)("div",{className:"text-center py-6 text-muted-foreground",children:[(0,e.jsx)(q,{className:"h-8 w-8 mx-auto mb-2 opacity-50"}),(0,e.jsx)("p",{children:"Nenhum curso bônus vinculado"})]}),h&&h.length>0&&(0,e.jsxs)("div",{className:"flex gap-2 pt-2 border-t",children:[(0,e.jsxs)(A,{value:f,onValueChange:y,children:[(0,e.jsx)(P,{className:"flex-1",children:(0,e.jsx)(F,{placeholder:"Selecione um curso para adicionar..."})}),(0,e.jsx)(T,{children:h.map(r=>(0,e.jsxs)(_,{value:r.id,children:[r.title," (",t(r.price_cents||0),")"]},r.id))})]}),(0,e.jsx)(w,{onClick:s,disabled:!f||v.isPending,children:v.isPending?(0,e.jsx)(S,{className:"h-4 w-4 animate-spin"}):(0,e.jsx)(X,{className:"h-4 w-4"})})]}),h&&h.length===0&&(0,e.jsx)("p",{className:"text-sm text-muted-foreground text-center py-2",children:"Não há mais cursos pagos disponÃveis para vincular"})]})]})},pe=oe({title:N().min(1,"TÃtulo é obrigatório"),slug:N().min(1,"Slug é obrigatório"),description:N().optional(),level:L(["beginner","intermediate","advanced"]),duration_hours:K.number().min(0).optional(),thumbnail_url:N().optional(),price_cents:K.number().min(0).default(0),is_free:z().default(!1),is_published:z().default(!1),access_type:L(["public","private"]).default("public")});function Le(){const{isLoading:o}=me(),{id:i}=Z(),j=re(),{toast:f}=ae(),y=V(),{data:n}=E({queryKey:["course",i],queryFn:async()=>{if(!i)return null;const{data:s,error:t}=await p.from("courses").select("*").eq("id",i).single();if(t)throw t;return s},enabled:!!i}),a=de({resolver:le(pe),defaultValues:{title:"",slug:"",description:"",level:"beginner",duration_hours:0,thumbnail_url:"",price_cents:0,is_free:!1,is_published:!1,access_type:"public"}});(0,D.useEffect)(()=>{n&&a.reset({title:n.title,slug:n.slug,description:n.description||"",level:n.level,duration_hours:n.duration_hours||0,thumbnail_url:n.thumbnail_url||"",price_cents:n.price_cents||0,is_free:n.is_free||!1,is_published:n.is_published||!1,access_type:n.access_type||"public"})},[n,a]);const h=k({mutationFn:async s=>{const{data:{session:t}}=await p.auth.getSession();if(!t)throw new Error("No session");if(i){const{error:r}=await p.from("courses").update({...s,access_type:s.access_type}).eq("id",i);if(r)throw r}else{const{error:r}=await p.from("courses").insert([{title:s.title,slug:s.slug,description:s.description,level:s.level,duration_hours:s.duration_hours,thumbnail_url:s.thumbnail_url,price_cents:s.is_free?0:s.price_cents,is_free:s.is_free,is_published:s.is_published,access_type:s.access_type,instructor_id:t.user.id}]);if(r)throw r}},onSuccess:()=>{y.invalidateQueries({queryKey:["admin-courses"]}),f({title:i?"Curso atualizado":"Curso criado",description:i?"O curso foi atualizado com sucesso.":"O curso foi criado com sucesso."}),j("/admin/cursos")},onError:()=>{f({title:"Erro ao salvar",description:"Não foi possÃvel salvar o curso.",variant:"destructive"})}}),v=s=>s.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g,"").replace(/[^\w\s-]/g,"").replace(/\s+/g,"-").replace(/-+/g,"-").trim(),b=s=>{h.mutate(s)};return o?(0,e.jsx)("div",{className:"flex min-h-screen items-center justify-center",children:(0,e.jsx)("div",{className:"text-lg",children:"Carregando..."})}):(0,e.jsx)(U,{children:(0,e.jsxs)("div",{className:"mx-auto max-w-3xl space-y-6",children:[(0,e.jsxs)("div",{children:[(0,e.jsx)("h1",{className:"text-3xl font-bold",children:i?"Editar Curso":"Novo Curso"}),(0,e.jsx)("p",{className:"text-muted-foreground",children:i?"Atualize as informações do curs
6o":"Preencha os dados para criar um novo curso"})]}),(0,e.jsx)(ue,{...a,children:(0,e.jsxs)("form",{onSubmit:a.handleSubmit(b),className:"space-y-6",children:[(0,e.jsx)(d,{control:a.control,name:"title",render:({field:s})=>(0,e.jsxs)(l,{children:[(0,e.jsx)(m,{children:"TÃtulo"}),(0,e.jsx)(u,{children:(0,e.jsx)(C,{...s,onChange:t=>{s.onChange(t),i||a.setValue("slug",v(t.target.value))}})}),(0,e.jsx)(x,{})]})}),(0,e.jsx)(d,{control:a.control,name:"slug",render:({field:s})=>(0,e.jsxs)(l,{children:[(0,e.jsx)(m,{children:"Slug (URL)"}),(0,e.jsx)(u,{children:(0,e.jsx)(C,{...s})}),(0,e.jsx)(x,{})]})}),(0,e.jsx)(d,{control:a.control,name:"description",render:({field:s})=>(0,e.jsxs)(l,{children:[(0,e.jsx)(m,{children:"Descrição"}),(0,e.jsx)(u,{children:(0,e.jsx)(J,{value:s.value||"",onChange:s.onChange,placeholder:"Descreva o curso usando Markdown...",minHeight:"200px"})}),(0,e.jsx)(x,{})]})}),(0,e.jsxs)("div",{className:"grid gap-6 md:grid-cols-2",children:[(0,e.jsx)(d,{control:a.control,name:"level",render:({field:s})=>(0,e.jsxs)(l,{children:[(0,e.jsx)(m,{children:"NÃvel"}),(0,e.jsxs)(A,{onValueChange:s.onChange,defaultValue:s.value,children:[(0,e.jsx)(u,{children:(0,e.jsx)(P,{children:(0,e.jsx)(F,{placeholder:"Selecione o nÃvel"})})}),(0,e.jsxs)(T,{children:[(0,e.jsx)(_,{value:"beginner",children:"Iniciante"}),(0,e.jsx)(_,{value:"intermediate",children:"Intermediário"}),(0,e.jsx)(_,{value:"advanced",children:"Avançado"})]})]}),(0,e.jsx)(x,{})]})}),(0,e.jsx)(d,{control:a.control,name:"duration_hours",render:({field:s})=>(0,e.jsxs)(l,{children:[(0,e.jsx)(m,{children:"Duração (horas)"}),(0,e.jsx)(u,{children:(0,e.jsx)(C,{type:"number",...s,readOnly:!0,className:"bg-muted cursor-not-allowed"})}),(0,e.jsx)("p",{className:"text-xs text-muted-foreground",children:"Calculado automaticamente baseado na soma das aulas"}),(0,e.jsx)(x,{})]})})]}),(0,e.jsx)(d,{control:a.control,name:"thumbnail_url",render:({field:s})=>(0,e.jsxs)(l,{children:[(0,e.jsx)(m,{children:"Imagem de Capa"}),(0,e.jsx)(u,{children:(0,e.jsx)(he,{value:s.value,onChange:s.onChange})}),(0,e.jsx)(x,{})]})}),(0,e.jsxs)("div",{className:"space-y-4",children:[(0,e.jsx)(d,{control:a.control,name:"access_type",render:({field:s})=>(0,e.jsxs)(l,{children:[(0,e.jsx)(m,{children:"Tipo de Acesso"}),(0,e.jsxs)(A,{onValueChange:s.onChange,value:s.value,children:[(0,e.jsx)(u,{children:(0,e.jsx)(P,{children:(0,e.jsx)(F,{placeholder:"Selecione o tipo de acesso"})})}),(0,e.jsxs)(T,{children:[(0,e.jsx)(_,{value:"public",children:"Público - Qualquer um pode se matricular"}),(0,e.jsx)(_,{value:"private",children:"Exclusivo para Clientes - Apenas matrÃcula manual"})]})]}),(0,e.jsx)(x,{})]})}),(0,e.jsx)(d,{control:a.control,name:"is_free",render:({field:s})=>(0,e.jsxs)(l,{className:"flex flex-row items-start space-x-3 space-y-0",children:[(0,e.jsx)(u,{children:(0,e.jsx)(Q,{checked:s.value,onCheckedChange:t=>{s.onChange(t),t&&a.setValue("price_cents",0)},disabled:a.watch("access_type")==="private"})}),(0,e.jsxs)("div",{className:"space-y-1 leading-none",children:[(0,e.jsx)(m,{children:"Curso Gratuito"}),a.watch("access_type")==="private"&&(0,e.jsx)("p",{className:"text-xs text-muted-foreground",children:"Cursos exclusivos não podem ser marcados como gratuitos"})]})]})}),!a.watch("is_free")&&a.watch("access_type")!=="private"&&(0,e.jsx)(d,{control:a.control,name:"price_cents",render:({field:s})=>(0,e.jsxs)(l,{children:[(0,e.jsx)(m,{children:"Preço (em centavos)"}),(0,e.jsx)(u,{children:(0,e.jsxs)("div",{className:"relative",children:[(0,e.jsx)("span",{className:"absolute left-3 top-1/2 -translate-y-1/2 text-muted-foreground",children:"R$"}),(0,e.jsx)(C,{type:"number",className:"pl-10",placeholder:"19900 = R$ 199,00",...s,onChange:t=>s.onChange(parseInt(t.target.value)||0)})]})}),(0,e.jsx)("p",{className:"text-xs text-muted-foreground",children:"Digite o valor em centavos. Ex: 19900 = R$ 199,00. Parcelamento em até 12x disponÃvel no checkout."}),(0,e.jsx)(x,{})]})}),(0,e.jsx)(d,{control:a.control,name:"is_published",render:({field:s})=>(0,e.jsxs)(l,{className:"flex flex-row items-start space-x-3 space-y-0",children:[(0,e.jsx)(u,{children:(0,e.jsx)(Q,{checked:s.value,onCheckedChange:s.onChange})}),(0,e.jsx)("div",{className:"space-y-1 leading-none",children:(0,e.jsx)(m,{children:"Publicar Curso"})})]})})]}),i&&a.watch("access_type")==="private"&&(0,e.jsx)(xe,{parentCourseId:i,parentCourseTitle:a.watch("title")}),(0,e.jsxs)("div",{className:"flex gap-4",children:[(0,e.jsx)(w,{type:"submit",disabled:h.isPending,children:h.isPending?"Salvando...":"Salvar Curso"}),(0,e.jsx)(w,{type:"button",variant:"outline",onClick:()=>j("/admin/cursos"),children:"Cancelar"})]})]})})]})})}export{Le 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.