

$(document).ready(function() {

  $('button.newDate').click(function(){

    var customerID = $(this).data('id');
    var backDateButton = $("button#backDateButton").attr("data-id", +customerID);
    
    });
    ////======
    $('button#backDateButton').click(function(e){
        e.preventDefault();
        
    $(this).text('Updating..');
    $(this).attr('disabled', 'disabled');
    
    var customerID = $(this).data('id');
    var NewTime = $('input#NewTime').val();
    var NewDate = $('input#NewDate').val();
    var Format = $('select#Format').val();
    
      $.post('/personal/admin/cPanelScript/updateDate.php',{customerID:customerID, NewTime:NewTime, NewDate:NewDate, Format:Format}, function(data, status) {
    
         $('button#backDateButton').text('Back Date');
         $('button#backDateButton').removeAttr('disabled');
         if (data == "<p style='text-align: center; color: green;'>SUCCESS</p>") {
             alert("Date changed successfully");
    
                window.location.href="/personal/admin/";
         }else {
             alert("Unknown Error Occurred, Please try again");
         }
      });
    
    
    });
    
    ///========
  
//======= COMPLETE TRANSACTIONS =============================
$('button#completeTrans').on("click", function(){
$('form#completeTrans').submit();  
});
// FORM ON SUBMIT ==================================
$('form#completeTrans').on("submit", function(e){
e.preventDefault();

var thisButton = $('button#completeTrans');
thisButton.text('Completing..');
thisButton.attr('disabled', 'disabled');

var transactionID = $(this).find('select#TransID').val();
var ActCode = $(this).find('input#ActCode').val();

$.post('/personal/admin/cPanelScript/updateTrans.php',{transactionID:transactionID, ActCode:ActCode}, function(data) {

thisButton.text('Complete');
thisButton.removeAttr('disabled');


if ($.trim(data) == "success") {
swal("Success", "You've successfully permitted that transaction", "success");
setInterval(() => {
location.reload();
}, 5000);
}else if($.trim(data) == "already") {
swal("Done", "That transaction is already permitted", "info");

}else {
swal("Unknown Error", "An Error has occured, Could not complete that transaction", "error");
}
});


});


// ==============================================================================================

$('button.openupdateModal').click(function() {
var controlfix = $(this).closest('td').find('.controlFixed');
controlfix.fadeIn(600, function() {
$('.givebillingCode').addClass('wow bounceIn animated');
$('button.cancelUpdateAcc').click(function() {
$('.controlFixed').fadeOut(600, function() {
$('.givebillingCode').removeClass('wow bounceIn animated');
});
});

});




});
$('button.updateBal').click(function(){
$(this).text('Updating..');
$(this).attr('disabled', 'disabled');
var customerID = $(this).data('id');
var Balance = $(this).closest('.controlFixed').find('input.newAccBalance').val();
var senderName = $(this).closest('.controlFixed').find('input.senderName').val();

$.post('/personal/admin/cPanelScript/updateBal.php',{customerID:customerID, Balance:Balance, senderName:senderName}, function(data, status) {
$('.operateSucesss').html(data);
$('button.updateBal').text('Save');
$('button.updateBal').removeAttr('disabled');
if (data == "<p style='text-align: center; color: green;'>SUCCESS</p>") {
window.location.href="/personal/admin/";
$('.controlFixed').fadeOut(600, function() {
$('.givebillingCode').removeClass('wow bounceIn animated');
});

}
});


});


$('button.DeleteCustomer').click(function() {
$(this).html('<i class="fa fa-ellipsis-h"></i>').fadeIn(300);
var CustomerID = $(this).data('id');
if (confirm('Are you sure you want to delete this customer ?')) {
$.post('/personal/admin/cPanelScript/deleteCustomer.php', {CustomerID:CustomerID}, function(data, status) {
$('#deleteCustomerData').html(data);

if (data == 'SUCCESS') {
$('.operateSucesss').html('');
alert("You've successfully deleted that customer");
window.location.href="/personal/admin/";
}else {
alert('An Error occured, Could not delete customer');
}
}); 

}else {
$('button.DeleteCustomer').html('<i class="fa fa-times"></i>');
}


});

//====================== TRANSACTION DETAILS TOGGLE =========================================
$('ul.transactions').click(function() {
$(this).find('li.moreInfo ul.transactionChildDetails').toggleClass('Open');
});

$('#AdminLogin').click(function(){
$(this).text('Please wait..');
$(this).attr('disabled', 'disabled');
var adminID = $('input#adminID').val();
var adminPassword = $('input#adminPassword').val();
$.post('/personal/admin/cPanelScript/AdminLogin.php', {adminID:adminID, adminPassword:adminPassword}, function(data, status) {
$('.data').html(data);
$('#AdminLogin').text('Login');
$('#AdminLogin').removeAttr('disabled');
}); 


});

});