Now update the import function to group threads by account_id and send separate requests per account.


Edit

async function importSelectedDomainThreads() {
    var checkboxes = document.querySelectorAll('.domain-thread-cb:checked');
    var threadIds = Array.from(checkboxes).map(function(cb) { return cb.value; });

    if (threadIds.length === 0) { alert('No threads selected'); return; }
    if (!confirm('Import ' + threadIds.length + ' threads from Gmail?')) return;

    var container = document.getElementById('sync-domain-results');
    container.innerHTML += '
Importing ' + threadIds.length + ' threads... This may take a moment.
';

    try {
        var response = await fetch('api.php?action=import_gmail_threads', {
            method: 'POST',
            headers: {'Content-Type': 'application/json'},
            body: JSON.stringify({thread_ids: threadIds})
        });
        var data = await response.json();

        if (data.success) {
            var successCount = data.imported || 0;
            alert('Imported ' + successCount + ' of ' + threadIds.length + ' threads successfully.');
            container.innerHTML = '
Import complete: ' + successCount + ' threads imported.
';
            loadContacts(1);
            loadEmails(1);
        } else {
            alert('Import error: ' + (data.error || 'Unknown'));
        }
    } catch(e) {
        alert('Import error: ' + e.message);
    }
}

async function importSelectedDomainThreads() {
    var checkboxes = document.querySelectorAll('.domain-thread-cb:checked');
    if (checkboxes.length === 0) { alert('No threads selected'); return; }
    if (!confirm('Import ' + checkboxes.length + ' threads from Gmail?')) return;

    // Group threads by account_id
    var byAccount = {};
    Array.from(checkboxes).forEach(function(cb) {
        var acctId = cb.getAttribute('data-account-id') || 'default';
        if (!byAccount[acctId]) byAccount[acctId] = [];
        byAccount[acctId].push(cb.value);
    });

    var container = document.getElementById('sync-domain-results');
    container.innerHTML += '
Importing ' + checkboxes.length + ' threads... This may take a moment.
';

    try {
        var totalImported = 0;
        var totalThreads = checkboxes.length;

        for (var acctId in byAccount) {
            var body = { thread_ids: byAccount[acctId] };
            if (acctId !== 'default') body.account_id = parseInt(acctId);

            var response = await fetch('api.php?action=import_gmail_threads', {
                method: 'POST',
                headers: {'Content-Type': 'application/json'},
                body: JSON.stringify(body)
            });
            var data = await response.json();
            if (data.success) {
                totalImported += (data.imported || 0);
            }
        }

        alert('Imported ' + totalImported + ' of ' + totalThreads + ' threads successfully.');
        container.innerHTML = '
Import complete: ' + totalImported + ' threads imported.
';
        loadContacts(1);
        loadEmails(1);
    } catch(e) {
        alert('Import error: ' + e.message);
    }
}