const fs = require('fs');

let spJs = fs.readFileSync('d:/برنامج مخازن/sales_purchases_module.js', 'utf8');

// The new logic to inject for Sales
const newSalesLogic = `      let actions = \`<button class="action-btn btn-view" onclick="openSalesModal(\${inv.id})">👁️ عرض/تعديل</button>\`;
      if(inv.status === 'draft') {
         if(currentUser && (currentUser.role === 'مدير عام' || currentUser.role.includes('مدير') || currentUser.username === 'admin')) {
            actions += \`<button class="action-btn btn-delete" onclick="deleteTransaction(\${inv.id})">🗑️ حذف</button>\`;
         }
         actions += \`<button class="action-btn" style="background:var(--accent); margin-right:5px;" onclick="sendInvoiceToStore(\${inv.id})">📤 إرسال للمخزن</button>\`;
      }
      if(inv.status === 'posted') {
         actions += \` <button class="action-btn" style="background:#28a745; color:white; margin-right:5px; padding: 5px 10px; font-weight: bold;" onclick="if(typeof openVoucherModal === 'function') { window.currentLinkedInvoiceId = \${inv.id}; openVoucherModal('receipt', \${inv.id}, \${inv.contact_id}); }">سداد 💵</button>\`;
      }

      tbody.innerHTML += \`<tr>`;

// The new logic to inject for Purchases
const newPurchasesLogic = `      let actions = \`<button class="action-btn btn-view" onclick="openPurchasesModal(\${inv.id})">👁️ عرض/تعديل</button>\`;
      if(inv.status === 'draft') {
         if(currentUser && (currentUser.role === 'مدير عام' || currentUser.role.includes('مدير') || currentUser.username === 'admin')) {
            actions += \`<button class="action-btn btn-delete" onclick="deleteTransaction(\${inv.id})">🗑️ حذف</button>\`;
         }
         actions += \`<button class="action-btn" style="background:var(--accent); margin-right:5px;" onclick="sendInvoiceToStore(\${inv.id})">📤 إرسال للمخزن</button>\`;
      }
      if(inv.status === 'posted') {
         actions += \` <button class="action-btn" style="background:#28a745; color:white; margin-right:5px; padding: 5px 10px; font-weight: bold;" onclick="if(typeof openVoucherModal === 'function') { window.currentLinkedInvoiceId = \${inv.id}; openVoucherModal('payment', \${inv.id}, \${inv.contact_id}); }">سداد 💵</button>\`;
      }

      tbody.innerHTML += \`<tr>`;

// First, fix renderSales
// We find where `let actions =` starts, up to `tbody.innerHTML += \`<tr>` inside renderSales.
spJs = spJs.replace(
    /let actions = `<button class="action-btn btn-view" onclick="openSalesModal\(\$\{inv\.id\}\)">👁️ عرض\/تعديل<\/button>`;[\s\S]*?tbody\.innerHTML \+= `<tr>/,
    newSalesLogic
);

// Second, fix renderPurchases
spJs = spJs.replace(
    /let actions = `<button class="action-btn btn-view" onclick="openPurchasesModal\(\$\{inv\.id\}\)">👁️ عرض\/تعديل<\/button>`;[\s\S]*?tbody\.innerHTML \+= `<tr>/,
    newPurchasesLogic
);

fs.writeFileSync('d:/برنامج مخازن/sales_purchases_module.js', spJs, 'utf8');

let html = fs.readFileSync('d:/برنامج مخازن/index.html', 'utf8');
const version = Date.now().toString();
html = html.replace(/v=\d+/g, 'v=' + version);
fs.writeFileSync('d:/برنامج مخازن/index.html', html, 'utf8');

console.log('Fixed buttons in sales and purchases');
