/* WooDoo Admin JS */ jQuery( function ( $ ) { const cfg = window.WooDooAdmin || {}; // ── 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( cfg.i18n.testing ); $result.html( '' ); $.post( cfg.ajax_url, { action : 'woodoo_test_connection', nonce : cfg.nonce, } ) .done( function ( res ) { if ( res.success ) { const d = res.data; const cls = d.success ? 'success' : 'error'; $result.html( '' + escHtml( d.message ) + '' ); } else { $result.html( '' + escHtml( res.data || cfg.i18n.error ) + '' ); } } ) .fail( function () { $result.html( '' + escHtml( cfg.i18n.error ) + '' ); } ) .always( function () { $btn.prop( 'disabled', false ).text( 'Test Connection' ); } ); } ); // ── Partner search on user profile ────────────────────────────────── $( '#woodoo-partner-search-btn' ).on( 'click', function () { const $input = $( '#woodoo-partner-search-input' ); const $results = $( '#woodoo-partner-search-results' ); const query = $input.val().trim(); const userId = $input.data( 'user-id' ); if ( ! query ) return; $results.html( '' + escHtml( cfg.i18n.searching ) + '' ); $.post( cfg.ajax_url, { action : 'woodoo_search_partners', nonce : cfg.nonce, query : query, } ) .done( function ( res ) { if ( ! res.success || ! res.data.length ) { $results.html( 'No partners found.' ); return; } let html = ''; $results.html( html ); } ) .fail( function () { $results.html( '' + escHtml( cfg.i18n.error ) + '' ); } ); } ); // ── Pick partner from search results ──────────────────────────────── $( document ).on( 'click', '.woodoo-pick-partner', function () { const $btn = $( this ); const partnerId = $btn.data( 'id' ); const name = $btn.data( 'name' ); const userId = $btn.data( 'user-id' ); $( '#woodoo_odoo_partner_id' ).val( partnerId ); $( '#woodoo-partner-name-display' ).text( name ).show(); $( '#woodoo-partner-search-results' ).html( '' + escHtml( cfg.i18n.link_done ) + ' → ' + escHtml( name ) + ' (#' + partnerId + ')' ); // Persist immediately if we have the AJAX handler if ( userId ) { $.post( cfg.ajax_url, { action : 'woodoo_link_customer', nonce : cfg.nonce, user_id : userId, partner_id : partnerId, } ); } } ); // ── Utility ────────────────────────────────────────────────────────── function escHtml( str ) { return String( str ) .replace( /&/g, '&' ) .replace( //g, '>' ) .replace( /"/g, '"' ); } function escAttr( str ) { return escHtml( str ).replace( /'/g, ''' ); } } );