const fs = require('fs');
let code = fs.readFileSync('d:/Easy ERP/petty_cash_module.js', 'utf8');

const targetFuncStart = 'window.approveSettlement = async function(id, skipConfirm = false) {';
const fixCode = `window.approveSettlement = async function(id, skipConfirm = false) {
    if (window._approvingSettlements && window._approvingSettlements[id]) {
        console.log("Settlement already being approved:", id);
        return;
    }
    window._approvingSettlements = window._approvingSettlements || {};
    window._approvingSettlements[id] = true;
    
    // Disable the approve button in the modal if it's open
    const approveBtn = document.querySelector(\`button[onclick="window.approveSettlement('\${id}')"]\`);
    if (approveBtn) {
        approveBtn.disabled = true;
        approveBtn.innerHTML = 'جاري الاعتماد...';
    }
`;

code = code.replace(targetFuncStart, fixCode);

fs.writeFileSync('d:/Easy ERP/petty_cash_module.js', code, 'utf8');
console.log("Patched petty_cash_module.js for approveSettlement");
