223 lines
6.8 KiB
JavaScript
223 lines
6.8 KiB
JavaScript
// Initialize components on document ready
|
|
$(function() {
|
|
initializeUI();
|
|
handleMessageCounts();
|
|
handleFormSubmissions();
|
|
setupTemplateSelections();
|
|
handleClientGroups();
|
|
initializeRecentMessages();
|
|
});
|
|
|
|
// Initialize UI components
|
|
function initializeUI() {
|
|
$('[data-toggle="tooltip"]').tooltip();
|
|
$('.select2').select2();
|
|
$('#client_groups').select2({
|
|
placeholder: app.lang.select_client_groups,
|
|
allowClear: true
|
|
});
|
|
}
|
|
|
|
// Handle message character counting
|
|
function handleMessageCounts() {
|
|
function updateCharCount(textarea, charCount, msgCount) {
|
|
var chars = $(textarea).val().length;
|
|
$(charCount).text(chars);
|
|
$(msgCount).text(Math.ceil(chars / 160));
|
|
}
|
|
|
|
$('#message').on('keyup', function() {
|
|
updateCharCount(this, '#char_count', '#messages_count');
|
|
});
|
|
|
|
$('#message_bulk').on('keyup', function() {
|
|
updateCharCount(this, '#char_count_bulk', '#messages_count_bulk');
|
|
});
|
|
}
|
|
|
|
// Handle form submissions
|
|
function handleFormSubmissions() {
|
|
$('#sms-form').on('submit', function() {
|
|
var $btn = $('#sendSmsBtn').prop('disabled', true);
|
|
$btn.html('<i class="fa fa-spinner fa-spin"></i> ' + app.lang.sending);
|
|
});
|
|
|
|
$('#bulk-sms-form').on('submit', function() {
|
|
if (!confirm(app.lang.bulk_sms_confirm)) {
|
|
return false;
|
|
}
|
|
var $btn = $('#sendBulkSmsBtn').prop('disabled', true);
|
|
$btn.html('<i class="fa fa-spinner fa-spin"></i> ' + app.lang.sending);
|
|
});
|
|
|
|
// Handle test SMS
|
|
$('#send_test_sms').on('click', function() {
|
|
handleTestSMS($(this));
|
|
});
|
|
}
|
|
|
|
// Setup template selections
|
|
function setupTemplateSelections() {
|
|
function handleTemplateSelection(templateId, messageField, charCount, msgCount) {
|
|
if (templateId) {
|
|
$.get(admin_url + 'hubtel_sms/get_template/' + templateId, function(response) {
|
|
if (response.success) {
|
|
$(messageField).val(response.template.template);
|
|
updateCharCount(messageField, charCount, msgCount);
|
|
}
|
|
}, 'json');
|
|
}
|
|
}
|
|
|
|
$('#template_id').on('change', function() {
|
|
handleTemplateSelection($(this).val(), '#message', '#char_count', '#messages_count');
|
|
});
|
|
|
|
$('#template_id_bulk').on('change', function() {
|
|
handleTemplateSelection($(this).val(), '#message_bulk', '#char_count_bulk', '#messages_count_bulk');
|
|
});
|
|
}
|
|
|
|
// Handle client groups selection
|
|
function handleClientGroups() {
|
|
$('#client_groups').on('change', function() {
|
|
var selectedGroups = $(this).val();
|
|
if (selectedGroups) {
|
|
$.get(admin_url + 'hubtel_sms/get_recipients_preview', {
|
|
groups: selectedGroups
|
|
}, function(response) {
|
|
$('#recipients_preview').html(response.html);
|
|
$('#recipients_count').text(response.count);
|
|
}, 'json');
|
|
} else {
|
|
$('#recipients_preview').html('');
|
|
$('#recipients_count').text('0');
|
|
}
|
|
});
|
|
}
|
|
|
|
// Initialize recent messages
|
|
function initializeRecentMessages() {
|
|
function loadRecentMessages() {
|
|
$.get(admin_url + 'hubtel_sms/get_recent_messages', function(response) {
|
|
if (response.success) {
|
|
renderRecentMessages(response.messages);
|
|
}
|
|
});
|
|
}
|
|
|
|
loadRecentMessages();
|
|
setInterval(loadRecentMessages, 30000);
|
|
}
|
|
|
|
// Modal Functions
|
|
function send_sms_modal() {
|
|
$('#send_sms_modal').modal('show');
|
|
$('#send_sms_modal').find('form')[0].reset();
|
|
$('#template_id').val('').trigger('change');
|
|
resetCounters('#char_count', '#messages_count');
|
|
}
|
|
|
|
function bulk_sms_modal() {
|
|
$('#bulk_sms_modal').modal('show');
|
|
$('#bulk_sms_modal').find('form')[0].reset();
|
|
$('#client_groups').val('').trigger('change');
|
|
$('#template_id_bulk').val('').trigger('change');
|
|
resetCounters('#char_count_bulk', '#messages_count_bulk');
|
|
$('#recipients_preview').html('');
|
|
$('#recipients_count').text('0');
|
|
}
|
|
|
|
function view_message(id) {
|
|
showLoadingModal('#message_details', '#view_message_modal');
|
|
$.get(admin_url + 'hubtel_sms/view_message/' + id, function(response) {
|
|
$('#message_details').html(response);
|
|
});
|
|
}
|
|
|
|
function preview_template(id) {
|
|
showLoadingModal('#template_preview', '#preview_template_modal');
|
|
$.get(admin_url + 'hubtel_sms/preview_template/' + id, function(response) {
|
|
$('#template_preview').html(response);
|
|
});
|
|
}
|
|
|
|
// Helper Functions
|
|
function resetCounters(charCount, msgCount) {
|
|
$(charCount).text('0');
|
|
$(msgCount).text('1');
|
|
}
|
|
|
|
function showLoadingModal(contentSelector, modalSelector) {
|
|
$(contentSelector).html('<div class="text-center"><i class="fa fa-spinner fa-spin fa-2x"></i></div>');
|
|
$(modalSelector).modal('show');
|
|
}
|
|
|
|
function handleTestSMS($btn) {
|
|
var number = $('#test_number').val();
|
|
|
|
if (!number) {
|
|
alert_float('warning', app.lang.enter_phone_number);
|
|
return;
|
|
}
|
|
|
|
$btn.prop('disabled', true)
|
|
.html('<i class="fa fa-spinner fa-spin"></i> ' + app.lang.sending);
|
|
|
|
$.post(admin_url + 'hubtel_sms/send_test', {
|
|
number: number
|
|
}, function(response) {
|
|
handleTestSMSResponse(response, $btn);
|
|
}).fail(function(xhr, status, error) {
|
|
alert_float('danger', 'Failed to send test SMS: ' + error);
|
|
resetTestButton($btn);
|
|
});
|
|
}
|
|
|
|
function handleTestSMSResponse(response, $btn) {
|
|
if (response.success) {
|
|
alert_float('success', response.message);
|
|
$('#test_number').val('');
|
|
} else {
|
|
alert_float('danger', response.message);
|
|
}
|
|
resetTestButton($btn);
|
|
}
|
|
|
|
function resetTestButton($btn) {
|
|
$btn.prop('disabled', false).html(app.lang.send_test_sms);
|
|
}
|
|
|
|
function resend_sms(id) {
|
|
if (confirm(app.lang.confirm_action_prompt)) {
|
|
$.get(admin_url + 'hubtel_sms/resend/' + id, function(response) {
|
|
if (response.success) {
|
|
alert_float('success', response.message);
|
|
location.reload();
|
|
} else {
|
|
alert_float('danger', response.message);
|
|
}
|
|
}, 'json');
|
|
}
|
|
}
|
|
|
|
function formatCurrency(amount) {
|
|
return app.options.currency_symbol + parseFloat(amount).toFixed(2);
|
|
}
|
|
|
|
function renderRecentMessages(messages) {
|
|
var html = messages.map(function(message) {
|
|
return `
|
|
<tr>
|
|
<td>${moment(message.date_sent).format('YYYY-MM-DD HH:mm')}</td>
|
|
<td>
|
|
<span class="label label-${message.status === 'sent' ? 'success' : 'danger'}">
|
|
${message.status}
|
|
</span>
|
|
</td>
|
|
<td>${message.rate ? formatCurrency(message.rate) : '-'}</td>
|
|
</tr>
|
|
`;
|
|
}).join('');
|
|
$('#recent_messages').html(html);
|
|
} |