"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; function goi_sp_tu_api() { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => { return resolve(listproduct_mau); }); }); } function hienthi_sp() { return __awaiter(this, void 0, void 0, function* () { const lay_sp_tu_api = yield goi_sp_tu_api(); // code hien thi danh sach dang dien ra // Usage example: showListProductHome(lay_sp_tu_api, 'started', 'js-holder-list-started'); showListProductHome(lay_sp_tu_api, 'coming', 'js-holder-list-coming'); showListProductHome(lay_sp_tu_api, 'ended', 'js-holder-list-ended'); }); } function showListProductHome(lay_sp_tu_api, status, holderId) { const html = []; const holder = document.getElementById(holderId); lay_sp_tu_api .filter((product) => product.status == status) .forEach(function (product, keyIndex) { html.push(xayhtml(product)); if (status != 'ended' || product.to_time > product.from_time) { const countdownTime = (status == 'started') ? product.to_time : product.from_time; countDown(`js-deal-time-${product.productId}`, countdownTime); } }); if (holder) { holder.innerHTML = html.join(''); } } function xayhtml(product) { var Htmlcheckstatus = ''; if (product.status == 'started') { Htmlcheckstatus = `
Còn lại:
Đấu giá ngay`; } else if (product.status == 'coming') { Htmlcheckstatus = `
Bắt đầu sau:
Xem chi tiết`; } else if (product.to_time < Date.now()) { Htmlcheckstatus = `
Thắng cuộc ${formatPrice(product.starting_price)}đ
Đã kết thúc
Xem chi tiết`; } return `
${product.product_name}
${product.product_name}
Giá gốc: ${formatPrice(product.price)}đ
${Htmlcheckstatus}
`; } function formatPrice(price) { var b = price.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1.").toString(); var len = b.length; b = b.substring(0, len - 3); return b; } function countDown(iid, endTime) { const updateCountdown = () => { const now = Date.now(); const distance = endTime - now; if (distance > 0) { const [days, hours, minutes, seconds] = getTimeComponents(distance); displayCountdown(iid, days, hours, minutes, seconds); } else { clearInterval(timer); } }; // Initial update to avoid delay updateCountdown(); const timer = setInterval(updateCountdown, 1000); } function getTimeComponents(distance) { const days = Math.floor(distance / (1000 * 60 * 60 * 24)); const hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); const seconds = Math.floor((distance % (1000 * 60)) / 1000); return [days, hours, minutes, seconds]; } function displayCountdown(iid, days, hours, minutes, seconds) { const element = document.querySelector(`.${iid}`); if (element) { if (days > 0) { element.innerHTML = `
${formatTimeComponent(days)}
${formatTimeComponent(hours)}
${formatTimeComponent(minutes)}
${formatTimeComponent(seconds)}
`; } else { element.innerHTML = `
${formatTimeComponent(hours)}
${formatTimeComponent(minutes)}
${formatTimeComponent(seconds)}
`; } } } function formatTimeComponent(component) { return component <= 9 ? '0' + component : component.toString(); } hienthi_sp();