<template>
  <div class="json-editor">
    <div class="editor-toolbar">
      <button @click="formatJson" title="格式化 JSON">🎨 格式化</button>
      <button @click="minifyJson" title="压缩 JSON">📦 压缩</button>
      <button @click="copyToClipboard" title="复制到剪贴板">📋 复制</button>
    </div>

    <textarea
      v-model="jsonText"
      :class="['editor-textarea', { error: !!error }]"
      @input="handleChange"
      spellcheck="false"
      placeholder="输入 JSON 配置..."
    />

    <div v-if="error" class="editor-error">❌ JSON 解析错误: {{ error }}</div>

    <div class="editor-info">
      <span>行数: {{ jsonText.split('\n').length }}</span>
      <span>字符数: {{ jsonText.length }}</span>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'

interface Props {
  value: any
}

interface Emits {
  (e: 'change', value: any): void
}

const props = defineProps<Props>()
const emit = defineEmits<Emits>()

const jsonText = ref('')
const error = ref<string | null>(null)

// 当外部 value 改变时，更新编辑器内容
watch(
  () => props.value,
  (newValue) => {
    jsonText.value = JSON.stringify(newValue, null, 2)
    error.value = null
  },
  { immediate: true }
)

const handleChange = () => {
  // 尝试解析 JSON
  try {
    const parsed = JSON.parse(jsonText.value)
    error.value = null
    emit('change', parsed)
  } catch (err) {
    error.value = (err as Error).message
  }
}

const formatJson = () => {
  try {
    const parsed = JSON.parse(jsonText.value)
    jsonText.value = JSON.stringify(parsed, null, 2)
    error.value = null
    emit('change', parsed)
  } catch (err) {
    error.value = (err as Error).message
  }
}

const minifyJson = () => {
  try {
    const parsed = JSON.parse(jsonText.value)
    jsonText.value = JSON.stringify(parsed)
    error.value = null
    emit('change', parsed)
  } catch (err) {
    error.value = (err as Error).message
  }
}

const copyToClipboard = () => {
  navigator.clipboard
    .writeText(jsonText.value)
    .then(() => alert('已复制到剪贴板'))
    .catch(() => alert('复制失败'))
}
</script>

<style scoped>
.json-editor {
  display: flex;
  flex-direction: column;
  height: 100%;
  border: 1px solid #e0e0e0;
  border-radius: 6px;
  overflow: hidden;
  background: white;
}

.editor-toolbar {
  display: flex;
  gap: 0.5rem;
  padding: 0.75rem;
  background: #f5f5f5;
  border-bottom: 1px solid #e0e0e0;
}

.editor-toolbar button {
  padding: 0.375rem 0.75rem;
  background: white;
  border: 1px solid #d0d0d0;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.875rem;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  gap: 0.25rem;
}

.editor-toolbar button:hover {
  background: #e8f4ff;
  border-color: #0052d9;
  color: #0052d9;
}

.editor-textarea {
  flex: 1;
  padding: 1rem;
  border: none;
  outline: none;
  font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
  font-size: 0.875rem;
  line-height: 1.5;
  resize: none;
  background: #fafafa;
  color: #333;
}

.editor-textarea.error {
  background: #fff5f5;
  border-left: 3px solid #e34d59;
}

.editor-error {
  padding: 0.75rem;
  background: #fff5f5;
  border-top: 1px solid #ffcdd2;
  color: #e34d59;
  font-size: 0.875rem;
}

.editor-info {
  display: flex;
  justify-content: space-between;
  padding: 0.5rem 0.75rem;
  background: #f5f5f5;
  border-top: 1px solid #e0e0e0;
  font-size: 0.75rem;
  color: #666;
}
</style>
