<!-- @AI-Begin D9V3M 20260612 @@Qoder -->
<template>
  <div class="tool-layout">
    <div class="tool-header">
      <div class="container tool-header__inner">
        <button class="back-btn" @click="router.back()">
          <ArrowLeft :size="16" />
          <span>返回</span>
        </button>
        <div class="tool-title">
          <div class="tool-icon" style="color: #F97316; background: rgba(249, 115, 22, 0.15)">
            <GitCompare :size="24" />
          </div>
          <div>
            <h1>文本对比</h1>
            <p>对比两段文本并高亮显示差异内容</p>
          </div>
        </div>
      </div>
    </div>

    <div class="container tool-content">
      <div class="toolbar">
        <button class="btn btn-primary" @click="compare">
          <GitCompare :size="16" />
          <span>开始对比</span>
        </button>
        <button class="btn btn-secondary" @click="swap">
          <ArrowLeftRight :size="16" />
          <span>交换</span>
        </button>
        <div class="divider"></div>
        <button class="btn btn-danger" @click="clearAll">
          <Trash2 :size="16" />
          <span>清空</span>
        </button>
      </div>

      <div class="editor-grid">
        <div class="editor-pane input-pane">
          <div class="pane-header">
            <span>原文本 (Original)</span>
          </div>
          <textarea
            v-model="originalText"
            class="code-editor"
            placeholder="在此输入原始文本..."
            spellcheck="false"
          ></textarea>
        </div>

        <div class="editor-pane input-pane">
          <div class="pane-header">
            <span>修改后文本 (Modified)</span>
          </div>
          <textarea
            v-model="modifiedText"
            class="code-editor"
            placeholder="在此输入修改后的文本..."
            spellcheck="false"
          ></textarea>
        </div>
      </div>

      <div class="editor-pane result-pane" v-if="hasCompared">
        <div class="pane-header">
          <span>对比结果</span>
          <div class="legend">
            <span class="legend-item added">新增</span>
            <span class="legend-item removed">删除</span>
          </div>
        </div>
        <div class="preview-content">
          <template v-for="(part, idx) in diffResult" :key="idx">
            <span 
              :class="{
                'diff-added': part.added,
                'diff-removed': part.removed
              }"
            >{{ part.value }}</span>
          </template>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ArrowLeft, GitCompare, ArrowLeftRight, Trash2 } from '@lucide/vue'
import { diffWordsWithSpace, type Change } from 'diff'

const router = useRouter()
const originalText = ref('')
const modifiedText = ref('')
const diffResult = ref<Change[]>([])
const hasCompared = ref(false)

function compare() {
  if (!originalText.value && !modifiedText.value) {
    hasCompared.value = false
    return
  }
  diffResult.value = diffWordsWithSpace(originalText.value, modifiedText.value)
  hasCompared.value = true
}

function swap() {
  const temp = originalText.value
  originalText.value = modifiedText.value
  modifiedText.value = temp
  if (hasCompared.value) compare()
}

function clearAll() {
  originalText.value = ''
  modifiedText.value = ''
  hasCompared.value = false
  diffResult.value = []
}
</script>

<style scoped>
.tool-layout {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
.container {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 24px;
  width: 100%;
}
.tool-header {
  border-bottom: 1px solid var(--border);
  background: var(--bg-card);
  padding: 24px 0;
}
.tool-header__inner {
  display: flex;
  flex-direction: column;
  gap: 24px;
}
.back-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-secondary);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 99px;
  padding: 8px 16px;
  cursor: pointer;
  transition: all var(--transition);
  width: fit-content;
}
.back-btn:hover {
  color: var(--text-primary);
  border-color: var(--border-hover);
  background: rgba(255, 255, 255, 0.05);
}
.tool-title {
  display: flex;
  align-items: center;
  gap: 16px;
}
.tool-icon {
  width: 48px;
  height: 48px;
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
}
.tool-title h1 {
  font-family: 'Fira Code', monospace;
  font-size: 24px;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 4px;
}
.tool-title p {
  font-size: 14px;
  color: var(--text-secondary);
}
.tool-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding-top: 24px;
  padding-bottom: 48px;
  gap: 24px;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  font-weight: 500;
  padding: 10px 20px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--transition);
  border: 1px solid transparent;
}

.btn-primary {
  background: var(--accent);
  color: #020617;
}

.btn-primary:hover {
  opacity: 0.9;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px var(--accent-glow);
}

.btn-secondary {
  background: var(--bg-card);
  color: var(--text-primary);
  border-color: var(--border);
}

.btn-secondary:hover {
  border-color: var(--border-hover);
  background: var(--bg-card-hover);
}

.btn-danger {
  background: rgba(239, 68, 68, 0.1);
  color: #EF4444;
  border-color: rgba(239, 68, 68, 0.2);
}

.btn-danger:hover {
  background: rgba(239, 68, 68, 0.2);
}

.divider {
  width: 1px;
  height: 24px;
  background: var(--border);
  margin: 0 4px;
}

.editor-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
  height: 300px;
}
.editor-pane {
  display: flex;
  flex-direction: column;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  overflow: hidden;
}
.pane-header {
  padding: 12px 16px;
  background: rgba(0, 0, 0, 0.2);
  border-bottom: 1px solid var(--border);
  font-family: 'Fira Code', monospace;
  font-size: 13px;
  color: var(--text-muted);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.code-editor {
  flex: 1;
  width: 100%;
  background: transparent;
  border: none;
  padding: 16px;
  font-family: 'Fira Code', monospace;
  font-size: 14px;
  color: var(--text-primary);
  resize: none;
  outline: none;
  line-height: 1.6;
}

.result-pane {
  min-height: 300px;
}

.preview-content {
  flex: 1;
  padding: 16px;
  overflow-y: auto;
  font-family: 'Fira Code', monospace;
  font-size: 14px;
  color: var(--text-primary);
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-all;
  background: var(--bg-primary);
}

.diff-added {
  background-color: rgba(34, 197, 94, 0.2);
  color: #4ade80;
}

.diff-removed {
  background-color: rgba(239, 68, 68, 0.2);
  color: #f87171;
  text-decoration: line-through;
}

.legend {
  display: flex;
  gap: 12px;
}

.legend-item {
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.legend-item::before {
  content: '';
  display: block;
  width: 10px;
  height: 10px;
  border-radius: 2px;
}

.legend-item.added::before {
  background-color: rgba(34, 197, 94, 0.5);
}

.legend-item.removed::before {
  background-color: rgba(239, 68, 68, 0.5);
}

@media (max-width: 900px) {
  .editor-grid {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr 1fr;
    height: auto;
    min-height: 600px;
  }
}
</style>
<!-- @AI-End D9V3M 20260612 @@Qoder -->
