let PAGE_NUMBER = 1,
ORDER_HISTORY = []
$(document).ready(function(){
$('#orderhistory-tab').addClass("active");
getOrderHistory()
$("body").delegate(".order-pagination-item", "click", function(e){
e.preventDefault()
e.stopImmediatePropagation()
let page = $(this).data('page')
PAGE_NUMBER = page
console.log('page:',PAGE_NUMBER)
getOrderHistory()
window.scrollTo(0, 0);
});
$("body").delegate(".order-pagination-previous", "click", function(e){
e.preventDefault()
e.stopImmediatePropagation()
PAGE_NUMBER -= 1
console.log('page:',PAGE_NUMBER)
getOrderHistory()
window.scrollTo(0, 0);
})
$("body").delegate(".order-pagination-next", "click", function(e){
e.preventDefault()
e.stopImmediatePropagation()
PAGE_NUMBER += 1
console.log('page:',PAGE_NUMBER)
getOrderHistory()
window.scrollTo(0, 0);
})
$("body").delegate(".detail-order-button", "click", function(){
let id = $(this).data("id")
$.ajax({
async: true,
url: `${ORDER_API_URL}by-id/${id}`,
type: 'GET',
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', `Bearer ${SESSION}`);
},
error: function(res) {
const response = JSON.parse(res.responseText)
console.log(`RESPONSE ORDER:`,response)
},
success: function(res) {
const response = res.data
console.log(`RESPONSE ORDER:`,response)
renderOrderDetail(response)
}
})
})
$("body").delegate(".track-button", "click", function(){
let courier = $(this).data("courier")
let awb = $(this).data("airway")
trackSalesOrder(courier, awb)
})
})
function getOrderHistory(){
$.ajax({
async: true,
url: `${ORDER_API_URL}?page_number=${PAGE_NUMBER-1}&page_size=5&draw=1`,
type: 'GET',
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', `Bearer ${SESSION}`);
},
error: function(res) {
const response = JSON.parse(res.responseText)
let isRetry = retryRequest(response, window.location.href)
if(isRetry) $.ajax(this)
},
success: function(res) {
console.log(res.data)
if(res.data.length > 0){
$('#order-notfound').css("display", "none")
let totalPage = Math.ceil(res.recordsTotal/5)
ORDER_HISTORY = res.data
renderOrderHistory()
renderOrderHistoryPagination(totalPage)
}else{
$('#order-history').css("display", "none")
$('#order-notfound').css("display", "block")
}
}
});
}
function trackSalesOrder(courier, awb){
$('#timeline').html(`
`)
$.ajax({
async: true,
url: `${VENDOR_API_URL}airwaybill-track?waybill=${awb}&courier=${courier}`,
type: 'GET',
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', `Bearer ${SESSION}`);
},
error: function(res) {
const response = JSON.parse(res.responseText)
let isRetry = retryRequest(response)
if(isRetry) $.ajax(this)
},
success: function(res) {
// console.log("TRACK")
let status = res.data.rajaongkir.status.code
if(status == 200){
let timeline = res.data.rajaongkir.result.manifest
renderTimeline(timeline)
}else{
$('#timeline').html(`Invalid Air Waybill Number!
`)
}
// $('#loading-page').hide()
// $('#timeline-container').css("display", "block")
}
});
}
function midtransPay(midtrans_token){
console.log("PAY..")
let res = snap.pay(midtrans_token)
console.log(res)
}
function renderOrderHistory(){
let itemsHtml = ``
for(let order of ORDER_HISTORY){
let button = ` `
let grandTotal = CURRENCY == 'usd' ? formatDollar(order.grand_total) : formatRupiah(order.grand_total, true)
if(order.status == 'PENDING'){
button += ` `
}else if(order.status == 'ON DELIVERY' || order.status == 'COMPLETED'){
button += ``
}
let itemHtml = `
${order.invoice_number}
${grandTotal}
${formatDatetime(order.created_at)}
${order.status}
${button}
`
itemsHtml += itemHtml
}
$('#order-history-section').html(itemsHtml)
}
function getBadge(status){
let color = 'light'
if(status == 'CANCELLED') color = 'danger'
else if(status == 'PENDING') color = 'secondary'
else if(status == 'PAID') color = 'success'
else if(status == 'ON DELIVERY') color = 'info'
else if(status == 'COMPLETED') color = 'primary'
return color
}
function renderOrderHistoryPagination(totalPage){
if(totalPage > 1){
let fullPagesHtml = ``
let previousDisable = PAGE_NUMBER == 1 ? 'disabled' : ''
let nextDisable = PAGE_NUMBER == totalPage ? 'disabled' : ''
let isPageLimit = totalPage > 5 ? true : false;
let previousHtml = `
`
let nextHtml = `
`
let pagesHtml = ``
for(let i=1; i<=totalPage; i++){
const pageActive = i == PAGE_NUMBER ? 'active' : ''
const pageDisabled = i == PAGE_NUMBER ? '' : 'href="#"'
let pageHtml = ``
if(isPageLimit){
if(i == PAGE_NUMBER){
pageHtml = `
`
}else if(i == PAGE_NUMBER-1 || i == PAGE_NUMBER+1){
pageHtml = `
`
}else if(i == PAGE_NUMBER-2 || i == PAGE_NUMBER+2){
pageHtml = `
`
}
}else{
pageHtml = `
`
}
pagesHtml += pageHtml
}
fullPagesHtml = `${previousHtml}${pagesHtml}${nextHtml}`
$('#order-pagination').html(fullPagesHtml)
}
}
function renderOrderDetail(order){
let orderItems = order.items
let awb = order.airway_bill ? order.airway_bill : '-'
let grandTotal = parseInt(order.grand_total) + parseInt(order.service_price)
let grandTotalCurr = CURRENCY == 'usd' ? formatDollar(grandTotal) : formatRupiah(grandTotal.toString(), true)
let servicePrice = CURRENCY == 'usd' ? formatDollar(order.service_price) : formatRupiah(order.service_price, true)
let subTotal = CURRENCY == 'usd' ? formatDollar(order.grand_total) : formatRupiah(order.grand_total, true)
let itemsHtml = ``
for(const item of orderItems){
let product = item.product
let variant = item.product_variant
let image = item.product_images.length > 0 ? item.product_images[0] : null
let imgUrl = image ? image.full_thumbnail_url : `${WEB_URL}assets/img/default.png`
let price = CURRENCY == 'usd' ? formatDollar(item.price) : formatRupiah(item.price.toString(), true)
let itemHtml = `
${product.title}
${price}
Variant: ${parseInt(variant) ? variant.variants : '-'}
Jumlah: ${item.quantity ? item.quantity : '-'}
Berat: ${product.weight ? `${product.weight} gram` : '-' }
`
itemsHtml += itemHtml
}
if(order.country){
let addressHtml = `${order.full_address}
${order.country} (${order.postal_code})
${order.receiver_name}, ${order.phone}`
$('#address-detail').html(addressHtml)
}else{
let addressHtml = `${order.full_address}
Kec. ${order.subdistrict}, ${order.city}, ${order.province} (${order.postal_code})
${order.receiver_name}, ${order.phone}`
$('#address-detail').html(addressHtml)
}
$('#order-status').html(order.status)
$('#order-number').html(order.invoice_number)
$('#order-airwaybill-number').html(awb)
$('#order-courier').html(`${order.service.toUpperCase()} - ${order.service_type}`)
$('#order-sub-total').html(subTotal)
$('#order-sub-total').data("price", order.grand_total)
$('#order-courier-price').html(servicePrice)
$('#order-courier-price').data("price", order.service_price)
$('#order-grand-total').html(grandTotalCurr)
$('#order-grand-total').data("price", grandTotal)
$('#order-items').html(itemsHtml)
}
function renderTimeline(data){
let timeline = data.reduce(function (r, a) {
r[a.manifest_date] = r[a.manifest_date] || [];
r[a.manifest_date].push(a);
return r;
}, Object.create(null));
console.log("TIMELINE: ", timeline)
let timelineHtml = ``
for(const key in timeline){
let timelineItems = timeline[key]
let timelineItemsHtml = `
${formatDate(key)}
`
for(const item of timelineItems){
timelineItemHtml = `
${item.manifest_time}
${item.manifest_description}
`
timelineItemsHtml += timelineItemHtml
}
timelineHtml += timelineItemsHtml
}
timelineHtml += `
`
$('#timeline').html(timelineHtml)
}