const fs = require('fs');
let js = fs.readFileSync('app_final.js', 'utf8');

// 1. Change the button in renderClientAbstracts
js = js.replace(/\$\{ab\.status === 'draft' \? \`\<button class="action-btn btn-edit" onclick="openClientAbstractModal\(\$\{ab\.id\}\)"\>.*?<\/button>\` : ''\}/,
    `<button class="action-btn btn-edit" style="background:#17a2b8" onclick="openClientAbstractModal(\${ab.id})">\${ab.status === 'draft' ? 'تعديل' : 'عرض'}</button>`);

// 2. Hide save button in openClientAbstractModal
js = js.replace(/window\.openClientAbstractModal = function\(id = null\) \{/, 
    `window.openClientAbstractModal = function(id = null) {\n    const btnSave = document.getElementById('btn-save-client-abstract');\n    if(btnSave) btnSave.style.display = 'inline-block';`);

js = js.replace(/document\.getElementById\('client-abstract-advance-payment'\)\.value = ab\.advance_payment \|\| 0;/, 
    `document.getElementById('client-abstract-advance-payment').value = ab.advance_payment || 0;\n      if(ab.status === 'approved' && btnSave) btnSave.style.display = 'none';`);

fs.writeFileSync('app_final.js', js, 'utf8');
console.log('Fixed view abstract in app_final.js!');
