$(document).on("change", "select", function(e) { send( "/set-lang/", { token: $(this).attr("data-token"), lang: $(this).find(":selected").text() }, function () { return 0; } ) }); $('#save-crm').on("submit", function(e) { e.preventDefault(); let formData = formDataToObj($(this).serializeArray()); disableForm($(this)); send( $(this).attr('action'), formData, function (data) { sessionStorage.setItem("createdMsg", data.message); document.location.replace( location.protocol.concat("//").concat(window.location.host) + data.url ); } ) }); $("#save").on("submit", function(e) { e.preventDefault(); let formData = formDataToObj($(this).serializeArray()); disableForm($(this)); send( $(this).attr('action'), formData, function (data) { M.toast({ html: data.message, displayLength: 1000, completeCallback: function(){ enableForm(); } }); } ) }); $("#add-bot").on("submit", function(e) { e.preventDefault(); disableForm($(this)); send( $(this).attr('action'), { connectionId: parseInt($(this).find('input[name=connectionId]').val()), token: $(this).find('input[name=token]').val(), }, function (data) { let bots = $("#bots"); if (bots.hasClass("hide")) { bots.removeClass("hide") } $("#bots tbody").append(getBotTemplate(data)); $("#token").val(""); $('select').formSelect(); enableForm(); } ) }); $(document).on("click", ".delete-bot", function(e) { e.preventDefault(); var but = $(this); var confirmText = JSON.parse(sessionStorage.getItem("confirmText")); but.addClass('disabled'); $.confirm({ title: false, content: confirmText["text"], useBootstrap: false, boxWidth: '30%', type: 'blue', backgroundDismiss: false, backgroundDismissAnimation: 'shake', buttons: { confirm: { text: confirmText["confirm"], action: function () { send("/delete-bot/", { token: but.attr("data-token"), connectionId: parseInt($('input[name=connectionId]').val()), }, function () { but.parents("tr").remove(); if ($("#bots tbody tr").length === 0) { $("#bots").addClass("hide"); } } ) }, }, cancel: { text: confirmText["cancel"], action: function () { but.removeClass('disabled'); }, }, } }); }); function send(url, data, callback) { $.ajax({ url: url, data: JSON.stringify(data), type: "POST", success: callback, error: function (res){ if (res.status >= 400) { M.toast({ html: res.responseJSON.error, displayLength: 1000, completeCallback: enableForm() }) } } }); } function getBotTemplate(data) { tmpl = `