(function(){
  function $(s,c){return (c||document).querySelector(s)} function $all(s,c){return Array.prototype.slice.call((c||document).querySelectorAll(s));}
  function esc(s){return String(s||'').replace(/[&<>"']/g,function(c){return {'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c];});}
  function base(){return (window.MJ_BASE_URL||'').replace(/\/$/,'');}
  function toast(title,msg,type){ if(window.MJ428&&window.MJ428.toast){window.MJ428.toast({type:type||'success',title:title,message:msg});return;} alert((title?title+'\n':'')+(msg||'')); }
  function openModal(id){var m=$(id); if(!m)return; m.classList.add('is-open'); document.body.classList.add('mj-modal-open');}
  function closeModal(m){m.classList.remove('is-open'); if(!$('.mj429-modal.is-open'))document.body.classList.remove('mj-modal-open');}
  function fill(form, btn){form.reset(); form.querySelector('[name=lead_id]').value=btn.dataset.leadId||''; var action=btn.dataset.action||'update_status'; form.querySelector('[name=action]').value=action; var t=$('.mj429-modal-title',form.closest('.mj429-modal')); var sub=$('.mj429-modal-subtitle',form.closest('.mj429-modal')); if(t)t.textContent=btn.dataset.modalTitle||btn.textContent||'Lead Action'; if(sub)sub.textContent=btn.dataset.leadTitle||''; var st=form.querySelector('[name=status]'); if(st&&btn.dataset.status)st.value=btn.dataset.status; var note=form.querySelector('[name=note]'); if(note)note.value=''; var follow=form.querySelector('[name=followup_at]'); if(follow)follow.value=btn.dataset.followup||''; $all('[data-mj-action-section]',form).forEach(function(el){el.style.display='none';}); var sec=$('[data-mj-action-section="'+action+'"]',form); if(sec)sec.style.display='block'; }
  document.addEventListener('click',function(e){
    var b=e.target.closest('[data-mj-lead-action]'); if(b){e.preventDefault(); var modal=$('#mj429LeadModal'); if(!modal)return; fill($('form',modal),b); openModal('#mj429LeadModal');}
    var c=e.target.closest('[data-mj-modal-close]'); if(c){e.preventDefault(); closeModal(c.closest('.mj429-modal'));}
  });
  document.addEventListener('keydown',function(e){if(e.key==='Escape')$all('.mj429-modal.is-open').forEach(closeModal);});
  document.addEventListener('submit',function(e){var form=e.target.closest('.mj429-lead-action-form'); if(!form)return; e.preventDefault(); var btn=form.querySelector('button[type=submit]'); var old=btn?btn.innerHTML:''; if(btn){btn.disabled=true;btn.innerHTML='Saving...';} var fd=new FormData(form); fd.append('csrf_token',window.MJ_CSRF_TOKEN||''); fetch(base()+'/ajax/mj-v30429-lead-action.php',{method:'POST',body:fd,credentials:'same-origin'}).then(function(r){return r.json();}).then(function(j){if(!j||!j.ok){toast('Action failed',(j&&j.error)||'Please try again.','error');return;} toast('Lead updated',j.message||'Saved.','success'); var id=fd.get('lead_id'); var row=document.querySelector('[data-lead-row="'+id+'"]'); if(row&&j.lead){var badge=row.querySelector('[data-lead-status-badge]'); if(badge){badge.textContent=j.lead.status_label||j.lead.status; badge.dataset.status=j.lead.status; badge.className='mj-badge mj-badge-'+(j.lead.status||'new')+' '+(j.lead.status||'new');} var btns=row.querySelectorAll('[data-mj-lead-action]'); btns.forEach(function(x){x.dataset.status=j.lead.status||x.dataset.status; if(j.lead.followup_at)x.dataset.followup=j.lead.followup_at;});}
        closeModal(form.closest('.mj429-modal'));
      }).catch(function(){toast('Network error','Please try again.','error');}).finally(function(){if(btn){btn.disabled=false;btn.innerHTML=old;}});
  });
})();
