const fs = require('fs');

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

const oldSave = `window.saveAbstract = async function() {
    try {
        const getVal = (id) => document.getElementById(id)?.value || '';
        const getNum = (id) => parseFloat(document.getElementById(id)?.value || 0) || 0;
        const contractId = getNum('ab-contract-id');
        
        if(!contractId) { alert('يجب اختيار العقد'); return; }
        
        const obj = {
            contract_id: contractId,
            abstract_number: getVal('ab-abstract-number'),
            date: getVal('ab-date'),
            net_value: getNum('ab-net-value'),
            advance_payment: getNum('ab-advance-payment'),
            status: 'draft',
            items: Array.from(document.querySelectorAll('#ab-items-tbody tr')).map(row => ({
                name: row.querySelector('.ab-item-name')?.value || '',
                unit: row.querySelector('.ab-item-unit')?.value || '',
                price: parseFloat(row.querySelector('.ab-item-price')?.value) || 0,
                total_qty: parseFloat(row.querySelector('.ab-item-total-qty')?.innerText) || 0,
                prev_qty: parseFloat(row.querySelector('.ab-item-prev-qty')?.innerText) || 0,
                current_qty: parseFloat(row.querySelector('.ab-item-curr-qty')?.value) || 0
            }))
        };
        
        const editId = document.getElementById('abstract-id')?.value;
        if(editId) { obj.id = parseInt(editId); await window.DB.update('subcontract_abstracts', obj); }
        else { await window.DB.add('subcontract_abstracts', obj); }
        
        alert('تم الحفظ');
        window.renderAbstracts();
        if(typeof closeModal === 'function') closeModal('abstract-modal');
    } catch(e) { alert('خطأ: ' + e.message); }
};`;

const newSave = `window.saveAbstract = async function() {
    try {
        const getVal = (id) => document.getElementById(id)?.value || '';
        const getNum = (id) => parseFloat(document.getElementById(id)?.value || 0) || 0;
        const getNumText = (id) => parseFloat((document.getElementById(id)?.innerText || '0').replace(/,/g, '')) || 0;
        const contractId = getNum('ab-contract-id');
        
        if(!contractId) { alert('يجب اختيار العقد'); return; }
        
        const obj = {
            contract_id: contractId,
            abstract_number: getVal('ab-abstract-number'),
            date: getVal('ab-date'),
            period: getVal('ab-period'),
            
            gross_value: getNumText('ab-total-works'),
            net_value: getNumText('ab-net-value'),
            
            tax_percent: getNum('ab-tax-percent'),
            tax_value: getNumText('ab-tax-value'),
            
            retention_percent: getNum('ab-retention-percent'),
            retention_value: getNumText('ab-retention-value'),
            
            advance_payment: getNum('ab-advance-payment'),
            other_deductions: getNum('ab-other-deductions'),
            
            status: 'draft',
            items: Array.from(document.querySelectorAll('#ab-items-tbody tr')).map(row => ({
                name: row.querySelector('.ab-item-name')?.value || '',
                unit: row.querySelector('.ab-item-unit')?.value || '',
                price: parseFloat(row.querySelector('.ab-item-price')?.value) || 0,
                total_qty: parseFloat((row.querySelector('.ab-item-total-qty')?.innerText || '0').replace(/,/g,'')) || 0,
                prev_qty: parseFloat((row.querySelector('.ab-item-prev-qty')?.innerText || '0').replace(/,/g,'')) || 0,
                current_qty: parseFloat(row.querySelector('.ab-item-curr-qty')?.value) || 0
            }))
        };
        
        const editId = document.getElementById('abstract-id')?.value;
        if(editId) { obj.id = parseInt(editId); await window.DB.update('subcontract_abstracts', obj); }
        else { await window.DB.add('subcontract_abstracts', obj); }
        
        currentSubcontractAbstracts = await window.DB.getAll('subcontract_abstracts') || [];
        alert('تم الحفظ');
        window.renderAbstracts();
        window.closeAbstractModal();
    } catch(e) { alert('خطأ: ' + e.message); }
};`;

code = code.replace(oldSave, newSave);
fs.writeFileSync('subcontracts.js', code, 'utf8');

let indexHtml = fs.readFileSync('index.html', 'utf8');
indexHtml = indexHtml.replace(/v=20260610t/g, 'v=20260610u');
fs.writeFileSync('index.html', indexHtml, 'utf8');

console.log('Fixed saveAbstract in subcontracts.js');
