Tạm tính:
0đ
Phí giao hàng:
Miễn phí
{ id: 10, name: "Trà xanh Không Độ vị chanh lốc 6 chai 455ml", cat: "do-uong", price: 48000, oldPrice: 54000, discount: "-11%", unit: "Lốc 6 chai", img: "https://images.unsplash.com/photo-1556679343-c7306c1976bc?auto=format&fit=crop&w=500&q=80" },
{ id: 11, name: "Thùng 30 gói mì Hảo Hảo tôm chua cay 75g", cat: "do-kho", price: 118000, oldPrice: 132000, discount: "-11%", unit: "Thùng 30 gói", img: "https://images.unsplash.com/photo-1612929633738-8fe44f7ec841?auto=format&fit=crop&w=500&q=80" },
{ id: 12, name: "Gạo thơm ST25 đặc sản túi 5kg", cat: "do-kho", price: 165000, oldPrice: 185000, discount: "-11%", unit: "Túi 5kg", img: "https://images.unsplash.com/photo-1586201375761-83865001e31c?auto=format&fit=crop&w=500&q=80" },
{ id: 13, name: "Dầu ăn thực vật Simply chai 1L", cat: "do-kho", price: 52000, oldPrice: 59000, discount: "-12%", unit: "Chai 1L", img: "https://images.unsplash.com/photo-1474979266404-7eaacbcd87c5?auto=format&fit=crop&w=500&q=80" },
{ id: 14, name: "Snack khoai tây Lay's vị tự nhiên gói 95g", cat: "an-vat", price: 19000, oldPrice: 23000, discount: "-17%", unit: "Gói 95g", img: "https://images.unsplash.com/photo-1566478989037-eec170784d0b?auto=format&fit=crop&w=500&q=80" },
{ id: 15, name: "Bánh quy bơ Danisa hộp thiếc 200g", cat: "an-vat", price: 58000, oldPrice: 68000, discount: "-15%", unit: "Hộp 200g", img: "https://images.unsplash.com/photo-1558961363-fa8fdf82db35?auto=format&fit=crop&w=500&q=80" }
];
let cart = [];
let currentCat = 'all';
function formatVND(num) {
return num.toLocaleString('vi-VN') + 'đ';
}
function renderProducts(list) {
const grid = document.getElementById('productGrid');
document.getElementById('productCount').innerText = `Hiển thị ${list.length} sản phẩm`;
if (list.length === 0) {
grid.innerHTML = `
Không tìm thấy sản phẩm phù hợp!
`;
return;
}
grid.innerHTML = list.map(p => `
${p.discount}
${p.unit}
${p.name}
${formatVND(p.oldPrice)}
`).join('');
}
function filterCategory(cat) {
currentCat = cat;
document.querySelectorAll('.cat-btn').forEach(btn => {
btn.classList.remove('bg-green-700', 'text-white');
btn.classList.add('text-gray-700');
});
event?.target?.classList?.add('bg-green-700', 'text-white');
event?.target?.classList?.remove('text-gray-700');
const filtered = cat === 'all' ? products : products.filter(p => p.cat === cat);
renderProducts(filtered);
}
function searchProducts() {
const kw = document.getElementById('searchInput').value.toLowerCase().trim();
const filtered = products.filter(p =>
(currentCat === 'all' || p.cat === currentCat) &&
p.name.toLowerCase().includes(kw)
);
renderProducts(filtered);
}
function addToCart(id) {
const item = products.find(p => p.id === id);
const existing = cart.find(c => c.id === id);
if (existing) existing.qty++;
else cart.push({ ...item, qty: 1 });
updateCartUI();
showToast(`Đã thêm "${item.name}" vào giỏ!`);
}
function changeQty(id, delta) {
const idx = cart.findIndex(c => c.id === id);
if (idx === -1) return;
cart[idx].qty += delta;
if (cart[idx].qty <= 0) cart.splice(idx, 1);
updateCartUI();
}
function updateCartUI() {
const totalQty = cart.reduce((sum, i) => sum + i.qty, 0);
document.getElementById('cartCount').innerText = totalQty;
const container = document.getElementById('cartItems');
if (cart.length === 0) {
container.innerHTML = `
`;
} else {
container.innerHTML = cart.map(i => `
${i.name}
${formatVND(i.price)}
${i.qty}
`).join('');
}
const subtotal = cart.reduce((sum, i) => sum + i.price * i.qty, 0);
const ship = subtotal === 0 || subtotal >= 150000 ? 0 : 15000;
document.getElementById('cartSubtotal').innerText = formatVND(subtotal);
document.getElementById('shippingFee').innerText = ship === 0 ? 'Miễn phí' : formatVND(ship);
document.getElementById('cartTotal').innerText = formatVND(subtotal + ship);
}
function toggleCart() {
const modal = document.getElementById('cartModal');
modal.classList.toggle('hidden');
modal.classList.toggle('flex');
}
function placeOrder() {
if (cart.length === 0) return alert('Giỏ hàng của bạn đang trống!');
const name = document.getElementById('custName').value.trim();
const phone = document.getElementById('custPhone').value.trim();
const addr = document.getElementById('custAddr').value.trim();
if (!name || !phone || !addr) return alert('Vui lòng nhập đầy đủ Họ tên, SĐT và Địa chỉ giao hàng!');
alert(`🎉 ĐẶT HÀNG THÀNH CÔNG!\n\nCảm ơn anh/chị ${name} đã mua sắm tại Green Mart HCM.\nĐơn hàng sẽ được giao tới: ${addr} trong vòng 2 giờ tới!`);
cart = [];
updateCartUI();
toggleCart();
}
function showToast(msg) {
const t = document.getElementById('toast');
document.getElementById('toastMsg').innerText = msg;
t.classList.remove('hidden');
t.classList.add('flex');
setTimeout(() => { t.classList.add('hidden'); t.classList.remove('flex'); }, 2200);
}
renderProducts(products);
updateCartUI();