414 lines
17 KiB
PHP
414 lines
17 KiB
PHP
@props([
|
|
'pageTitle' => '',
|
|
'data' => [],
|
|
'columns' => [],
|
|
'actions' => [],
|
|
'showAddButton' => false,
|
|
'addButtonUrl' => '#',
|
|
'showCheckboxes' => false,
|
|
'showBatchDelete' => false,
|
|
'showEditModal' => false,
|
|
'showViewModal' => false,
|
|
'currentPage' => 1,
|
|
'lastPage' => 1,
|
|
'total' => 0,
|
|
])
|
|
|
|
<div class="card-header border-0 bg-transparent">
|
|
<div class="d-flex justify-content-between align-items-center">
|
|
<h5 class="mb-0 fw-bold text-dark">{{ $pageTitle }}</h5>
|
|
@if ($showAddButton)
|
|
<a href="{{ $addButtonUrl }}" class="btn btn-primary btn-sm px-3">
|
|
<i class="fa-solid fa-plus me-1"></i> Add {{ $pageTitle }}
|
|
</a>
|
|
@endif
|
|
</div>
|
|
</div>
|
|
<div class="card-body">
|
|
<!-- Alerts -->
|
|
<div id="alert-container"></div>
|
|
|
|
<!-- Search and Filters -->
|
|
<div class="row mb-3 align-items-center">
|
|
<div class="col-12 col-md-6 mb-2 mb-md-0">
|
|
<div class="input-group input-group-sm">
|
|
<span class="input-group-text bg-light border-end-0">
|
|
<i class="fa-solid fa-magnifying-glass text-muted"></i>
|
|
</span>
|
|
<input type="text" class="form-control border-start-0" placeholder="Search..." id="searchInput" data-search-param="_search">
|
|
</div>
|
|
</div>
|
|
<div class="col-12 col-md-6 d-flex justify-content-end">
|
|
<button class="btn btn-outline-secondary btn-sm" id="clearFilters">
|
|
<i class="fa-solid fa-filter-circle-xmark me-1"></i> Clear Filters
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Table -->
|
|
<div class="table-container">
|
|
<table class="table table-hover align-middle">
|
|
<thead class="table-light">
|
|
<tr>
|
|
@if ($showCheckboxes)
|
|
<th class="text-center" style="width: 40px;">
|
|
<input type="checkbox" id="selectAll">
|
|
</th>
|
|
@endif
|
|
@foreach ($columns as $index => $column)
|
|
<th class="{{ $column['sortable'] ? 'sortable' : '' }}" data-column="{{ $index + ($showCheckboxes ? 1 : 0) }}">
|
|
{{ $column['name'] }}
|
|
@if ($column['sortable'])
|
|
<i class="fa-solid fa-sort"></i>
|
|
@endif
|
|
</th>
|
|
@endforeach
|
|
@if (!empty($actions))
|
|
<th class="text-center" style="width: 120px;">Action</th>
|
|
@endif
|
|
</tr>
|
|
</thead>
|
|
<tbody id="tableBody"></tbody>
|
|
</table>
|
|
</div>
|
|
|
|
<!-- Batch Delete and Pagination -->
|
|
<div class="d-flex justify-content-between align-items-center mt-4">
|
|
@if ($showBatchDelete)
|
|
<div>
|
|
<button class="btn btn-danger btn-sm" id="deleteSelected" disabled>
|
|
<i class="fa-solid fa-trash-can me-1"></i> Delete Selected
|
|
</button>
|
|
</div>
|
|
@endif
|
|
<nav aria-label="Page navigation">
|
|
<ul class="pagination pagination-sm" id="pagination"></ul>
|
|
</nav>
|
|
</div>
|
|
</div>
|
|
|
|
<style>
|
|
.card, .table, .btn, .form-control, .input-group-text {
|
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
|
|
font-weight: 400;
|
|
line-height: 1.5;
|
|
}
|
|
.table-container {
|
|
overflow-x: auto;
|
|
}
|
|
.table th, .table td {
|
|
white-space: nowrap;
|
|
}
|
|
.sortable {
|
|
cursor: pointer;
|
|
position: relative;
|
|
}
|
|
.sortable:hover {
|
|
background-color: #f8f9fa;
|
|
}
|
|
.sortable i {
|
|
margin-left: 5px;
|
|
color: #6c757d;
|
|
}
|
|
.clickable-row:hover {
|
|
background-color: #f1f1f1;
|
|
cursor: pointer;
|
|
}
|
|
.view-btn { border-color: #0d6efd; color: #0d6efd; }
|
|
.view-btn:hover { background-color: #0d6efd; color: #fff; }
|
|
.edit-btn { border-color: #ffc107; color: #ffc107; }
|
|
.edit-btn:hover { background-color: #ffc107; color: #fff; }
|
|
.delete-btn { border-color: #dc3545; color: #dc3545; }
|
|
.delete-btn:hover { background-color: #dc3545; color: #fff; }
|
|
</style>
|
|
|
|
<script>
|
|
const tableConfig = {
|
|
data: @json($data),
|
|
columns: @json($columns),
|
|
actions: @json($actions),
|
|
showCheckboxes: {{ json_encode($showCheckboxes) }},
|
|
showBatchDelete: {{ json_encode($showBatchDelete) }},
|
|
showEditModal: {{ json_encode($showEditModal) }},
|
|
showViewModal: {{ json_encode($showViewModal) }},
|
|
pageTitle: @json($pageTitle),
|
|
csrfToken: '{{ csrf_token() }}',
|
|
currentPage: {{ $currentPage }},
|
|
lastPage: {{ $lastPage }},
|
|
total: {{ $total }},
|
|
};
|
|
|
|
const rowsPerPage = 5;
|
|
let currentPage = tableConfig.currentPage;
|
|
let filteredRows = [...tableConfig.data];
|
|
let sortDirection = {};
|
|
|
|
function renderTable() {
|
|
const tableBody = document.getElementById('tableBody');
|
|
if (!tableBody) return;
|
|
tableBody.innerHTML = '';
|
|
|
|
const startIndex = (currentPage - 1) * rowsPerPage;
|
|
const endIndex = startIndex + rowsPerPage;
|
|
const paginatedRows = filteredRows.slice(startIndex, endIndex);
|
|
|
|
paginatedRows.forEach(row => {
|
|
const tr = document.createElement('tr');
|
|
tr.setAttribute('data-id', row.id);
|
|
tr.classList.add('clickable-row');
|
|
let rowHtml = '';
|
|
|
|
if (tableConfig.showCheckboxes) {
|
|
rowHtml += `<td class="text-center"><input type="checkbox" class="rowCheckbox" value="${row.id}"></td>`;
|
|
}
|
|
|
|
tableConfig.columns.forEach(col => {
|
|
let value = row[col.key] || '';
|
|
if (col.key.includes('Date')) {
|
|
value = value ? new Date(value).toLocaleDateString() : 'N/A';
|
|
}
|
|
rowHtml += `<td>${value}</td>`;
|
|
});
|
|
|
|
if (tableConfig.actions.length > 0) {
|
|
rowHtml += `<td class="text-center">`;
|
|
tableConfig.actions.forEach(action => {
|
|
const routeBase = tableConfig.pageTitle === 'Promotions' ? 'promotions' : '';
|
|
if (action === 'view') {
|
|
rowHtml += `<a href="/promotions/${row.id}" class="btn btn-sm view-btn me-1" title="View"><i class="fa-solid fa-eye"></i></a>`;
|
|
} else if (action === 'edit') {
|
|
rowHtml += `<a href="/promotions/${row.id}/edit" class="btn btn-sm edit-btn me-1" title="Edit"><i class="fa-solid fa-pen"></i></a>`;
|
|
} else if (action === 'delete') {
|
|
rowHtml += `<a href="/promotions/${row.id}" class="btn btn-sm delete-btn" title="Delete" onclick="event.preventDefault(); if(confirm('Are you sure you want to delete this promotion?')) { document.getElementById('delete-form-${row.id}').submit(); }"><i class="fa-solid fa-trash"></i></a>
|
|
<form id="delete-form-${row.id}" action="/promotions/${row.id}" method="POST" style="display: none;">
|
|
@csrf
|
|
@method('DELETE')
|
|
</form>`;
|
|
}
|
|
});
|
|
rowHtml += `</td>`;
|
|
}
|
|
|
|
tr.innerHTML = rowHtml;
|
|
tableBody.appendChild(tr);
|
|
});
|
|
|
|
attachEventListeners();
|
|
updateNoDataMessage();
|
|
}
|
|
|
|
function renderPagination() {
|
|
const pagination = document.getElementById('pagination');
|
|
if (!pagination) return;
|
|
pagination.innerHTML = '';
|
|
|
|
const totalPages = Math.max(1, Math.ceil(filteredRows.length / rowsPerPage));
|
|
|
|
const prevLi = document.createElement('li');
|
|
prevLi.className = `page-item ${currentPage === 1 ? 'disabled' : ''}`;
|
|
prevLi.innerHTML = `<a class="page-link" href="#" aria-label="Previous"><span aria-hidden="true">«</span></a>`;
|
|
prevLi.addEventListener('click', (e) => {
|
|
e.preventDefault();
|
|
if (currentPage > 1) {
|
|
currentPage--;
|
|
renderTable();
|
|
renderPagination();
|
|
}
|
|
});
|
|
pagination.appendChild(prevLi);
|
|
|
|
for (let i = 1; i <= totalPages; i++) {
|
|
const li = document.createElement('li');
|
|
li.className = `page-item ${currentPage === i ? 'active' : ''}`;
|
|
li.innerHTML = `<a class="page-link" href="#">${i}</a>`;
|
|
li.addEventListener('click', (e) => {
|
|
e.preventDefault();
|
|
currentPage = i;
|
|
renderTable();
|
|
renderPagination();
|
|
});
|
|
pagination.appendChild(li);
|
|
}
|
|
|
|
const nextLi = document.createElement('li');
|
|
nextLi.className = `page-item ${currentPage === totalPages ? 'disabled' : ''}`;
|
|
nextLi.innerHTML = `<a class="page-link" href="#" aria-label="Next"><span aria-hidden="true">»</span></a>`;
|
|
nextLi.addEventListener('click', (e) => {
|
|
e.preventDefault();
|
|
if (currentPage < totalPages) {
|
|
currentPage++;
|
|
renderTable();
|
|
renderPagination();
|
|
}
|
|
});
|
|
pagination.appendChild(nextLi);
|
|
}
|
|
|
|
function updateNoDataMessage() {
|
|
const tableBody = document.getElementById('tableBody');
|
|
if (filteredRows.length === 0) {
|
|
tableBody.innerHTML = `<tr><td colspan="${tableConfig.columns.length + (tableConfig.showCheckboxes ? 1 : 0) + (tableConfig.actions.length > 0 ? 1 : 0)}" class="text-center">No data available</td></tr>`;
|
|
}
|
|
}
|
|
|
|
function attachEventListeners() {
|
|
const searchInput = document.getElementById('searchInput');
|
|
if (searchInput) {
|
|
searchInput.addEventListener('input', function() {
|
|
const searchTerm = this.value.toLowerCase();
|
|
filteredRows = tableConfig.data.filter(row => {
|
|
return Object.values(row).some(value =>
|
|
value && value.toString().toLowerCase().includes(searchTerm)
|
|
);
|
|
});
|
|
currentPage = 1;
|
|
renderTable();
|
|
renderPagination();
|
|
});
|
|
}
|
|
|
|
document.querySelectorAll('.sortable').forEach(header => {
|
|
header.addEventListener('click', function() {
|
|
const columnIndex = parseInt(this.getAttribute('data-column')) - (tableConfig.showCheckboxes ? 1 : 0);
|
|
const key = tableConfig.columns[columnIndex].key;
|
|
|
|
sortDirection[columnIndex] = !sortDirection[columnIndex] ? 'asc' : sortDirection[columnIndex] === 'asc' ? 'desc' : 'asc';
|
|
|
|
document.querySelectorAll('.sortable i').forEach(icon => {
|
|
icon.classList.remove('fa-sort-up', 'fa-sort-down');
|
|
icon.classList.add('fa-sort');
|
|
});
|
|
const icon = this.querySelector('i');
|
|
if (icon) {
|
|
icon.classList.remove('fa-sort');
|
|
icon.classList.add(sortDirection[columnIndex] === 'asc' ? 'fa-sort-up' : 'fa-sort-down');
|
|
}
|
|
|
|
filteredRows.sort((a, b) => {
|
|
let aValue = (a[key] || '').toString().toLowerCase();
|
|
let bValue = (b[key] || '').toString().toLowerCase();
|
|
if (key.includes('Date')) {
|
|
aValue = new Date(a[key] || '1970-01-01').getTime();
|
|
bValue = new Date(b[key] || '1970-01-01').getTime();
|
|
return sortDirection[columnIndex] === 'asc' ? aValue - bValue : bValue - aValue;
|
|
}
|
|
return sortDirection[columnIndex] === 'asc' ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue);
|
|
});
|
|
|
|
currentPage = 1;
|
|
renderTable();
|
|
renderPagination();
|
|
});
|
|
});
|
|
|
|
const clearFilters = document.getElementById('clearFilters');
|
|
if (clearFilters) {
|
|
clearFilters.addEventListener('click', function() {
|
|
if (searchInput) searchInput.value = '';
|
|
document.querySelectorAll('.sortable i').forEach(icon => {
|
|
icon.classList.remove('fa-sort-up', 'fa-sort-down');
|
|
icon.classList.add('fa-sort');
|
|
});
|
|
sortDirection = {};
|
|
filteredRows = [...tableConfig.data];
|
|
currentPage = 1;
|
|
renderTable();
|
|
renderPagination();
|
|
});
|
|
}
|
|
|
|
const checkboxes = document.querySelectorAll('.rowCheckbox');
|
|
const selectAll = document.getElementById('selectAll');
|
|
const deleteSelected = document.getElementById('deleteSelected');
|
|
|
|
if (selectAll) {
|
|
selectAll.addEventListener('change', function() {
|
|
checkboxes.forEach(checkbox => checkbox.checked = this.checked);
|
|
updateDeleteButton();
|
|
});
|
|
}
|
|
|
|
if (checkboxes.length > 0) {
|
|
checkboxes.forEach(checkbox => {
|
|
checkbox.addEventListener('change', updateDeleteButton);
|
|
});
|
|
}
|
|
|
|
if (tableConfig.showBatchDelete && deleteSelected) {
|
|
deleteSelected.addEventListener('click', function() {
|
|
const checkboxes = document.querySelectorAll('.rowCheckbox:checked');
|
|
const selectedIds = Array.from(checkboxes)
|
|
.map(cb => cb.closest('tr').getAttribute('data-id'));
|
|
|
|
if (selectedIds.length === 0) {
|
|
showAlert('danger', 'Please select at least one promotion to delete.');
|
|
return;
|
|
}
|
|
|
|
if (confirm('Are you sure you want to delete the selected promotions?')) {
|
|
axios({
|
|
method: 'post',
|
|
url: '{{ route('promotions.batchDelete') }}',
|
|
headers: {
|
|
'X-CSRF-TOKEN': tableConfig.csrfToken,
|
|
'Content-Type': 'application/json',
|
|
},
|
|
data: {
|
|
promotion_uuid: selectedIds
|
|
}
|
|
}).then(response => {
|
|
// Remove deleted rows from filteredRows and tableConfig.data
|
|
filteredRows = filteredRows.filter(row => !selectedIds.includes(row.id));
|
|
tableConfig.data = tableConfig.data.filter(row => !selectedIds.includes(row.id));
|
|
tableConfig.total -= selectedIds.length;
|
|
tableConfig.lastPage = Math.max(1, Math.ceil(tableConfig.total / rowsPerPage));
|
|
if (currentPage > tableConfig.lastPage) {
|
|
currentPage = tableConfig.lastPage;
|
|
}
|
|
renderTable();
|
|
renderPagination();
|
|
showAlert('success', response.data.message || 'Selected promotions deleted successfully.');
|
|
}).catch(error => {
|
|
console.error('Batch delete error:', error);
|
|
const errorMessage = error.response?.data?.message || 'Failed to delete promotions.';
|
|
showAlert('danger', errorMessage);
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
document.querySelectorAll('.clickable-row').forEach(row => {
|
|
row.addEventListener('click', function(e) {
|
|
if (e.target.closest('.rowCheckbox, .edit-btn, .view-btn, .delete-btn')) {
|
|
return;
|
|
}
|
|
const promotionId = this.getAttribute('data-id');
|
|
window.location.href = `/promotions/${promotionId}`;
|
|
});
|
|
});
|
|
}
|
|
|
|
function showAlert(type, message) {
|
|
const alertContainer = document.getElementById('alert-container') || document.querySelector('.card-body');
|
|
const alert = document.createElement('div');
|
|
alert.className = `alert alert-${type} alert-dismissible fade show`;
|
|
alert.innerHTML = `
|
|
${message}
|
|
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
|
`;
|
|
alertContainer.prepend(alert);
|
|
setTimeout(() => alert.remove(), 5000);
|
|
}
|
|
|
|
function updateDeleteButton() {
|
|
const deleteSelected = document.getElementById('deleteSelected');
|
|
const checkedBoxes = document.querySelectorAll('.rowCheckbox:checked');
|
|
if (deleteSelected) {
|
|
deleteSelected.disabled = checkedBoxes.length === 0;
|
|
}
|
|
}
|
|
|
|
renderTable();
|
|
renderPagination();
|
|
</script>
|
|
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script> |