// Migration page JavaScript
let routes = {};
let csrfToken = '';
// Initialize on page load
document.addEventListener('DOMContentLoaded', function() {
if (window.migrationRoutes) {
routes = window.migrationRoutes;
csrfToken = document.querySelector('meta[name="csrf-token"]')?.content || '';
}
});
function startMigration() {
const storeMapping = {};
const selects = document.querySelectorAll('select[name^="store_mapping"]');
let hasMapping = false;
selects.forEach(select => {
if (select.value) {
storeMapping[select.name.match(/\[(\d+)\]/)[1]] = select.value;
hasMapping = true;
}
});
if (!hasMapping) {
alert('Please map at least one store before starting migration.');
return;
}
if (!confirm('Are you sure you want to start the migration? This will modify your Magento 2 database.')) {
return;
}
document.getElementById('migrateBtn').disabled = true;
document.getElementById('loading').classList.add('active');
const migrationLogContent = document.getElementById('migrationLogContent');
migrationLogContent.innerHTML = '
Starting migration...
';
document.getElementById('migrationLogContainer').scrollIntoView({ behavior: 'smooth', block: 'nearest' });
fetch(routes.migrate, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': csrfToken
},
body: JSON.stringify({ store_mapping: storeMapping })
})
.then(response => response.json())
.then(data => {
document.getElementById('loading').classList.remove('active');
document.getElementById('migrateBtn').disabled = false;
if (data.success) {
addLogEntry(`✓ Migration completed successfully!`, 'success');
addLogEntry(`Total categories processed: ${data.migrated_count}`, 'success');
if (data.added_count !== undefined) {
addLogEntry(` - Added new categories: ${data.added_count}`, 'success');
}
if (data.existing_count !== undefined) {
addLogEntry(` - Found existing categories: ${data.existing_count}`, 'success');
}
if (data.log && data.log.length > 0) {
data.log.forEach(log => {
const type = log.includes('ERROR') ? 'error' : 'success';
addLogEntry(log, type);
});
}
} else {
addLogEntry(`✗ Migration failed: ${data.message}`, 'error');
if (data.log && data.log.length > 0) {
data.log.forEach(log => {
addLogEntry(log, log.includes('ERROR') ? 'error' : 'success');
});
}
}
})
.catch(error => {
document.getElementById('loading').classList.remove('active');
document.getElementById('migrateBtn').disabled = false;
addLogEntry(`✗ Error: ${error.message}`, 'error');
});
}
function addLogEntry(message, type = '') {
const migrationLogContent = document.getElementById('migrationLogContent');
const entry = document.createElement('div');
entry.className = `log-entry ${type}`;
entry.textContent = message;
migrationLogContent.appendChild(entry);
const migrationLogContainer = document.getElementById('migrationLogContainer');
migrationLogContainer.scrollTop = migrationLogContainer.scrollHeight;
}
// Make startMigration available globally
window.startMigration = startMigration;