This commit is contained in:
Thanakorn S
2026-04-25 16:02:55 +07:00
parent 3cc5baba3f
commit 76b9b2a2cb
12 changed files with 1625 additions and 669 deletions
+310 -417
View File
@@ -2,496 +2,389 @@
session_start();
require '../config.php';
require '../include_header.php';
?>
<body>
<?php require '../include_topbar.php';?>
<?php require '../include_sidebar.php';?>
<!-- MAIN CONTENT -->
<main id="content" class="content py-15">
<div class="container-fluid">
<div class="row ">
<!-- Page header -->
<div class="row">
<div class="col-12">
<div class="mb-6">
<h1 class="fs-3 mb-1">Dashboard</h1>
<p>Your main content goes here…</p>
<div class="d-flex flex-column flex-md-row justify-content-between align-items-md-center mb-6 gap-3">
<div>
<h1 class="fs-3 mb-1">Dashboard</h1>
<p class="mb-0">Inventory overview across all warehouses</p>
</div>
<div class="d-flex align-items-center gap-3">
<label class="mb-0 text-nowrap fw-semibold">Month</label>
<select id="month_filter" class="form-select" style="min-width:160px;">
<option value="">Loading...</option>
</select>
</div>
</div>
</div>
</div>
<!-- Stat cards -->
<div class="row g-5 mb-5">
<div class="col-lg-3 col-12">
<div class="card p-4 bg-primary bg-opacity-10 border border-primary border-opacity-25 rounded-2">
<div class="d-flex gap-3 ">
<div class="col-lg-3 col-sm-6 col-12">
<div class="card p-4 bg-primary bg-opacity-10 border border-primary border-opacity-25 rounded-2" style="position:relative;">
<div class="d-flex gap-3">
<div class="icon-shape icon-md bg-primary text-white rounded-2">
<i class="ti ti-report-analytics fs-4"></i>
<i class="ti ti-trending-up fs-4"></i>
</div>
<div>
<h2 class="mb-3 fs-6">Total Sales</h2>
<h3 class="fw-bold mb-0">$25,000</h3>
<p class="text-primary mb-0 small">+5% since last month</p>
<h2 class="mb-3 fs-6">Total Stock In</h2>
<h3 class="fw-bold mb-0" id="stat_total_in">—</h3>
<p class="text-primary mb-0 small">Units received this month</p>
</div>
</div>
<div class="card-loader-overlay" id="overlay_in">
<div class="spinner-border spinner-border-sm text-primary"></div>
</div>
</div>
</div>
<div class="col-lg-3 col-12">
<div class="card p-4 bg-success bg-opacity-10 border border-success border-opacity-25 rounded-2">
<div class="d-flex gap-3 ">
<div class="col-lg-3 col-sm-6 col-12">
<div class="card p-4 bg-danger bg-opacity-10 border border-danger border-opacity-25 rounded-2" style="position:relative;">
<div class="d-flex gap-3">
<div class="icon-shape icon-md bg-danger text-white rounded-2">
<i class="ti ti-trending-down fs-4"></i>
</div>
<div>
<h2 class="mb-3 fs-6">Total Stock Out</h2>
<h3 class="fw-bold mb-0" id="stat_total_out">—</h3>
<p class="text-danger mb-0 small">Units dispatched this month</p>
</div>
</div>
<div class="card-loader-overlay" id="overlay_out">
<div class="spinner-border spinner-border-sm text-primary"></div>
</div>
</div>
</div>
<div class="col-lg-3 col-sm-6 col-12">
<div class="card p-4 bg-success bg-opacity-10 border border-success border-opacity-25 rounded-2" style="position:relative;">
<div class="d-flex gap-3">
<div class="icon-shape icon-md bg-success text-white rounded-2">
<i class="ti ti-repeat fs-4"></i>
<i class="ti ti-package fs-4"></i>
</div>
<div>
<h2 class="mb-3 fs-6">Total Purchase</h2>
<h3 class="fw-bold mb-0">$18,000</h3>
<p class="text-success mb-0 small">+22% since last month</p>
<h2 class="mb-3 fs-6">Active Products</h2>
<h3 class="fw-bold mb-0" id="stat_active_products">—</h3>
<p class="text-success mb-0 small">SKUs with movement</p>
</div>
</div>
</div>
</div>
<div class="col-lg-3 col-12">
<div class="card p-4 bg-info bg-opacity-10 border border-info border-opacity-25 rounded-2">
<div class="d-flex gap-3 ">
<div class="icon-shape icon-md bg-info text-white rounded-2">
<i class="ti ti-currency-dollar fs-4"></i>
</div>
<div>
<h2 class="mb-3 fs-6">Total Expenses</h2>
<h3 class="fw-bold mb-0">$9,000</h3>
<p class="text-info mb-0 small">+10% since last month</p>
</div>
<div class="card-loader-overlay" id="overlay_active">
<div class="spinner-border spinner-border-sm text-primary"></div>
</div>
</div>
</div>
<div class="col-lg-3 col-12">
<div class="card p-4 bg-warning bg-opacity-10 border border-warning border-opacity-25 rounded-2">
<div class="d-flex gap-3 ">
<div class="col-lg-3 col-sm-6 col-12">
<div class="card p-4 bg-warning bg-opacity-10 border border-warning border-opacity-25 rounded-2" style="position:relative;">
<div class="d-flex gap-3">
<div class="icon-shape icon-md bg-warning text-white rounded-2">
<i class="ti ti-notes fs-4"></i>
<i class="ti ti-alert-triangle fs-4"></i>
</div>
<div>
<h2 class="mb-3 fs-6">Invoice Due</h2>
<h3 class="fw-bold mb-0">$25,000</h3>
<p class="text-warning mb-0 small">+35% since last month</p>
<h2 class="mb-3 fs-6">Low Stock Alerts</h2>
<h3 class="fw-bold mb-0" id="stat_low_stock">—</h3>
<p class="text-warning mb-0 small">Products below min stock</p>
</div>
</div>
<div class="card-loader-overlay" id="overlay_low">
<div class="spinner-border spinner-border-sm text-primary"></div>
</div>
</div>
</div>
</div>
<!-- Stock Movement Chart -->
<div class="row g-5 mb-5">
<div class="col-lg-4 col-12">
<div class="card">
<div class="card-body p-6">
<div class="d-flex justify-content-between border-bottom pb-5 mb-3">
<div>
<h3 class="fw-bold h4">$25,458</h3>
<span>Total Profit</span>
</div>
<div>
<i class="ti ti-layers-subtract fs-1 text-primary"></i>
</div>
</div>
<div class="d-flex justify-content-between align-items-center small">
<div class="text-muted"><span class="text-success">+35%</span> vs Last Month</div>
<div><a href="#" class="link-primary text-decoration-underline">View</a></div>
</div>
<div class="col-12">
<div class="card" style="position:relative;">
<div class="card-body p-5">
<h3 class="h5 mb-0">Stock Movement</h3>
<p class="mb-4 small text-secondary">Last 12 months across all warehouses</p>
<div id="ics_dashboardMovementChart"></div>
</div>
</div>
</div>
<div class="col-lg-4 col-12">
<div class="card">
<div class="card-body p-6">
<div class="d-flex justify-content-between border-bottom pb-5 mb-3">
<div>
<h3 class="fw-bold h4">$45,458</h3>
<span>Total Payment Returns</span>
</div>
<div>
<i class="ti ti-credit-card fs-1 text-danger"></i>
</div>
</div>
<div class="d-flex justify-content-between align-items-center small">
<div class="text-muted"><span class="text-danger">-20%</span> vs Last Month</div>
<div><a href="#" class="link-primary text-decoration-underline">View</a></div>
</div>
</div>
</div>
</div>
<div class="col-lg-4 col-12">
<div class="card">
<div class="card-body p-6">
<div class="d-flex justify-content-between border-bottom pb-5 mb-3">
<div>
<h3 class="fw-bold h4">$34,458</h3>
<span>Total Expenses</span>
</div>
<div>
<i class="ti ti-cash-banknote fs-1 text-warning"></i>
</div>
</div>
<div class="d-flex justify-content-between align-items-center small">
<div class="text-muted"><span class="text-warning">-20%</span> vs Last Month</div>
<div><a href="#" class="link-primary text-decoration-underline">View</a></div>
</div>
</div>
</div>
</div>
</div>
<div class="row g-5 mb-5">
<div class="col-12 col-lg-6">
<div class="card">
<div class="card-header d-flex justify-content-between align-items-center bg-transparent px-4 py-3">
<h3 class="h5 mb-0">Sales vs Purchase</h3>
<div>
<select class="form-select form-select-sm">
<option selected>This Year</option>
<option>This Month</option>
<option>This Week</option>
</select>
</div>
</div>
<div class="card-body p-6">
<div id="salesPurchaseChart"></div>
</div>
</div>
</div>
<div class="col-12 col-lg-6">
<div class="card">
<div class="card-header d-flex justify-content-between align-items-center bg-transparent px-4 py-3">
<h3 class="h5 mb-0">Overall Information</h3>
<div>
<select class="form-select form-select-sm">
<option selected>Last 6 Months</option>
<option>This Month</option>
<option>This Week</option>
</select>
</div>
</div>
<div class="card-body p-6">
<h3 class="h6">Customers Overview</h3>
<div class="row align-items-center">
<div class="col-sm-6">
<div id="customerChart">
</div>
</div>
<div class="col-sm-6">
<div class="row">
<div class="col-6 border-end">
<div class="text-center ">
<h2 class="mb-1">5.5K</h2>
<p class="text-success mb-2">First Time</p>
<span class="badge bg-success"><i class="ti ti-arrow-up-left me-1"></i>25%</span>
</div>
</div>
<div class="col-6">
<div class="text-center">
<h2 class="mb-1">3.5K</h2>
<p class="text-warning mb-2">Return</p>
<span class="badge bg-success badge-xs d-inline-flex align-items-center"><i
class="ti ti-arrow-up-left me-1"></i>21%</span>
</div>
</div>
</div>
</div>
</div>
<div class="row text-center border-top mt-4 pt-4">
<div class="col-4 border-end">
<h3 class="fw-bold mb-2">6987</h3>
<small class="text-secondary">Suppliers</small>
</div>
<div class="col-4 border-end">
<h3 class="fw-bold mb-2">4896</h3>
<small class="text-secondary">Customers</small>
</div>
<div class="col-4">
<h3 class="fw-bold mb-2">487</h3>
<small class="text-secondary">Orders</small>
</div>
</div>
<div class="card-loader-overlay" id="overlay_chart">
<div class="spinner-border spinner-border-sm text-primary"></div>
</div>
</div>
</div>
</div>
<!-- Bottom 3 cards -->
<div class="row g-5">
<!-- CARD 1 — Top Selling Products -->
<div class="col-lg-4">
<div class="card h-100">
<!-- Most Moved Products -->
<div class="col-lg-4 col-12">
<div class="card h-100" style="position:relative;">
<div class="card-header bg-white d-flex justify-content-between align-items-center px-4 py-3">
<h4 class="mb-0 h5">Top Selling Products</h4>
<button class="btn btn-sm btn-outline-secondary">
<i class="ti ti-calendar"></i> Today
</button>
<h4 class="mb-0 h5">Most Moved Products</h4>
<span class="small text-muted" id="most_moved_month"></span>
</div>
<ul class="list-group list-group-flush">
<!-- item -->
<li class="list-group-item d-flex align-items-center gap-3">
<img src="<?php echo $server_url?>assets/images/product-2.png" class="rounded" width="48">
<div class="flex-grow-1">
<p class="mb-1">Wireless Earphones</p>
<div class="d-flex align-items-center gap-2 text-muted">
<small class="fw-semibold">$89 </small>
<small>•</small>
<small>1,250 Units</small>
</div>
</div>
<span class="badge bg-danger-subtle text-danger border border-danger">18%</span>
</li>
<!-- repeat -->
<li class="list-group-item d-flex align-items-center gap-3">
<img src="<?php echo $server_url?>assets/images/product-1.png" class="rounded" width="48">
<div class="flex-grow-1">
<p class="mb-1">Gaming Joy Stick</p>
<div class="d-flex align-items-center gap-2 text-muted">
<small class="fw-semibold">$49 </small>
<small>•</small>
<small>5,420 Units</small>
</div>
</div>
<span class="badge bg-primary-subtle text-primary border border-primary">32%</span>
</li>
<li class="list-group-item d-flex align-items-center gap-3">
<img src="<?php echo $server_url?>assets/images/product-3.png" class="rounded" width="48">
<div class="flex-grow-1">
<p class="mb-1">Smart Watch Pro</p>
<div class="d-flex align-items-center gap-2 text-muted">
<small class="fw-semibold">$98 </small>
<small>•</small>
<small>862 Units</small>
</div>
</div>
<span class="badge bg-info-subtle text-info border border-info">22%</span>
</li>
<li class="list-group-item d-flex align-items-center gap-3">
<img src="<?php echo $server_url?>assets/images/product-4.png" class="rounded" width="48">
<div class="flex-grow-1">
<p class="mb-1">USB-C Fast Charger</p>
<div class="d-flex align-items-center gap-2 text-muted">
<small class="fw-semibold">$35 </small>
<small>•</small>
<small>3,200 Units</small>
</div>
</div>
<span class="badge bg-success-subtle text-success border border-success">28%</span>
</li>
<li class="list-group-item d-flex align-items-center gap-3">
<img src="<?php echo $server_url?>assets/images/product-5.png" class="rounded" width="48">
<div class="flex-grow-1">
<p class="mb-1">Portable Bluetooth Speaker</p>
<div class="d-flex align-items-center gap-2 text-muted">
<small class="fw-semibold">$65 </small>
<small>•</small>
<small>2,890 Units</small>
</div>
</div>
<span class="badge bg-warning-subtle text-warning border border-warning">25%</span>
</li>
<ul class="list-group list-group-flush" id="list_most_moved">
<li class="list-group-item text-center text-muted py-5">Loading...</li>
</ul>
<div class="card-loader-overlay" id="overlay_moved">
<div class="spinner-border spinner-border-sm text-primary"></div>
</div>
</div>
</div>
<!-- CARD 2 — Low Stock Products -->
<div class="col-lg-4">
<div class="card h-100">
<!-- Low Stock Products -->
<div class="col-lg-4 col-12">
<div class="card h-100" style="position:relative;">
<div class="card-header bg-white d-flex justify-content-between align-items-center px-4 py-3">
<div class="d-flex align-items-center">
<h4 class="mb-0 h5">Low Stock Products</h4>
</div>
<a href="#" class="small text-primary text-decoration-underline">View All</a>
<h4 class="mb-0 h5">Low Stock Products</h4>
<a href="<?php echo $server_url?>dashboard/low-stock-products.php" class="small text-primary text-decoration-underline">View All</a>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item d-flex align-items-center gap-3">
<img src="<?php echo $server_url?>assets/images/product-8.png" class="rounded" width="48">
<div class="flex-grow-1">
<p class="mb-1">Wireless Headphones</p>
<small>ID: #554433</small>
</div>
<div class="d-flex flex-column gap-0 align-items-center">
<span class="fw-semibold text-primary">06</span>
<small class="text-muted">In Stock</small>
</div>
</li>
<li class="list-group-item d-flex align-items-center gap-3">
<img src="<?php echo $server_url?>assets/images/product-4.png" class="rounded" width="48">
<div class="flex-grow-1">
<p class="mb-1">USB-C Cable Pack</p>
<small>ID: #887766</small>
</div>
<div class="d-flex flex-column gap-0 align-items-center">
<span class="fw-semibold text-primary">09</span>
<small class="text-muted">In Stock</small>
</div>
</li>
<li class="list-group-item d-flex align-items-center gap-3">
<img src="<?php echo $server_url?>assets/images/product-10.png" class="rounded" width="48">
<div class="flex-grow-1">
<p class="mb-1">Phone Screen Protector</p>
<small>ID: #332211</small>
</div>
<div class="d-flex flex-column gap-0 align-items-center">
<span class="fw-semibold text-primary">03</span>
<small class="text-muted">In Stock</small>
</div>
</li>
<li class="list-group-item d-flex align-items-center gap-3">
<img src="<?php echo $server_url?>assets/images/product-4.png" class="rounded" width="48">
<div class="flex-grow-1">
<p class="mb-1">Portable Charger 20000mAh</p>
<small>ID: #998877</small>
</div>
<div class="d-flex flex-column gap-0 align-items-center">
<span class="fw-semibold text-primary">07</span>
<small class="text-muted">In Stock</small>
</div>
</li>
<li class="list-group-item d-flex align-items-center gap-3">
<img src="<?php echo $server_url?>assets/images/product-6.png" class="rounded" width="48">
<div class="flex-grow-1">
<p class="mb-1">Mechanical Keyboard RGB</p>
<small>ID: #665544</small>
</div>
<div class="d-flex flex-column gap-0 align-items-center">
<span class="fw-semibold text-primary">02</span>
<small class="text-muted">In Stock</small>
</div>
</li>
<ul class="list-group list-group-flush" id="list_low_stock">
<li class="list-group-item text-center text-muted py-5">Loading...</li>
</ul>
<div class="card-loader-overlay" id="overlay_low_list">
<div class="spinner-border spinner-border-sm text-primary"></div>
</div>
</div>
</div>
<!-- CARD 3 — Recent Sales -->
<div class="col-lg-4">
<div class="card h-100">
<!-- Recent Activity -->
<div class="col-lg-4 col-12">
<div class="card h-100" style="position:relative;">
<div class="card-header bg-white d-flex justify-content-between align-items-center px-4 py-3">
<h4 class="mb-0 h5">Recent Sales</h4>
<button class="btn btn-sm btn-outline-secondary">
<i class="ti ti-calendar-event"></i> Weekly
</button>
<h4 class="mb-0 h5">Recent Activity</h4>
<span class="small text-muted">All warehouses</span>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item d-flex align-items-center gap-3">
<img src="<?php echo $server_url?>assets/images/product-7.png" class="rounded" width="48">
<div class="flex-grow-1">
<p class="mb-1">MacBook Pro 16"</p>
<div class="d-flex align-items-center gap-2 text-muted">
<small class="fw-semibold">Computers </small>
<small>•</small>
<small>2,$2,499</small>
</div>
</div>
<span class="badge bg-success-subtle text-success">Completed</span>
</li>
<li class="list-group-item d-flex align-items-center gap-3">
<img src="<?php echo $server_url?>assets/images/product-9.png" class="rounded" width="48">
<div class="flex-grow-1">
<p class="mb-1">AirPods Pro Max</p>
<div class="d-flex align-items-center gap-2 text-muted">
<small class="fw-semibold">Audio </small>
<small>•</small>
<small>$549</small>
</div>
</div>
<span class="badge bg-primary-subtle text-primary">Processing</span>
</li>
<li class="list-group-item d-flex align-items-center gap-3">
<img src="<?php echo $server_url?>assets/images/product-8.png" class="rounded" width="48">
<div class="flex-grow-1">
<p class="mb-1">iPad Air 11"</p>
<div class="d-flex align-items-center gap-2 text-muted">
<small class="fw-semibold">Tablets </small>
<small>•</small>
<small>$799</small>
</div>
</div>
<span class="badge bg-success-subtle text-success">Completed</span>
</li>
<li class="list-group-item d-flex align-items-center gap-3">
<img src="<?php echo $server_url?>assets/images/product-3.png" class="rounded" width="48">
<div class="flex-grow-1">
<p class="mb-1">Apple Watch Ultra</p>
<div class="d-flex align-items-center gap-2 text-muted">
<small class="fw-semibold">Wearables </small>
<small>•</small>
<small>$799</small>
</div>
</div>
<span class="badge bg-warning-subtle text-warning">Pending</span>
</li>
<li class="list-group-item d-flex align-items-center gap-3">
<img src="<?php echo $server_url?>assets/images/product-6.png" class="rounded" width="48">
<div class="flex-grow-1">
<p class="mb-1">Magic Keyboard</p>
<div class="d-flex align-items-center gap-2 text-muted">
<small class="fw-semibold">Accessories </small>
<small>•</small>
<small>$299</small>
</div>
</div>
<span class="badge bg-danger-subtle text-danger">Cancelled</span>
</li>
<ul class="list-group list-group-flush" id="list_recent_activity">
<li class="list-group-item text-center text-muted py-5">Loading...</li>
</ul>
<div class="card-loader-overlay" id="overlay_activity">
<div class="spinner-border spinner-border-sm text-primary"></div>
</div>
</div>
</div>
</div>
</div>
</main>
<?php require "../include_ending.php";?>
<!-- Bootstrap JS -->
<script>
var movementChart = null;
// ── Show all overlays ─────────────────────────────────────────────────
function show_overlays() {
$('#overlay_in, #overlay_out, #overlay_active, #overlay_low, #overlay_chart, #overlay_moved, #overlay_low_list, #overlay_activity').show();
}
// ── Render all sections ───────────────────────────────────────────────
async function render(d, month) {
// Stats
$('#stat_total_in').text(format_number(d.total_in, 2));
$('#stat_total_out').text(format_number(d.total_out, 2));
$('#stat_active_products').text(format_number(d.active_products));
$('#stat_low_stock').text(format_number(d.low_stock));
$('#overlay_in, #overlay_out, #overlay_active, #overlay_low').hide();
// Chart
var mv = d.stock_movement;
if (movementChart) {
await movementChart.updateOptions({ xaxis: { categories: mv.labels || [] } }, false, false);
await movementChart.updateSeries([
{ name: 'Stock In', data: mv.stock_in || [] },
{ name: 'Stock Out', data: mv.stock_out || [] }
]);
} else {
$('#ics_dashboardMovementChart').empty();
movementChart = new ApexCharts(document.querySelector('#ics_dashboardMovementChart'), {
series: [
{ name: 'Stock In', data: mv.stock_in || [] },
{ name: 'Stock Out', data: mv.stock_out || [] }
],
chart: { type: 'bar', height: 300, toolbar: { show: false } },
plotOptions: { bar: { horizontal: false, columnWidth: '55%', borderRadius: 5, borderRadiusApplication: 'end' } },
colors: ['#00C951', '#E66239'],
dataLabels: { enabled: false },
stroke: { show: true, width: 2, colors: ['transparent'] },
xaxis: { categories: mv.labels || [] },
yaxis: { title: { text: 'Quantity' } },
fill: { opacity: 1 },
tooltip: { y: { formatter: function(a) { return Number(a).toLocaleString() + ' units'; } } }
});
await movementChart.render();
}
$('#overlay_chart').hide();
// Most Moved Products
$('#most_moved_month').text(format_month(month));
var moved_html = '';
if (!d.most_moved || d.most_moved.length === 0) {
moved_html = '<li class="list-group-item text-center text-muted py-4">No data for this month.</li>';
} else {
$.each(d.most_moved.slice(0, 5), function(i, item) {
moved_html += `<li class="list-group-item d-flex align-items-center gap-3 py-3">
<div class="icon-shape icon-sm bg-danger bg-opacity-10 text-danger rounded-2 flex-shrink-0">
<i class="ti ti-trending-down"></i>
</div>
<div class="flex-grow-1">
<p class="mb-0 fw-semibold">${item.product_name || item.product_sku}</p>
<small class="text-muted">${item.product_sku}</small>
</div>
<div class="text-end">
<span class="fw-bold text-danger">-${Number(item.total_out).toLocaleString()}</span><br>
<small class="text-success">+${Number(item.total_in).toLocaleString()}</small>
</div>
</li>`;
});
}
$('#list_most_moved').html(moved_html);
$('#overlay_moved').hide();
// Low Stock Products
var low_html = '';
if (!d.low_stock_items || d.low_stock_items.length === 0) {
low_html = '<li class="list-group-item text-center text-muted py-4">No low stock items.</li>';
} else {
$.each(d.low_stock_items.slice(0, 5), function(i, item) {
var status_cls = item.status === 'critical' ? 'danger' : 'warning';
low_html += `<li class="list-group-item d-flex align-items-center gap-3 py-3">
<div class="icon-shape icon-sm bg-${status_cls} bg-opacity-10 text-${status_cls} rounded-2 flex-shrink-0">
<i class="ti ti-alert-triangle"></i>
</div>
<div class="flex-grow-1">
<p class="mb-0 fw-semibold">${item.product_name || item.product_sku}</p>
<small class="text-muted">${item.product_sku}</small>
</div>
<div class="text-end">
<span class="fw-bold text-${status_cls}">${Number(item.balance).toLocaleString()}</span><br>
<small class="text-muted">min: ${Number(item.min_stock).toLocaleString()}</small>
</div>
</li>`;
});
}
$('#list_low_stock').html(low_html);
$('#overlay_low_list').hide();
// Recent Activity
var act_html = '';
if (!d.recent_activity || d.recent_activity.length === 0) {
act_html = '<li class="list-group-item text-center text-muted py-4">No recent activity.</li>';
} else {
$.each(d.recent_activity.slice(0, 5), function(i, item) {
var is_in = item.direction === 'in';
var color = is_in ? 'success' : 'danger';
var icon = is_in ? 'ti-trending-up' : 'ti-trending-down';
var sign = is_in ? '+' : '-';
act_html += `<li class="list-group-item d-flex align-items-center gap-3 py-3">
<div class="icon-shape icon-sm bg-${color} bg-opacity-10 text-${color} rounded-2 flex-shrink-0">
<i class="ti ${icon}"></i>
</div>
<div class="flex-grow-1">
<p class="mb-0 fw-semibold">${item.product_name}</p>
<small class="text-muted">${item.warehouse_name}</small>
</div>
<div class="text-end">
<span class="fw-bold text-${color}">${sign}${Number(item.qty).toLocaleString()}</span><br>
<small class="text-muted">${time_ago(item.date)}</small>
</div>
</li>`;
});
}
$('#list_recent_activity').html(act_html);
$('#overlay_activity').hide();
}
// ── Fetch + render ────────────────────────────────────────────────────
function retrieve_dashboard() {
var month = $('#month_filter').val() || '<?php echo date('Y-m'); ?>';
show_overlays();
return ajax_request({
url: "<?php echo $server_url?>dashboard/api/engine_report/reports_stats.php",
autoPrepare: true,
checkRequired: 0,
action: 'read',
data: { "month": month },
onSuccess: async function(res) {
await render(res.output, month);
}
});
}
// ── Helpers ───────────────────────────────────────────────────────────
function format_month(ym) {
if (!ym) return '';
var parts = ym.split('-');
var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
return months[parseInt(parts[1]) - 1] + ' ' + parts[0];
}
function time_ago(dateStr) {
if (!dateStr) return '';
var diff = Math.floor((new Date() - new Date(dateStr.replace(/-/g,'/'))) / 1000);
if (diff < 60) return 'Just now';
if (diff < 3600) return Math.floor(diff / 60) + ' min ago';
if (diff < 86400) return Math.floor(diff / 3600) + ' hr ago';
if (diff < 2592000) return Math.floor(diff / 86400) + ' day ago';
return Math.floor(diff / 2592000) + ' mo ago';
}
// ── Boot ─────────────────────────────────────────────────────────────
$(async function() {
try {
var month = '<?php echo date('Y-m'); ?>';
show_overlays();
await ajax_request({
url: "<?php echo $server_url?>dashboard/api/engine_report/reports_stats.php",
autoPrepare: true,
checkRequired: 0,
action: 'read',
data: { "month": month },
onSuccess: async function(res) {
// Populate month picker
var option = '';
var months = res.output.months || [month];
$.each(months, function(i, m) {
var sel = (m === month) ? 'selected' : '';
option += `<option value="${m}" ${sel}>${format_month(m)}</option>`;
});
if (!months.length) {
option = `<option value="${month}">${format_month(month)}</option>`;
}
$('#month_filter').html(option);
await render(res.output, month);
}
});
} catch(e) {
console.log(e);
}
});
// ── Month filter change ───────────────────────────────────────────────
$('#month_filter').on('change', function() {
retrieve_dashboard();
});
</script>
</body>
</html>