/* WooDoo Admin JS */ jQuery( function ( $ ) { const cfg = window.WooDooAdmin || {}; const i18n = cfg.i18n || {}; const ajaxUrl = cfg.ajax_url; const nonce = cfg.nonce; // ── Tab navigation ────────────────────────────────────────────────── $( '.woodoo-settings .nav-tab' ).on( 'click', function ( e ) { e.preventDefault(); const target = $( this ).attr( 'href' ); $( '.woodoo-settings .nav-tab' ).removeClass( 'nav-tab-active' ); $( this ).addClass( 'nav-tab-active' ); $( '.woodoo-tab' ).hide(); $( target ).show(); } ); // ── Test Connection ────────────────────────────────────────────────── $( '#woodoo-test-connection' ).on( 'click', function () { const $btn = $( this ); const $result = $( '#woodoo-test-result' ); $btn.prop( 'disabled', true ).text( i18n.testing ); $result.html( '' ); ajax( 'woodoo_test_connection', {} ) .done( function ( res ) { if ( res.success ) { const d = res.data; const cls = d.success ? 'green' : 'red'; $result.html( badge( cls, d.message ) ); } else { $result.html( badge( 'red', res.data || i18n.error ) ); } } ) .fail( function () { $result.html( badge( 'red', i18n.error ) ); } ) .always( function () { $btn.prop( 'disabled', false ).text( 'Test Connection' ); } ); } ); // ══════════════════════════════════════════════════════════════════════ // CUSTOMER LINKING TAB // ══════════════════════════════════════════════════════════════════════ let selectedWcUser = null; // { id, display_name, email } // ── Step 1: search WooCommerce customers ───────────────────────────── function searchWcCustomers() { const q = $( '#woo-customer-search' ).val().trim(); if ( q.length < 2 ) return; const $results = $( '#woo-customer-results' ); $results.html( '' + esc( i18n.searching ) + '' ); $( '#woodoo-odoo-col' ).hide(); selectedWcUser = null; ajax( 'woodoo_search_wc_customers', { query: q } ) .done( function ( res ) { if ( ! res.success || ! res.data.length ) { $results.html( '
' + esc( i18n.no_wc_customers ) + '
' ); return; } let html = '| Name | WC Email | Odoo Partner | ' + ' |
|---|---|---|---|
| ' + esc( u.display_name ) + ' | ' + '' + esc( u.email ) + ' | ' + '' + linked + ' | ' + '' + ''; if ( u.partner_id ) { html += ' '; } html += ' |
' + esc( i18n.error ) + '
' ); } ); } $( '#woo-customer-search-btn' ).on( 'click', searchWcCustomers ); $( '#woo-customer-search' ).on( 'keypress', function ( e ) { if ( e.which === 13 ) { e.preventDefault(); searchWcCustomers(); } } ); // ── Select a WC customer → open Odoo partner search panel ─────────── $( document ).on( 'click', '.woodoo-select-wc-user', function () { const $btn = $( this ); selectedWcUser = { id : $btn.data( 'id' ), name : $btn.data( 'name' ), email : $btn.data( 'email' ), }; // Highlight selected row $( '#woo-customer-results tr' ).css( 'background', '' ); $btn.closest( 'tr' ).css( 'background', '#eff6ff' ); // Show step 2 panel $( '#woodoo-linking-for' ).html( 'Linking: ' + esc( selectedWcUser.name ) + ' (' + esc( selectedWcUser.email ) + ')' ); $( '#woodoo-odoo-col' ).show(); $( '#woodoo-odoo-partner-search' ).val( '' ).focus(); $( '#woodoo-odoo-partner-results' ).html( '' ); } ); // ── Step 2: search Odoo partners ───────────────────────────────────── function searchOdooPartners() { if ( ! selectedWcUser ) return; const q = $( '#woodoo-odoo-partner-search' ).val().trim(); if ( q.length < 2 ) return; const $results = $( '#woodoo-odoo-partner-results' ); $results.html( '' + esc( i18n.searching ) + '' ); ajax( 'woodoo_search_partners', { query: q } ) .done( function ( res ) { if ( ! res.success || ! res.data.length ) { $results.html( '' + esc( i18n.no_odoo_partners ) + '
' ); return; } let html = '| Name | Odoo Email | Type | ' + ' |
|---|---|---|---|
| ' + esc( p.name ) + ' | ' + '' + esc( p.email || '—' ) + ' | ' + '' + ( p.is_company ? 'Company' : 'Individual' ) + ' | ' + '' + ' |
' + esc( i18n.error ) + '
' ); } ); } $( '#woodoo-odoo-partner-search-btn' ).on( 'click', searchOdooPartners ); $( '#woodoo-odoo-partner-search' ).on( 'keypress', function ( e ) { if ( e.which === 13 ) { e.preventDefault(); searchOdooPartners(); } } ); // ── Confirm link: save WC user ↔ Odoo partner ──────────────────────── $( document ).on( 'click', '.woodoo-confirm-link', function () { if ( ! selectedWcUser ) return; const $btn = $( this ); const partnerId = $btn.data( 'partner-id' ); const partnerName = $btn.data( 'partner-name' ); $btn.prop( 'disabled', true ).text( 'Saving…' ); ajax( 'woodoo_link_customer', { user_id : selectedWcUser.id, partner_id : partnerId, } ) .done( function ( res ) { if ( res.success ) { // Update the partner status cell in the WC customers table const $row = $( '#woo-customer-results tr[data-user-id="' + selectedWcUser.id + '"]' ); $row.find( '.woodoo-partner-status' ).html( badge( 'green', partnerName ) ); $row.find( '.woodoo-link-actions' ).html( '' + ' ' ); $row.css( 'background', '#ecfdf5' ); // Success feedback in step 2 $( '#woodoo-odoo-partner-results' ).prepend( '✓ ' + esc( i18n.link_done ) + ': ' + esc( selectedWcUser.name ) + ' → ' + esc( partnerName ) + '
' ); $btn.text( 'Linked!' ); } else { $btn.prop( 'disabled', false ).text( 'Link' ); alert( res.data || i18n.error ); } } ) .fail( function () { $btn.prop( 'disabled', false ).text( 'Link' ); alert( i18n.error ); } ); } ); // ── Unlink ──────────────────────────────────────────────────────────── $( document ).on( 'click', '.woodoo-unlink-user', function () { if ( ! confirm( i18n.unlink_confirm ) ) return; const $btn = $( this ); const userId = $btn.data( 'id' ); $btn.prop( 'disabled', true ); ajax( 'woodoo_unlink_customer', { user_id: userId } ) .done( function ( res ) { if ( res.success ) { const $row = $( '#woo-customer-results tr[data-user-id="' + userId + '"]' ); $row.find( '.woodoo-partner-status' ).html( badge( 'grey', i18n.not_linked ) ); $row.find( '.woodoo-link-actions button.woodoo-unlink-user' ).remove(); $row.css( 'background', '' ); if ( selectedWcUser && selectedWcUser.id == userId ) { $( '#woodoo-odoo-col' ).hide(); selectedWcUser = null; } } else { $btn.prop( 'disabled', false ); alert( res.data || i18n.error ); } } ) .fail( function () { $btn.prop( 'disabled', false ); } ); } ); // ══════════════════════════════════════════════════════════════════════ // USER PROFILE PAGE – partner search // ══════════════════════════════════════════════════════════════════════ $( '#woodoo-partner-search-btn' ).on( 'click', function () { const $input = $( '#woodoo-partner-search-input' ); const $results = $( '#woodoo-partner-search-results' ); const q = $input.val().trim(); const userId = $input.data( 'user-id' ); if ( ! q ) return; $results.html( '' + esc( i18n.searching ) + '' ); ajax( 'woodoo_search_partners', { query: q } ) .done( function ( res ) { if ( ! res.success || ! res.data.length ) { $results.html( '' + esc( i18n.no_odoo_partners ) + '' ); return; } let html = '