// TG Admin Template - Main JavaScript

document.addEventListener('DOMContentLoaded', () => {
  initSidebar()
  initDropdowns()
})

/**
 * Sidebar Toggle (Mobile)
 */
function initSidebar() {
  const sidebarToggle = document.getElementById('sidebar-toggle')
  const sidebarClose = document.getElementById('sidebar-close')
  const sidebar = document.getElementById('sidebar')
  const overlay = document.getElementById('sidebar-overlay')

  if (sidebarToggle && sidebar) {
    sidebarToggle.addEventListener('click', () => {
      sidebar.classList.remove('-translate-x-full')
      overlay?.classList.remove('hidden')
    })
  }

  if (sidebarClose && sidebar) {
    sidebarClose.addEventListener('click', () => {
      sidebar.classList.add('-translate-x-full')
      overlay?.classList.add('hidden')
    })
  }

  if (overlay && sidebar) {
    overlay.addEventListener('click', () => {
      sidebar.classList.add('-translate-x-full')
      overlay.classList.add('hidden')
    })
  }
}

/**
 * Dropdown Menus
 */
function initDropdowns() {
  const dropdownToggles = document.querySelectorAll('[data-dropdown-toggle]')

  dropdownToggles.forEach((toggle) => {
    const targetId = toggle.getAttribute('data-dropdown-toggle')
    const dropdown = document.getElementById(targetId)

    if (dropdown) {
      toggle.addEventListener('click', (e) => {
        e.stopPropagation()
        dropdown.classList.toggle('hidden')
      })
    }
  })

  // Close dropdowns when clicking outside
  document.addEventListener('click', () => {
    document.querySelectorAll('[data-dropdown]').forEach((dropdown) => {
      dropdown.classList.add('hidden')
    })
  })
}

/**
 * Delete Confirmation
 */
window.confirmDelete = function (message = '确定要删除吗？') {
  return confirm(message)
}
