import{r as R,j as o,A as Q,C as G,t as U,F as O,v as X,c2 as V}from"./vendor-BKmmeuxg.js";import{B as Y}from"./button-BpNwv2aE.js";import{C as q}from"./card-D9_SJYq6.js";import{L as D}from"./label-HmIcv5Yx.js";import{R as B,a as _}from"./radio-group-C0C2vfl-.js";import{E as M}from"./jspdf.es.min-BFMcGtlR.js";import{t as P}from"./index-D9-QKOod.js";const se=({template:h,courseName:p,studentName:g,completionDate:j})=>{const[c,N]=R.useState("png"),S=R.useRef(null),f={width:900,height:600},{template_data:t}=h,l=async()=>{S.current&&(c==="pdf"?await L():await k())},$=e=>new Promise((a,i)=>{const r=new Image;r.crossOrigin="anonymous",r.onload=()=>a(r),r.onerror=i,r.src=e}),k=async()=>{const e=document.createElement("canvas"),a=e.getContext("2d");if(!a)return;e.width=f.width,e.height=f.height;let i=null;if(h.logo_path)try{i=await $(h.logo_path)}catch(r){console.error("Failed to load logo:",r)}await v(a,f,i),e.toBlob(r=>{if(!r)return;const n=URL.createObjectURL(r),d=document.createElement("a");d.href=n,d.download=`${g}_${p}_Certificate.png`,document.body.appendChild(d),d.click(),document.body.removeChild(d),URL.revokeObjectURL(n),P.success("Certificate saved as PNG!")},"image/png")},L=async()=>{const e=f.width>f.height,a=new M({orientation:e?"landscape":"portrait",unit:"px",format:[f.width,f.height]}),i=document.createElement("canvas"),r=i.getContext("2d");if(!r)return;i.width=f.width,i.height=f.height;let n=null;if(h.logo_path)try{n=await $(h.logo_path)}catch(s){console.error("Failed to load logo:",s)}await v(r,f,n);const d=i.toDataURL("image/png");a.addImage(d,"PNG",0,0,f.width,f.height),a.save(`${g}_${p}_Certificate.pdf`),P.success("Certificate saved as PDF!")},A=(e,a,i,r,n,d)=>{const s=a.split(" ");let y="",x="";const b=[];for(let m=0;mn&&m>0?(b.push({text:y.trim(),width:e.measureText(y).width}),x=`${s[m]} `,y=`${s[m]} `):y=x;b.push({text:y.trim(),width:e.measureText(y).width});let C=r;return b.forEach(m=>{e.fillText(m.text,i,C),C+=d}),C},v=async(e,a,i=null)=>{e.fillStyle=t.backgroundColor,e.fillRect(0,0,a.width,a.height),e.strokeStyle=t.borderColor,e.lineWidth=8,e.strokeRect(20,20,a.width-40,a.height-40),e.strokeStyle=t.primaryColor,e.lineWidth=2,e.strokeRect(40,40,a.width-80,a.height-80),e.textAlign="center";let r=100;if(i){const s=(a.width-80)/2,y=60;e.drawImage(i,s,y,80,80),r=y+80+30}e.font=`bold 42px ${t.fontFamily}`,e.fillStyle=t.primaryColor,e.fillText(t.titleText,a.width/2,r),r+=20,e.strokeStyle=t.borderColor,e.lineWidth=3,e.beginPath(),e.moveTo(a.width/2-150,r),e.lineTo(a.width/2+150,r),e.stroke(),r+=50,e.font=`22px ${t.fontFamily}`,e.fillStyle=t.secondaryColor,r=A(e,t.descriptionText,a.width/2,r,a.width-100,30),r+=50,e.font=`bold 36px ${t.fontFamily}`,e.fillStyle=t.primaryColor,e.fillText(g,a.width/2,r);const n=e.measureText(g).width;e.strokeStyle=t.borderColor,e.lineWidth=3,e.beginPath(),e.moveTo((a.width-n)/2-20,r+10),e.lineTo((a.width+n)/2+20,r+10),e.stroke(),r+=60,e.font=`22px ${t.fontFamily}`,e.fillStyle=t.secondaryColor,e.fillText(t.completionText,a.width/2,r),r+=50,e.font=`bold 28px ${t.fontFamily}`,e.fillStyle=t.primaryColor,e.fillText(p,a.width/2,r),r+=60,e.font=`18px ${t.fontFamily}`,e.fillStyle=t.secondaryColor,e.fillText(`Completed on: ${j}`,a.width/2,r),r+=60,e.font=`16px ${t.fontFamily}`,e.fillStyle=t.secondaryColor,e.fillText(t.footerText,a.width/2,a.height-60)};return o.jsxs(q,{className:"mx-auto max-w-[800px] space-y-7 p-6",children:[o.jsxs("div",{ref:S,className:"relative flex flex-col justify-center rounded-lg border-4 p-8 text-center",style:{backgroundColor:t.backgroundColor,borderColor:t.borderColor,fontFamily:t.fontFamily},children:[o.jsx("div",{className:"absolute inset-4 rounded border-2",style:{borderColor:t.primaryColor}}),o.jsxs("div",{className:"relative z-10",children:[h.logo_path?o.jsx("div",{className:"mx-auto mb-6 flex h-16 w-16 items-center justify-center",children:o.jsx("img",{src:h.logo_path,alt:"Logo",className:"h-full w-full object-contain"})}):o.jsx(Q,{className:"mx-auto mb-3 h-12 w-12",style:{color:t.borderColor}}),o.jsxs("div",{className:"mb-6",children:[o.jsx("h2",{className:"mb-2 font-serif text-2xl font-bold",style:{color:t.primaryColor},children:t.titleText}),o.jsx("div",{className:"mx-auto h-0.5 w-32",style:{backgroundColor:t.borderColor}})]}),o.jsxs("div",{className:"space-y-4",children:[o.jsx("p",{className:"font-serif text-lg",style:{color:t.secondaryColor},children:t.descriptionText}),o.jsxs("div",{className:"relative",children:[o.jsx("p",{className:"mx-8 pb-2 font-serif text-2xl font-bold",style:{color:t.primaryColor},children:g}),o.jsx("div",{className:"absolute bottom-0 left-1/2 h-0.5 w-48 -translate-x-1/2 transform",style:{backgroundColor:t.borderColor}})]}),o.jsx("p",{className:"font-serif text-lg",style:{color:t.secondaryColor},children:t.completionText}),o.jsx("p",{className:"font-serif text-xl font-semibold",style:{color:t.primaryColor},children:p}),o.jsxs("div",{className:"mt-6 flex items-center justify-center gap-2",children:[o.jsx(G,{className:"h-4 w-4",style:{color:t.secondaryColor}}),o.jsxs("p",{className:"text-muted-foreground font-serif text-sm",style:{color:t.secondaryColor},children:["Completed on: ",j]})]})]}),o.jsx("div",{className:"mt-6 border-t pt-4",style:{borderColor:t.borderColor},children:o.jsx("p",{className:"font-serif text-sm",style:{color:t.secondaryColor},children:t.footerText})})]})]}),o.jsxs("div",{className:"space-y-4",children:[o.jsxs(B,{value:c,onValueChange:N,className:"flex justify-center space-x-6",children:[o.jsxs("div",{className:"flex items-center space-x-2",children:[o.jsx(_,{className:"cursor-pointer",value:"png",id:"png"}),o.jsxs(D,{htmlFor:"png",className:"flex cursor-pointer items-center gap-2",children:[o.jsx(U,{className:"h-4 w-4"}),"PNG Image"]})]}),o.jsxs("div",{className:"flex items-center space-x-2",children:[o.jsx(_,{className:"cursor-pointer",value:"pdf",id:"pdf"}),o.jsxs(D,{htmlFor:"pdf",className:"flex cursor-pointer items-center gap-2",children:[o.jsx(O,{className:"h-4 w-4"}),"PDF Document"]})]})]}),o.jsxs(Y,{variant:"outline",className:"w-full",onClick:l,children:[o.jsx(X,{className:"mr-2 h-4 w-4"}),"Download as ",c.toUpperCase()]})]})]})},ae=({template:h,courseName:p,studentName:g,completionDate:j,studentMarks:c})=>{const[N,S]=R.useState("png"),f=R.useRef(null),t={width:700,height:900},{template_data:l}=h,$=async()=>{f.current&&(N==="pdf"?await A():await L())},k=e=>new Promise((a,i)=>{const r=new Image;r.crossOrigin="anonymous",r.onload=()=>a(r),r.onerror=i,r.src=e}),L=async()=>{const e=document.createElement("canvas"),a=e.getContext("2d");if(!a)return;e.width=t.width,e.height=t.height;let i=null;if(h.logo_path)try{i=await k(h.logo_path)}catch(r){console.error("Failed to load logo:",r)}await v(a,t,i),e.toBlob(r=>{if(!r)return;const n=URL.createObjectURL(r),d=document.createElement("a");d.href=n,d.download=`${g}_${p}_Marksheet.png`,document.body.appendChild(d),d.click(),document.body.removeChild(d),URL.revokeObjectURL(n),P.success("Marksheet saved as PNG!")},"image/png")},A=async()=>{const e=t.width>t.height,a=new M({orientation:e?"landscape":"portrait",unit:"px",format:[t.width,t.height]}),i=document.createElement("canvas"),r=i.getContext("2d");if(!r)return;i.width=t.width,i.height=t.height;let n=null;if(h.logo_path)try{n=await k(h.logo_path)}catch(s){console.error("Failed to load logo:",s)}await v(r,t,n);const d=i.toDataURL("image/png");a.addImage(d,"PNG",0,0,t.width,t.height),a.save(`${g}_${p}_Marksheet.pdf`),P.success("Marksheet saved as PDF!")},v=async(e,a,i=null)=>{e.fillStyle=l.backgroundColor,e.fillRect(0,0,a.width,a.height),e.strokeStyle=l.borderColor,e.lineWidth=6,e.strokeRect(15,15,a.width-30,a.height-30);const r=80,n=a.width-80,d=a.width/2;let s=60;const y=60,x=r,b=r+y+20;i&&e.drawImage(i,x,s,y,y),e.textAlign="left",e.font=`bold 28px ${l.fontFamily}`,e.fillStyle=l.primaryColor,e.fillText(l.headerText,b,s+25),e.font=`18px ${l.fontFamily}`,e.fillStyle=l.secondaryColor,e.fillText(l.institutionName,b,s+50),s+=y+30,e.strokeStyle=l.borderColor,e.lineWidth=2,e.beginPath(),e.moveTo(r,s),e.lineTo(n,s),e.stroke(),s+=40;const C=r,m=d+20,u=25;e.font=`16px ${l.fontFamily}`,e.fillStyle=l.secondaryColor,e.fillText("Student Name",C,s),e.font=`bold 20px ${l.fontFamily}`,e.fillStyle=l.primaryColor,e.fillText(g,C,s+u),e.font=`16px ${l.fontFamily}`,e.fillStyle=l.secondaryColor,e.fillText("Course",m,s),e.font=`bold 20px ${l.fontFamily}`,e.fillStyle=l.primaryColor;const W=n-m-10;if(e.measureText(p).width>W){const E=p.split(" ");let T="",I=s+u;for(let F=0;FW&&F>0?(e.fillText(T,m,I),T=E[F]+" ",I+=25):T=z}e.fillText(T,m,I)}else e.fillText(p,m,s+u);s+=92,e.font=`16px ${l.fontFamily}`,e.fillStyle=l.secondaryColor,e.fillText("Completion Date",C,s),e.font=`18px ${l.fontFamily}`,e.fillStyle=l.primaryColor,e.fillText(j,C,s+u),e.font=`16px ${l.fontFamily}`,e.fillStyle=l.secondaryColor,e.fillText("Overall Grade",m,s),e.font=`bold 20px ${l.fontFamily}`,e.fillStyle=l.primaryColor,e.fillText(`${c.overall.grade} (${c.overall.percentage}%)`,m,s+u),s+=80,e.textAlign="left",e.font=`bold 22px ${l.fontFamily}`,e.fillStyle=l.primaryColor,e.fillText("Exam Type",r,s),s+=35;const w=n-r;e.fillStyle=`${l.primaryColor}30`,e.fillRect(r,s,w,45),e.font=`bold 18px ${l.fontFamily}`,e.fillStyle=l.primaryColor,e.fillText("Exam Type",r+15,s+28),e.textAlign="right",e.fillText("Total Marks",n-15,s+28),e.strokeStyle=l.borderColor,e.lineWidth=2,e.strokeRect(r,s,w,45),s+=45,e.fillStyle=l.backgroundColor,e.fillRect(r,s,w,45),e.textAlign="left",e.font=`18px ${l.fontFamily}`,e.fillStyle=l.secondaryColor,e.fillText("Assignment",r+15,s+28),e.textAlign="right",e.font=`bold 18px ${l.fontFamily}`,e.fillStyle=l.primaryColor,e.fillText(`${c.assignment.obtained}/${c.assignment.total}`,n-15,s+28),e.strokeStyle=l.borderColor,e.lineWidth=1,e.strokeRect(r,s,w,45),s+=45,e.fillStyle=l.backgroundColor,e.fillRect(r,s,w,45),e.textAlign="left",e.font=`18px ${l.fontFamily}`,e.fillStyle=l.secondaryColor,e.fillText("Quiz",r+15,s+28),e.textAlign="right",e.font=`bold 18px ${l.fontFamily}`,e.fillStyle=l.primaryColor,e.fillText(`${c.quiz.obtained}/${c.quiz.total}`,n-15,s+28),e.strokeStyle=l.borderColor,e.lineWidth=1,e.strokeRect(r,s,w,45),s+=70,e.strokeStyle=l.borderColor,e.lineWidth=2,e.beginPath(),e.moveTo(r,s),e.lineTo(n,s),e.stroke(),s+=30,e.textAlign="center",e.font=`16px ${l.fontFamily}`,e.fillStyle=l.secondaryColor,e.fillText(l.footerText,a.width/2,s)};return o.jsxs(q,{className:"mx-auto max-w-[800px] space-y-7 p-6",children:[o.jsx("div",{ref:f,className:"relative rounded-lg border-4 p-8",style:{backgroundColor:l.backgroundColor,borderColor:l.borderColor,fontFamily:l.fontFamily},children:o.jsxs("div",{className:"space-y-6",children:[o.jsx("div",{className:"mb-6 border-b-2 pb-4",style:{borderColor:l.borderColor},children:o.jsx("div",{className:"flex items-center justify-between",children:o.jsxs("div",{className:"flex items-center gap-4",children:[h.logo_path?o.jsx("div",{className:"h-16 w-16",children:o.jsx("img",{src:h.logo_path,alt:"Logo",className:"h-full w-full object-contain"})}):o.jsx(V,{className:"h-12 w-12",style:{color:l.primaryColor}}),o.jsxs("div",{children:[o.jsx("h2",{className:"text-2xl font-bold",style:{color:l.primaryColor,fontFamily:l.fontFamily},children:l.headerText}),o.jsx("p",{className:"text-lg",style:{color:l.secondaryColor,fontFamily:l.fontFamily},children:l.institutionName})]})]})})}),o.jsxs("div",{className:"mb-6 grid grid-cols-2 gap-4",children:[o.jsxs("div",{children:[o.jsx("p",{className:"text-sm",style:{color:l.secondaryColor},children:"Student Name"}),o.jsx("p",{className:"text-lg font-semibold",style:{color:l.primaryColor},children:g})]}),o.jsxs("div",{children:[o.jsx("p",{className:"text-sm",style:{color:l.secondaryColor},children:"Course"}),o.jsx("p",{className:"text-lg font-semibold",style:{color:l.primaryColor},children:p})]}),o.jsxs("div",{children:[o.jsx("p",{className:"text-sm",style:{color:l.secondaryColor},children:"Completion Date"}),o.jsxs("div",{className:"flex items-center gap-2",children:[o.jsx(G,{className:"h-4 w-4",style:{color:l.secondaryColor}}),o.jsx("p",{className:"font-medium",style:{color:l.primaryColor},children:j})]})]}),o.jsxs("div",{children:[o.jsx("p",{className:"text-sm",style:{color:l.secondaryColor},children:"Overall Grade"}),o.jsxs("p",{className:"text-2xl font-bold",style:{color:l.primaryColor},children:[c.overall.grade," (",c.overall.percentage,"%)"]})]})]}),o.jsxs("div",{children:[o.jsx("h3",{className:"mb-3 text-lg font-semibold",style:{color:l.primaryColor},children:"Exam Type"}),o.jsx("div",{className:"overflow-hidden rounded-lg border",style:{borderColor:l.borderColor},children:o.jsxs("table",{className:"w-full",children:[o.jsx("thead",{children:o.jsxs("tr",{style:{backgroundColor:`${l.primaryColor}20`},children:[o.jsx("th",{className:"border-b p-3 text-left font-semibold",style:{color:l.primaryColor,borderColor:l.borderColor},children:"Exam Type"}),o.jsx("th",{className:"border-b p-3 text-right font-semibold",style:{color:l.primaryColor,borderColor:l.borderColor},children:"Total Marks"})]})}),o.jsxs("tbody",{children:[o.jsxs("tr",{className:"border-b",style:{borderColor:l.borderColor},children:[o.jsx("td",{className:"p-3",style:{color:l.secondaryColor},children:"Assignment"}),o.jsxs("td",{className:"p-3 text-right font-medium",style:{color:l.primaryColor},children:[c.assignment.obtained,"/",c.assignment.total]})]}),o.jsxs("tr",{children:[o.jsx("td",{className:"p-3",style:{color:l.secondaryColor},children:"Quiz"}),o.jsxs("td",{className:"p-3 text-right font-medium",style:{color:l.primaryColor},children:[c.quiz.obtained,"/",c.quiz.total]})]})]})]})})]}),o.jsx("div",{className:"mt-8 border-t-2 pt-4 text-center",style:{borderColor:l.borderColor},children:o.jsx("p",{className:"text-sm",style:{color:l.secondaryColor,fontFamily:l.fontFamily},children:l.footerText})})]})}),o.jsxs("div",{className:"space-y-4",children:[o.jsxs(B,{value:N,onValueChange:S,className:"flex justify-center space-x-6",children:[o.jsxs("div",{className:"flex items-center space-x-2",children:[o.jsx(_,{className:"cursor-pointer",value:"png",id:"marksheet-png"}),o.jsxs(D,{htmlFor:"marksheet-png",className:"flex cursor-pointer items-center gap-2",children:[o.jsx(U,{className:"h-4 w-4"}),"PNG Image"]})]}),o.jsxs("div",{className:"flex items-center space-x-2",children:[o.jsx(_,{className:"cursor-pointer",value:"pdf",id:"marksheet-pdf"}),o.jsxs(D,{htmlFor:"marksheet-pdf",className:"flex cursor-pointer items-center gap-2",children:[o.jsx(O,{className:"h-4 w-4"}),"PDF Document"]})]})]}),o.jsxs(Y,{variant:"outline",className:"w-full",onClick:$,children:[o.jsx(X,{className:"mr-2 h-4 w-4"}),"Download as ",N.toUpperCase()]})]})]})};export{se as D,ae as a};