2024-08-22 12:33:36 +00:00
|
|
|
import { formatCurrency } from "./common.js"
|
2024-08-22 08:01:54 +00:00
|
|
|
|
2024-08-23 08:04:06 +00:00
|
|
|
export let pageSize = 2;
|
|
|
|
export let currentPage = 1;
|
|
|
|
|
|
|
|
export function showTable(api, ps, cp) {
|
|
|
|
const token = localStorage.getItem("token");
|
|
|
|
const tbody = document.getElementById("sales-table").getElementsByTagName("tbody")[0];
|
|
|
|
|
|
|
|
// reset tbody
|
|
|
|
tbody.innerHTML = ``
|
|
|
|
|
|
|
|
const body = {
|
|
|
|
page_size: parseInt(ps, 10),
|
|
|
|
page: parseInt(cp, 10),
|
|
|
|
};
|
2024-08-21 21:18:51 +00:00
|
|
|
|
|
|
|
const requestOptions = {
|
|
|
|
method: 'post',
|
|
|
|
headers: {
|
|
|
|
'Accept': `application/json`,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Authorization': 'Bearer ' + token,
|
|
|
|
},
|
2024-08-23 08:04:06 +00:00
|
|
|
body: JSON.stringify(body),
|
2024-08-21 21:18:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
fetch(api + "/api/admin/all-sales", requestOptions)
|
|
|
|
.then(response => response.json())
|
|
|
|
.then(function (data) {
|
2024-08-23 08:04:06 +00:00
|
|
|
if (data.orders) {
|
|
|
|
data.orders.forEach(function (i) {
|
2024-08-22 12:33:36 +00:00
|
|
|
let newRow = tbody.insertRow();
|
|
|
|
let newCell = newRow.insertCell();
|
|
|
|
|
|
|
|
newCell.innerHTML = `<a href="/admin/sales/${i.id}">Order ${i.id}</a>`;
|
|
|
|
|
|
|
|
newCell = newRow.insertCell();
|
|
|
|
let item = document.createTextNode(i.customer.last_name + ", " + i.customer.first_name);
|
|
|
|
newCell.appendChild(item)
|
|
|
|
|
|
|
|
newCell = newRow.insertCell();
|
|
|
|
item = document.createTextNode(i.widget.name);
|
|
|
|
newCell.appendChild(item)
|
|
|
|
|
|
|
|
let cur = formatCurrency(i.transaction.amount)
|
|
|
|
newCell = newRow.insertCell();
|
|
|
|
item = document.createTextNode(cur);
|
|
|
|
newCell.appendChild(item)
|
|
|
|
|
|
|
|
newCell = newRow.insertCell();
|
|
|
|
if (i.status_id != 1) {
|
|
|
|
newCell.innerHTML = `<span class="badge bg-danger">Refunded</span>`
|
|
|
|
} else {
|
|
|
|
newCell.innerHTML = `<span class="badge bg-success">Charged</span>`
|
|
|
|
}
|
2024-08-23 08:04:06 +00:00
|
|
|
paginator(api, data.last_page, data.current_page)
|
2024-08-22 12:33:36 +00:00
|
|
|
});
|
2024-08-21 21:18:51 +00:00
|
|
|
} else {
|
|
|
|
let newRow = tbody.insertRow();
|
|
|
|
let newCell = newRow.insertCell();
|
2024-08-23 08:04:06 +00:00
|
|
|
newCell.setAttribute("colspan", "5");
|
2024-08-21 21:18:51 +00:00
|
|
|
newCell.innerHTML = "No data available";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2024-08-23 08:04:06 +00:00
|
|
|
|
|
|
|
function paginator(api, pages, curPage) {
|
|
|
|
const p = document.getElementById("paginator")
|
|
|
|
let html = `<li class="page-item"><a href="#!" class="page-link pager" data-page="${curPage - 1}"><</a></li>`;
|
|
|
|
|
|
|
|
for (var i = 0; i < pages; i++) {
|
|
|
|
html += `<li class="page-item"><a href="#!" class="page-link pager" data-page="${i + 1}">${i + 1}</a></li>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
html += `<li class="page-item"><a href="#!" class="page-link pager" data-page="${curPage + 1}">></a></li>`;
|
|
|
|
|
|
|
|
p.innerHTML = html;
|
|
|
|
|
|
|
|
let pageBtns = document.getElementsByClassName("pager");
|
|
|
|
for (var j = 0; j < pageBtns.length; j++) {
|
|
|
|
pageBtns[j].addEventListener("click", function(evt){
|
|
|
|
let desiredPage = evt.target.getAttribute("data-page");
|
|
|
|
if ((desiredPage > 0) && (desiredPage <= pages)) {
|
|
|
|
console.log("would go to page", desiredPage);
|
|
|
|
showTable(api, pageSize, desiredPage);
|
|
|
|
currentPage = desiredPage
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|