function b(){const m=document.getElementById("childNsTable");if(!m)return;const i=m.querySelector("tbody"),r=window.childNsConfig||{},d=r.routes||{},y=r.csrfToken,l=r.translations||{};function h({id:e="",hostname:t="",ip:n="",isNew:o=!1}={}){const s=document.createElement("tr");return s.className="ns-row"+(o?" ns-row-new":""),s.dataset.nsId=e,s.dataset.original=JSON.stringify({hostname:t,ip:n}),s.innerHTML=`
            <td class="ns-cell-hostname">
                <span class="ns-view"></span>
                <input type="text" class="form-control form-control-sm ns-input-hostname d-none" placeholder="ns1.example.com">
            </td>
            <td class="ns-cell-ip">
                <span class="ns-view"></span>
                <input type="text" class="form-control form-control-sm ns-input-ip d-none" placeholder="192.0.2.1">
            </td>
            <td class="text-end ns-actions">
                <button type="button" class="btn btn-sm btn-outline-primary ns-edit-btn" title="${l.edit||"Edit"}">
                    <i class="fas fa-edit"></i>
                </button>
                <button type="button" class="btn btn-sm btn-outline-danger ns-delete-btn" title="${l.delete||"Delete"}">
                    <i class="fas fa-trash"></i>
                </button>
                <button type="button" class="btn btn-sm btn-success ns-save-btn d-none" title="${l.save||"Save"}">
                    <i class="fas fa-check"></i>
                </button>
                <button type="button" class="btn btn-sm btn-outline-secondary ns-cancel-btn d-none" title="${l.cancel||"Cancel"}">
                    <i class="fas fa-times"></i>
                </button>
            </td>
        `,s.querySelector(".ns-cell-hostname .ns-view").textContent=t,s.querySelector(".ns-cell-ip .ns-view").textContent=n,s.querySelector(".ns-input-hostname").value=t,s.querySelector(".ns-input-ip").value=n,s}function f(e){e.classList.add("ns-editing"),e.querySelectorAll(".ns-view").forEach(t=>t.classList.add("d-none")),e.querySelectorAll(".ns-input-hostname, .ns-input-ip").forEach(t=>t.classList.remove("d-none")),e.querySelector(".ns-edit-btn").classList.add("d-none"),e.querySelector(".ns-delete-btn").classList.add("d-none"),e.querySelector(".ns-save-btn").classList.remove("d-none"),e.querySelector(".ns-cancel-btn").classList.remove("d-none"),e.querySelector(".ns-input-hostname").focus()}function v(e){e.classList.remove("ns-editing"),e.querySelectorAll(".ns-view").forEach(t=>t.classList.remove("d-none")),e.querySelectorAll(".ns-input-hostname, .ns-input-ip").forEach(t=>t.classList.add("d-none")),e.querySelector(".ns-edit-btn").classList.remove("d-none"),e.querySelector(".ns-delete-btn").classList.remove("d-none"),e.querySelector(".ns-save-btn").classList.add("d-none"),e.querySelector(".ns-cancel-btn").classList.add("d-none")}function u(e,t,n="POST"){const o=document.createElement("form");o.method="POST",o.action=e;const s=document.createElement("input");if(s.type="hidden",s.name="_token",s.value=y,o.appendChild(s),n!=="POST"){const a=document.createElement("input");a.type="hidden",a.name="_method",a.value=n,o.appendChild(a)}Object.entries(t).forEach(([a,w])=>{const c=document.createElement("input");c.type="hidden",c.name=a,c.value=w,o.appendChild(c)}),document.body.appendChild(o),o.submit()}function S(e){const t=e.querySelector(".ns-input-hostname").value.trim(),n=e.querySelector(".ns-input-ip").value.trim();return!t||!n?(window.showError?window.showError(l.fillAllFields||"Please fill all fields"):alert(l.fillAllFields||"Please fill all fields"),null):{hostname:t,ip:n}}i.addEventListener("click",function(e){const t=e.target.closest("tr.ns-row");if(t){if(e.target.closest(".ns-edit-btn")){f(t);return}if(e.target.closest(".ns-cancel-btn")){if(t.classList.contains("ns-row-new"))t.remove(),i.querySelectorAll("tr.ns-row").length===0&&(i.innerHTML=`<tr class="ns-empty"><td colspan="3" class="text-center text-muted">${l.empty||"No records"}</td></tr>`);else{const n=JSON.parse(t.dataset.original);t.querySelector(".ns-input-hostname").value=n.hostname,t.querySelector(".ns-input-ip").value=n.ip,v(t)}return}if(e.target.closest(".ns-save-btn")){const n=S(t);if(!n)return;t.classList.contains("ns-row-new")?u(d.add,{hostname:n.hostname,ip:n.ip}):u(d.update,{nameserver_id:t.dataset.nsId,hostname:n.hostname,ip:n.ip});return}if(e.target.closest(".ns-delete-btn")){const n=l.confirmDelete||"Are you sure?";if(!window.confirm(n))return;u(d.delete,{nameserver_id:t.dataset.nsId},"DELETE")}}});const p=document.getElementById("childNsAddBtn");p&&p.addEventListener("click",function(){const e=i.querySelector(".ns-empty");e&&e.remove();const t=h({isNew:!0});i.insertBefore(t,i.firstChild),f(t)})}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",b):b();
