document.addEventListener("DOMContentLoaded",function(){document.getElementById("menuToggler").addEventListener("click",function(){document.getElementById("menuPanel").classList.toggle("active")}),document.getElementById("menuClose").addEventListener("click",function(){document.getElementById("menuPanel").classList.remove("active")}),$(".select-2").length>0&&$(".select-2").select2();for(var k=document.getElementsByClassName("validatePhone"),T=["input","keyup","change"],v=0;ve.ok?e.json():e.json().then(l=>{console.error(l.message)})).then(e=>{const l=document.getElementById("likes-count");l.textContent=`${e.likes} Likes`,e.liked?(r.innerHTML="Liked",r.classList.remove("btn-dark"),r.classList.add("btn-main")):(r.innerHTML="Like",r.classList.remove("btn-main"),r.classList.add("btn-dark"))}).catch(e=>{console.error("Error liking the ad:",e)})});const a=document.getElementById("save-button");a&&a.addEventListener("click",function(){const t=a.closest("article"),s=t?t.getAttribute("data-save-route"):null;s&&fetch(s,{method:"POST",headers:{"X-CSRF-TOKEN":document.querySelector('meta[name="csrf-token"]').getAttribute("content"),"Content-Type":"application/json"}}).then(e=>e.ok?e.json():e.json().then(l=>{console.error(l.message)})).then(e=>{e.saved?(a.innerHTML="Saved",a.classList.remove("btn-dark"),a.classList.add("btn-main")):(a.innerHTML="Save",a.classList.remove("btn-main"),a.classList.add("btn-dark"))}).catch(e=>{console.error("Error saving the ad:",e)})});const L=document.getElementById("toggleShare");L&&L.addEventListener("click",function(){const t=document.getElementById("sharePanel");t.style.display=t.style.display==="block"?"none":"block"});const p=document.getElementById("phoneField"),h=document.getElementById("ccPhoneField"),y=document.getElementById("changePhone"),i=document.getElementById("sendOtp"),O=document.getElementById("verifyOtpPanel"),c=document.getElementById("verifyOtp"),P=document.getElementById("sendOtpUrl");if(P)var x=P.getAttribute("data-value");const S=document.getElementById("verifyOtpUrl");if(S)var C=S.getAttribute("data-value");y&&i&&y.addEventListener("click",function(){i.style.display="block",y.style.display="none",p.disabled=!1,h.disabled=!1}),i&&c&&i.addEventListener("click",function(){i.style.display="none",p.disabled=!0,h.disabled=!0;const t=p.value.trim(),s=h.value.trim(),e=document.querySelector(".verify-alerts");if(!t||!s){e.innerHTML='
Please enter a valid phone number and country code.
';return}e&&(e.innerHTML='
Sending the OTP ...
');var l=new FormData;l.append("phone",t),l.append("country_code",s),fetch(x,{method:"POST",headers:{"X-CSRF-TOKEN":document.querySelector('meta[name="csrf-token"]').getAttribute("content")},body:l}).then(n=>{if(!n.ok)throw new Error(n.statusText);return n.json()}).then(n=>{n.status?(c.style.display="block",O.style.display="block",e.innerHTML="",console.log(n.message)):e.innerHTML='
'+n.message+"
"}).catch(n=>{e.innerHTML='
Error sending OTP. Please try again.
'})}),c&&c.addEventListener("click",function(){const t=document.querySelector("#otpField"),s=document.querySelector(".verify-alerts"),e=t.value.trim();if(!e){s.innerHTML='
Please enter the OTP.
';return}s.innerHTML='
Verifying the OTP...
';var l=new FormData;l.append("otp",e),fetch(C,{method:"POST",headers:{"X-CSRF-TOKEN":document.querySelector('meta[name="csrf-token"]').getAttribute("content")},body:l}).then(n=>{if(!n.ok)throw new Error(n.statusText);return n.json()}).then(n=>{n.status?(s.innerHTML='
OTP verified successfully.
',setTimeout(()=>{s.innerHTML=""},1500),c.style.display="none",O.style.display="none",y.style.display="block",t.value="",console.log(n.message)):s.innerHTML='
'+n.message+"
"}).catch(n=>{s.innerHTML='
Error verifying OTP. Please try again.
'})});const b=document.getElementById("whatsappField"),E=document.getElementById("ccWhatsappField"),f=document.getElementById("changeWpPhone"),o=document.getElementById("sendWpOtp"),B=document.getElementById("verifyWpOtpPanel"),d=document.getElementById("verifyWpOtp"),I=document.getElementById("sendWpOtpUrl");if(I)var D=I.getAttribute("data-value");const A=document.getElementById("verifyWpOtpUrl");if(A)var N=A.getAttribute("data-value");f&&o&&f.addEventListener("click",function(){o.style.display="block",f.style.display="none",b.disabled=!1,E.disabled=!1}),o&&d&&o.addEventListener("click",function(){o.style.display="none",b.disabled=!0,E.disabled=!0;const t=b.value.trim(),s=E.value.trim(),e=document.querySelector(".verify-wp-alerts");if(!t||!s){e.innerHTML='
Please enter a valid WhatsApp number and country code.
';return}e&&(e.innerHTML='
Sending the OTP...
');var l=new FormData;l.append("whatsapp",t),l.append("country_code",s),fetch(D,{method:"POST",headers:{"X-CSRF-TOKEN":document.querySelector('meta[name="csrf-token"]').getAttribute("content")},body:l}).then(n=>{if(!n.ok)throw new Error(n.statusText);return n.json()}).then(n=>{n.status?(d.style.display="block",B.style.display="block",e.innerHTML="",console.log(n.message)):(e.innerHTML='
'+n.message+"
",o.style.display="block")}).catch(n=>{e.innerHTML='
Error sending OTP. Please try again.
',o.style.display="block"})}),d&&d.addEventListener("click",function(){const t=document.querySelector("#otpWpField"),s=document.querySelector(".verify-wp-alerts"),e=t.value.trim();if(!e){s.innerHTML='
Please enter the OTP.
';return}s.innerHTML='
Verifying the OTP...
';var l=new FormData;l.append("otp",e),fetch(N,{method:"POST",headers:{"X-CSRF-TOKEN":document.querySelector('meta[name="csrf-token"]').getAttribute("content")},body:l}).then(n=>{if(!n.ok)throw new Error(n.statusText);return n.json()}).then(n=>{n.status?(s.innerHTML='
OTP verified successfully.
',setTimeout(()=>{s.innerHTML=""},1500),d.style.display="none",B.style.display="none",f.style.display="block",t.value="",console.log(n.message)):s.innerHTML='
'+n.message+"
"}).catch(n=>{s.innerHTML='
Error verifying OTP. Please try again.
'})});const M=document.getElementById("select-all"),u=document.querySelectorAll(".ad-checkbox"),m=document.getElementById("bulk-delete-btn"),w=document.getElementById("bulk-delete-route");if(w)var R=w.value;m&&(m.disabled=!0),M&&M.addEventListener("change",function(){this.checked?u.forEach(t=>{t.checked=!0}):u.forEach(t=>{t.checked=!1}),H()});function H(){const t=Array.from(u).some(s=>s.checked);m.disabled=!t}u.forEach(function(t){t.addEventListener("change",H)}),m&&m.addEventListener("click",function(){const t=Array.from(u).filter(s=>s.checked);if(t.length>0){if(confirm("Are you sure you want to delete the selected ads?")){const s=t.map(l=>l.value),e=new FormData;s.forEach(l=>e.append("ids[]",l)),fetch(R,{method:"POST",headers:{"X-CSRF-TOKEN":document.querySelector('meta[name="csrf-token"]').getAttribute("content")},body:e}).then(l=>l.json()).then(l=>{l.status?(alert(l.message),location.reload()):alert(l.message)}).catch(l=>{alert("Error deleting ads. Please try again."),console.error(l)})}}else alert("Please select at least one ad to delete.")});const F=document.getElementById("newPost");F&&F.addEventListener("submit",function(){document.getElementById("submitAd").disabled=!0,document.getElementById("overlay").style.display="flex"}),document.querySelectorAll(".optionForm").forEach(t=>{let s=!1;t.addEventListener("submit",function(e){if(s){e.preventDefault();return}const n=`Are you sure to ${t.querySelector('select[name="option"]').value} this ad?`;if(n&&!confirm(n)){e.preventDefault();return}s=!0;const q=t.querySelector(".optionSubmit"),W=t.querySelector(".optionOverlay");q&&(q.disabled=!0),W&&(W.style.display="flex")})})});