<template>
  <section class="terminal-panel">
    <div class="terminal-container">
      <div v-if="sessions.length === 0" class="empty-terminal">
        <div class="empty-card">
          <div class="empty-icon">⌨</div>
          <h3>准备连接</h3>
          <p>点击右下角 <strong>⊕</strong> 按钮，<br />填写主机信息后打开终端。</p>
        </div>
      </div>

      <div
        v-for="session in sessions"
        :key="session.id"
        class="terminal-wrapper"
        v-show="session.id === activeId"
      >
        <div class="terminal-header">
          <div class="terminal-tabs">
            <button
              v-for="s in sessions"
              :key="s.id"
              class="t-tab"
              :class="{ active: s.id === activeId }"
              @click="setActive(s.id)"
            >
              <span class="t-tab-dot" :class="s.status"></span>
              <span class="t-tab-label">{{ s.title }}</span>
              <span class="t-tab-close" @click.stop="closeSession(s.id)">×</span>
            </button>
          </div>
          <span class="status-badge" :class="session.status">{{ statusLabel(session.status) }}</span>
        </div>
        <div class="terminal-body" :ref="(el) => setTerminalRef(session.id, el)"></div>
      </div>
    </div>

    <QuickKeys v-if="sessions.length && !focusMode" />
  </section>
</template>

<script setup>
import { useSessions } from "../composables/useSessions";
import { useUI } from "../composables/useUI";
import QuickKeys from "./QuickKeys.vue";

const { sessions, activeId, setTerminalRef, statusLabel, setActive, closeSession } = useSessions();
const { focusMode } = useUI();
</script>

<style scoped>
.terminal-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg-0);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  min-height: 0;
  padding-bottom: var(--keyboard-inset, 0px);
}

.terminal-container {
  flex: 1;
  position: relative;
  background: var(--bg-0);
  overflow: hidden;
  min-height: 0;
}

.empty-terminal {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-0);
}

.empty-card {
  padding: 28px 32px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-2);
  text-align: center;
  max-width: 260px;
}
.empty-icon { font-size: 32px; margin-bottom: 12px; opacity: 0.5; }
.empty-card h3 { margin: 0 0 8px; font-size: 15px; color: var(--text-1); }
.empty-card p { margin: 0; font-size: 13px; color: var(--text-2); line-height: 1.6; }
.empty-card strong { color: var(--accent); }

.terminal-wrapper {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.terminal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 8px 0 4px;
  background: var(--bg-1);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  min-height: 30px;
  gap: 6px;
}

.terminal-tabs {
  display: flex;
  align-items: center;
  gap: 2px;
  overflow-x: auto;
  scrollbar-width: none;
  flex: 1;
  min-width: 0;
}
.terminal-tabs::-webkit-scrollbar { display: none; }

.t-tab {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 10px;
  border-radius: 4px 4px 0 0;
  border: 1px solid transparent;
  border-bottom: none;
  background: transparent;
  color: var(--text-2);
  font-size: 12px;
  cursor: pointer;
  outline: none;
  white-space: nowrap;
  flex-shrink: 0;
  transition: background 0.12s, color 0.12s;
  font-family: inherit;
}
.t-tab:hover { background: var(--bg-2); color: var(--text-1); }
.t-tab.active { background: var(--bg-0); color: var(--text-1); border-color: var(--border); }

.t-tab-dot { width: 6px; height: 6px; border-radius: 50%; background: var(--text-3); flex-shrink: 0; }
.t-tab-dot.ready { background: var(--accent); }
.t-tab-dot.connecting { background: #e3b341; }
.t-tab-dot.error { background: var(--danger); }
.t-tab-label { max-width: 140px; white-space: nowrap; text-overflow: ellipsis; overflow: hidden; }
.t-tab-close { font-size: 11px; color: var(--text-3); padding: 0 1px; }
.t-tab-close:hover { color: var(--danger); }

.status-badge { font-size: 11px; color: var(--text-3); white-space: nowrap; flex-shrink: 0; }
.status-badge.ready { color: var(--accent); }
.status-badge.connecting { color: #e3b341; }
.status-badge.error { color: var(--danger); }

.terminal-body {
  flex: 1;
  padding: 4px;
  min-height: 0;
  overflow: hidden;
  overscroll-behavior: contain;
  background: var(--bg-0);
}

@media (max-width: 768px) {
  .terminal-panel { border-radius: 0; border-left: none; border-right: none; }
}
</style>
