File: /var/www/vhost/disk-apps/agile-selling-mia/public/js/coupon.js
function createCoupon() {
$("#createYoutube").validator("update");
$("#createYoutube")
.validator("update")
.on("submit", function (e) {
if (e.isDefaultPrevented()) {
} else {
e.preventDefault();
$("#btn-create-youtube").addClass("displayNone");
$("#spinnerButton").removeClass("displayNone");
var info = {
name: $("#createYoutube input[name='name']").val(),
coupon_image: $(
"#createYoutube input[name='coupon_image']"
).val(),
code_coupon: $(
"#createYoutube input[name='code_coupon']"
).val(),
link: $("#createYoutube input[name='link']").val(),
};
swal(Lang.get("messagesClient.coupon_tag3"), {
//swal("¿Crear fotos instagram?", {
buttons: {
cancel: "No",
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$.ajax({
url: "/coupons/create",
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) {
var coupon_id = r.d.id;
var image = document.getElementById(
"coupon_image"
);
if (image != null) {
saveImage(coupon_id);
} else {
swal(r.m, {
icon: "success",
buttons: {
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$(location).attr(
"href",
"/coupons"
);
}
});
}
} else {
swal(r.m, "error");
$("#btn-create-youtube").removeClass(
"displayNone"
);
$("#spinnerButton").addClass("displayNone");
}
},
error: function (e) {
swal(
Lang.get("messagesClient.coupon_tag4"),
"error"
);
$("#btn-create-youtube").removeClass(
"displayNone"
);
$("#spinnerButton").addClass("displayNone");
},
});
} else {
$("#btn-create-youtube").removeClass("displayNone");
$("#spinnerButton").addClass("displayNone");
}
});
}
});
}
function saveImage(id) {
var fImage = document.getElementById("coupon_image");
fImage = fImage.files[0];
if (fImage) {
var formData = new FormData();
formData.append("coupon_id", id);
formData.append("image", fImage);
$.ajax({
type: "POST",
dataType: "json",
processData: false,
contentType: false,
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
},
data: formData,
url: "/coupons/saveImage",
success: function (r) {
if (r.r) {
swal(r.m, {
icon: "success",
buttons: {
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$(location).attr("href", "/coupons");
}
});
} else {
swal("Ok", Lang.get("messagesClient.coupon_tag1"), "error");
}
},
});
} else {
swal("Ok", "Cupon actualizado sin imagen.", {
icon: "success",
buttons: {
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$(location).attr("href", "/coupons");
}
});
}
}
function clickEditCoupon(value) {
$(location).attr("href", "/coupons/edit/" + value);
}
function editCoupon() {
$("#editYoutube").validator("update");
$("#editYoutube")
.validator("update")
.on("submit", function (e) {
if (e.isDefaultPrevented()) {
} else {
e.preventDefault();
$("#btn-edit-youtube").addClass("displayNone");
$("#spinnerButton").removeClass("displayNone");
var info = {
id: $("#btn-edit-youtube").attr("data-id"),
name: $("#editYoutube input[name='name']").val(),
coupon_image: $(
"#editYoutube input[name='coupon_image']"
).val(),
code_coupon: $(
"#editYoutube input[name='code_coupon']"
).val(),
link: $("#editYoutube input[name='link']").val(),
};
swal(Lang.get("messagesClient.coupon_tag5"), {
//swal("¿Modificar fotos instagram?", {
buttons: {
cancel: "No",
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$.ajax({
url: "/coupons/update",
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) {
var coupon_id = r.d.id;
var image = document.getElementById(
"coupon_image"
).value;
if (image != null) {
saveImage(coupon_id);
} else {
swal(r.m, {
icon: "success",
buttons: {
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
localStorage.setItem(
"setPageTable",
true
);
$(location).attr(
"href",
"/coupons"
);
}
});
}
} else {
swal(r.m, "error");
$("#btn-edit-youtube").removeClass(
"displayNone"
);
$("#spinnerButton").addClass("displayNone");
}
},
error: function (e) {
swal(
Lang.get("messagesClient.coupon_tag6"),
"error"
);
$("#btn-edit-youtube").removeClass(
"displayNone"
);
$("#spinnerButton").addClass("displayNone");
},
});
} else {
$("#btn-edit-youtube").removeClass("displayNone");
$("#spinnerButton").addClass("displayNone");
}
});
}
});
}
function setPageTable() {
localStorage.setItem("setPageTable", true);
$(location).attr("href", "/coupons");
}
/**Activar/Desactivar Banner usando switch*/
function chkCoupon(value) {
var state;
if ($("#Checkactive" + value).is(":checked")) {
state = 1;
} else {
state = 0;
}
var info = {
id: value,
state: state,
};
$.ajax({
url: "/coupons/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.coupon_tag7"), "error");
},
});
function getUrlImagen() {
var url = document.getElementById("coupon_image").files[0].name;
alert(url);
}
}
// $(document).ready(function() {
// $('#mytable').DataTable( {
// "language": {
// "lengthMenu": "Mostrar _MENU_ registros",
// "zeroRecords": "Nothing found - sorry",
// "info": "Mostrando página _PAGE_ de _PAGES_",
// "infoEmpty": "No records available",
// "infoFiltered": "(filtered from _MAX_ total records)",
// "sSearch": "Buscar: ",
// "oPaginate":{
// "sFirst":"Primero",
// "sLast":"Último",
// "sNext":"Siguiente",
// "sPrevious":"Anterior"
// }
// }
// } );
// } );