promotion functionality works

This commit is contained in:
armiejean 2025-05-16 13:24:28 +08:00
parent 060f41ada6
commit 50f0663369
7 changed files with 1079 additions and 160 deletions

View File

@ -0,0 +1,402 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Session;
class PromotionController extends Controller
{
protected $apiBaseUrl = 'http://192.168.100.6:8081/api/cms';
public function index(Request $request)
{
try {
$user = Session::get('user');
$accessToken = $user['access_token'] ?? null;
if (!$accessToken) {
Log::info('No access token found, redirecting to login from promotions');
return redirect()->route('login')->with('error', 'Please log in to view promotions.');
}
$page = $request->input('page', 1);
$pageSize = 5;
$search = $request->input('_search', null);
$response = Http::withHeaders([
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $accessToken,
])->get("{$this->apiBaseUrl}/promotion", [
'page' => $page,
'page_size' => $pageSize,
'_search' => $search,
]);
if ($response->status() === 401 || $response->status() === 403) {
Log::warning('Unauthorized or Forbidden API response: ', $response->json());
return redirect()->route('login')->with('error', 'Your session has expired. Please log in again.');
}
$json = $response->json();
if ($response->successful() && isset($json['data']) && is_array($json['data'])) {
$promotions = array_map(function ($promotion) {
return [
'id' => $promotion['promotion_uuid'] ?? null,
'title' => $promotion['title'] ?? '',
'type' => $promotion['type'] ?? '',
'startDate' => $promotion['date_start'] ?? '',
'endDate' => $promotion['date_end'] ?? '',
'status' => $promotion['status'] ?? '',
];
}, $json['data']);
$total = $json['meta']['total'] ?? count($promotions);
$lastPage = $json['meta']['last_page'] ?? ceil($total / $pageSize);
} else {
Log::warning('No promotion data found or invalid API response: ', $json);
$promotions = [];
$total = 0;
$lastPage = 1;
}
return view('pages.promotions', [
'promotions' => $promotions,
'currentPage' => $page,
'lastPage' => $lastPage,
'total' => $total,
'search' => $search,
]);
} catch (\Exception $e) {
Log::error('Error fetching promotions: ' . $e->getMessage());
return view('pages.promotions', [
'promotions' => [],
'currentPage' => 1,
'lastPage' => 1,
'total' => 0,
'search' => $search,
]);
}
}
public function create()
{
try {
$user = Session::get('user');
$accessToken = $user['access_token'] ?? null;
if (!$accessToken) {
return redirect()->route('login')->with('error', 'Please log in to add a promotion.');
}
$response = Http::withHeaders([
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $accessToken,
])->get("{$this->apiBaseUrl}/promoTypes");
$json = $response->json();
Log::info('Promo types API response: ', $json);
$promoTypes = $response->successful() && isset($json['data']) ? $json['data'] : [];
if (empty($promoTypes)) {
Log::warning('Failed to fetch promo types from API, using fallback: ', $json);
$promoTypes = ['1' => 'General', '2' => 'Loyalty Exclusive', '3' => 'User Specific'];
}
return view('pages.add-promotions', ['promoTypes' => $promoTypes]);
} catch (\Exception $e) {
Log::error('Error fetching promo types: ' . $e->getMessage());
return view('pages.add-promotions', ['promoTypes' => ['1' => 'General', '2' => 'Loyalty Exclusive', '3' => 'User Specific']]);
}
}
public function store(Request $request)
{
try {
$user = Session::get('user');
$accessToken = $user['access_token'] ?? null;
if (!$accessToken) {
return redirect()->route('login')->with('error', 'Please log in to add a promotion.');
}
$request->validate([
'title' => 'required|string|max:100',
'type' => 'required',
'promo_type' => 'required|in:1,2,3', // Based on the promo types IDs
'description' => 'required|string',
'image' => 'required|file|mimes:jpeg,png,jpg,gif|max:2048', // 2MB max
'start_date' => 'required|date',
'end_date' => 'required|date|after_or_equal:start_date',
'status' => 'required|in:On Going,Done',
'is_toppromotion' => 'nullable|in:1,0',
'is_gps' => 'nullable|in:1,0',
]);
$payload = [
'title' => $request->input('title'),
'type' => $request->input('type'),
'promo_type' => $request->input('promo_type'),
'description' => $request->input('description'),
'date_start' => $request->input('start_date'),
'date_end' => $request->input('end_date'),
'status' => $request->input('status'),
'is_toppromotion' => $request->has('is_toppromotion') ? 1 : 0,
'is_gps' => $request->has('is_gps') ? 1 : 0,
'station_uuid' => $request->input('station_uuid', '[]'),
];
// Handle the image upload
if ($request->hasFile('image')) {
$image = $request->file('image');
$payload['image'] = fopen($image->getPathname(), 'r');
}
Log::info('Data being sent to API for creating promotion: ', $payload);
$response = Http::withHeaders([
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $accessToken,
])->attach('image', $payload['image'], $image->getClientOriginalName())
->post("{$this->apiBaseUrl}/promotion", array_diff_key($payload, ['image' => null]));
$json = $response->json();
Log::info('API response for creating promotion: ', $json);
if ($response->successful()) {
Log::info('Promotion created successfully: ', $json);
return redirect()->route('promotions')->with('success', $json['message'] ?? 'Promotion added successfully.');
} else {
$errorMessage = $json['message'] ?? 'Failed to add promotion. Please try again.';
if (isset($json['errors'])) {
$errorMessage .= ' Errors: ' . json_encode($json['errors']);
}
throw new \Exception($errorMessage);
}
} catch (\Exception $e) {
Log::error('Error creating promotion: ' . $e->getMessage());
return redirect()->back()->with('error', 'An error occurred while adding the promotion: ' . $e->getMessage());
}
}
public function show($uuid)
{
try {
$user = Session::get('user');
$accessToken = $user['access_token'] ?? null;
if (!$accessToken) {
return redirect()->route('login')->with('error', 'Please log in to view a promotion.');
}
$response = Http::withHeaders([
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $accessToken,
])->get("{$this->apiBaseUrl}/promotion/{$uuid}");
$json = $response->json();
if ($response->successful() && isset($json['data'])) {
$promotion = [
'id' => $json['data']['promotion_uuid'] ?? null,
'title' => $json['data']['title'] ?? '',
'type' => $json['data']['type'] ?? '',
'startDate' => $json['data']['date_start'] ?? '',
'endDate' => $json['data']['date_end'] ?? '',
'status' => $json['data']['status'] ?? '',
];
return view('pages.promotion-view', ['promotion' => $promotion]);
} else {
Log::warning('No promotion found or invalid API response: ', $json);
return redirect()->back()->with('error', 'Promotion not found.');
}
} catch (\Exception $e) {
Log::error('Error fetching promotion for view: ' . $e->getMessage());
return redirect()->back()->with('error', 'An error occurred while loading the promotion.');
}
}
public function edit($uuid)
{
try {
$user = Session::get('user');
$accessToken = $user['access_token'] ?? null;
if (!$accessToken) {
return redirect()->route('login')->with('error', 'Please log in to edit a promotion.');
}
$response = Http::withHeaders([
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $accessToken,
])->get("{$this->apiBaseUrl}/promotion/{$uuid}");
$json = $response->json();
if ($response->successful() && isset($json['data'])) {
$promotion = [
'id' => $json['data']['promotion_uuid'] ?? null,
'title' => $json['data']['title'] ?? '',
'type' => $json['data']['type'] ?? '',
'description' => $json['data']['description'] ?? '',
'image' => $json['data']['image'] ?? '',
'startDate' => $json['data']['date_start'] ?? '',
'endDate' => $json['data']['date_end'] ?? '',
'status' => $json['data']['status'] ?? '',
'is_toppromotion' => $json['data']['is_toppromotion'] ?? 0,
'is_gps' => $json['data']['is_gps'] ?? 0,
];
// Determine promo_type ID based on type
$typeResponse = Http::withHeaders([
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $accessToken,
])->get("{$this->apiBaseUrl}/promoTypes");
$promoTypes = $typeResponse->successful() ? $typeResponse->json()['data'] ?? [] : ['1' => 'General', '2' => 'Loyalty Exclusive', '3' => 'User Specific'];
$promotion['promo_type'] = array_search($promotion['type'], $promoTypes) ?: '';
return view('pages.promotion-edit', ['promotion' => $promotion, 'promoTypes' => $promoTypes]);
} else {
Log::warning('No promotion found or invalid API response: ', $json);
return redirect()->back()->with('error', 'Promotion not found.');
}
} catch (\Exception $e) {
Log::error('Error fetching promotion for edit: ' . $e->getMessage());
return redirect()->back()->with('error', 'An error occurred while loading the promotion.');
}
}
public function update(Request $request, $uuid)
{
try {
$user = Session::get('user');
$accessToken = $user['access_token'] ?? null;
if (!$accessToken) {
return redirect()->route('login')->with('error', 'Please log in to update a promotion.');
}
$request->validate([
'title' => 'required|string|max:100',
'type' => 'required',
'promo_type' => 'required|in:1,2,3',
'description' => 'required|string',
'image' => 'nullable|file|mimes:jpeg,png,jpg,gif|max:2048',
'start_date' => 'required|date',
'end_date' => 'required|date|after_or_equal:start_date',
'status' => 'required|in:On Going,Done',
'is_toppromotion' => 'nullable|in:1,0',
'is_gps' => 'nullable|in:1,0',
]);
$payload = [
'title' => $request->input('title'),
'type' => $request->input('type'),
'promo_type' => $request->input('promo_type'),
'description' => $request->input('description'),
'date_start' => $request->input('start_date'),
'date_end' => $request->input('end_date'),
'status' => $request->input('status'),
'is_toppromotion' => $request->has('is_toppromotion') ? 1 : 0,
'is_gps' => $request->has('is_gps') ? 1 : 0,
'station_uuid' => $request->input('station_uuid', '[]'),
];
$client = Http::withHeaders([
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $accessToken,
]);
if ($request->hasFile('image')) {
$image = $request->file('image');
$payload['image'] = fopen($image->getPathname(), 'r');
$response = $client->attach('image', $payload['image'], $image->getClientOriginalName())
->post("{$this->apiBaseUrl}/updatePromotion/{$uuid}", array_diff_key($payload, ['image' => null]));
} else {
$response = $client->post("{$this->apiBaseUrl}/updatePromotion/{$uuid}", $payload);
}
$json = $response->json();
Log::info('API response for updating promotion: ', $json);
if ($response->successful()) {
Log::info('Promotion updated successfully: ', $json);
return redirect()->route('promotions')->with('success', $json['message'] ?? 'Promotion updated successfully.');
} else {
$errorMessage = $json['message'] ?? 'Failed to update promotion. Please try again.';
if (isset($json['errors'])) {
$errorMessage .= ' Errors: ' . json_encode($json['errors']);
}
return redirect()->back()->with('error', $errorMessage);
}
} catch (\Exception $e) {
Log::error('Error updating promotion: ' . $e->getMessage());
return redirect()->back()->with('error', 'An error occurred while updating the promotion: ' . $e->getMessage());
}
}
public function destroy($uuid)
{
try {
$user = Session::get('user');
$accessToken = $user['access_token'] ?? null;
if (!$accessToken) {
return redirect()->route('login')->with('error', 'Please log in to delete a promotion.');
}
$response = Http::withHeaders([
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $accessToken,
])->delete("{$this->apiBaseUrl}/promotion/{$uuid}");
if ($response->successful()) {
Log::info('Promotion deleted successfully: ' . $uuid);
return redirect()->route('promotions')->with('success', 'Promotion deleted successfully.');
} else {
Log::warning('Failed to delete promotion: ', $response->json());
return redirect()->back()->with('error', $response->json()['message'] ?? 'Failed to delete promotion. Please try again.');
}
} catch (\Exception $e) {
Log::error('Error deleting promotion: ' . $e->getMessage());
return redirect()->back()->with('error', 'An error occurred while deleting the promotion.');
}
}
public function batchDelete(Request $request)
{
try {
$user = Session::get('user');
$accessToken = $user['access_token'] ?? null;
if (!$accessToken) {
return redirect()->route('login')->with('error', 'Please log in to delete promotions.');
}
$uuids = $request->input('promotion_uuid', []);
if (empty($uuids)) {
return redirect()->back()->with('error', 'No promotions selected for deletion.');
}
$response = Http::withHeaders([
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $accessToken,
])->delete("{$this->apiBaseUrl}/promotionBatchDelete", [
'promotion_uuid' => $uuids,
]);
if ($response->successful()) {
Log::info('Promotions batch deleted successfully: ', $uuids);
return redirect()->route('promotions')->with('success', 'Selected promotions deleted successfully.');
} else {
Log::warning('Failed to batch delete promotions: ', $response->json());
return redirect()->back()->with('error', $response->json()['message'] ?? 'Failed to delete promotions. Please try again.');
}
} catch (\Exception $e) {
Log::error('Error batch deleting promotions: ' . $e->getMessage());
return redirect()->back()->with('error', 'An error occurred while deleting the promotions.');
}
}
}

