File: /var/www/vhost/disk-apps/alq-cali.bikenow.co/public/js/coupon.js
$(document).ready(function () {
changeQuantity(null);
changeQuantityPerUser(1);
});
function create() {
$("#createCoupon").validator("update");
$("#createCoupon")
.validator("update")
.on("submit", function (e) {
if (e.isDefaultPrevented()) {
} else {
e.preventDefault();
var term = document.getElementById("coupon_term");
var link = $("#createCoupon input[name='link']").val();
if (!term.value && !link) {
swal("Error", Lang.get("messagesClient.coupon_term_error"), "error");
return;
}
var info = {
name: $("#createCoupon input[name='name']").val(),
coupon_image: $("#createCoupon input[name='coupon_image']").val(),
code_coupon: $("#createCoupon input[name='code_coupon']").val(),
link: (link != '' ? link : 'terms'),
quantity: $("#createCoupon input[name='quantity']").val(),
quantity_per_user: $("#createCoupon input[name='quantity_per_user']").val(),
tags: $("#createCoupon select[name='generalMultiselect']").val(),
codes: $("#codes").val(),
message: $("#createCoupon textarea[name='message']").val(),
link_redeem: $("#createCoupon input[name='link_redeem']").val(),
};
$("#btn-create").addClass("displayNone");
$("#spinnerButton").removeClass("displayNone");
swal(Lang.get("messagesClient.coupon_tag3"), {
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 id = r.d.id;
if (term.value)
saveTerm(id, term);
var image = document.getElementById("coupon_image");
if (image.value) {
saveImage(id, image);
} else {
swal(r.m, {
icon: "success",
buttons: {
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$(location).attr("href", "/coupons");
}
});
}
} else {
swal("Error", r.m, "error");
$("#btn-create").removeClass("displayNone");
$("#spinnerButton").addClass("displayNone");
}
},
error: function (e) {
swal("Error", Lang.get("messagesClient.coupon_tag4"), "error");
$("#btn-create").removeClass("displayNone");
$("#spinnerButton").addClass("displayNone");
},
});
} else {
$("#btn-create").removeClass("displayNone");
$("#spinnerButton").addClass("displayNone");
}
});
}
});
}
function saveImage(id, image) {
image = image.files[0];
if (image) {
var formData = new FormData();
formData.append("id", id);
formData.append("image", image);
$.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("Error", Lang.get("messagesClient.coupon_tag1"), "error");
}
},
});
}
}
function saveTerm(id, term) {
term = term.files[0];
if (term) {
var formData = new FormData();
formData.append("id", id);
formData.append("term", term);
$.ajax({
type: "POST",
dataType: "json",
processData: false,
contentType: false,
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
},
data: formData,
url: "/coupons/saveTerm",
success: function (r) {
},
});
}
}
function clickEditCoupon(value) {
$(location).attr("href", "/coupons/edit/" + value);
}
function edit() {
$("#editCoupon").validator("update");
$("#editCoupon")
.validator("update")
.on("submit", function (e) {
if (e.isDefaultPrevented()) {
} else {
e.preventDefault();
var term = document.getElementById("coupon_term");
var link = $("#editCoupon input[name='link']").val();
if (!term.value && !link) {
swal("Error", Lang.get("messagesClient.coupon_term_error"), "error");
return;
}
var info = {
id: $("#editCoupon").attr("object_id"),
name: $("#editCoupon input[name='name']").val(),
coupon_image: $("#editCoupon input[name='coupon_image']").val(),
code_coupon: $("#editCoupon input[name='code_coupon']").val(),
link: $("#editCoupon input[name='link']").val(),
quantity: $("#editCoupon input[name='quantity']").val(),
quantity_per_user: $("#editCoupon input[name='quantity_per_user']").val(),
tags: $("#editCoupon select[name='generalMultiselect']").val(),
codes: $("#codes").val(),
message: $("#editCoupon textarea[name='message']").val(),
link_redeem: $("#editCoupon input[name='link_redeem']").val(),
};
$("#btn-edit").addClass("displayNone");
$("#spinnerButton").removeClass("displayNone");
swal(Lang.get("messagesClient.coupon_tag5"), {
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 id = r.d.id;
if (term.value)
saveTerm(id, term);
var image = document.getElementById("coupon_image");
if (image.value) {
saveImage(id, image);
} else {
swal(r.m, {
icon: "success",
buttons: {
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
localStorage.setItem("setPageTable", true);
$(location).attr("href", "/coupons");
}
});
}
} else {
swal("Error", r.m, "error");
$("#btn-edit").removeClass("displayNone");
$("#spinnerButton").addClass("displayNone");
}
},
error: function (e) {
swal("Error", Lang.get("messagesClient.coupon_tag6"), "error");
$("#btn-edit").removeClass("displayNone");
$("#spinnerButton").addClass("displayNone");
},
});
} else {
$("#btn-edit").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);
}
}
function manageQuantityContainer(value) {
if (value == 'custom' || value > 0) {
$("#container_quantity").removeClass("hide");
$("#quantity_selector_container").removeClass("col-md-12");
$("#quantity_selector_container").addClass("col-md-6");
} else {
$("#container_quantity").addClass("hide");
$("#quantity_selector_container").removeClass("col-md-6");
$("#quantity_selector_container").addClass("col-md-12");
}
}
function changeQuantity(value) {
$('#quantity').val(value == 'custom' ? '' : value);
manageQuantityContainer(value);
}
function manageQuantityPerUserContainer(value) {
if (value == 'custom' || value > 1) {
$("#container_quantity_per_user").removeClass("hide");
$("#quantity_per_user_selector_container").removeClass("col-md-12");
$("#quantity_per_user_selector_container").addClass("col-md-6");
} else {
$("#container_quantity_per_user").addClass("hide");
$("#quantity_per_user_selector_container").removeClass("col-md-6");
$("#quantity_per_user_selector_container").addClass("col-md-12");
}
}
function changeQuantityPerUser(value) {
$('#quantity_per_user').val(value == 'custom' ? '' : value);
manageQuantityPerUserContainer(value);
}
function clickDeleteCoupon(value) {
swal(Lang.get("messagesClient.coupon_tag8"), {
buttons: {
cancel: "No",
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
var info = {
id: value,
};
$.ajax({
url: "/coupons/delete/",
type: "DELETE",
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", "/coupons");
}
});
} else {
swal("Error", Lang.get("messagesClient.coupon_tag9"), "error");
}
},
error: function (r) {
swal("Error", Lang.get("messagesClient.coupon_tag9"), "error");
},
});
}
});
}