Now update `loadEmails`:


649→}
   650→
   651→// ============================================
   652→// Email Threads (grouped conversations)
   653→// ============================================
   654→async function loadEmails(page = 1, contactId = null) {
   655→    const container = document.getElementById('emails-list');
   656→    container.innerHTML = '
Loading threads...
';
   657→
   658→    try {
   659→        state.emails.page = page;
   660→        if (contactId !== null) state.emails.contactId = contactId;
   661→
   662→        const params = { page: page, limit: 10 };
   663→        if (state.emails.contactId) params.contact = state.emails.contactId;
   664→
   665→        const data = await apiCall('threads', params);
   666→
   667→        if (data.threads.length === 0) {
   668→            container.innerHTML = '
No threads found
';

}

// ============================================
// Email Threads (grouped conversations)
// ============================================
async function loadEmails(page = 1, contactId = null) {
    const container = document.getElementById('emails-list');
    container.innerHTML = '
Loading threads...
';

    try {
        state.emails.page = page;
        if (contactId !== null) state.emails.contactId = contactId;

        const params = { page: page, limit: 10 };
        if (state.emails.contactId) params.contact = state.emails.contactId;

        const data = await apiCall('threads', params);

        if (data.threads.length === 0) {
            container.innerHTML = '
No threads found
';


Edit

        const params = { page: page, limit: 10 };
        if (state.emails.contactId) params.contact = state.emails.contactId;

        const data = await apiCall('threads', params);

        var threadSortVal = (document.getElementById('thread-sort')?.value || 'last_date:DESC').split(':');
        const params = { page: page, limit: 10, sort: threadSortVal[0], order: threadSortVal[1] };
        if (state.emails.contactId) params.contact = state.emails.contactId;

        var tSearch = document.getElementById('email-search')?.value || '';
        var tContactFilter = document.getElementById('thread-contact-filter')?.value || '';
        var tDateFrom = document.getElementById('thread-date-from')?.value || '';
        var tDateTo = document.getElementById('thread-date-to')?.value || '';
        var tStatus = document.getElementById('thread-status-filter')?.value || '';
        if (tSearch) params.search = tSearch;
        if (tContactFilter) params.contact_name = tContactFilter;
        if (tDateFrom) params.date_from = tDateFrom;
        if (tDateTo) params.date_to = tDateTo;
        if (tStatus) params.status = tStatus;

        const data = await apiCall('threads', params);