$(document).ready(function() {
  // Open modal when clicking user icon
  $('.user-login a').click(function(e) {
    e.preventDefault();
    $('#loginModal').modal('show');
  });

  // Handle tab switching
  $('.tab-btn').click(function() {
    $('.tab-btn').removeClass('active');
    $(this).addClass('active');

    const tab = $(this).data('tab');
    const loginInput = $('#loginInput');
    const passwordGroup = $('.password-group');
    const verificationGroup = $('.verification-group');

    if (tab === 'sms') {
      loginInput.attr('placeholder', 'Enter Phone Number')
               .attr('type', 'tel')
               .attr('pattern', '[0-9]{10,15}');
      passwordGroup.hide();
      verificationGroup.show();
    } else {
      loginInput.attr('placeholder', 'Enter Email/Account ID')
               .attr('type', 'text')
               .removeAttr('pattern');
      passwordGroup.show();
      verificationGroup.hide();
    }

    // Reset form validation
    loginInput.removeClass('is-invalid');
    $('.invalid-feedback').hide();
  });

  // Handle QR code refresh
  $('.refresh-btn').click(function() {
    const qrCode = $('.qr-code');
    const qrStatus = $('.qr-status');
    
    qrStatus.text('Refreshing QR code...');
    
    // Simulate QR code refresh (replace with actual API call)
    setTimeout(() => {
      qrCode.attr('src', 'assets/images/qr-code.png?' + new Date().getTime());
      qrStatus.text('QR code refreshed');
    }, 1000);
  });

  // Handle verification code sending
  $('.send-code-btn').click(function() {
    const loginInput = $('#loginInput');
    const input = loginInput.val().trim();
    
    if (!input) {
      loginInput.addClass('is-invalid');
      return;
    }

    // Simulate sending verification code (replace with actual API call)
    $(this).prop('disabled', true).text('Sending...');
    setTimeout(() => {
      $(this).prop('disabled', false).text('Send Code');
      alert('Verification code sent to ' + input);
    }, 1500);
  });

  // Handle form submission
  $('#loginForm').on('submit', function(e) {
    e.preventDefault();
    
    const loginInput = $('#loginInput');
    const passwordInput = $('#passwordInput');
    const verificationCode = $('#verificationCode');
    const activeTab = $('.tab-btn.active').data('tab');
    
    let isValid = true;
    
    // Validate login input
    if (!loginInput.val().trim()) {
      loginInput.addClass('is-invalid');
      isValid = false;
    } else {
      loginInput.removeClass('is-invalid');
    }

    // Validate based on active tab
    if (activeTab === 'sms') {
      if (!verificationCode.val().trim()) {
        verificationCode.addClass('is-invalid');
        isValid = false;
      } else {
        verificationCode.removeClass('is-invalid');
      }
    } else {
      if (!passwordInput.val().trim()) {
        passwordInput.addClass('is-invalid');
        isValid = false;
      } else {
        passwordInput.removeClass('is-invalid');
      }
    }

    if (!isValid) {
      return;
    }

    // Prepare form data
    const formData = {
      loginInput: loginInput.val().trim(),
      password: passwordInput.val().trim(),
      verificationCode: verificationCode.val().trim(),
      loginType: activeTab
    };

    // Simulate form submission (replace with actual API call)
    console.log('Submitting form data:', formData);
    
    // Show loading state
    $('.login-next').prop('disabled', true).text('Logging in...');
    
    // Simulate API call
    setTimeout(() => {
      $('.login-next').prop('disabled', false).text('Next');
      alert('Login successful!');
      $('#loginModal').modal('hide');
    }, 1500);
  });

  // Clear validation on input
  $('input').on('input', function() {
    $(this).removeClass('is-invalid');
  });

  // Handle social login buttons
  $('.social-btn').click(function() {
    const provider = $(this).hasClass('wechat') ? 'WeChat' :
                    $(this).hasClass('google') ? 'Google' :
                    $(this).hasClass('apple') ? 'Apple' :
                    $(this).hasClass('telegram') ? 'Telegram' : 'LinkedIn';
    
    // Simulate social login (replace with actual OAuth implementation)
    console.log('Initiating ' + provider + ' login');
    alert('Redirecting to ' + provider + ' login...');
  });
});

document.querySelectorAll('.wiki-ea .wiki-nav-item').forEach(function(tab) {
  tab.addEventListener('click', function() {
    // Remove active from all
    document.querySelectorAll('.wiki-ea .wiki-nav-item').forEach(function(t) {
      t.classList.remove('active');
    });
    // Hide all panels
    document.querySelectorAll('.ea-tab-panel').forEach(function(panel) {
      panel.style.display = 'none';
    });
    // Activate clicked tab
    this.classList.add('active');
    // Show corresponding panel
    var tabName = this.getAttribute('data-tab');
    document.getElementById('ea-tab-' + tabName).style.display = 'block';
  });
});
// Show the first tab by default
document.getElementById('ea-tab-tools').style.display = 'block';




let currentSlide = 0;
const totalSlides = 3; // Number of cards
const slider = document.querySelector('.ranking-slider-cards');
const tabBtns = document.querySelectorAll('.ranking-slider-tabs .tab-btn');

function showSlide(idx) {
  slider.style.transform = `translateX(-${idx * 364}px)`; // 340px card + 24px margin
  currentSlide = idx;
}

function autoSlide() {
  showSlide((currentSlide + 1) % totalSlides);
}
let autoSlideInterval = setInterval(autoSlide, 3500);

slider.addEventListener('mouseenter', () => clearInterval(autoSlideInterval));
slider.addEventListener('mouseleave', () => autoSlideInterval = setInterval(autoSlide, 3500));

// Tab switching (if you want to filter data by period)
tabBtns.forEach(btn => {
  btn.addEventListener('click', function() {
    tabBtns.forEach(b => b.classList.remove('active'));
    this.classList.add('active');
    // TODO: Update card data for the selected period
  });
});




document.addEventListener('DOMContentLoaded', function() {
  const icons = document.querySelectorAll('.header-icons i');

  icons.forEach(icon => {
    icon.addEventListener('click', function() {
      // Remove active class from all icons and sections
      icons.forEach(i => i.classList.remove('active'));
      document.querySelectorAll('.content-section').forEach(section => {
        section.classList.remove('active');
      });

      // Add active class to clicked icon and its corresponding section
      this.classList.add('active');
      const sectionId = this.getAttribute('data-section');
      document.getElementById(sectionId).classList.add('active');
    });
  });
});
