// Authentication state let isAuthenticated = false; let currentUser = null; // Check authentication on page load function checkAuth() { const token = localStorage.getItem('authToken'); if (token && window.location.pathname.includes('login.html')) { window.location.href = 'index.html'; } else if (!token && !window.location.pathname.includes('login.html')) { window.location.href = 'login.html'; } } // Login function function login(email, password) { // In a real app, this would be an API call if (email === 'admin@shopsphere.com' && password === 'admin123') { localStorage.setItem('authToken', 'dummy-token'); localStorage.setItem('userRole', 'admin'); isAuthenticated = true; currentUser = { email, role: 'admin' }; return true; } return false; } // Logout function function logout() { localStorage.removeItem('authToken'); localStorage.removeItem('userRole'); isAuthenticated = false; currentUser = null; window.location.href = 'login.html'; } document.addEventListener('DOMContentLoaded', function() { checkAuth(); // Handle login form submission const loginForm = document.getElementById('login-form'); if (loginForm) { loginForm.addEventListener('submit', function(e) { e.preventDefault(); const email = document.getElementById('email').value; const password = document.getElementById('password').value; if (login(email, password)) { window.location.href = 'index.html'; } else { alert('Invalid credentials'); } }); } // Sample product data - in a real app, this would come from an API const products = [ { id: 1, name: 'Wireless Headphones', price: 99.99, stock: 45, image: 'http://static.photos/technology/200x200/1' }, { id: 2, name: 'Smart Watch', price: 199.99, stock: 12, image: 'http://static.photos/technology/200x200/2' }, { id: 3, name: 'Bluetooth Speaker', price: 59.99, stock: 30, image: 'http://static.photos/technology/200x200/3' } ]; // Render products table const productsTable = document.getElementById('products-table'); if (productsTable) { products.forEach(product => { const row = document.createElement('tr'); row.className = 'hover:bg-gray-50'; row.innerHTML = `
${product.name}
${product.name}
$${product.price.toFixed(2)} ${product.stock} in stock Edit `; productsTable.appendChild(row); }); } // Handle product form submission const productForm = document.getElementById('product-form'); if (productForm) { productForm.addEventListener('submit', function(e) { e.preventDefault(); alert('Product saved successfully!'); window.location.href = 'index.html'; }); } }); function deleteProduct(id) { if (!isAuthenticated) { alert('Please login to perform this action'); return; } if (confirm('Are you sure you want to delete this product?')) { alert(`Product ${id} deleted`); // In a real app, you would call an API to delete the product // Then refresh the page or remove the row from the table } } // Add logout button to navbar function setupNavbar() { const navbar = document.querySelector('custom-navbar'); if (navbar && isAuthenticated) { const userMenu = navbar.shadowRoot.getElementById('user-menu-dropdown'); if (userMenu) { userMenu.innerHTML = ` Your Profile Settings Sign out `; const logoutBtn = navbar.shadowRoot.getElementById('logout-btn'); if (logoutBtn) { logoutBtn.addEventListener('click', logout); } } } } // Call setup functions setupNavbar();