"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(List_product);
});
});
}
function goi_sp_user_tu_api() {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
return resolve(User);
});
});
}
function hienthi_nhomsp() {
return __awaiter(this, void 0, void 0, function* () {
const lay_sp_user_tu_api = yield goi_sp_user_tu_api();
console.log(lay_sp_user_tu_api);
DisplayListProductGroup(lay_sp_user_tu_api, 'js-holder-product-group');
});
}
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
console.log(lay_sp_tu_api);
showListProductHome(lay_sp_tu_api, 'out_stock', 'js-holder-list-out-stock');
showListProductHome(lay_sp_tu_api, 'started', 'js-holder-list-started');
showListProductHome(lay_sp_tu_api, 'coming', 'js-holder-list-coming');
});
}
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 != 'out_stock' && 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) {
const quantity_sold = product.quantity - product.sale_quantity;
const line_sold = Math.ceil((quantity_sold / product.quantity) * 100);
const HtmlSl_nhom = product.quantity > product.quantity_member
? `${product.quantity / product.quantity_member} nhóm duy nhất`
: '';
let HtmlProductItem = '';
if (product.status === 'started' && product.price > product.sale_quantity) {
let htmlCheckGroup = '';
let HtmlListMember = '';
let htmlBtnProduct = '';
let addMember = '';
let noteMember = '';
if (product.list_group.length > 0) {
product.list_group.forEach((item) => {
item.list_member.forEach((user, index) => {
var more_qnt_member = product.quantity_member - index - 1;
const isLastMember = index > product.quantity_member;
console.log(isLastMember);
HtmlListMember += `
${formatName(user.name_member)}
`;
addMember = isLastMember ? `+` : '';
noteMember = isLastMember ? `Cần thêm ${more_qnt_member} thành viên` : '';
});
});
htmlCheckGroup = `
${formatPrice(product.retail_price)}đ
${formatPrice(product.price)}đ
${HtmlListMember} ${addMember}
${noteMember}
`;
htmlBtnProduct = `
Mời thêm bạn
`;
}
else {
htmlCheckGroup = `
${formatPrice(product.retail_price)}đ
Khi mua nhóm ${product.quantity_member} người
Giá bán lẻ: ${formatPrice(product.price)}đ
`;
htmlBtnProduct = `
Tạo nhóm ngay
`;
}
HtmlProductItem = `
`;
}
else if (product.status === 'coming') {
HtmlProductItem = `
`;
}
else if (product.status === 'out_stock') {
HtmlProductItem = `
`;
}
else {
HtmlProductItem = `Sản phẩm đang được cập nhật..!
`;
}
return HtmlProductItem;
}
function DisplayListProductGroup(ProductUser, holderId) {
const html = [];
const holder = document.getElementById(holderId);
ProductUser.forEach(function (product, keyIndex) {
html.push(xayhtmlProductGroup(product));
});
if (holder) {
holder.innerHTML = html.join('');
}
}
function xayhtmlProductGroup(product) {
var HtmlProductItem = '';
var HtmlListMember = '';
var addMember = '';
var noteMember = '';
if (product.list_group.length > 0) {
product.list_group.forEach((Item) => {
const quantity_sold = Item.quantity - Item.sale_quantity;
const line_sold = Math.ceil((Item.quantity - Item.sale_quantity) / Item.quantity * 100);
var HtmlSl_nhom = '';
if (Item.quantity > Item.quantity_member) {
HtmlSl_nhom = `${Item.quantity / Item.quantity_member} nhóm duy nhất`;
}
Item.list_member.forEach((user, index) => {
if (index > Item.quantity_member) {
HtmlListMember += `
${formatName(user.name_member)}
`;
addMember = ``;
noteMember = ``;
}
else {
var more_qnt_member = Item.quantity_member - index - 1;
HtmlListMember += `
${formatName(user.name_member)}
`;
addMember = `
+
`;
noteMember = `
Cần thêm ${more_qnt_member} thành viên
`;
}
});
HtmlProductItem += `
${HtmlSl_nhom}
${Item.product_name}
${formatPrice(Item.retail_price)}đ
${formatPrice(Item.price)}đ
${HtmlListMember}${addMember}
${noteMember}
${quantity_sold} đã bán hết
${product.leaderEmail} vừa tạo nhóm thành công
`;
});
}
return HtmlProductItem;
}
function closeBackground() {
var eleBackground = document.getElementById('overlay');
eleBackground.classList.remove('active');
var eleGlobalPopup = document.getElementById('js-global-popup');
eleGlobalPopup.classList.remove('active');
document.body.classList.remove('active');
}
function copyLink() {
// Get the pre element
var preElement = document.getElementById("linkshare");
// Check if the element exists
if (preElement) {
// Create a textarea element
var textarea = document.createElement("textarea");
textarea.value = preElement.textContent || "";
document.body.appendChild(textarea);
textarea.select();
document.execCommand("copy");
document.body.removeChild(textarea);
alert("Copy link share thành công!");
}
}
// them o nhap email
let emailCounter = 3;
function addEmail() {
const contentEmail = document.getElementById('list-email-share');
const newEmailInput = document.createElement('input');
newEmailInput.type = 'text';
newEmailInput.id = 'email' + emailCounter;
newEmailInput.placeholder = 'Email ' + emailCounter;
contentEmail === null || contentEmail === void 0 ? void 0 : contentEmail.appendChild(newEmailInput);
emailCounter++;
}
function CountdownJoin() {
let countdown = 10 * 60;
const countdownElement = document.getElementById('js-time-join');
function updateCountdown() {
const minutes = Math.floor(countdown / 60);
const seconds = countdown % 60;
const formattedMinutes = minutes.toString().padStart(2, '0');
const formattedSeconds = seconds.toString().padStart(2, '0');
const formattedTime = `${formattedMinutes}${formattedSeconds}`;
if (countdownElement) {
countdownElement.innerHTML = formattedTime;
}
}
function decrementCountdown() {
if (countdown > 0) {
countdown--;
updateCountdown();
}
else {
clearInterval(intervalId);
closeBackground();
}
}
updateCountdown();
const intervalId = setInterval(decrementCountdown, 1000);
}
function ShowPopupGlobal(key) {
const globalPopup = document.getElementById('js-global-popup');
globalPopup === null || globalPopup === void 0 ? void 0 : globalPopup.classList.add('active');
const eleBackground = document.getElementById('overlay');
eleBackground.classList.add('active');
document.body.classList.add('active');
const holderPupop = document.getElementById('js-content-popup');
if (key == 'AddGroup') {
holderPupop.innerHTML = AddGroupTemplate;
}
else if (key == 'AddMember') {
holderPupop.innerHTML = ShareGroupTemplate;
}
}
// run function
hienthi_sp();
hienthi_nhomsp();
CountdownJoin();