/* Mahour Professional 2 — Phase 5 UI/UX Refresh. No data/business-logic mutation. */
(function(){
 function q(s,r=document){return r.querySelector(s)}
 function qa(s,r=document){return Array.from(r.querySelectorAll(s))}
 function label(title,sub){const d=document.createElement('div');d.className='p5-section-label';d.innerHTML='<b>'+title+'</b><small>'+sub+'</small>';return d}
 function refreshDashboardLayout(){
   const page=q('#page-dashboard'); if(!page||page.dataset.p5==='1')return;
   page.dataset.p5='1';page.classList.add('p5-command');
   const head=q('.page-head',page), cards=qa(':scope > .cards',page), panel=q(':scope > .panel',page);
   if(panel)panel.id='todayPanel';
   const hero=q('.m2-hero',page), insights=q('.m2-insights',page);
   const week=q('#m2WeekWrap',page);
   const grid=document.createElement('div');grid.className='p5-command-grid';
   const primary=document.createElement('div');primary.className='p5-primary-zone';
   const side=document.createElement('aside');side.className='p5-side-zone';
   if(panel){primary.appendChild(label('جریان مراجعه امروز','جستجو، حضور و پرداخت'));primary.appendChild(panel)}
   if(hero){side.appendChild(label('اقدامات سریع','کارهای پرتکرار'));side.appendChild(hero)}
   if(insights){side.appendChild(label('وضعیت مدیریتی','نمای سریع'));side.appendChild(insights)}
   if(head)head.after(grid); else page.prepend(grid);grid.append(primary,side);
   if(cards.length){const anchor=grid; cards.forEach(c=>anchor.before(c))}
   if(week)page.appendChild(week);
 }
 function improveLabels(){
   const map={'برنامه امروز':'امروز','نوبت‌های آینده':'نوبت‌های آینده','همه نوبت‌ها':'تقویم و نوبت‌ها','گزارش درآمد':'مالی و درآمد','حضور‌وغیاب و حقوق':'پرسنل و حقوق','تنظیمات و پشتیبان':'تنظیمات'};
   qa('.nav button').forEach(b=>{const t=b.textContent.trim();if(map[t])b.textContent=map[t]});
 }
 function run(){improveLabels();refreshDashboardLayout()}
 if(document.readyState==='loading')document.addEventListener('DOMContentLoaded',run);else run();
 const old=window.showPage;if(typeof old==='function')window.showPage=function(){const r=old.apply(this,arguments);setTimeout(refreshDashboardLayout,0);return r};
})();


/* FINAL CLEAN CORRECTIONS — no table reconstruction */
(function(){
 function clean(){
   var w=document.getElementById('m2WeekWrap'); if(w)w.remove();
   var t=document.getElementById('todayTable');
   if(t){t.style.display='block';t.style.visibility='visible';t.style.opacity='1'}
 }
 if(document.readyState==='loading')document.addEventListener('DOMContentLoaded',function(){setTimeout(clean,0)});
 else setTimeout(clean,0);
 var r=window.renderDashboard;
 if(typeof r==='function'){window.renderDashboard=function(){var x=r.apply(this,arguments);setTimeout(clean,0);return x}}
})();


/* ONLY REQUESTED CHANGES: remove work-center + light-blue selected cabin */
(function(){
 function applyOnlyRequested(){
   var page=document.getElementById('page-dashboard');
   if(!page)return;

   var hero=document.getElementById('m2Hero') || page.querySelector('.m2-hero');
   if(hero) hero.style.display='none';

   var root=document.getElementById('todayTable');
   if(!root)return;
   root.querySelectorAll('button').forEach(function(b){
     var txt=(b.textContent||'').trim();
     var oc=b.getAttribute('onclick')||'';
     if(txt.indexOf('کابین')<0 && oc.toLowerCase().indexOf('cabin')<0)return;

     b.classList.remove('cabin-ui-selected');
     var cell=b.closest('td');
     var selected=b.classList.contains('active') ||
                  b.getAttribute('aria-pressed')==='true' ||
                  (cell && /ورود\s*شد|وارد\s*شد|ثبت\s*شد/.test(cell.textContent||''));
     if(selected)b.classList.add('cabin-ui-selected');

     if(!b.dataset.cabinOnlyBound){
       b.dataset.cabinOnlyBound='1';
       b.addEventListener('click',function(){setTimeout(applyOnlyRequested,40)});
     }
   });
 }
 if(document.readyState==='loading')document.addEventListener('DOMContentLoaded',function(){setTimeout(applyOnlyRequested,0)});
 else setTimeout(applyOnlyRequested,0);

 var rd=window.renderDashboard;
 if(typeof rd==='function'){
   window.renderDashboard=function(){var r=rd.apply(this,arguments);setTimeout(applyOnlyRequested,0);return r}
 }
})();


/* ROOT FIX ONLY: remove empty dashboard side column after Work Center removal */
(function(){
 function fixDashboardWidth(){
   var page=document.getElementById('page-dashboard'); if(!page)return;
   var grid=page.querySelector('.p5-command-grid');
   var primary=page.querySelector('.p5-primary-zone');
   var side=page.querySelector('.p5-side-zone');
   if(side) side.style.display='none';
   if(grid){
     grid.style.gridTemplateColumns='minmax(0,1fr)';
     grid.style.gap='0';
     grid.style.width='100%';
   }
   if(primary){primary.style.width='100%';primary.style.maxWidth='100%'}
 }
 if(document.readyState==='loading')document.addEventListener('DOMContentLoaded',function(){setTimeout(fixDashboardWidth,0)});
 else setTimeout(fixDashboardWidth,0);
 var sp=window.showPage;
 if(typeof sp==='function'){
   window.showPage=function(){var r=sp.apply(this,arguments);setTimeout(fixDashboardWidth,0);return r}
 }
})();
