258 lines
7.7 KiB
PHP
258 lines
7.7 KiB
PHP
<div class="container mt-4">
|
||
<div class="card shadow-sm">
|
||
<div class="card-header">
|
||
<h5>Dashbard</h5>
|
||
</div>
|
||
<div class="card-body">
|
||
|
||
<?php //print_r($phone_numbers); ?>
|
||
|
||
<div class="card-body">
|
||
<form id="form-modern" class="form-row-modern">
|
||
<!-- Nomor WhatsApp -->
|
||
<div class="form-group-modern">
|
||
<label class="form-label-modern" for="phone_number">Nomor WhatsApp</label>
|
||
<select class="form-control-modern" name="phone_number" id="phone_number">
|
||
<?php foreach ($phone_numbers as $p): ?>
|
||
<option value="<?= $p->phone_number ?>">
|
||
+<?= substr($p->phone_number, 0, 2) === '62' ? $p->phone_number : '62' . $p->phone_number ; echo ' ( ' . $p->number_name . ' )'; ?>
|
||
</option>
|
||
<?php endforeach; ?>
|
||
</select>
|
||
</div>
|
||
|
||
<!-- Range Tanggal -->
|
||
<div class="form-group-modern">
|
||
<label class="form-label-modern" for="date_range">Pilih Tanggal</label>
|
||
<input type="text" id="date_range" class="form-control-modern" placeholder="Dari – Sampai">
|
||
<input type="hidden" name="date_start" id="date_start">
|
||
<input type="hidden" name="date_end" id="date_end">
|
||
</div>
|
||
|
||
<div class="form-group-modern">
|
||
<button type="submit" class="btn btn-primary btn-modern">Submit</button>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
|
||
<div id="grafik-container">
|
||
<div class="row">
|
||
<div class="col-md-4">
|
||
<div class="card-body">
|
||
<div id="grafik-summary"></div>
|
||
</div>
|
||
</div>
|
||
<div class="col-md-8">
|
||
<div class="card-body">
|
||
<canvas id="grafik-analytics" height="120"></canvas>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="card-body">
|
||
<canvas id="grafik-pricing" height="120" class="mt-4"></canvas>
|
||
</div>
|
||
</div>
|
||
|
||
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
||
<script>
|
||
let chartAnalytics = null;
|
||
let chartPricing = null;
|
||
|
||
function formatDate(date, time) {
|
||
const pad = n => n.toString().padStart(2, '0');
|
||
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${time}`;
|
||
}
|
||
|
||
// Default 7 hari terakhir
|
||
let defaultStart = new Date(); defaultStart.setDate(defaultStart.getDate() - 7);
|
||
let defaultEnd = new Date(); defaultEnd.setDate(defaultEnd.getDate() - 1);
|
||
document.getElementById('date_start').value = formatDate(defaultStart, "00:00:00");
|
||
document.getElementById('date_end').value = formatDate(defaultEnd, "23:59:59");
|
||
|
||
// Flatpickr setup
|
||
flatpickr("#date_range", {
|
||
mode: "range",
|
||
dateFormat: "Y-m-d",
|
||
altInput: true,
|
||
altFormat: "d M Y",
|
||
defaultDate: [defaultStart, defaultEnd],
|
||
onClose: function (selectedDates) {
|
||
if (selectedDates.length === 2) {
|
||
document.getElementById('date_start').value = formatDate(selectedDates[0], "00:00:00");
|
||
document.getElementById('date_end').value = formatDate(selectedDates[1], "23:59:59");
|
||
}
|
||
}
|
||
});
|
||
|
||
function renderAnalyticsChart(points) {
|
||
// 🔹 Tambah 7 jam (WIB offset)
|
||
const labels = points.map(p => {
|
||
const dateWIB = new Date((p.start + (7 * 3600)) * 1000);
|
||
return dateWIB.toISOString().slice(0, 10);
|
||
});
|
||
|
||
const sent = points.map(p => p.sent || 0);
|
||
const delivered = points.map(p => p.delivered || 0);
|
||
|
||
if (chartAnalytics) chartAnalytics.destroy(); // 🔹 hapus grafik lama
|
||
|
||
chartAnalytics = new Chart(document.getElementById('grafik-analytics'), {
|
||
type: 'bar',
|
||
data: {
|
||
labels: labels,
|
||
datasets: [
|
||
{ label: 'Sent', data: sent, backgroundColor: 'rgba(0,123,255,0.6)' },
|
||
{ label: 'Delivered', data: delivered, backgroundColor: 'rgba(40,167,69,0.6)' }
|
||
]
|
||
},
|
||
options: {
|
||
responsive: true,
|
||
plugins: {
|
||
title: { display: true, text: 'Analytics (Pesan Terkirim & Diterima)' },
|
||
legend: { position: 'bottom' }
|
||
},
|
||
scales: {
|
||
y: { beginAtZero: true, title: { display: true, text: 'Jumlah Pesan' } },
|
||
x: { title: { display: true, text: 'Tanggal (WIB)' } }
|
||
}
|
||
}
|
||
});
|
||
|
||
// 🔹 Ringkasan analytics
|
||
const totalSent = sent.reduce((a, b) => a + b, 0);
|
||
const totalDelivered = delivered.reduce((a, b) => a + b, 0);
|
||
|
||
return `
|
||
<tr><td>Pesan Terkirim</td><td>${totalSent.toLocaleString()}</td></tr>
|
||
<tr><td>Pesan Diterima</td><td>${totalDelivered.toLocaleString()}</td></tr>
|
||
`;
|
||
}
|
||
|
||
|
||
function renderPricingChart(rawData) {
|
||
const dataArr = rawData.sort((a, b) => a.start - b.start);
|
||
|
||
// 🔹 Konversi timestamp ke tanggal WIB (UTC+7)
|
||
const labels = dataArr.map(p => {
|
||
const dateWIB = new Date((p.start + (7 * 3600)) * 1000);
|
||
return dateWIB.toISOString().slice(0, 10);
|
||
});
|
||
|
||
const volume = dataArr.map(p => p.volume || 0);
|
||
const cost = dataArr.map(p => p.cost || 0);
|
||
|
||
if (chartPricing) chartPricing.destroy(); // 🔹 hapus grafik lama
|
||
|
||
chartPricing = new Chart(document.getElementById('grafik-pricing'), {
|
||
type: 'line',
|
||
data: {
|
||
labels: labels,
|
||
datasets: [{
|
||
label: 'Volume',
|
||
data: volume,
|
||
borderColor: '#007bff',
|
||
backgroundColor: 'rgba(0,123,255,0.1)',
|
||
pointBackgroundColor: '#007bff',
|
||
fill: false,
|
||
tension: 0.3
|
||
}]
|
||
},
|
||
options: {
|
||
responsive: true,
|
||
plugins: {
|
||
title: { display: true, text: 'Pricing Analytics (Volume per Hari - WIB)' },
|
||
legend: { position: 'bottom' },
|
||
tooltip: {
|
||
backgroundColor: 'rgba(255, 255, 255, 0.95)',
|
||
borderColor: '#007bff',
|
||
borderWidth: 1,
|
||
titleColor: '#333',
|
||
bodyColor: '#333',
|
||
displayColors: false,
|
||
padding: 12,
|
||
cornerRadius: 8,
|
||
usePointStyle: true,
|
||
callbacks: {
|
||
title: function (context) {
|
||
return `📅 ${labels[context[0].dataIndex]}`;
|
||
},
|
||
label: function (context) {
|
||
const index = context.dataIndex;
|
||
return [
|
||
`📦 Volume: ${volume[index].toLocaleString()}`,
|
||
`💰 Harga: Rp ${cost[index].toLocaleString()}`
|
||
];
|
||
}
|
||
}
|
||
}
|
||
},
|
||
scales: {
|
||
y: {
|
||
beginAtZero: true,
|
||
title: { display: true, text: 'Volume' },
|
||
grid: { color: 'rgba(0,0,0,0.05)' }
|
||
},
|
||
x: {
|
||
title: { display: true, text: 'Tanggal (WIB)' },
|
||
grid: { color: 'rgba(0,0,0,0.05)' }
|
||
}
|
||
}
|
||
}
|
||
});
|
||
|
||
// 🔹 Ringkasan pricing
|
||
const totalVol = volume.reduce((a, b) => a + b, 0);
|
||
const totalCost = cost.reduce((a, b) => a + b, 0);
|
||
|
||
return `
|
||
<tr><td>Volume (Total)</td><td>${totalVol.toLocaleString()}</td></tr>
|
||
<tr><td>Biaya (Total)</td><td>Rp ${totalCost.toLocaleString()}</td></tr>
|
||
`;
|
||
}
|
||
|
||
|
||
function loadGrafik() {
|
||
const data = {
|
||
phone_number: $('#phone_number').val(),
|
||
date_start: $('#date_start').val(),
|
||
date_end: $('#date_end').val()
|
||
};
|
||
|
||
$.post("<?= base_url('dashboard/get_grafik') ?>", data, function (res) {
|
||
if (typeof res === "string") res = JSON.parse(res);
|
||
let summaryRows = "";
|
||
|
||
if (res.analytics?.data?.analytics) {
|
||
summaryRows += renderAnalyticsChart(res.analytics.data.analytics.data_points || []);
|
||
}
|
||
|
||
if (res.pricing_analytics?.data?.pricing_analytics) {
|
||
summaryRows += renderPricingChart(res.pricing_analytics.data.pricing_analytics.data?.[0]?.data_points || []);
|
||
}
|
||
|
||
if (summaryRows) {
|
||
$('#grafik-summary').html(`
|
||
<table class="table table-bordered table-sm mt-3">
|
||
<thead class="thead-light">
|
||
<tr><th>Kategori</th><th>Total</th></tr>
|
||
</thead>
|
||
<tbody>${summaryRows}</tbody>
|
||
</table>
|
||
`);
|
||
}
|
||
});
|
||
}
|
||
|
||
$(document).ready(loadGrafik);
|
||
$('#form-modern').on('submit', function (e) {
|
||
e.preventDefault();
|
||
loadGrafik();
|
||
});
|
||
</script>
|
||
|
||
|
||
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|