495 lines
18 KiB
PHP
495 lines
18 KiB
PHP
@props([
|
|
'pageTitle' => '',
|
|
'data' => [],
|
|
'columns' => [],
|
|
'actions' => [],
|
|
'showAddButton' => false,
|
|
'addButtonUrl' => '#',
|
|
'showCheckboxes' => false,
|
|
'showBatchDelete' => 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">
|
|
<!-- 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">
|
|
</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 + 1 }}">
|
|
{{ $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;
|
|
}
|
|
.card-header h5 {
|
|
font-weight: 500;
|
|
}
|
|
.table thead th {
|
|
font-weight: 500;
|
|
font-size: 0.8rem;
|
|
}
|
|
.form-label {
|
|
font-weight: 500;
|
|
font-size: 0.9rem;
|
|
}
|
|
.card {
|
|
border-radius: 10px;
|
|
}
|
|
.card-header {
|
|
background-color: transparent;
|
|
}
|
|
.btn-primary {
|
|
background-color: #E74610;
|
|
border-color: #E74610;
|
|
}
|
|
.btn-primary:hover {
|
|
background-color: #E74610;
|
|
border-color: #E74610;
|
|
}
|
|
.sortable {
|
|
cursor: pointer;
|
|
}
|
|
.sortable:hover {
|
|
background-color: #f1f3f5;
|
|
}
|
|
.table {
|
|
font-size: 0.85rem;
|
|
width: 100%;
|
|
table-layout: auto;
|
|
}
|
|
.table th,
|
|
.table td {
|
|
padding: 0.5rem;
|
|
vertical-align: middle;
|
|
white-space: nowrap;
|
|
overflow: hidden;
|
|
text-overflow: ellipsis;
|
|
}
|
|
.table th:first-child,
|
|
.table td:first-child {
|
|
width: 40px;
|
|
text-align: center;
|
|
}
|
|
.table th:last-child,
|
|
.table td:last-child {
|
|
width: 120px;
|
|
text-align: center;
|
|
}
|
|
.table td:nth-child(5),
|
|
.table th:nth-child(5) {
|
|
width: 100px;
|
|
}
|
|
.table td:nth-child(6),
|
|
.table th:nth-child(6) {
|
|
max-width: 200px;
|
|
}
|
|
.table td:nth-child(7),
|
|
.table th:nth-child(7) {
|
|
width: 120px;
|
|
}
|
|
.edit-btn {
|
|
border-color: #E74610;
|
|
color: #E74610;
|
|
}
|
|
.edit-btn:hover {
|
|
background-color: #E74610;
|
|
border-color: #E74610;
|
|
color: #fff;
|
|
}
|
|
.table-hover tbody tr:hover {
|
|
cursor: pointer;
|
|
background-color: #f8f9fa;
|
|
}
|
|
@media (max-width: 576px) {
|
|
.table {
|
|
font-size: 0.75rem;
|
|
}
|
|
.table thead th {
|
|
font-size: 0.75rem;
|
|
}
|
|
.table th,
|
|
.table td {
|
|
padding: 0.3rem;
|
|
}
|
|
.btn-sm {
|
|
padding: 0.2rem 0.4rem;
|
|
font-size: 0.75rem;
|
|
}
|
|
.input-group-sm>.form-control {
|
|
font-size: 0.75rem;
|
|
}
|
|
.pagination-sm .page-link {
|
|
padding: 0.2rem 0.4rem;
|
|
font-size: 0.75rem;
|
|
}
|
|
.table td:nth-child(6) {
|
|
max-width: 150px;
|
|
}
|
|
.form-control,
|
|
.form-select {
|
|
font-size: 0.85rem;
|
|
}
|
|
}
|
|
</style>
|
|
|
|
<script>
|
|
const tableConfig = {
|
|
data: @json($data),
|
|
columns: @json($columns),
|
|
actions: @json($actions),
|
|
showCheckboxes: {{ json_encode($showCheckboxes) }},
|
|
showBatchDelete: {{ json_encode($showBatchDelete) }},
|
|
pageTitle: @json($pageTitle),
|
|
csrfToken: '{{ csrf_token() }}',
|
|
currentPage: {{ $currentPage }},
|
|
lastPage: {{ $lastPage }},
|
|
total: {{ $total }},
|
|
};
|
|
|
|
const rowsPerPage = 5;
|
|
let currentPage = tableConfig.currentPage;
|
|
let filteredRows = [...tableConfig.data];
|
|
let originalRows = [...tableConfig.data].map(row => ({ ...row }));
|
|
let sortDirection = {};
|
|
|
|
function renderTable() {
|
|
const tableBody = document.getElementById('tableBody');
|
|
if (!tableBody) return;
|
|
tableBody.innerHTML = '';
|
|
|
|
const paginatedRows = filteredRows;
|
|
|
|
if (paginatedRows.length === 0) {
|
|
tableBody.innerHTML = `<tr><td colspan="${tableConfig.showCheckboxes ? tableConfig.columns.length + 1 : tableConfig.columns.length}" class="text-center">No data available</td></tr>`;
|
|
} else {
|
|
paginatedRows.forEach(row => {
|
|
const tr = document.createElement('tr');
|
|
tr.setAttribute('data-id', row.topup_uuid || row.id);
|
|
tr.classList.add('clickable-row');
|
|
let rowHtml = '';
|
|
|
|
if (tableConfig.showCheckboxes) {
|
|
rowHtml += `<td class="text-center"><input type="checkbox" class="rowCheckbox" value="${row.topup_uuid || row.id}"></td>`;
|
|
}
|
|
|
|
tableConfig.columns.forEach(col => {
|
|
let value = row[col.key] || '';
|
|
rowHtml += `<td>${value}</td>`;
|
|
});
|
|
|
|
if (tableConfig.actions.length > 0) {
|
|
rowHtml += `<td class="text-center">`;
|
|
tableConfig.actions.forEach(action => {
|
|
if (action === 'edit') {
|
|
rowHtml += `
|
|
<a href="{{ route('top-up.edit', '') }}/${row.topup_uuid || row.id}" class="btn btn-sm edit-btn me-1" title="Edit">
|
|
<i class="fa-solid fa-pen-to-square"></i>
|
|
</a>`;
|
|
} else if (action === 'view') {
|
|
rowHtml += `
|
|
<a href="{{ route('top-up.show', '') }}/${row.topup_uuid || row.id}" class="btn btn-sm btn-outline-secondary me-1 view-btn" title="View">
|
|
<i class="fa-solid fa-eye"></i>
|
|
</a>`;
|
|
} else if (action === 'delete') {
|
|
rowHtml += `
|
|
<button class="btn btn-sm btn-outline-danger delete-btn" title="Delete" data-id="${row.topup_uuid || row.id}">
|
|
<i class="fa-solid fa-trash-can"></i>
|
|
</button>`;
|
|
}
|
|
});
|
|
rowHtml += `</td>`;
|
|
}
|
|
|
|
tr.innerHTML = rowHtml;
|
|
tableBody.appendChild(tr);
|
|
});
|
|
}
|
|
|
|
attachEventListeners();
|
|
updateDeleteButtonState();
|
|
}
|
|
|
|
function renderPagination() {
|
|
const pagination = document.getElementById('pagination');
|
|
if (!pagination) return;
|
|
pagination.innerHTML = '';
|
|
|
|
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) {
|
|
window.location.href = `{{ route('top-up') }}?page=${currentPage - 1}`;
|
|
}
|
|
});
|
|
pagination.appendChild(prevLi);
|
|
|
|
for (let i = 1; i <= tableConfig.lastPage; 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();
|
|
window.location.href = `{{ route('top-up') }}?page=${i}`;
|
|
});
|
|
pagination.appendChild(li);
|
|
}
|
|
|
|
const nextLi = document.createElement('li');
|
|
nextLi.className = `page-item ${currentPage === tableConfig.lastPage ? '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 < tableConfig.lastPage) {
|
|
window.location.href = `{{ route('top-up') }}?page=${currentPage + 1}`;
|
|
}
|
|
});
|
|
pagination.appendChild(nextLi);
|
|
}
|
|
|
|
function updateDeleteButtonState() {
|
|
if (tableConfig.showBatchDelete) {
|
|
const checkboxes = document.querySelectorAll('.rowCheckbox:checked');
|
|
const deleteButton = document.getElementById('deleteSelected');
|
|
if (deleteButton) {
|
|
deleteButton.disabled = checkboxes.length === 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
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) => {
|
|
const aValue = (a[key] || '').toString().toLowerCase();
|
|
const bValue = (b[key] || '').toString().toLowerCase();
|
|
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();
|
|
});
|
|
}
|
|
|
|
if (tableConfig.showCheckboxes) {
|
|
const selectAll = document.getElementById('selectAll');
|
|
if (selectAll) {
|
|
selectAll.addEventListener('change', function() {
|
|
const checkboxes = document.querySelectorAll('.rowCheckbox');
|
|
checkboxes.forEach(checkbox => {
|
|
checkbox.checked = this.checked;
|
|
});
|
|
updateDeleteButtonState();
|
|
});
|
|
}
|
|
|
|
document.querySelectorAll('.rowCheckbox').forEach(checkbox => {
|
|
checkbox.addEventListener('change', updateDeleteButtonState);
|
|
});
|
|
}
|
|
|
|
if (tableConfig.showBatchDelete) {
|
|
const deleteSelected = document.getElementById('deleteSelected');
|
|
if (deleteSelected) {
|
|
deleteSelected.addEventListener('click', function() {
|
|
const checkboxes = document.querySelectorAll('.rowCheckbox:checked');
|
|
const selectedIds = Array.from(checkboxes).map(cb => cb.value);
|
|
|
|
if (selectedIds.length === 0) {
|
|
alert('Please select at least one top-up to delete.');
|
|
return;
|
|
}
|
|
|
|
if (confirm('Are you sure you want to delete the selected top-ups?')) {
|
|
console.log('Submitting batch delete form with UUIDs:', selectedIds);
|
|
const form = document.createElement('form');
|
|
form.method = 'POST';
|
|
form.action = '{{ route('top-up.batchDelete') }}';
|
|
form.style.display = 'none';
|
|
|
|
const csrfInput = document.createElement('input');
|
|
csrfInput.type = 'hidden';
|
|
csrfInput.name = '_token';
|
|
csrfInput.value = tableConfig.csrfToken;
|
|
form.appendChild(csrfInput);
|
|
|
|
const uuidInput = document.createElement('input');
|
|
uuidInput.type = 'hidden';
|
|
uuidInput.name = 'topup_uuid';
|
|
uuidInput.value = JSON.stringify(selectedIds);
|
|
form.appendChild(uuidInput);
|
|
|
|
document.body.appendChild(form);
|
|
form.submit();
|
|
}
|
|
});
|
|
}
|
|
}
|
|
document.querySelectorAll('.delete-btn').forEach(button => {
|
|
button.addEventListener('click', function(e) {
|
|
e.stopPropagation();
|
|
const topupId = this.getAttribute('data-id');
|
|
if (confirm('Are you sure you want to delete this top-up?')) {
|
|
axios.delete(`{{ route('top-up.destroy', '') }}/${topupId}`, {
|
|
headers: {
|
|
'X-CSRF-TOKEN': tableConfig.csrfToken
|
|
}
|
|
}).then(response => {
|
|
if (response.data.status === 'success') {
|
|
filteredRows = filteredRows.filter(row => row.topup_uuid !== topupId);
|
|
tableConfig.data = tableConfig.data.filter(row => row.topup_uuid !== topupId);
|
|
tableConfig.total -= 1;
|
|
tableConfig.lastPage = Math.ceil(tableConfig.total / rowsPerPage);
|
|
|
|
if (filteredRows.length === 0 && currentPage > 1) {
|
|
window.location.href = `{{ route('top-up') }}?page=${currentPage - 1}`;
|
|
} else {
|
|
renderTable();
|
|
renderPagination();
|
|
alert(response.data.message || 'Top-up deleted successfully');
|
|
}
|
|
} else {
|
|
alert(response.data.message || 'Failed to delete top-up.');
|
|
}
|
|
}).catch(error => {
|
|
console.error('Delete error:', error);
|
|
alert(error.response?.data?.message || 'Failed to delete top-up.');
|
|
});
|
|
}
|
|
});
|
|
});
|
|
|
|
document.querySelectorAll('.clickable-row').forEach(row => {
|
|
row.addEventListener('click', function(e) {
|
|
if (e.target.closest('.rowCheckbox, .edit-btn, .view-btn, .delete-btn')) {
|
|
return;
|
|
}
|
|
const topupId = this.getAttribute('data-id');
|
|
window.location.href = `{{ route('top-up.show', '') }}/${topupId}`;
|
|
});
|
|
});
|
|
}
|
|
|
|
renderTable();
|
|
renderPagination();
|
|
</script>
|
|
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script> |