View File

@ -0,0 +1,391 @@
@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">
<!-- 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 originalRows = [...tableConfig.data].map(row => ({ ...row }));
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.ceil(filteredRows.length / rowsPerPage);
const routeName = tableConfig.pageTitle === 'Promotions' ? 'promotions.index' : '';
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--;
window.location.href = `/promotions?page=${currentPage}&_search=${tableConfig.search || ''}`;
}
});
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;
window.location.href = `/promotions?page=${currentPage}&_search=${tableConfig.search || ''}`;
});
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++;
window.location.href = `/promotions?page=${currentPage}&_search=${tableConfig.search || ''}`;
}
});
pagination.appendChild(nextLi);
}
function updateNoDataMessage() {
const noDataMessage = document.getElementById('no-data-message');
if (noDataMessage) {
noDataMessage.style.display = filteredRows.length === 0 ? 'block' : 'none';
}
}
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();
const url = new URL(window.location);
if (searchTerm) {
url.searchParams.set('_search', searchTerm);
} else {
url.searchParams.delete('_search');
}
window.history.pushState({}, '', url);
});
}
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 url = new URL(window.location);
url.searchParams.delete('_search');
window.history.pushState({}, '', url);
});
}
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 (deleteSelected) {
deleteSelected.addEventListener('click', function() {
const selectedUuids = Array.from(document.querySelectorAll('.rowCheckbox:checked')).map(cb => cb.value);
if (selectedUuids.length > 0 && confirm('Are you sure you want to delete the selected promotions?')) {
fetch('/promotions/batch-delete', {
method: 'POST',
headers: {
'X-CSRF-TOKEN': '{{ csrf_token() }}',
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify({ promotion_uuid: selectedUuids })
}).then(response => response.json())
.then(data => {
if (data.success) {
location.reload();
} else {
alert(data.message || 'Error deleting promotions');
}
})
.catch(error => {
console.error('Error:', error);
alert('An error occurred while deleting the promotions.');
});
}
});
}
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 updateDeleteButton() {
const deleteSelected = document.getElementById('deleteSelected');
const checkedBoxes = document.querySelectorAll('.rowCheckbox:checked');
deleteSelected.disabled = checkedBoxes.length === 0;
}
renderTable();
renderPagination();
</script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

View File

@ -3,102 +3,119 @@
@section('page_title', 'Add Promotion')
@section('content')
<div class="card-header border-0 bg-transparent py-2">
<h5 class="mb-0 fw-bold text-dark" style="font-size: 1.25rem;">Add Promotion</h5>
</div>
<div class="row justify-content-center">
<div class="card-body p-3">
<form id="addPromotionForm">
<div class="card">
<div class="card-header">
<h5 class="mb-0 fw-bold text-dark">Add New Promotion</h5>
</div>
<div class="card-body">
@if (session('error'))
<div class="alert alert-danger alert-dismissible fade show" role="alert">
{{ session('error') }}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
@endif
@if (session('success'))
<div class="alert alert-success alert-dismissible fade show" role="alert">
{{ session('success') }}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
@endif
<form action="{{ route('promotions.store') }}" method="POST" enctype="multipart/form-data">
@csrf
<div class="mb-3">
<label for="title" class="form-label">Title</label>
<input type="text" class="form-control" id="title" placeholder="Enter title" required>
<input type="text" class="form-control" id="title" name="title" value="{{ old('title') }}" required maxlength="100">
@error('title')
<div class="text-danger">{{ $message }}</div>
@enderror
</div>
<div class="mb-3">
<label for="type" class="form-label">Type</label>
<select class="form-select" id="type" required>
<option value="" disabled selected>Select type</option>
<option value="Discount">Discount</option>
<option value="Flash Sale">Flash Sale</option>
<option value="Reward">Reward</option>
<option value="Bundle">Bundle</option>
<select class="form-select" id="type" name="type" required>
@foreach ($promoTypes as $key => $type)
<option value="{{ $type }}" data-id="{{ $key }}" {{ old('type') == $type ? 'selected' : '' }}>{{ $type }}</option>
@endforeach
</select>
<input type="hidden" name="promo_type" id="promo_type" value="{{ old('promo_type') }}">
@error('type')
<div class="text-danger">{{ $message }}</div>
@enderror
@error('promo_type')
<div class="text-danger">{{ $message }}</div>
@enderror
</div>
<div class="mb-3">
<label for="startDate" class="form-label">Start Date</label>
<input type="date" class="form-control" id="startDate" required>
<label for="description" class="form-label">Description</label>
<textarea class="form-control" id="description" name="description" required>{{ old('description') }}</textarea>
@error('description')
<div class="text-danger">{{ $message }}</div>
@enderror
</div>
<div class="mb-3">
<label for="endDate" class="form-label">End Date</label>
<input type="date" class="form-control" id="endDate" required>
<label for="image" class="form-label">Image</label>
<input type="file" class="form-control" id="image" name="image" accept="image/*" required>
@error('image')
<div class="text-danger">{{ $message }}</div>
@enderror
</div>
<div class="mb-3">
<label for="start_date" class="form-label">Start Date</label>
<input type="date" class="form-control" id="start_date" name="start_date" value="{{ old('start_date') }}" required>
@error('start_date')
<div class="text-danger">{{ $message }}</div>
@enderror
</div>
<div class="mb-3">
<label for="end_date" class="form-label">End Date</label>
<input type="date" class="form-control" id="end_date" name="end_date" value="{{ old('end_date') }}" required>
@error('end_date')
<div class="text-danger">{{ $message }}</div>
@enderror
</div>
<div class="mb-3">
<label for="status" class="form-label">Status</label>
<select class="form-select" id="status" required>
<option value="" disabled selected>Select status</option>
<option value="Done">Done</option>
<option value="On Going">On Going</option>
<select class="form-select" id="status" name="status" required>
<option value="On Going" {{ old('status') == 'On Going' ? 'selected' : '' }}>On Going</option>
<option value="Done" {{ old('status') == 'Done' ? 'selected' : '' }}>Done</option>
</select>
@error('status')
<div class="text-danger">{{ $message }}</div>
@enderror
</div>
<div class="d-flex justify-content-end mt-3">
<button type="button" class="btn btn-outline-secondary me-2" style="margin-right:5px">Cancel</button>
<button type="submit" class="btn btn-primary">Add Promotion</button>
<div class="mb-3 form-check">
<input type="checkbox" class="form-check-input" id="is_toppromotion" name="is_toppromotion" value="1" {{ old('is_toppromotion') ? 'checked' : '' }}>
<label class="form-check-label" for="is_toppromotion">Is Top Promotion</label>
@error('is_toppromotion')
<div class="text-danger">{{ $message }}</div>
@enderror
</div>
<div class="mb-3 form-check">
<input type="checkbox" class="form-check-input" id="is_gps" name="is_gps" value="1" {{ old('is_gps') ? 'checked' : '' }}>
<label class="form-check-label" for="is_gps">Is GPS-Based</label>
@error('is_gps')
<div class="text-danger">{{ $message }}</div>
@enderror
</div>
<input type="hidden" name="station_uuid" value="[]">
<button type="submit" class="btn btn-primary">Add Promotion</button>
<a href="{{ route('promotions') }}" class="btn btn-secondary ms-2">Cancel</a>
</form>
</div>
</div>
<style>
.card {
border-radius: 5px;
border: 1px solid #dee2e6;
}
.form-label {
font-size: 0.95rem;
}
.form-control,
.form-select {
font-size: 0.9rem;
width: 100%;
}
</style>
<script>
document.getElementById('addPromotionForm').addEventListener('submit', function(e) {
e.preventDefault();
const title = document.getElementById('title').value;
const type = document.getElementById('type').value;
const startDate = document.getElementById('startDate').value;
const endDate = document.getElementById('endDate').value;
const status = document.getElementById('status').value;
if (!title || !type || !startDate || !endDate || !status) {
alert('Please fill out all fields.');
return;
}
// Simulate adding promotion (frontend-only)
const newPromotion = {
id: Date.now(),
title: title,
type: type,
startDate: startDate,
endDate: endDate,
status: status
};
// Store in sessionStorage
let promotions = JSON.parse(sessionStorage.getItem('promotions') || '[]');
promotions.push(newPromotion);
sessionStorage.setItem('promotions', JSON.stringify(promotions));
alert('Promotion added successfully!');
window.location.href = '/promotions';
document.getElementById('type').addEventListener('change', function() {
const selectedOption = this.options[this.selectedIndex];
const promoTypeId = selectedOption.getAttribute('data-id');
document.getElementById('promo_type').value = promoTypeId;
});
// Cancel button click handler
document.querySelector('.btn-outline-secondary').addEventListener('click', function() {
window.location.href = '/promotions';
// Set initial promo_type value on page load
window.addEventListener('load', function() {
const typeSelect = document.getElementById('type');
const selectedOption = typeSelect.options[typeSelect.selectedIndex];
const promoTypeId = selectedOption.getAttribute('data-id');
document.getElementById('promo_type').value = promoTypeId;
});
</script>
@endsection

View File

@ -0,0 +1,120 @@
@extends('layouts.app')
@section('page_title', 'Edit Promotion')
@section('content')
<div class="card">
<div class="card-header">
<h5 class="mb-0 fw-bold text-dark">Edit Promotion</h5>
</div>
<div class="card-body">
@if (session('error'))
<div class="alert alert-danger alert-dismissible fade show" role="alert">
{{ session('error') }}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
@endif
@if (session('success'))
<div class="alert alert-success alert-dismissible fade show" role="alert">
{{ session('success') }}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
@endif
<form action="{{ route('promotions.update', $promotion['id']) }}" method="POST" enctype="multipart/form-data">
@csrf
@method('PUT')
<div class="mb-3">
<label for="title" class="form-label">Title</label>
<input type="text" class="form-control" id="title" name="title" value="{{ $promotion['title'] }}" required maxlength="100">
@error('title')
<div class="text-danger">{{ $message }}</div>
@enderror
</div>
<div class="mb-3">
<label for="type" class="form-label">Type</label>
<select class="form-select" id="type" name="type" required>
@foreach ($promoTypes as $key => $type)
<option value="{{ $type }}" data-id="{{ $key }}" {{ $promotion['type'] == $type ? 'selected' : '' }}>{{ $type }}</option>
@endforeach
</select>
<input type="hidden" name="promo_type" id="promo_type" value="{{ $promotion['promo_type'] }}">
@error('type')
<div class="text-danger">{{ $message }}</div>
@enderror
@error('promo_type')
<div class="text-danger">{{ $message }}</div>
@enderror
</div>
<div class="mb-3">
<label for="description" class="form-label">Description</label>
<textarea class="form-control" id="description" name="description" required>{{ $promotion['description'] }}</textarea>
@error('description')
<div class="text-danger">{{ $message }}</div>
@enderror
</div>
<div class="mb-3">
<label for="image" class="form-label">Image</label>
@if ($promotion['image'])
<div class="mb-2">
<p>Current Image: <a href="{{ $promotion['image'] }}" target="_blank">View Image</a></p>
</div>
@endif
<input type="file" class="form-control" id="image" name="image" accept="image/*">
<small class="form-text text-muted">Leave blank to keep the current image.</small>
@error('image')
<div class="text-danger">{{ $message }}</div>
@enderror
</div>
<div class="mb-3">
<label for="start_date" class="form-label">Start Date</label>
<input type="date" class="form-control" id="start_date" name="start_date" value="{{ $promotion['startDate'] }}" required>
@error('start_date')
<div class="text-danger">{{ $message }}</div>
@enderror
</div>
<div class="mb-3">
<label for="end_date" class="form-label">End Date</label>
<input type="date" class="form-control" id="end_date" name="end_date" value="{{ $promotion['endDate'] }}" required>
@error('end_date')
<div class="text-danger">{{ $message }}</div>
@enderror
</div>
<div class="mb-3">
<label for="status" class="form-label">Status</label>
<select class="form-select" id="status" name="status" required>
<option value="On Going" {{ $promotion['status'] == 'On Going' ? 'selected' : '' }}>On Going</option>
<option value="Done" {{ $promotion['status'] == 'Done' ? 'selected' : '' }}>Done</option>
</select>
@error('status')
<div class="text-danger">{{ $message }}</div>
@enderror
</div>
<div class="mb-3 form-check">
<input type="checkbox" class="form-check-input" id="is_toppromotion" name="is_toppromotion" value="1" {{ $promotion['is_toppromotion'] ? 'checked' : '' }}>
<label class="form-check-label" for="is_toppromotion">Is Top Promotion</label>
@error('is_toppromotion')
<div class="text-danger">{{ $message }}</div>
@enderror
</div>
<div class="mb-3 form-check">
<input type="checkbox" class="form-check-input" id="is_gps" name="is_gps" value="1" {{ $promotion['is_gps'] ? 'checked' : '' }}>
<label class="form-check-label" for="is_gps">Is GPS-Based</label>
@error('is_gps')
<div class="text-danger">{{ $message }}</div>
@enderror
</div>
<input type="hidden" name="station_uuid" value="[]">
<button type="submit" class="btn btn-primary">Update Promotion</button>
<a href="{{ route('promotions') }}" class="btn btn-secondary ms-2">Cancel</a>
</form>
</div>
</div>
<script>
document.getElementById('type').addEventListener('change', function() {
const selectedOption = this.options[this.selectedIndex];
const promoTypeId = selectedOption.getAttribute('data-id');
document.getElementById('promo_type').value = promoTypeId;
});
</script>
@endsection

View File

@ -0,0 +1,25 @@
@extends('layouts.app')
@section('page_title', 'View Promotion')
@section('content')
<div class="card">
<div class="card-header">
<h5 class="mb-0 fw-bold text-dark">View Promotion</h5>
</div>
<div class="card-body">
@if (session('error'))
<div class="alert alert-danger alert-dismissible fade show" role="alert">
{{ session('error') }}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
@endif
<div class="mb-3"><strong>Title:</strong> {{ $promotion['title'] }}</div>
<div class="mb-3"><strong>Type:</strong> {{ $promotion['type'] }}</div>
<div class="mb-3"><strong>Start Date:</strong> {{ $promotion['startDate'] }}</div>
<div class="mb-3"><strong>End Date:</strong> {{ $promotion['endDate'] }}</div>
<div class="mb-3"><strong>Status:</strong> {{ $promotion['status'] }}</div>
<a href="{{ route('promotions') }}" class="btn btn-secondary">Back</a>
</div>
</div>
@endsection

View File

@ -3,85 +3,42 @@
@section('page_title', 'Promotions')
@section('content')
@php
$promotions = [
[
'id' => 1,
'title' => 'Spring Sale',
'type' => 'Discount',
'startDate' => '2025-04-10',
'endDate' => '2025-04-20',
'status' => 'Done'
<div id="promotions-table">
@if (session('success'))
<div class="alert alert-success alert-dismissible fade show" role="alert">
{{ session('success') }}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
@endif
@if (session('error'))
<div class="alert alert-danger alert-dismissible fade show" role="alert">
{{ session('error') }}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
@endif
@include('components.promotion-component', [
'pageTitle' => 'Promotions',
'data' => $promotions ?? [],
'columns' => [
['name' => 'Title', 'key' => 'title', 'sortable' => true],
['name' => 'Type', 'key' => 'type', 'sortable' => true],
['name' => 'Start Date', 'key' => 'startDate', 'sortable' => true],
['name' => 'End Date', 'key' => 'endDate', 'sortable' => true],
['name' => 'Status', 'key' => 'status', 'sortable' => true]
],
[
'id' => 2,
'title' => 'Flash Deal',
'type' => 'Flash Sale',
'startDate' => '2025-04-15',
'endDate' => '2025-04-17',
'status' => 'On Going'
],
[
'id' => 3,
'title' => 'Loyalty Promo',
'type' => 'Reward',
'startDate' => '2025-04-20',
'endDate' => '2025-05-01',
'status' => 'Done'
],
[
'id' => 4,
'title' => 'Holiday Bundle',
'type' => 'Bundle',
'startDate' => '2025-04-25',
'endDate' => '2025-05-05',
'status' => 'On Going'
],
[
'id' => 5,
'title' => 'Back-to-School',
'type' => 'Discount',
'startDate' => '2025-04-30',
'endDate' => '2025-05-10',
'status' => 'Done'
],
[
'id' => 6,
'title' => 'Clearance Sale',
'type' => 'Flash Sale',
'startDate' => '2025-05-01',
'endDate' => '2025-05-03',
'status' => 'On Going'
]
];
@endphp
@include('components.table-component', [
'pageTitle' => 'Promotions',
'data' => $promotions,
'columns' => [
['name' => 'Title', 'key' => 'title', 'sortable' => true],
['name' => 'Type', 'key' => 'type', 'sortable' => true],
['name' => 'Start Date', 'key' => 'startDate', 'sortable' => true],
['name' => 'End Date', 'key' => 'endDate', 'sortable' => true],
['name' => 'Status', 'key' => 'status', 'sortable' => true]
],
'actions' => ['edit', 'view', 'delete'],
'showAddButton' => true,
'addButtonUrl' => '/add-promotions',
'showCheckboxes' => true,
'showBatchDelete' => true,
'showEditModal' => true,
'showViewModal' => true
])
<script>
const storedPromotions = JSON.parse(sessionStorage.getItem('promotions') || '[]');
if (storedPromotions.length > 0) {
const tableConfig = window.tableConfig || {};
tableConfig.data = [...tableConfig.data, ...storedPromotions];
window.renderTable();
window.renderPagination();
}
</script>
'actions' => ['edit', 'view', 'delete'],
'showAddButton' => true,
'addButtonUrl' => route('promotions.create'),
'showCheckboxes' => true,
'showBatchDelete' => true,
'showEditModal' => false,
'showViewModal' => false,
'currentPage' => $currentPage ?? 1,
'lastPage' => $lastPage ?? 1,
'total' => $total ?? 0,
])
<div id="no-data-message" style="display: {{ empty($promotions ?? []) ? 'block' : 'none' }}; text-align: center; margin-top: 20px;">
<p>No promotions found.</p>
</div>
</div>
@endsection

View File

@ -8,6 +8,9 @@ use App\Http\Controllers\PhotoSliderController;
use App\Http\Controllers\TopUpController;
use App\Http\Controllers\NotificationController;
use App\Http\Controllers\CardMemberController;
use App\Http\Controllers\PromotionController;
Route::get('/', function () {
return redirect()->route('login');
@ -34,12 +37,6 @@ Route::get('/locked-accounts', function () {
return view('pages.member management.locked-accounts');
})->name('locked-accounts');
Route::get('/promotions', function () {
return view('pages.promotions');
})->name('promotions');
Route::get('/top-up', function () {
return view('pages.top-up');
})->name('top-up');
@ -189,3 +186,13 @@ Route::get('/locked-account', [CardMemberController::class, 'lockedAccounts'])->
Route::get('/card-member/{uuid}', [CardMemberController::class, 'show'])->name('card-member.show');
Route::get('/locked-account/{uuid}', [CardMemberController::class, 'show'])->name('locked-account.show');
Route::post('/locked-account/activate/{uuid}', [CardMemberController::class, 'activate'])->name('locked-account.activate');
//Promotion
Route::get('/promotions', [PromotionController::class, 'index'])->name('promotions');
Route::get('/promotions/create', [PromotionController::class, 'create'])->name('promotions.create');
Route::post('/promotions', [PromotionController::class, 'store'])->name('promotions.store');
Route::get('/promotions/{uuid}', [PromotionController::class, 'show'])->name('promotions.show');
Route::get('/promotions/{uuid}/edit', [PromotionController::class, 'edit'])->name('promotions.edit');
Route::put('/promotions/{uuid}', [PromotionController::class, 'update'])->name('promotions.update');
Route::delete('/promotions/{uuid}', [PromotionController::class, 'destroy'])->name('promotions.destroy');
Route::post('/promotions/batch-delete', [PromotionController::class, 'batchDelete'])->name('promotions.batch-delete');