const fs = require('fs');
let html = fs.readFileSync('index.html', 'utf8');

// The exact string to replace in the Purchases Returns modal
const searchStr = \`          <div class="flex-col"><label>حالة الفاتورة</label>
            <select id="purchases-returns-status">
              <option value="draft">مسودة (لا تؤثر على الحسابات)</option>
              <option value="posted">مرحلة (تؤثر مالياً ومخزنياً)</option>
            </select>
          </div>\`;

const replaceStr = \`          <div class="flex-col"><label>الخزينة</label><select id="purchases-returns-treasury"></select></div>
          <div class="flex-col"><label>الإجمالي</label><input type="number" id="purchases-returns-total" readonly></div>
          <div class="flex-col"><label>ملاحظات</label><input type="text" id="purchases-returns-notes"></div>
          <div class="flex-col"><label>حالة الفاتورة</label>
            <select id="purchases-returns-status">
              <option value="draft">مسودة (لا تؤثر على الحسابات)</option>
              <option value="posted">مرحلة (تؤثر مالياً ومخزنياً)</option>
            </select>
          </div>\`;

if (html.includes(searchStr) && !html.includes('id="purchases-returns-total"')) {
    html = html.replace(searchStr, replaceStr);
    html = html.replace(/v=\d+/g, 'v=' + Date.now());
    fs.writeFileSync('index.html', html, 'utf8');
    console.log('Successfully patched Purchases Returns HTML!');
} else if (html.includes('id="purchases-returns-total"')) {
    console.log('Already patched!');
} else {
    console.log('Could not find the search string in HTML.');
}
