File: /var/www/vhost/disk-apps/qas.sports-crowd.com/public/js/field_datasources.js
function create() {
$("#form_create").validator("update");
$("#form_create")
.validator("update")
.on("submit", function (e) {
if (e.isDefaultPrevented()) {
} else {
e.preventDefault();
var type = $(" #form_create select[name='type']").val();
var url = $(" #form_create input[name='url']").val();
var main_datasource_id = $(
" #form_create select[name='main_datasource_id']"
).val();
if (!type) {
swal(Lang.get("messagesClient.field_datasources.tag_4"));
return;
}
if (
(type == "service" && !url) ||
(type == "service" && url == "")
) {
swal(Lang.get("messagesClient.field_datasources.tag_3"));
return;
}
if (type == "hard" && !main_datasource_id) {
swal(Lang.get("messagesClient.field_datasources.tag_5"));
return;
}
$("#btn-create").addClass("displayNone");
$("#spinnerButton").removeClass("displayNone");
var info = {
name: $(" #form_create input[name='name']").val(),
type,
url: type == "service" ? url : null,
main_datasource_id:
type == "hard" ? main_datasource_id : null,
active: $(" #form_create select[name='active']").val(),
};
swal(Lang.get("messagesClient.field_datasources.tag_1"), {
buttons: {
cancel: "No",
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$.ajax({
url: "/field_datasources",
type: "POST",
contentType: "application/json",
headers: {
"X-CSRF-TOKEN": $(
'meta[name="csrf-token"]'
).attr("content"),
"Content-Type": "application/json",
},
data: JSON.stringify(info),
success: function (r) {
if (r.r) {
swal(r.m, {
icon: "success",
buttons: {
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$(location).attr(
"href",
"/field_datasources"
);
}
});
} else {
swal(r.m, "error");
$("#btn-create").removeClass("displayNone");
$("#spinnerButton").addClass("displayNone");
}
},
error: function (e) {
swal(
"¡Hubo un Error al crear banner!",
"error"
);
$("#btn-create").removeClass("displayNone");
$("#spinnerButton").addClass("displayNone");
},
});
} else {
$("#btn-create").removeClass("displayNone");
$("#spinnerButton").addClass("displayNone");
}
});
}
});
}
function goToMain() {
$(location).attr("href", "/main_datasources");
}
function clickEdit(value) {
$(location).attr("href", "/field_datasources/" + value + "/edit");
}
function edit() {
$("#form_edit").validator("update");
$("#form_edit")
.validator("update")
.on("submit", function (e) {
if (e.isDefaultPrevented()) {
} else {
e.preventDefault();
var type = $(" #form_edit select[name='type']").val();
var url = $(" #form_edit input[name='url']").val();
var main_datasource_id = $(
" #form_edit select[name='main_datasource_id']"
).val();
if (!type) {
swal(Lang.get("messagesClient.field_datasources.tag_4"));
return;
}
if (
(type == "service" && !url) ||
(type == "service" && url == "")
) {
swal(Lang.get("messagesClient.field_datasources.tag_3"));
return;
}
if (type == "hard" && !main_datasource_id) {
swal(Lang.get("messagesClient.field_datasources.tag_5"));
return;
}
$("#btn-edit").addClass("displayNone");
$("#spinnerButton").removeClass("displayNone");
var info = {
_method: $("input[name=_method]").val(),
name: $(" #form_edit input[name='name']").val(),
type,
url: type == "service" ? url : null,
main_datasource_id:
type == "hard" ? main_datasource_id : null,
active: $(" #form_edit select[name='active']").val(),
};
var id = $("#form_edit").attr("datasource_id");
swal(Lang.get("messagesClient.field_datasources.tag_2"), {
buttons: {
cancel: "No",
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$.ajax({
url: "/field_datasources/" + id,
type: "PUT",
contentType: "application/json",
headers: {
"X-CSRF-TOKEN": $(
'meta[name="csrf-token"]'
).attr("content"),
"Content-Type": "application/json",
},
data: JSON.stringify(info),
success: function (r) {
if (r.r) {
swal(r.m, {
icon: "success",
buttons: {
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$(location).attr(
"href",
"/field_datasources"
);
}
});
} else {
swal(r.m, "error");
$("#btn-edit").removeClass("displayNone");
$("#spinnerButton").addClass("displayNone");
}
},
error: function (e) {
swal("¡Hubo un Error al actualizar!", "error");
$("#btn-edit").removeClass("displayNone");
$("#spinnerButton").addClass("displayNone");
},
});
} else {
$("#btn-edit").removeClass("displayNone");
$("#spinnerButton").addClass("displayNone");
}
});
}
});
}
function setPageTable() {
$(location).attr("href", "/field_datasources");
}
/**Activar/Desactivar Banner usando switch*/
function chk(value) {
var state;
if ($("#Checkactive" + value).is(":checked")) {
state = 1;
} else {
state = 0;
}
var info = {
id: value,
state: state,
};
$.ajax({
url: "/field_datasources/active",
type: "POST",
contentType: "application/json",
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
"Content-Type": "application/json",
},
data: JSON.stringify(info),
success: function (r) {
if (r.r) {
swal("Ok", r.m);
} else {
swal("Error", r.m, "error");
}
},
error: function (e) {
swal(
"Error",
Lang.get("messagesClient.field_datasources.error_update_image"),
"error"
);
},
});
}
/**Método para eliminar banner*/
function clickDelete(value) {
swal(Lang.get("messagesClient.field_datasources.tag_6"), {
buttons: {
cancel: "No",
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
var info = {
_method: "DELETE",
};
$.ajax({
url: "/field_datasources/" + value,
type: "POST",
contentType: "application/json",
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr(
"content"
),
"Content-Type": "application/json",
},
data: JSON.stringify(info),
success: function (r) {
console.log(r);
if (r.r) {
swal(r.m, {
icon: "success",
buttons: {
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$(location).attr("href", "/field_datasources");
}
});
} else {
swal("Error", r.m, "error");
}
},
error: function (e) {
swal(
"Error",
Lang.get(
"messagesClient.field_datasources.error_delete"
),
"error"
);
},
});
}
});
}
function changeType(type, main_selected = false) {
if (type == "service") {
$("#container_url").removeClass("hide");
$("#container_main").addClass("hide");
} else {
$("#container_url").addClass("hide");
$("#container_main").removeClass("hide");
$("#main_datasource_id").empty();
$.ajax({
type: "GET",
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
},
url: "/field_datasources/list_main",
data: null,
success: function (response) {
if (response.r) {
$("#main_datasource_id").prop("disabled", false);
$("#main_datasource_id")
.append(
'<option value="0" selected disabled>-- Seleccionar</option>'
)
.select("refresh");
for (var i = 0; i < response.data.length; i++) {
if (
main_selected &&
main_selected == response.data[i].id
) {
$("#main_datasource_id")
.append(
'<option value="' +
response.data[i].id +
'" selected>' +
response.data[i].name +
"</option>"
)
.select("refresh");
} else {
$("#main_datasource_id")
.append(
'<option value="' +
response.data[i].id +
'">' +
response.data[i].name +
"</option>"
)
.select("refresh");
}
}
}
},
error: function (textStatus, errorThrown) {
swal(
"Error",
Lang.get("messagesClient.field_datasources.error_delete"),
"error"
);
},
});
}
}