<template>
  <div class="tab-bar">
    <div
      v-for="tab in tabsStore.tabs"
      :key="tab.key"
      class="tab-item"
      :class="{ active: tabsStore.activeTab === tab.key }"
      @click="handleTabClick(tab.key)"
    >
      <span class="tab-label">{{ tab.label }}</span>
      <button
        v-if="tab.key !== 1 && tabsStore.activeTab !== tab.key"
        class="close-btn"
        type="button"
        @click.stop="handleTabClose(tab.key)"
      >
        ×
      </button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useTabsStore } from "@/stores/tab";

const tabsStore = useTabsStore();

const handleTabClick = (tabKey: number) => tabsStore.setActiveTab(tabKey);
const handleTabClose = (tabKey: number) => tabsStore.removeTab(tabKey);
</script>

<style scoped>
.tab-bar {
  display: flex;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  background: #ffffff;
  border-bottom: 1px solid #e5e7eb;
  overflow-x: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.tab-bar::-webkit-scrollbar {
  display: none;
}

.tab-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem;
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 0.5rem;
  cursor: pointer;
  transition: all 0.2s ease;
  white-space: nowrap;
  min-width: fit-content;
  position: relative;
}

.tab-item:hover {
  background: #f1f5f9;
  border-color: #cbd5e1;
}

.tab-item.active {
  background: #3b82f6;
  border-color: #3b82f6;
  color: #ffffff;
}

.tab-item.active:hover {
  background: #2563eb;
  border-color: #2563eb;
}

.tab-label {
  font-size: 0.875rem;
  font-weight: 500;
  line-height: 1.25rem;
}

.close-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 1.25rem;
  height: 1.25rem;
  background: transparent;
  border: none;
  border-radius: 0.25rem;
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  color: inherit;
  transition: background-color 0.2s ease;
  flex-shrink: 0;
}

.close-btn:hover {
  background: rgba(0, 0, 0, 0.1);
}

.tab-item.active .close-btn:hover {
  background: rgba(255, 255, 255, 0.2);
}
</style>
