diff --git a/admin/class-twp-admin.php b/admin/class-twp-admin.php index 210c5e0..4876ba7 100644 --- a/admin/class-twp-admin.php +++ b/admin/class-twp-admin.php @@ -5598,13 +5598,22 @@ class TWP_Admin { - @@ -6064,6 +6073,7 @@ class TWP_Admin { $('#answer-btn').hide(); $('#hangup-btn').show(); $('#phone-controls-extra').show(); + $('#admin-call-controls-panel').show(); startCallTimer(); }); @@ -6075,8 +6085,13 @@ class TWP_Admin { $('#answer-btn').hide(); $('#call-btn').show(); $('#phone-controls-extra').hide(); + $('#admin-call-controls-panel').hide(); $('#call-timer').hide(); stopCallTimer(); + + // Reset button states + $('#admin-hold-btn').text('Hold').removeClass('btn-active'); + $('#admin-record-btn').text('Record').removeClass('btn-active'); }); // Call rejected @@ -6287,6 +6302,31 @@ class TWP_Admin { } }); + // Admin call control buttons + $('#admin-hold-btn').on('click', function() { + if (currentCall) { + adminToggleHold(); + } + }); + + $('#admin-transfer-btn').on('click', function() { + if (currentCall) { + adminShowTransferDialog(); + } + }); + + $('#admin-requeue-btn').on('click', function() { + if (currentCall) { + adminShowRequeueDialog(); + } + }); + + $('#admin-record-btn').on('click', function() { + if (currentCall) { + adminToggleRecording(); + } + }); + // Check if SDK loaded and initialize $(window).on('load', function() { setTimeout(function() { @@ -6431,6 +6471,345 @@ class TWP_Admin { button.prop('disabled', false).text('Save Changes'); }); }); + + // Admin call control functions + var adminIsOnHold = false; + var adminIsRecording = false; + var adminRecordingSid = null; + + function adminToggleHold() { + if (!currentCall) return; + + var callSid = currentCall.parameters.CallSid || currentCall.customParameters.CallSid; + var $holdBtn = $('#admin-hold-btn'); + + $.post(ajaxurl, { + action: 'twp_toggle_hold', + call_sid: callSid, + hold: !adminIsOnHold, + nonce: '' + }, function(response) { + if (response.success) { + adminIsOnHold = !adminIsOnHold; + if (adminIsOnHold) { + $holdBtn.html(' Unhold').addClass('btn-active'); + showNotice('Call placed on hold', 'info'); + } else { + $holdBtn.html(' Hold').removeClass('btn-active'); + showNotice('Call resumed', 'info'); + } + } else { + showNotice('Failed to toggle hold: ' + (response.data || 'Unknown error'), 'error'); + } + }).fail(function() { + showNotice('Failed to toggle hold', 'error'); + }); + } + + function adminShowTransferDialog() { + if (!currentCall) return; + + // Load available agents + $.post(ajaxurl, { + action: 'twp_get_online_agents', + nonce: '' + }, function(response) { + if (response.success && response.data.length > 0) { + adminShowAgentTransferDialog(response.data); + } else { + adminShowManualTransferDialog(); + } + }).fail(function() { + adminShowManualTransferDialog(); + }); + } + + function adminShowAgentTransferDialog(agents) { + var agentOptions = '
'; + + agents.forEach(function(agent) { + var statusClass = agent.is_available ? 'available' : 'busy'; + var statusText = agent.is_available ? '🟢 Available' : '🔴 Busy'; + var methodIcon = agent.has_phone ? '📱' : '💻'; + + agentOptions += '
'; + agentOptions += '
' + agent.name + ' ' + methodIcon + '
'; + agentOptions += '
' + statusText + '
'; + agentOptions += '
'; + }); + + agentOptions += '
'; + + var dialogHtml = '
'; + dialogHtml += '

Transfer Call to Agent

'; + dialogHtml += '

Select an agent to transfer this call to:

'; + dialogHtml += agentOptions; + dialogHtml += '

Or enter phone number manually:

'; + dialogHtml += ''; + dialogHtml += '
'; + dialogHtml += ''; + dialogHtml += ''; + dialogHtml += '
'; + dialogHtml += '
'; + dialogHtml += '
'; + + $('body').append(dialogHtml); + + var selectedAgent = null; + + $('.agent-option').on('click', function() { + $('.agent-option').css('background', ''); + $(this).css('background', '#e7f3ff'); + selectedAgent = { + id: $(this).data('agent-id'), + method: $(this).data('transfer-method'), + value: $(this).data('transfer-value') + }; + $('#admin-transfer-manual').val(''); + $('#admin-confirm-transfer').prop('disabled', false); + }); + + $('#admin-transfer-manual').on('input', function() { + var number = $(this).val().trim(); + if (number) { + $('.agent-option').css('background', ''); + selectedAgent = null; + $('#admin-confirm-transfer').prop('disabled', false); + } else { + $('#admin-confirm-transfer').prop('disabled', !selectedAgent); + } + }); + + $('#admin-confirm-transfer').on('click', function() { + var manualNumber = $('#admin-transfer-manual').val().trim(); + if (manualNumber) { + adminTransferCall(manualNumber); + } else if (selectedAgent) { + adminTransferToAgent(selectedAgent); + } + }); + + $('#admin-cancel-transfer, #admin-transfer-overlay').on('click', function() { + adminHideTransferDialog(); + }); + } + + function adminShowManualTransferDialog() { + var dialogHtml = '
'; + dialogHtml += '

Transfer Call

'; + dialogHtml += '

Enter the phone number to transfer this call:

'; + dialogHtml += ''; + dialogHtml += '
'; + dialogHtml += ''; + dialogHtml += ''; + dialogHtml += '
'; + dialogHtml += '
'; + dialogHtml += '
'; + + $('body').append(dialogHtml); + + $('#admin-confirm-transfer').on('click', function() { + var number = $('#admin-transfer-number').val().trim(); + if (number) { + adminTransferCall(number); + } + }); + + $('#admin-cancel-transfer, #admin-transfer-overlay').on('click', function() { + adminHideTransferDialog(); + }); + } + + function adminTransferCall(phoneNumber) { + if (!currentCall) return; + + var callSid = currentCall.parameters.CallSid || currentCall.customParameters.CallSid; + + $.post(ajaxurl, { + action: 'twp_transfer_call', + call_sid: callSid, + agent_number: phoneNumber, + nonce: '' + }, function(response) { + if (response.success) { + showNotice('Call transferred successfully', 'success'); + adminHideTransferDialog(); + if (currentCall) { + currentCall.disconnect(); + } + } else { + showNotice('Failed to transfer call: ' + (response.data || 'Unknown error'), 'error'); + } + }).fail(function() { + showNotice('Failed to transfer call', 'error'); + }); + } + + function adminTransferToAgent(agent) { + if (!currentCall) return; + + var callSid = currentCall.parameters.CallSid || currentCall.customParameters.CallSid; + + $.post(ajaxurl, { + action: 'twp_transfer_to_agent_queue', + call_sid: callSid, + agent_id: agent.id, + transfer_method: agent.method, + transfer_value: agent.value, + nonce: '' + }, function(response) { + if (response.success) { + showNotice('Call transferred successfully', 'success'); + adminHideTransferDialog(); + if (currentCall) { + currentCall.disconnect(); + } + } else { + showNotice('Failed to transfer call: ' + (response.data || 'Unknown error'), 'error'); + } + }).fail(function() { + showNotice('Failed to transfer call', 'error'); + }); + } + + function adminHideTransferDialog() { + $('#admin-transfer-dialog, #admin-transfer-overlay').remove(); + } + + function adminShowRequeueDialog() { + if (!currentCall) return; + + $.post(ajaxurl, { + action: 'twp_get_all_queues', + nonce: '' + }, function(response) { + if (response.success && response.data.length > 0) { + var options = ''; + response.data.forEach(function(queue) { + options += ''; + }); + + var dialogHtml = '
'; + dialogHtml += '

Requeue Call

'; + dialogHtml += '

Select a queue to transfer this call to:

'; + dialogHtml += ''; + dialogHtml += '
'; + dialogHtml += ''; + dialogHtml += ''; + dialogHtml += '
'; + dialogHtml += '
'; + dialogHtml += '
'; + + $('body').append(dialogHtml); + + $('#admin-confirm-requeue').on('click', function() { + var queueId = $('#admin-requeue-select').val(); + if (queueId) { + adminRequeueCall(queueId); + } + }); + + $('#admin-cancel-requeue, #admin-requeue-overlay').on('click', function() { + $('#admin-requeue-dialog, #admin-requeue-overlay').remove(); + }); + } else { + showNotice('No queues available', 'error'); + } + }).fail(function() { + showNotice('Failed to load queues', 'error'); + }); + } + + function adminRequeueCall(queueId) { + if (!currentCall) return; + + var callSid = currentCall.parameters.CallSid || currentCall.customParameters.CallSid; + + $.post(ajaxurl, { + action: 'twp_requeue_call', + call_sid: callSid, + queue_id: queueId, + nonce: '' + }, function(response) { + if (response.success) { + showNotice('Call requeued successfully', 'success'); + $('#admin-requeue-dialog, #admin-requeue-overlay').remove(); + if (currentCall) { + currentCall.disconnect(); + } + } else { + showNotice('Failed to requeue call: ' + (response.data || 'Unknown error'), 'error'); + } + }).fail(function() { + showNotice('Failed to requeue call', 'error'); + }); + } + + function adminToggleRecording() { + if (!currentCall) return; + + if (adminIsRecording) { + adminStopRecording(); + } else { + adminStartRecording(); + } + } + + function adminStartRecording() { + var callSid = currentCall.parameters.CallSid || currentCall.customParameters.CallSid; + var $recordBtn = $('#admin-record-btn'); + + $.post(ajaxurl, { + action: 'twp_start_recording', + call_sid: callSid, + nonce: '' + }, function(response) { + if (response.success) { + adminIsRecording = true; + adminRecordingSid = response.data.recording_sid; + $recordBtn.html(' Stop Recording').addClass('btn-active'); + showNotice('Recording started', 'success'); + } else { + showNotice('Failed to start recording: ' + (response.data || 'Unknown error'), 'error'); + } + }).fail(function() { + showNotice('Failed to start recording', 'error'); + }); + } + + function adminStopRecording() { + if (!adminRecordingSid) return; + + var callSid = currentCall ? (currentCall.parameters.CallSid || currentCall.customParameters.CallSid) : ''; + var $recordBtn = $('#admin-record-btn'); + + $.post(ajaxurl, { + action: 'twp_stop_recording', + call_sid: callSid, + recording_sid: adminRecordingSid, + nonce: '' + }, function(response) { + if (response.success) { + adminIsRecording = false; + adminRecordingSid = null; + $recordBtn.html(' Record').removeClass('btn-active'); + showNotice('Recording stopped', 'info'); + } else { + showNotice('Failed to stop recording: ' + (response.data || 'Unknown error'), 'error'); + } + }).fail(function() { + showNotice('Failed to stop recording', 'error'); + }); + } + + function showNotice(message, type) { + var noticeClass = type === 'error' ? 'notice-error' : (type === 'success' ? 'notice-success' : 'notice-info'); + var notice = $('

' + message + '

'); + $('.browser-phone-container').prepend(notice); + setTimeout(function() { + notice.fadeOut(); + }, 4000); + } });