$(document).ready(function(){hideTableLoading();let r=window.initCustomDataTable("#customerTable",{url:window.Laravel.routes.query,columns:[{data:null,orderable:!1,render:function(e,t,a,n){const s=n.settings.json.pagination.current_page,c=n.settings.json.pagination.per_page;return(s-1)*c+n.row+1}},{data:null,orderable:!1,render:function(e,t,a,n){return`<div class="td-name">${a.name}</div>`}},{data:null,orderable:!1,render:function(e,t,a,n){return`<div>${l(a.type)}</div>`}},{data:null,orderable:!1,render:function(e,t,a,n){return`<div>${a.owner_name}</div>`}},{data:null,orderable:!1,render:function(e,t,a,n){return`<div style="font-size:12px;color:var(--gray-600);max-width:200px">${a.address}</div>`}},{data:null,orderable:!1,render:function(e,t,a,n){return`<div style="font-size:12px;color:var(--gray-600);white-space:nowrap">${a.phone_number}</div>`}},{data:null,orderable:!1,render:function(e,t,a,n){return`<div>${d(a)}</div>`}},{data:null,orderable:!1,render:function(e,t,a,n){return`<div style="text-align:center;white-space:nowrap;width:140px;margin:0 auto;display:flex;gap:4px;justify-content:center">
                        <a href="/customer/detail/${a.id}" class="action-btn" style="text-decoration:none;color:var(--red-main);display:flex;align-items:center;justify-content:center;font-weight:500">Detail</a>
                        <a href="/customer/edit/${a.id}" class="action-btn" style="text-decoration:none;color:var(--gray-600);display:flex;align-items:center;justify-content:center">Edit</a>
                        <button class="action-btn action-del" onclick="askDelete(${a.id}, '${a.name.replace(/'/g,"\\'")}')">Hapus</button>
                    </div>`}}],extraData:function(e){e.name=$("#f-nama").val(),e.type=$("#f-tipe").val(),e.address=$("#f-alamat").val()}});function l(e){return`<span class="c-badge ${e==="Dealer"?"c-blue":e==="Retailer"?"c-brown":"c-green"}">${e}</span>`}function d(e){if(e.type==="Petani"&&(e.land_area||e.type_crop)){const t=[];return e.type_crop&&t.push(`Crop: ${e.type_crop}`),e.land_area&&t.push(`Lahan: ${Number(e.land_area).toLocaleString("id")} m²`),t.length?`<div style="font-size:11px;color:var(--gray-600)">${t.join(" · ")}</div>`:"—"}return'<span style="color:var(--gray-300);font-size:12px">—</span>'}window.applyFilter=function(){showTableLoading(),window.currentPage=1,r.ajax.reload(function(){hideTableLoading()})},window.resetFilter=function(){$("#f-nama").val(""),$("#f-tipe").val(""),$("#f-alamat").val(""),showTableLoading(),window.currentPage=1,r.ajax.reload(function(){hideTableLoading()})};let i=null;window.askDelete=function(e,t){i=e,document.getElementById("confirm-text").textContent=`Data "${t}" akan dihapus secara permanen dan tidak dapat dikembalikan.`,document.getElementById("confirm-modal").classList.add("open")},window.closeConfirm=function(){document.getElementById("confirm-modal").classList.remove("open"),i=null},window.confirmDelete=function(e){i!==null&&(e.classList.add("is-loading"),axios.delete(window.Laravel.routes.delete.replace(":id",i),{headers:{"X-CSRF-TOKEN":$('meta[name="csrf-token"]').attr("content"),Accept:"application/json"}}).then(t=>{e.classList.remove("is-loading");const a=t.data.message||"Data berhasil dihapus";Toast.show(a,"success"),closeConfirm(),showTableLoading(),window.currentPage=1,r.ajax.reload(function(){hideTableLoading()})}).catch(t=>{e.classList.remove("is-loading");const a=t.response?.data?.message||"Terjadi kesalahan sistem";Toast.show(a,"error")}))};let o;window.searchFilter=function(){clearTimeout(o),o=setTimeout(function(){showTableLoading(),window.currentPage=1,r.ajax.reload(function(){hideTableLoading()})},500)},document.getElementById("confirm-modal").addEventListener("click",function(e){e.target===this&&closeConfirm()})});
