document.getElementById('login-form').addEventListener('submit', async (e) => {
    e.preventDefault();
  
    const username = document.getElementById('username').value;
    const password = document.getElementById('password').value;
  
    try {
      // Send login request to the backend
      const response = await fetch('/api/auth/login', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({ username, password })
      });
  
      if (!response.ok) throw new Error('Login failed');
  
      const data = await response.json();
  
      // Store token and user details in session storage
      sessionStorage.setItem('authToken', data.token);
  
      // Decode token to extract company and branch information
      const decodedToken = JSON.parse(atob(data.token.split('.')[1]));
      
      sessionStorage.setItem('companyName', decodedToken.companyName);
      sessionStorage.setItem('branchName', decodedToken.branchName);
      sessionStorage.setItem('companyId', decodedToken.companyId);
      sessionStorage.setItem('branchId', decodedToken.branchId);
      sessionStorage.setItem('userId', decodedToken.userId);
      sessionStorage.setItem('branchType', decodedToken.branchType);
      sessionStorage.setItem('defaultAction', decodedToken.defaultAction);
      sessionStorage.setItem('kitchenId', decodedToken.kitchenId);
      
      sessionStorage.setItem('permission', decodedToken.permission);
     // sessionStorage.setItem('allowedBranchId', decodedToken.allowedBranchId);
     sessionStorage.setItem('allowedBranchId', JSON.stringify(decodedToken.allowedBranchId));

      // Redirect to another page if necessary
       window.location.href = '/hh';
    } catch (error) {
      console.error('Error during login:', error);
      alert('Invalid username or password');
    }
  });
  