document.querySelectorAll("[data-tab]").forEach(button=>button.addEventListener("click",()=>{document.querySelectorAll(".admin-tab,.admin-view").forEach(x=>x.classList.remove("active"));button.classList.add("active");document.querySelector("#tab-"+button.dataset.tab)?.classList.add("active")}));
document.querySelectorAll("[data-open]").forEach(button=>button.addEventListener("click",()=>{const modal=document.querySelector("#"+button.dataset.open);const form=modal.querySelector("form");if(button.hasAttribute("data-reset")&&form.classList.contains("crud-form")){form.reset();form.action=form.dataset.store;form.querySelector(".method-slot").innerHTML=""}if(button.dataset.edit){const item=JSON.parse(button.dataset.edit);form.reset();form.action=form.dataset.update+"/"+item.id;form.querySelector(".method-slot").innerHTML='<input type="hidden" name="_method" value="PUT">';Object.entries(item).forEach(([name,value])=>{const field=form.elements[name];if(field&&value!==null)field.value=String(value).slice(0,10)})}modal.classList.add("open")}));
document.querySelectorAll("[data-close]").forEach(button=>button.addEventListener("click",()=>button.closest(".modal-backdrop").classList.remove("open")));
document.querySelectorAll(".modal-backdrop").forEach(modal=>modal.addEventListener("click",event=>{if(event.target===modal)modal.classList.remove("open")}));
document.querySelectorAll("[data-confirm]").forEach(form=>form.addEventListener("submit",event=>{if(!confirm("Hapus data ini?"))event.preventDefault()}));
