class Interviews{constructor(e){this.interviewForm=e;this.convertToAJAX()}addInterview(e){let t=this;e.addEventListener("submit",(async r=>{r.preventDefault(),r.stopPropagation();const i=new FormData(e),o=Object.fromEntries(i.entries());if(""===o.name)return void t.notify("Please Enter Company Name 🤷‍♂️","error");if(""===o.date)return void t.notify("Please Enter a Date 🤷‍♂️","error");if(new Date(o.date)<new Date)return void t.notify("Please Enter a Valid Date 🤷‍♂️","error");const n=await fetch("/interviews/add",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o)}),a=await n.json();if("error"===a.status)return t.notify(a.message,"error");e.reset(),t.notify(a.message,"success");let d=t.createInterviewInDOM(a.company),s=d.querySelector(".delete-interview-button");t.toggleAccordion(d),t.deleteInterview(s),new StudentsList(a.company.id)}))}createInterviewInDOM(e){let t=document.querySelectorAll("template")[1].content.cloneNode(!0).querySelector(".interview-accordion-item");t.classList.add(`accordion-item-${e.id}`),t.id=`accordion-item-${e.id}`,t.querySelector(".accordion-header").id=`interview-heading-${e.id}`;let r=t.querySelector(".accordion-button");r.textContent=e.name+" 🚀",r.setAttribute("data-bs-target",`#interview-${e.id}`),r.setAttribute("aria-controls",`interview-${e.id}`);let i=t.querySelector(".accordion-collapse");return i.id=`interview-${e.id}`,i.setAttribute("aria-labelledby",`interview-heading-${e.id}`),e.date=new Date(e.date).toLocaleDateString(),i.querySelector("form").setAttribute("id",`company-${e.id}-students-form`),i.querySelector(".interview-data p").appendChild(document.createTextNode(e.date)),i.querySelector(".delete-interview-button").setAttribute("data-id",e.id),i.querySelector(".fa-circle-plus.add-student-BTN").id=`add-student-${e.id}`,document.getElementById("interviews").appendChild(t),t}toggleAccordion(e){e.querySelectorAll(".accordion-collapse").forEach((e=>{const t=e.previousElementSibling;t.children[0].addEventListener("click",(e=>{e.preventDefault(),e.stopPropagation(),"true"===e.target.getAttribute("aria-expanded")?(t.classList.add("round"),e.target.classList.add("round")):(t.classList.remove("round"),e.target.classList.remove("round"))}))}))}deleteTableRow(e){let{students:t,id:r}=e;for(let e of t){let t=document.querySelector(`.student-accordion-item.accordion-item-${e._id}`);t.querySelectorAll(".student-data > p > span")[6].textContent=e.status;let i=t.querySelector("table").querySelector("tbody").querySelectorAll("tr");0!==i.length&&i.forEach((e=>{e.id===`${r}`&&e.remove()}))}}deleteInterview(e){let t=this;e.addEventListener("click",(async e=>{e.preventDefault(),e.stopPropagation();const r=e.target.getAttribute("data-id"),i=await fetch(`/interviews/delete/${r}`,{method:"DELETE"}),o=await i.json();if("error"===o.status)return t.notify(o.message,"error");t.notify(o.message,"success"),t.deleteTableRow(o),e.target.closest(".interview-accordion-item").remove()}))}convertToAJAX(){let e=this;document.querySelectorAll(".interview-accordion-item").forEach((t=>{let r=t.querySelector(".delete-interview-button");e.toggleAccordion(t),e.deleteInterview(r);let i=t.id.split("-")[2];new StudentsList(i)})),e.addInterview(e.interviewForm)}notify(e,t){new Noty({theme:"success"===t?"relax":"sunset",text:e,type:t,layout:"topRight",progressBar:!0,closeWith:["click","button"],timeout:6e3}).show()}}try{const e=document.getElementsByClassName("interview-form");Array.from(e).forEach((e=>{new Interviews(e)}))}catch(e){console.log(e)}