<template>
  <div class="text-process-config">
    <el-card class="config-card">
      <template #header>
        <div class="card-header">
          <span>文本处理配置</span>
          <el-button type="primary" @click="handleSave" :loading="saving"> 保存配置 </el-button>
        </div>
      </template>

      <el-form ref="formRef" :model="configData" :rules="rules" label-width="200px" class="config-form">
        <!-- 意识形态检测配置 -->
        <el-divider content-position="left">意识形态检测配置</el-divider>

        <el-form-item label="启用意识形态检测" prop="enable_ideology_check">
          <el-switch v-model="configData.enable_ideology_check" />
          <div class="form-tip">启用后将对文本进行意识形态相关内容的检测和过滤</div>
        </el-form-item>

        <!-- 敏感词过滤配置 -->
        <el-divider content-position="left">敏感词过滤配置</el-divider>

        <el-form-item label="启用敏感词过滤" prop="enable_sensitive_word_filter">
          <el-switch v-model="configData.enable_sensitive_word_filter" />
          <div class="form-tip">启用后将检测并处理文本中的敏感词</div>
        </el-form-item>

        <el-form-item
          v-if="configData.enable_sensitive_word_filter"
          label="自动替换敏感词"
          prop="auto_replace_sensitive_words">
          <el-switch v-model="configData.auto_replace_sensitive_words" />
          <div class="form-tip">启用后自动将敏感词替换为指定内容</div>
        </el-form-item>

        <el-form-item
          v-if="configData.enable_sensitive_word_filter && configData.auto_replace_sensitive_words"
          label="自定义替换内容"
          prop="custom_replacement">
          <el-input
            v-model="configData.custom_replacement"
            placeholder="请输入替换内容，默认为***"
            maxlength="50"
            show-word-limit />
          <div class="form-tip">当检测到敏感词时，将替换为此内容</div>
        </el-form-item>

        <!-- 敏感人物过滤配置 -->
        <el-divider content-position="left">敏感人物过滤配置</el-divider>

        <el-form-item label="启用敏感人物过滤" prop="enable_sensitive_person_filter">
          <el-switch v-model="configData.enable_sensitive_person_filter" />
          <div class="form-tip">启用后将检测并处理文本中涉及的敏感人物</div>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 移除测试区域 -->
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { textProcessStorage } from '@/utils/llmStorage'

// 表单引用
const formRef = ref<FormInstance>()
const saving = ref(false)

// 配置数据
const configData = reactive({
  enable_ideology_check: true,
  enable_sensitive_word_filter: true,
  enable_sensitive_person_filter: true,
  auto_replace_sensitive_words: true,
  custom_replacement: '***',
})

// 移除测试相关变量

// 表单验证规则
const rules: FormRules = {
  custom_replacement: [{ max: 50, message: '替换内容不能超过50个字符', trigger: 'blur' }],
}

// 获取级别标签类型
const getLevelTagType = (level: string) => {
  const typeMap: Record<string, string> = {
    low: 'info',
    medium: 'warning',
    high: 'danger',
  }
  return typeMap[level] || 'info'
}

// 获取级别标签
const getLevelLabel = (level: string) => {
  const labelMap: Record<string, string> = {
    low: '低',
    medium: '中',
    high: '高',
  }
  return labelMap[level] || level
}

// 移除测试相关方法

// 加载配置
const loadConfig = () => {
  try {
    const config = textProcessStorage.get()
    Object.assign(configData, config)
  } catch (error: any) {
    ElMessage.error(error.message || '加载配置失败')
  }
}

// 保存配置
const handleSave = async () => {
  if (!formRef.value) return

  const valid = await formRef.value.validate()
  if (!valid) return

  saving.value = true
  try {
    textProcessStorage.update(configData)
    ElMessage.success('配置保存成功')
  } catch (error: any) {
    ElMessage.error(error.message || '保存失败')
  } finally {
    saving.value = false
  }
}

// 移除测试文本处理功能

onMounted(() => {
  loadConfig()
})
</script>

<style lang="scss" scoped>
.text-process-config {
  .config-card,
  .test-card {
    margin-bottom: 20px;
  }

  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .config-form {
    .form-tip {
      color: #909399;
      font-size: 12px;
      margin-top: 5px;
    }
  }

  .test-result {
    margin-top: 20px;

    .result-section {
      margin-bottom: 20px;

      h4 {
        margin-bottom: 10px;
        color: #303133;
      }
    }

    .sensitive-words,
    .sensitive-persons {
      margin-top: 10px;

      .word-tag,
      .person-tag {
        margin-right: 8px;
        margin-bottom: 8px;
      }
    }

    .processed-text {
      padding: 15px;
      background: #f5f7fa;
      border-radius: 6px;
      border: 1px solid #e4e7ed;
      line-height: 1.6;
      color: #606266;
    }

    .score-tip {
      margin-top: 10px;
      color: #909399;
      font-size: 12px;
    }

    .score-text {
      color: #fff;
      font-weight: bold;
    }
  }
}
</style>
