const fs = require('fs');

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

const jsFix = `
let replaceBlockMatch = js.match(/let actions = \\\`[\\s\\S]*?<button class="action-btn btn-danger" onclick="deleteClientAbstract\\(\\\\\\$\\{ab\\.id\\}\\)">حذف<\\/button>\\\` : ''\\}\\s*\\\`;/);
if (replaceBlockMatch) {
    let replaceBlock = replaceBlockMatch[0];
    let newBlock = \`let actions = \\\`
      <button class="action-btn" style="background:#6c757d; color:#fff; border:none; padding: 4px 10px; border-radius: 4px; font-size: 0.9rem; margin: 2px;" onclick="printClientAbstract(\\\${ab.id})">طباعة</button>
      \\\${ab.status === 'draft' ? \\\`<button class="action-btn" style="background:#28a745; color:#fff; border:none; padding: 4px 10px; border-radius: 4px; font-size: 0.9rem; margin: 2px;" onclick="approveClientAbstract(\\\${ab.id})">اعتماد</button>\\\` : ''}
      \\\${ab.status === 'approved' ? \\\`<button class="action-btn" style="background:#28a745; color:#fff; border:none; padding: 4px 10px; border-radius: 4px; font-size: 0.9rem; margin: 2px;" onclick="payClientAbstract(\\\${ab.id})">سند قبض</button>\\\` : ''}
      <button class="action-btn" style="background:#17a2b8; color:#fff; border:none; padding: 4px 10px; border-radius: 4px; font-size: 0.9rem; margin: 2px;" onclick="openClientAbstractModal(\\\${ab.id})">\\\${ab.status === 'draft' ? 'تعديل' : 'عرض'}</button>
      <button class="action-btn" style="background:#dc3545; color:#fff; border:none; padding: 4px 10px; border-radius: 4px; font-size: 0.9rem; margin: 2px;" onclick="deleteClientAbstract(\\\${ab.id})">حذف</button>
    \\\`;\`;
    js = js.replace(replaceBlock, newBlock);
    console.log("Found and replaced actions block.");
} else {
    // Attempt 2: More generic match
    let replaceBlockMatch2 = js.match(/let actions = \\\`[\\s\\S]*?deleteClientAbstract[\\s\\S]*?\\\`;/);
    if(replaceBlockMatch2) {
        let replaceBlock = replaceBlockMatch2[0];
        let newBlock = \`let actions = \\\`
      <button class="action-btn" style="background:#6c757d; color:#fff; border:none; padding: 4px 10px; border-radius: 4px; font-size: 0.9rem; margin: 2px;" onclick="printClientAbstract(\\\${ab.id})">طباعة</button>
      \\\${ab.status === 'draft' ? \\\`<button class="action-btn" style="background:#28a745; color:#fff; border:none; padding: 4px 10px; border-radius: 4px; font-size: 0.9rem; margin: 2px;" onclick="approveClientAbstract(\\\${ab.id})">اعتماد</button>\\\` : ''}
      \\\${ab.status === 'approved' ? \\\`<button class="action-btn" style="background:#28a745; color:#fff; border:none; padding: 4px 10px; border-radius: 4px; font-size: 0.9rem; margin: 2px;" onclick="payClientAbstract(\\\${ab.id})">سند قبض</button>\\\` : ''}
      <button class="action-btn" style="background:#17a2b8; color:#fff; border:none; padding: 4px 10px; border-radius: 4px; font-size: 0.9rem; margin: 2px;" onclick="openClientAbstractModal(\\\${ab.id})">\\\${ab.status === 'draft' ? 'تعديل' : 'عرض'}</button>
      <button class="action-btn" style="background:#dc3545; color:#fff; border:none; padding: 4px 10px; border-radius: 4px; font-size: 0.9rem; margin: 2px;" onclick="deleteClientAbstract(\\\${ab.id})">حذف</button>
    \\\`;\`;
        js = js.replace(replaceBlock, newBlock);
        console.log("Found and replaced actions block using generic match.");
    } else {
        console.log("Could not find the block in app_final.js at all!");
    }
}
`;

eval(jsFix);

fs.writeFileSync('app_final.js', js, 'utf8');
