
// pwa_customer_view.js - shows single customer and followups/tasks quick actions
window.showCustomerFromHash = window.showCustomerFromHash || (async function(){
  const cont = document.getElementById('view_customer');
  if(!cont) return;
  const qs = new URLSearchParams(location.hash.split('?')[1]||'');
  const id = qs.get('id');
  if(!id){ cont.innerHTML = '<div class="loading">No customer specified</div>'; return; }
  cont.innerHTML = '<div class="loading">Loading customer...</div>';
  try{
    const res = await fetch((window.API_BASE||'') + '/api/customers.php?action=get&id='+encodeURIComponent(id), { headers: Auth.authHeaders() });
    if(!res.ok){ cont.innerHTML = '<div class="error">Load failed</div>'; return; }
    const j = await res.json();
    if(!j.status){ cont.innerHTML = '<div class="error">Not found or auth required</div>'; return; }
    const c = j.data;
    cont.innerHTML = '<div class="card"><h3>'+ (c.name||'') +'</h3><div>Phone: '+(c.phone||'')+'</div><div style="margin-top:8px"><button id="btn_add_followup">Add Followup</button> <button id="btn_add_task">Add Task</button></div></div><div id="cust_followups" class="card"></div><div id="cust_tasks" class="card"></div>';
    document.getElementById('btn_add_followup').addEventListener('click', async ()=>{
      const note = prompt('Followup note');
      if(!note) return;
      const payload = { action:'create', customer_id: id, note };
      try{
        const res = await fetch((window.API_BASE||'') + '/api/followups.php?action=create', { method:'POST', headers: Object.assign({'Content-Type':'application/json'}, Auth.authHeaders()), body: JSON.stringify({ customer_id: id, note }) });
        const j = await res.json();
        if(j.status==1){ alert('Followup added'); loadFollowups(); }
        else alert('Error: '+(j.error||'unknown'));
      }catch(err){
        console.warn('offline followup', err);
        await Outbox.enqueue('POST', '/api/followups.php?action=create', { action:'create', customer_id: id, note });
        alert('Offline — followup queued');
        loadFollowups();
      }
    });
    document.getElementById('btn_add_task').addEventListener('click', async ()=>{
      const title = prompt('Task title');
      if(!title) return;
      const payload = { title, customer_id: id };
      try{
        const res = await fetch((window.API_BASE||'') + '/api/tasks.php?action=create', { method:'POST', headers: Object.assign({'Content-Type':'application/json'}, Auth.authHeaders()), body: JSON.stringify(payload) });
        const j = await res.json();
        if(j.status==1){ alert('Task created'); loadTasks(); } else alert('Error: '+(j.error||'unknown'));
      }catch(err){
        console.warn('offline task', err);
        await Outbox.enqueue('POST', '/api/tasks.php?action=create', payload);
        alert('Offline — task queued');
        loadTasks();
      }
    });

    async function loadFollowups(){
      const el = document.getElementById('cust_followups'); el.innerHTML = '<div class="loading">Loading followups...</div>';
      try{
        const r = await fetch((window.API_BASE||'') + '/api/followups.php?action=list&customer_id='+encodeURIComponent(id), { headers: Auth.authHeaders() });
        const jj = await r.json();
        if(!jj.status){ el.innerHTML = '<div class="error">Auth required</div>'; return; }
        el.innerHTML = '<h4>Followups</h4>' + (jj.data.length? jj.data.map(f=>'<div class="card"><div>'+f.note+'</div><div style="font-size:12px;color:#666">'+f.created_at+'</div></div>').join('') : '<div class="loading">No followups</div>');
      }catch(e){ el.innerHTML = '<div class="error">Network</div>'; }
    }
    async function loadTasks(){
      const el = document.getElementById('cust_tasks'); el.innerHTML = '<div class="loading">Loading tasks...</div>';
      try{
        const r = await fetch((window.API_BASE||'') + '/api/tasks.php?action=list&customer_id='+encodeURIComponent(id), { headers: Auth.authHeaders() });
        const jj = await r.json();
        if(!jj.status){ el.innerHTML = '<div class="error">Auth required</div>'; return; }
        el.innerHTML = '<h4>Tasks</h4>' + (jj.data.length? jj.data.map(t=>'<div class="card"><div><strong>'+t.title+'</strong></div><div style="font-size:12px;color:#666">'+t.status+'</div></div>').join('') : '<div class="loading">No tasks</div>');
      }catch(e){ el.innerHTML = '<div class="error">Network</div>'; }
    }

    loadFollowups(); loadTasks();
  }catch(err){
    console.error('customer load err', err);
    cont.innerHTML = '<div class="error">Load error</div>';
  }
});