class StudentsList{constructor(e){this.companyID=e,this.companyContainer=document.querySelector(`.interview-accordion-item#accordion-item-${this.companyID}`),this.studentsListForm=this.companyContainer.querySelector(`#company-${this.companyID}-students-form`);this.convertToAJAX()}updateTable(e,t,r){let s=document.querySelector(`.student-accordion-item.accordion-item-${e._id}`);s.querySelectorAll(".student-data > p > span")[6].textContent=e.status;let n=s.querySelector("table").querySelector("tbody").querySelectorAll("tr");for(let e of n)if(e.id===t._id){e.querySelectorAll("td")[1].textContent=r.result;break}}removeTable(e,t){let r=document.querySelector(`.student-accordion-item.accordion-item-${e._id}`);r.querySelectorAll(".student-data > p > span")[6].textContent=e.status;let s=r.querySelector("table").querySelector("tbody").querySelectorAll("tr");for(let e of s)if(e.id===t._id){e.remove();break}}editStudent(e,t){let r=this;t.addEventListener("click",(async t=>{t.preventDefault(),t.stopPropagation();let s=r.studentsListForm.querySelector(`#student-${e}-select`),n=r.studentsListForm.querySelector(`#result-${e}-select`),i=s.parentElement,d=n.parentElement;"none"===window.getComputedStyle(i).display&&i.classList.remove("hide"),"none"===window.getComputedStyle(d).display&&d.classList.remove("hide");let o=i.parentElement,l=o.querySelector(".fixed");s.querySelectorAll("option").forEach((e=>{e.textContent===l.querySelectorAll("p > span")[0].textContent&&e.setAttribute("selected","selected")})),s.disabled=!0;let a=o.querySelector(".update-student-interview"),c=o.querySelector(".edit-student-interview"),u=o.querySelector(".delete-student-interview");a.classList.remove("hide"),l.classList.add("hide"),c.classList.add("hide"),u.classList.add("hide"),a.setAttribute("data-id",e),a.addEventListener("click",(async t=>{t.preventDefault(),t.stopPropagation();const s=new FormData;s.append("interviewID",e),s.append("result",n.value);const o=Object.fromEntries(s.entries()),p=`/interviews/students/edit/${e}`;let y=await fetch(p,{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify(o)}),m=await y.json();if("error"===m.status)return void r.notify(m.message,"error");r.notify(m.message,"success");let{student:S,company:v,result:h}=m;a.classList.add("hide"),l.classList.remove("hide"),c.classList.remove("hide"),u.classList.remove("hide"),l.querySelectorAll("p > span")[0].textContent=S.name,l.querySelectorAll("p > span")[1].textContent=h.result,i.classList.add("hide"),d.classList.add("hide"),r.updateTable(S,v,h)}))}))}deleteStudent(e,t){let r=this;t.addEventListener("click",(async t=>{t.preventDefault(),t.stopPropagation();const s=`/interviews/students/delete/${e}`;let n=await fetch(s,{method:"DELETE",headers:{"Content-Type":"application/json"}}),i=await n.json();if("error"===i.status)return void r.notify(i.message,"error");r.notify(i.message,"success"),r.companyContainer.querySelector(`.student-interview-${e}`).remove();let{student:d,company:o}=i;r.removeTable(d,o)}))}createTable(e,t,r){let s=document.querySelector(`.student-accordion-item.accordion-item-${e._id}`);s.querySelectorAll(".student-data > p > span")[6].textContent=e.status;let n=s.querySelector("table").querySelector("tbody"),i=document.createElement("tr");i.setAttribute("id",`${t._id}`);let d=document.createElement("th");d.setAttribute("scope","row"),d.textContent=`${t.name}`,i.appendChild(d);let o=document.createElement("td");o.textContent=`${new Date(t.date).toLocaleDateString()}`,i.appendChild(o);let l=document.createElement("td");l.textContent=`${r.result}`,i.appendChild(l),n.appendChild(i)}deleteInterviewSlot(e,t){let r=this;t.addEventListener("click",(async t=>{t.preventDefault(),t.stopPropagation();const s=`/interviews/delete-interview/${e}`;let n=await fetch(s,{method:"DELETE"}),i=await n.json();"error"!==i.status?(r.notify(i.message,"success"),r.companyContainer.querySelector(`.student-interview-${e}`).remove()):r.notify(i.message,"error")}))}addStudent(e,t){let r=this;t.addEventListener("click",(async t=>{t.preventDefault(),t.stopPropagation();let s=r.studentsListForm.querySelector(`#student-${e}-select`),n=r.studentsListForm.querySelector(`#result-${e}-select`);const i=new FormData;i.append("interviewID",e),i.append("name",s.value),i.append("result",n.value);const d=Object.fromEntries(i.entries());let o=await fetch("/interviews/students/add",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(d)}),l=await o.json();if("error"===l.status)return r.notify(l.message,"error");r.notify(l.message,"success");let{student:a,interview:c,company:u,result:p}=l,y=document.querySelectorAll("template")[2].content.cloneNode(!0).querySelector(".student"),m=[!1,!1,!1],S=r.studentsListForm.querySelector(`.student-interview-${c._id}`),v=S.querySelectorAll(".form-group")[0],h=S.querySelectorAll(".form-group")[1];v.classList.add("hide"),h.classList.add("hide"),S.querySelector(".add-student-interview").classList.add("hide"),S.querySelector(".delete-interview-slot").classList.add("hide"),S.querySelector(".update-student-interview").setAttribute("data-id",`${e}`);let q=S.querySelector(".fixed");q||(q=y.querySelector(".fixed"),m[0]=!0),q.classList.remove("hide"),q.querySelectorAll("p > span")[0].textContent=a.name,q.querySelectorAll("p > span")[1].textContent=p.result;let w=S.querySelector(".edit-student-interview");w||(w=y.querySelector(".edit-student-interview"),m[1]=!0),w.classList.remove("hide"),w.setAttribute("data-id",`${c._id}`);let f=S.querySelector(".delete-student-interview");f||(f=y.querySelector(".delete-student-interview"),m[2]=!0),f.classList.remove("hide"),f.setAttribute("data-id",`${c._id}`),m[0]&&S.appendChild(q),m[1]&&S.appendChild(w),m[2]&&S.appendChild(f),r.createTable(a,u,p),r.deleteStudent(c._id,f),r.editStudent(c._id,w)}))}createStudentInTheListDOM(e){let t=this;e.addEventListener("click",(async r=>{r.preventDefault(),r.stopPropagation();const s=e.id.split("-")[2],n=await fetch("/interviews/create-interview",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({id:s})}),i=await n.json();if("error"===i.status)return t.notify(i.message,"error");let{students:d,interview:o,company:l}=i,a=o._id,c=document.querySelectorAll("template")[2].content.cloneNode(!0).querySelector(".student");c.id=`${a}`,c.classList.add(`student-interview-${a}`);let u=c.querySelectorAll(".form-group");u[0].classList.remove("hide"),u[1].classList.remove("hide");let p=u[0].querySelector("select");p.id=`student-${a}-select`,u[1].querySelector("select").id=`result-${a}-select`;for(let e of d){let t=document.createElement("option");t.value=e._id,t.textContent=e.name,p.appendChild(t)}let y=c.querySelector(".delete-interview-slot");y.classList.remove("hide"),y.setAttribute("data-id",`${a}`);let m=c.querySelector(".add-student-interview");m.classList.remove("hide"),m.setAttribute("data-id",`${a}`),c.querySelector(".update-student-interview").setAttribute("data-id",`${a}`),t.studentsListForm.appendChild(c),t.addStudent(a,m),t.deleteInterviewSlot(a,y)}))}notify(e,t){new Noty({theme:"success"===t?"relax":"sunset",text:e,type:t,layout:"topRight",progressBar:!0,closeWith:["click","button"],timeout:6e3}).show()}convertToAJAX(){let e=this,t=e.companyContainer.querySelectorAll(`#add-student-${e.companyID}`),r=e.studentsListForm?.querySelectorAll(".add-student-interview"),s=e.studentsListForm?.querySelectorAll(".delete-interview-slot"),n=e.studentsListForm?.querySelectorAll(".delete-student-interview"),i=e.studentsListForm?.querySelectorAll(".edit-student-interview");t.forEach((t=>{e.addStudentListBtn=t,e.createStudentInTheListDOM(t)})),r.forEach((t=>{let r=t.getAttribute("data-id");e.addStudent(r,t)})),s.forEach((t=>{let r=t.getAttribute("data-id");e.deleteInterviewSlot(r,t)})),n.forEach((t=>{let r=t.getAttribute("data-id");e.deleteStudent(r,t)})),i.forEach((t=>{let r=t.getAttribute("data-id");e.editStudent(r,t)}))}}