const fs = require('fs');

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

// The incorrect headers are:
// <th>الصنف</th>
// <th>الخصم</th>
// <th>الكمية</th>
// <th>السعر</th>
// <th>الإجمالي</th>
// <th>إجراء</th>

const incorrectHeaders = `<th>الصنف</th>
            <th>الخصم</th>
            <th>الكمية</th>
            <th>السعر</th>
            <th>الإجمالي</th>
            <th>إجراء</th>`;

const correctHeaders = `<th>الصنف</th>
            <th>الكمية</th>
            <th>السعر</th>
            <th>الخصم</th>
            <th>الإجمالي</th>
            <th>إجراء</th>`;

indexHtml = indexHtml.replace(incorrectHeaders, correctHeaders);

// Just in case it has different indentation, let's use regex
indexHtml = indexHtml.replace(
    /<th>الصنف<\/th>\s*<th>الخصم<\/th>\s*<th>الكمية<\/th>\s*<th>السعر<\/th>/,
    '<th>الصنف</th>\n            <th>الكمية</th>\n            <th>السعر</th>\n            <th>الخصم</th>'
);

indexHtml = indexHtml.replace(/v=\d+/g, 'v=' + Date.now());

fs.writeFileSync('index.html', indexHtml, 'utf8');

console.log('Fixed Sales headers order!');
