NonInventPurchasingSystem/CPRNIMS.WebApps/wwwroot/JsFunctions/SMTP/SMTP.js
2026-01-20 07:44:30 +08:00

225 lines
8.1 KiB
JavaScript

var UserRights;
var prTable;
var emailTable;
var SMTPCredentialId;
var smtpCredentialId;
function hasRolePermission(role) {
return UserRights.includes(role);
}
function renderItembtns(data, row) {
var jsonData = JSON.stringify(row).replace(/"/g, """);
var buttonsHtml = '';
SMTPCredentialId = jsonData.smtpCredentialId
// var statusNumber = parseInt(row.status, 10);
buttonsHtml += '<button onclick="viewSmtpDetail(' + jsonData + ')" class="btn btn-default">' +
'<i class="fa-solid fa-eye fa-xl" style="color: #008080;" aria-hidden="true"></i>' +
'</button > ';
return buttonsHtml;
}
function postPutSmtp(isUpdate) {
var loader = $('#overlay, #loader');
const smtpCredentialNameInput = document.getElementById('smtpCredentialName');
const senderEmailInput = document.getElementById('senderEmail');
const senderUserNameInput = document.getElementById('senderUserName');
const senderDisplayNameInput = document.getElementById('senderDisplayName');
const passwordInput = document.getElementById('password');
const serverInput = document.getElementById('server');
const outgoingPortInput = document.getElementById('outgoingPort');
const incomingPortInput = document.getElementById('incomingPort');
const isActiveInput = document.getElementById('isActive');
const SMTPCredentialName = smtpCredentialNameInput.value;
const SenderEmail = senderEmailInput.value;
const SenderUserName = senderUserNameInput.value;
const SenderDisplayName = senderDisplayNameInput.value;
const Password = passwordInput.value;
const Server = serverInput.value;
const OutgoingPort = outgoingPortInput.value;
const IncomingPort = incomingPortInput.value;
const IsActive = isActiveInput.value === "true"; // Convert to boolean
if (!SMTPCredentialName || !SenderEmail || !Password || !SenderUserName || !SenderDisplayName || !Server || !OutgoingPort || !IncomingPort || isActiveInput.value === "") {
alert('Please fill the required fields!');
if (!SMTPCredentialName) {
smtpCredentialNameInput.classList.add('error-input');
}
if (!SenderEmail) {
senderEmailInput.classList.add('error-input');
}
if (!SenderUserName) {
senderUserNameInput.classList.add('error-input');
}
if (!SenderDisplayName) {
senderDisplayNameInput.classList.add('error-input');
}
if (!Server) {
serverInput.classList.add('error-input');
}
if (!Password) {
passwordInput.classList.add('error-input');
}
if (!OutgoingPort) {
outgoingPortInput.classList.add('error-input');
}
if (!IncomingPort) {
incomingPortInput.classList.add('error-input');
}
if (isActiveInput.value === "") {
isActiveInput.classList.add('error-input');
}
return;
}
console.log('IsActive', IsActive);
const confirmation = confirm('Are you sure you want to proceed?');
if (confirmation) {
$.ajax({
url: '/SMTPMgmt/PostPutSmtp',
type: 'POST',
data: { SMTPCredentialId, SMTPCredentialName, SenderEmail, SenderUserName, Password, SenderDisplayName, Server, OutgoingPort, IncomingPort, IsActive },
success: function (response) {
if (response.success) {
emailTable.ajax.reload();
$('#viewSmtpDetails').modal('hide');
alert('Successfully updated!');
} else {
emailTable.ajax.reload();
alert('Failed: ' + response.response);
}
},
beforeSend: function () {
loader.show();
},
complete: function () {
loader.hide();
}
});
}
}
function viewSmtpDetail(data) {
var loader = $('#overlay, #loader');
SMTPCredentialId = data.smtpCredentialId;
if (!SMTPCredentialId) {
// Display the modal
$('#viewSmtpDetails').modal('show');
// Set the z-index of viewArtWorkDetails modal
$('#viewSmtpDetails').css('z-index', 1050);
} else {
$.ajax({
url: '/SMTPMgmt/GetMySMTP',
type: 'POST',
data: { SMTPCredentialId: SMTPCredentialId }, // Assuming SMTPCredentialId is the UserId
beforeSend: function () {
loader.show();
},
complete: function () {
loader.hide();
},
success: function (data) {
if (data && data.data && data.data.length > 0) {
var item = data.data[0];
$('#smtpCredentialName').val(item.smtpCredentialName);
$('#senderEmail').val(item.senderEmail);
$('#senderUserName').val(item.senderUserName);
$('#senderDisplayName').val(item.senderDisplayName);
$('#password').val(item.password);
$('#server').val(item.server);
$('#outgoingPort').val(item.outgoingPort);
$('#incomingPort').val(item.incomingPort);
$('#isActive').val(item.isActive ? 'true' : 'false');
$('#smtpCredentialId').val(item.smtpCredentialId);
$('#viewSmtpDetails').modal('show');
} else {
console.log('Data is null or undefined');
}
},
error: function (xhr, error, thrown) {
console.log('Authentication error:', error);
console.log('Details:', thrown);
window.location.href = '/Home/Logout';
}
});
}
}
$(document).ready(function () {
var loader = $('#overlay, #loader');
UserRights = document.getElementById("roleRights").value;
emailTable = $('#EmailTable').DataTable({
ajax: {
url: '/SMTPMgmt/GetAllSmtp', // Replace with your API endpoint
type: 'GET',
beforeSend: function () {
// Show the loader before making the AJAX request
loader.show();
},
complete: function () {
loader.hide();
}
},
// Check for the "No Data" response and display the message
initComplete: function () {
var api = this.api();
var data = api.ajax.json();
if (!data || !data.data || data.data === "No Data") {
// Display the "No record available" message
$('.dataTables_empty').html("No record available");
}
},
columns: [
{ data: 'smtpCredentialName' },
{ data: 'senderEmail' },
{ data: 'senderUserName' },
{ data: 'senderDisplayName' },
{ data: 'server' },
{ data: 'outgoingPort' },
{ data: 'incomingPort' },
{ data: 'isActive' },
{
data: null,
render: function (data, type, row) {
return renderItembtns(data, row);
}
},
],
responsive: true,
rowCallback: function (row, data) {
var statusCell = $('td:eq(7)', row);
var myStatus = statusCell.text().trim();
// Check if myStatus is 'Active', 'true' or true
if (myStatus === 'Active' || myStatus === 'true' || myStatus === true) {
statusCell.text('Active').addClass('status-active');
} else {
statusCell.text('InActive').addClass('status-partial');
}
},
language: {
emptyTable: "No record available"
},
error: function (xhr, error, thrown) {
console.log('DataTables error:', error);
console.log('Status:', Status);
console.log('Details:', xhr.responseText);
window.location.href = '/Home/Logout';
}
});
})
function removeErrorClass() {
if (this.value.trim() !== '') {
this.classList.remove('error-input');
}
}