/**
* popular-products.js - Script for handling product popularity report functionality
*
* This script manages:
* - DataTable initialization
* - Date filtering and form handling
* - View toggling (grid vs table)
* - Export functionality (PDF, Excel)
* - Chart initialization
*/
// Document ready function
$(document).ready(function () {
// Initialize datatable with error handling
initializeDataTable();
// Handle date preset selection
handleDatePresets();
// Handle limit and sort changes
handleFilterChanges();
// Form validation
setupFormValidation();
// View toggle
setupViewToggle();
// Export functionality
setupExportFunctionality();
// Initialize Chart.js charts if they exist
if (typeof initializeCharts === 'function') {
initializeCharts();
}
// Hide loading overlay when page is fully loaded
hideLoadingWithDelay(500);
// Check user role for hiding UI elements
checkUserPermissions();
});
/**
* Check user permissions and adjust UI accordingly
*/
function checkUserPermissions() {
// Check if body has class 'employee' or 'user'
if ($('body').hasClass('employee') || $('body').hasClass('user')) {
// Hide any admin-only elements that might not have been hidden by PHP
$('.admin-only').hide();
}
}
/**
* Initialize the DataTable with appropriate settings
*/
function initializeDataTable() {
try {
$('.datanew').DataTable({
responsive: true,
language: {
search: 'Cari: _INPUT_',
searchPlaceholder: 'Cari produk...',
lengthMenu: 'Tampilkan: _MENU_',
paginate: {
'first': 'Pertama',
'last': 'Terakhir',
'next': 'Berikutnya',
'previous': 'Sebelumnya'
},
info: "Menampilkan _START_ sampai _END_ dari _TOTAL_ entri",
infoEmpty: "Menampilkan 0 sampai 0 dari 0 entri",
infoFiltered: "(disaring dari _MAX_ entri total)",
emptyTable: "Tidak ada data tersedia untuk periode yang dipilih",
zeroRecords: "Tidak ditemukan catatan yang cocok"
},
dom: '<"top"fl>rt<"bottom"ip><"clear">',
lengthMenu: [10, 25, 50, 100],
pageLength: 10,
drawCallback: function() {
hideLoadingOverlay();
}
});
} catch (e) {
console.error("DataTable initialization error:", e);
hideLoadingOverlay();
}
}
/**
* Handle date preset selection changes
*/
function handleDatePresets() {
$('#preset').on('change', function() {
if ($(this).val() === '') {
// Custom date range selected - show custom date inputs
$('#custom-date-inputs').show();
} else {
// Preset selected - hide custom date inputs and show loading overlay
$('#custom-date-inputs').hide();
showLoadingOverlay();
// Add a small delay before submitting to ensure loading overlay is visible
setTimeout(function() {
$('#date-filter-form').submit();
}, 100);
}
});
}
/**
* Handle limit and sort_by changes (auto-submit form)
*/
function handleFilterChanges() {
$('#limit, #sort_by').on('change', function() {
showLoadingOverlay();
// Add a small delay before submitting
setTimeout(function() {
$('#date-filter-form').submit();
}, 100);
});
// Handle reset button - show loading overlay
$('a[href="?reset=1"]').on('click', function() {
showLoadingOverlay();
});
}
/**
* Setup form validation
*/
function setupFormValidation() {
$('#date-filter-form').on('submit', function(e) {
showLoadingOverlay();
// Only validate if custom date range is selected
if ($('#preset').val() === '') {
var startDate = $('#start_date').val();
var endDate = $('#end_date').val();
if (!startDate || !endDate) {
alert('Harap pilih tanggal awal dan akhir');
hideLoadingOverlay();
e.preventDefault();
return false;
}
if (new Date(startDate) > new Date(endDate)) {
alert('Tanggal awal tidak boleh lebih besar dari tanggal akhir');
hideLoadingOverlay();
e.preventDefault();
return false;
}
}
return true;
});
}
/**
* Setup view toggle between grid and table
*/
function setupViewToggle() {
$('.view-toggle button').on('click', function() {
const viewType = $(this).data('view');
// Update active button
$('.view-toggle button').removeClass('active');
$(this).addClass('active');
// Show/hide appropriate view
if (viewType === 'grid') {
$('#grid-view').show();
$('#table-view').hide();
} else {
$('#grid-view').hide();
$('#table-view').show();
}
});
}
/**
* Setup export functionality
*/
function setupExportFunctionality() {
// PDF Export
$('.pdf-export').on('click', function(e) {
e.preventDefault();
showLoadingOverlay();
// Check if the user has permission (controlled by CSS class)
if ($(this).hasClass('disabled-for-employee')) {
showPermissionModal('Ekspor PDF', 'Laporan ini tidak dapat diekspor ke PDF oleh akun karyawan.');
hideLoadingOverlay();
return;
}
// If we have permission, proceed with export
exportToPDF();
});
// Excel Export
$('.excel-export').on('click', function(e) {
e.preventDefault();
showLoadingOverlay();
// Check if the user has permission
if ($(this).hasClass('disabled-for-employee')) {
showPermissionModal('Ekspor Excel', 'Laporan ini tidak dapat diekspor ke Excel oleh akun karyawan.');
hideLoadingOverlay();
return;
}
// If we have permission, proceed with export
exportToExcel();
});
// Print Report
$('.print-report').on('click', function(e) {
e.preventDefault();
// Check if the user has permission
if ($(this).hasClass('disabled-for-employee')) {
showPermissionModal('Cetak Laporan', 'Pencetakan laporan dibatasi hanya untuk akun administrator.');
return;
}
// If we have permission, proceed with print
printReport();
});
// For employee access - handle restricted action attempts
$('.employee-print, .employee-export').on('click', function(e) {
e.preventDefault();
// Get the action type from data attribute or default
var actionType = $(this).data('action') === 'print_attempt' ? 'Cetak Laporan' : 'Ekspor Laporan';
var message = $(this).data('action') === 'print_attempt'
? 'Pencetakan laporan dibatasi hanya untuk akun administrator.'
: 'Ekspor data dibatasi hanya untuk akun administrator.';
showPermissionModal(actionType, message);
});
}
/**
* Show permission denied modal
*/
function showPermissionModal(actionType, message) {
// Set the action details
$('#actionDetails').html(
'Tindakan yang Dicoba: ' + actionType + '
' +
'' + message + ''
);
// Show the modal
var permissionModal = new bootstrap.Modal(document.getElementById('permissionModal'));
permissionModal.show();
}
/**
* Print the current report
*/
function printReport() {
// Add print-specific styling
$('