File: /var/www/vhost/disk-apps/alq-cali.bikenow.co/public/js/academy_news.js
var table;
$(document).ready(function () {
//---------------------------------------------table----------------------------------------------------
table = $('#example2').DataTable({
"orderCellsTop": true,
"responsive": true,
"processing": true,
"scrollX": true,
"scrollCollapse": true,
"paging": true,
"language": {
url: window.DATATABLES_LANGUAGE_URL
}
});
//------------------------------------------date_filer--------------------------------------------------
$.fn.datepicker.dates['en'] = {
days: ['Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado'],
daysShort: ['Dom', 'Lun', 'Mar', 'Mié', 'Juv', 'Vie', 'Sáb'],
daysMin: ['Do', 'Lu', 'Ma', 'Mi', 'Ju', 'Vi', 'Sá'],
months: ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre',
'Octubre', 'Noviembre', 'Diciembre'
],
monthsShort: ['Ene', 'Feb', 'Mar', 'Abr', 'May', 'Jun', 'Jul', 'Ago', 'Sep', 'Oct', 'Nov',
'Dic'
],
today: "Hoy",
clear: "Clear",
format: "yyyy-mm-dd",
titleFormat: "MM yyyy",
weekStart: 0
};
$("input[name='min_date']").datepicker({
dateFormat: 'yyyy-mm-dd'
});
$("input[name='max_date']").datepicker({
dateFormat: 'yyyy-mm-dd'
});
$("input[name='min_date']").on('change', function (event) {
minDate = this.value;
table.draw()
});
$("input[name='max_date']").on('change', function (event) {
maxDate = this.value;
table.draw()
});
// New filters
$('select[name=locations]').on('change', function () {
franchises = $(this).find('option:selected').text();
table.draw();
});
$('select[name=categories]').on('change', function () {
category = $(this).find('option:selected').text();
table.draw();
});
$('select[name=schedules]').on('change', function () {
schedule = $(this).find('option:selected').text();
table.draw();
});
$('select[name=coaches]').on('change', function () {
coaches = $(this).find('option:selected').text();
table.draw();
});
$.fn.dataTable.ext.search.push(function (settings, data, dataIndex) {
let min = $("#min_date").val();
let max = $("#max_date").val();
let types = $("select[name='type']").val();
let date = new Date(data[0]);
let franchises = $("select[name='locations'] option:selected").text();
let categories = $("select[name='categories'] option:selected").text();
let schedules = $("select[name='schedules'] option:selected").text();
let coaches = $("select[name='coaches'] option:selected").text();
if (min && date <= new Date(min)) {
return false;
}
if (max && date >= new Date(max)) {
return false;
}
if (types && !types.includes(data[4])) {
return false;
}
if (franchises && data[11] !== franchises) {
return false;
}
if (categories && data[12] !== categories) {
return false;
}
if (schedules && data[13] !== schedules) {
return false;
}
if (coaches && !data[14].includes(coaches)) {
return false;
}
return true;
});
//-----------------------------------------\date_filer--------------------------------------------------
//-------------------------------------------select2----------------------------------------------------
$('.select2').select2();
$("select[name='type']").on('change', function () {
table.draw();
});
//------------------------------------------\select2----------------------------------------------------
// New filters
const existingLocationId = localRetrieve(`${SELECTOR_PREFIX}_filterLocations`);
const existingCategoryId = localRetrieve(`${SELECTOR_PREFIX}_filterCategories`);
const existingScheduleId = localRetrieve(`${SELECTOR_PREFIX}_filterSchedules`);
const existingCoachId = localRetrieve(`${SELECTOR_PREFIX}_filterCoaches`);
const existingFilteredStates = localRetrieve(`${SELECTOR_PREFIX}_filterStatesAux`);
const locationRequest = getAcademyLocations(existingLocationId);
const categoryRequest = getAcademyCategories(existingCategoryId);
const scheduleRequest = getAcademySchedules(existingScheduleId);
const coachRequest = getAcademyCoaches(existingCoachId);
$.when(locationRequest, categoryRequest, scheduleRequest, coachRequest).done(function () {
if (existingLocationId || existingCategoryId || existingScheduleId || existingCoachId) {
restoreFilteredStates(existingFilteredStates, existingLocationId, existingCategoryId, existingScheduleId, existingCoachId);
restoreFilters();
cleanPagination();
getData();
table.draw();
} else {
getData();
}
}).fail(function () {
console.error('Alguna de las peticiones falló');
});
$("select[name='locations']").on('change', function (event) {
generalFilter(event);
cleanPagination();
getData();
});
$("select[name='categories']").on('change', function (event) {
generalFilter(event);
cleanPagination();
getData();
});
$("select[name='schedules']").on('change', function (event) {
generalFilter(event);
cleanPagination();
getData();
});
$("select[name='coaches']").on('change', function (event) {
generalFilter(event);
cleanPagination();
getData();
});
});
function changeStatus(element, academyNewId) {
$.ajax({
url: "/academy_news/updateStatus/" + academyNewId + '/' + element.value,
type: "GET",
contentType: "application/json",
headers: {
"X-CSRF-TOKEN": $(
'meta[name="csrf-token"]'
).attr("content"),
"Content-Type": "application/json",
},
success: function (r) {
console.log(r);
$('#loading').hide();
if (r.r) {
swal(r.m, {
icon: "success",
buttons: {
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
localStorage.setItem("setPageTable", true);
var academyUserId = document.location.href.match(
/[0-9]+/)[0];
$(location).attr("href", "/academy_user_payment/" +
academyUserId);
}
});
} else {
swal("Error", r.m, "error");
}
},
error: function (e) {
$('#loading').hide();
swal("Error", Lang.get(
"messagesClient.academy_payments.error_confirm_payment"),
"error");
},
});
}
function clickExportAll() {
var validateDownloadUrl = document.location.origin + "/academy_news/validateExport";
var downloadUrl = document.location.origin + "/academy_news/export/";
info = {
data: table.rows({ filter: 'applied' }).data().toArray()
}
$.ajax({
url: validateDownloadUrl,
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 (response) {
$('#loading').hide();
if (response.success) {
window.location.assign(downloadUrl + response.data);
} else {
swal("Error", response.message, "error");
}
},
error: function (response) {
$('#loading').hide();
swal("Error", response.responseJSON ? response.responseJSON.message :
'Hubo un error en el servidor, espere unos segundos e intente de nuevo',
"error");
}
});
}
function getData() {
let locations = $("select[name='locations']").val();
let categories = $("select[name='categories']").val();
let schedules = $("select[name='schedules']").val();
let coaches = $("select[name='coaches']").val();
localStore(`${SELECTOR_PREFIX}_filterLocations`, locations);
localStore(`${SELECTOR_PREFIX}_filterCategories`, categories);
localStore(`${SELECTOR_PREFIX}_filterSchedules`, schedules);
localStore(`${SELECTOR_PREFIX}_filterCoaches`, coaches);
}
let cleanPagination = function () {
localRemove(`DataTables_${SELECTOR_PREFIX}`);
}
function restoreFilters() {
const locations = localRetrieve(`${SELECTOR_PREFIX}_filterLocations`);
const categories = localRetrieve(`${SELECTOR_PREFIX}_filterCategories`);
const schedules = localRetrieve(`${SELECTOR_PREFIX}_filterSchedules`);
const coaches = localRetrieve(`${SELECTOR_PREFIX}_filterCoaches`);
if (locations) $("select[name='locations']").val(locations).trigger('change');
if (categories) $("select[name='categories']").val(categories).trigger('change');
if (schedules) $("select[name='schedules']").val(schedules).trigger('change');
if (coaches) $("select[name='coaches']").val(coaches).trigger('change');
}