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

// ===================================================
// Fix 1: editSalesReturn - add warehouse population
// ===================================================
const oldSalesEdit = `    document.getElementById('sales-returns-notes').value = r.notes || '';
    document.getElementById('sales-returns-status').value = r.status || 'draft';`;

const newSalesEdit = `    // === تعبئة المخزن ===
    const whSelectEdit = document.getElementById('sales-returns-warehouse');
    if (whSelectEdit) {
        const whs = typeof currentWarehouses !== 'undefined' ? currentWarehouses : [];
        whSelectEdit.innerHTML = '<option value="">-- اختر المخزن --</option>';
        whs.forEach(w => { whSelectEdit.innerHTML += \`<option value="\${w.id}" \${String(w.id) === String(r.to_warehouse_id) ? 'selected' : ''}>\${w.name}</option>\`; });
    }
    // === تعبئة المشروع ===
    const projSelectEdit = document.getElementById('sales-returns-project');
    if (projSelectEdit) {
        const projs = typeof currentProjects !== 'undefined' ? currentProjects : [];
        projSelectEdit.innerHTML = '<option value="">-- بدون مشروع --</option>';
        projs.forEach(p => { projSelectEdit.innerHTML += \`<option value="\${p.id}" \${String(p.id) === String(r.project_id) ? 'selected' : ''}>\${p.name}</option>\`; });
    }

    document.getElementById('sales-returns-notes').value = r.notes || '';
    document.getElementById('sales-returns-status').value = r.status || 'draft';`;

if (js.includes(oldSalesEdit)) {
    js = js.replace(oldSalesEdit, newSalesEdit);
    console.log('✅ Fixed editSalesReturn warehouse population');
} else {
    console.log('❌ editSalesReturn pattern not found');
    const idx = js.indexOf("'sales-returns-notes').value = r.notes");
    console.log('sales-returns-notes found at:', idx);
    if (idx > -1) console.log(js.substring(idx - 100, idx + 200));
}

// ===================================================
// Fix 2: Find editPurchasesReturn and add warehouse
// ===================================================
const purchEdit = js.indexOf("window.editPurchasesReturn");
if (purchEdit > -1) {
    console.log('Found editPurchasesReturn at:', purchEdit);
    console.log(js.substring(purchEdit, purchEdit + 400));
}

// Try to find the purchases return notes field
const oldPurchEdit = `    document.getElementById('purchases-returns-notes').value = r.notes || '';
    document.getElementById('purchases-returns-status').value = r.status || 'draft';`;

const newPurchEdit = `    // === تعبئة المخزن ===
    const whSelectPEdit = document.getElementById('purchases-returns-warehouse');
    if (whSelectPEdit) {
        const whs = typeof currentWarehouses !== 'undefined' ? currentWarehouses : [];
        whSelectPEdit.innerHTML = '<option value="">-- اختر المخزن --</option>';
        whs.forEach(w => { whSelectPEdit.innerHTML += \`<option value="\${w.id}" \${String(w.id) === String(r.from_warehouse_id || r.warehouse_id) ? 'selected' : ''}>\${w.name}</option>\`; });
    }
    // === تعبئة المشروع ===
    const projSelectPEdit = document.getElementById('purchases-returns-project');
    if (projSelectPEdit) {
        const projs = typeof currentProjects !== 'undefined' ? currentProjects : [];
        projSelectPEdit.innerHTML = '<option value="">-- بدون مشروع --</option>';
        projs.forEach(p => { projSelectPEdit.innerHTML += \`<option value="\${p.id}" \${String(p.id) === String(r.project_id) ? 'selected' : ''}>\${p.name}</option>\`; });
    }

    document.getElementById('purchases-returns-notes').value = r.notes || '';
    document.getElementById('purchases-returns-status').value = r.status || 'draft';`;

if (js.includes(oldPurchEdit)) {
    js = js.replace(oldPurchEdit, newPurchEdit);
    console.log('✅ Fixed editPurchasesReturn warehouse population');
} else {
    console.log('❌ editPurchasesReturn pattern not found');
    const idx2 = js.indexOf("'purchases-returns-notes').value = r.notes");
    console.log('purchases-returns-notes found at:', idx2);
    if (idx2 > -1) console.log(js.substring(idx2 - 100, idx2 + 200));
}

fs.writeFileSync('vouchers_returns.js', js);
console.log('\nDone.');
