<template>
  <div class="quadrant-panel" :class="{ 'quadrant-panel--collapsed': collapsed }">
    <div class="quadrant-panel__header">
      <div class="quadrant-panel__tabs" role="tablist" :aria-label="`${storageKey || 'panel'} tabs`">
        <button
          v-for="tab in tabs"
          :key="tab.key"
          :class="['quadrant-panel__tab', { 'quadrant-panel__tab--active': activeTab === tab.key }]"
          role="tab"
          :aria-selected="activeTab === tab.key"
          :tabindex="activeTab === tab.key ? 0 : -1"
          @click="activeTab = tab.key"
          @keydown="onTabKeydown"
        >
          <component :is="tab.icon" v-if="tab.icon" class="quadrant-panel__tab-icon" />
          <span>{{ tab.label }}</span>
        </button>
      </div>
      <button
        class="quadrant-panel__collapse-btn"
        @click="collapsed = !collapsed"
        :title="collapsed ? '展开' : '折叠'"
        :aria-label="collapsed ? '展开面板' : '折叠面板'"
      >
        <MinusOutlined v-if="!collapsed" />
        <PlusOutlined v-else />
      </button>
    </div>
    <div v-show="!collapsed" class="quadrant-panel__body">
      <div
        v-for="tab in activeTabList"
        :key="tab.key"
        role="tabpanel"
        class="quadrant-panel__content"
      >
        <slot :name="tab.key" />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, watch, computed, type Component } from 'vue'
import { MinusOutlined, PlusOutlined } from '@ant-design/icons-vue'

export interface QuadrantTab {
  key: string
  label: string
  icon?: Component
}

const props = withDefaults(defineProps<{
  tabs: QuadrantTab[]
  defaultTab?: string
  storageKey?: string
}>(), {
  defaultTab: '',
})

const savedTab = props.storageKey
  ? localStorage.getItem(props.storageKey) || props.defaultTab
  : props.defaultTab

const activeTab = ref(savedTab || (props.tabs[0]?.key ?? ''))
const collapsed = ref(props.storageKey ? localStorage.getItem(props.storageKey + '-collapsed') === 'true' : false)

// Only render the active tab (v-if via computed filter)
const activeTabList = computed(() => props.tabs.filter(t => t.key === activeTab.value))

watch(activeTab, (val) => {
  if (props.storageKey) localStorage.setItem(props.storageKey, val)
})

watch(collapsed, (val) => {
  if (props.storageKey) localStorage.setItem(props.storageKey + '-collapsed', String(val))
})

function setActiveTab(tabKey: string) {
  if (props.tabs.some(t => t.key === tabKey)) {
    activeTab.value = tabKey
  }
}

function onTabKeydown(e: KeyboardEvent) {
  const currentIndex = props.tabs.findIndex(t => t.key === activeTab.value)
  let nextIndex = currentIndex

  if (e.key === 'ArrowRight' || e.key === 'ArrowDown') {
    e.preventDefault()
    nextIndex = (currentIndex + 1) % props.tabs.length
  } else if (e.key === 'ArrowLeft' || e.key === 'ArrowUp') {
    e.preventDefault()
    nextIndex = (currentIndex - 1 + props.tabs.length) % props.tabs.length
  } else if (e.key === 'Home') {
    e.preventDefault()
    nextIndex = 0
  } else if (e.key === 'End') {
    e.preventDefault()
    nextIndex = props.tabs.length - 1
  }

  if (nextIndex !== currentIndex) {
    activeTab.value = props.tabs[nextIndex].key
  }
}

defineExpose({ setActiveTab })
</script>

<style scoped>
.quadrant-panel {
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
  background: var(--bg-primary);
  border-radius: var(--radius-lg);
  border: 1px solid var(--border-color-split);
  transition: border-color var(--transition-normal);
}

.quadrant-panel:hover {
  border-color: var(--border-color);
}

.quadrant-panel--collapsed {
  height: auto !important;
}

.quadrant-panel__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 38px;
  padding: 0 var(--space-3);
  border-bottom: 1px solid var(--border-color-split);
  background: var(--bg-secondary);
  flex-shrink: 0;
}

.quadrant-panel__tabs {
  display: flex;
  gap: 2px;
  overflow-x: auto;
  scrollbar-width: none;
}

.quadrant-panel__tabs::-webkit-scrollbar {
  display: none;
}

.quadrant-panel__tab {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  padding: var(--space-1) var(--space-3);
  border: none;
  background: transparent;
  color: var(--text-tertiary);
  font-size: var(--font-xs);
  cursor: pointer;
  border-radius: var(--radius-md);
  white-space: nowrap;
  transition: all var(--transition-fast);
  position: relative;
}

.quadrant-panel__tab:hover {
  color: var(--text-secondary);
  background: var(--bg-tertiary);
}

.quadrant-panel__tab--active {
  color: var(--color-primary);
  background: var(--color-primary-light);
  font-weight: var(--font-weight-medium);
}

.quadrant-panel__tab-icon {
  font-size: 12px;
}

.quadrant-panel__collapse-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  border: none;
  background: transparent;
  color: var(--text-placeholder);
  cursor: pointer;
  border-radius: var(--radius-md);
  flex-shrink: 0;
  transition: all var(--transition-fast);
}

.quadrant-panel__collapse-btn:hover {
  color: var(--text-secondary);
  background: var(--bg-tertiary);
}

.quadrant-panel__body {
  flex: 1;
  overflow: hidden;
}

.quadrant-panel__content {
  height: 100%;
  overflow: auto;
}
</style>
