<template>
  <div class="basic-info-container">
    <el-card class="box-card search-card">
      <div class="search-header">
        <span class="title">施工方案管理</span>
        <el-button type="primary" :icon="Plus" @click="handleAdd">新增施工方案</el-button>
      </div>
      
      <el-form :model="searchForm" label-width="80px" class="search-form" label-position="top">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="方案名称">
              <el-input v-model="searchForm.title" placeholder="请输入方案名称" clearable @keyup.enter="handleSearch" />
            </el-form-item>
          </el-col>
          
          <el-col :span="6">
            <el-form-item label="方案类别">
              <el-select v-model="searchForm.plan_category" placeholder="请选择方案类别" clearable style="width: 100%">
                <el-option v-for="item in planCategoryOptions" :key="item" :label="item" :value="item" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="一级分类">
              <el-input v-model="searchForm.level_1_classification" placeholder="请输入一级分类" clearable @keyup.enter="handleSearch" />
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="二级分类">
              <el-select v-model="searchForm.level_2_classification" placeholder="请选择二级分类" clearable style="width: 100%">
                <el-option v-for="item in level2Options" :key="item" :label="item" :value="item" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="三级分类">
              <el-select v-model="searchForm.level_3_classification" placeholder="请选择三级分类" clearable style="width: 100%">
                <el-option v-for="item in level3Options" :key="item" :label="item" :value="item" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="四级分类">
              <el-select v-model="searchForm.level_4_classification" placeholder="请选择四级分类" clearable style="width: 100%">
                <el-option v-for="item in level4Options" :key="item" :label="item" :value="item" />
              </el-select>
            </el-form-item>
          </el-col>
          
          <el-col :span="6">
            <el-form-item label="编制单位">
              <el-input v-model="searchForm.issuing_authority" placeholder="请输入编制单位" clearable @keyup.enter="handleSearch" />
            </el-form-item>
          </el-col>
          
          <el-col :span="6">
            <el-form-item label="编制开始日期">
              <el-date-picker
                v-model="searchForm.release_date_start"
                type="date"
                placeholder="选择开始日期"
                style="width: 100%"
                value-format="YYYY-MM-DD"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="编制结束日期">
              <el-date-picker
                v-model="searchForm.release_date_end"
                type="date"
                placeholder="选择结束日期"
                style="width: 100%"
                value-format="YYYY-MM-DD"
              />
            </el-form-item>
          </el-col>
        </el-row>
        
        <div class="search-buttons">
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </div>
      </el-form>
    </el-card>

    <el-card class="box-card table-card">
      <el-table :data="tableData" v-loading="loading" style="width: 100%" border stripe>
        <el-table-column prop="title" label="方案名称" min-width="200" show-overflow-tooltip />
        <el-table-column prop="kb_name" label="所属知识库" width="150" show-overflow-tooltip>
          <template #default="scope">
            <span v-if="scope.row.kb_name" class="kb-name-tag">
              <el-icon><Tickets /></el-icon> {{ scope.row.kb_name }}
            </span>
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column prop="issuing_authority" label="编制单位" width="180" show-overflow-tooltip />
        <el-table-column prop="release_date" label="编制日期" width="120">
          <template #default="scope">
            {{ formatDate(scope.row.release_date) }}
          </template>
        </el-table-column>
        <el-table-column prop="plan_category" label="方案类别" width="120" />
        <el-table-column prop="level_1_classification" label="一级分类" width="120" />
        <el-table-column prop="level_2_classification" label="二级分类" width="120" />
        <el-table-column prop="level_3_classification" label="三级分类" width="120" />
        <el-table-column prop="level_4_classification" label="四级分类" width="120" />
        <el-table-column label="备注" min-width="150" show-overflow-tooltip>
          <template #default="scope">
            {{ scope.row.note || '-' }}
          </template>
        </el-table-column>
        <el-table-column prop="creator_name" label="创建人" width="100" />
        <el-table-column prop="created_time" label="创建时间" width="180">
          <template #default="scope">
            {{ formatDateTime(scope.row.created_time) }}
          </template>
        </el-table-column>
        <el-table-column prop="updater_name" label="修改人" width="100" />
        <el-table-column prop="updated_time" label="修改时间" width="180">
          <template #default="scope">
            {{ formatDateTime(scope.row.updated_time) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" fixed="right" align="center">
          <template #default="scope">
            <div class="action-buttons">
              <el-tooltip content="详情" placement="top">
                <el-button link type="primary" @click="handleAction('view', scope.row)">
                  <el-icon><View /></el-icon>
                </el-button>
              </el-tooltip>
              <el-tooltip content="下载" placement="top" v-if="scope.row.file_url">
                <el-button link type="success" @click="handleAction('download', scope.row)">
                  <el-icon><Download /></el-icon>
                </el-button>
              </el-tooltip>
              <el-tooltip content="编辑" placement="top">
                <el-button link type="primary" @click="handleAction('edit', scope.row)">
                  <el-icon><Edit /></el-icon>
                </el-button>
              </el-tooltip>
              <el-tooltip content="删除" placement="top">
                <el-button link type="danger" @click="handleAction('delete', scope.row)">
                  <el-icon><Delete /></el-icon>
                </el-button>
              </el-tooltip>
              <el-tooltip content="入库" placement="top">
                <el-button link type="warning" @click="handleAction('ingest', scope.row)">
                  <el-icon><Document /></el-icon>
                </el-button>
              </el-tooltip>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 新增/编辑对话框 -->
    <el-dialog v-model="formDialogVisible" :title="formTitle" width="800px">
      <el-form :model="editForm" label-width="110px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="目标知识库">
              <el-select v-model="editForm.kb_id" placeholder="请选择目标知识库" clearable style="width: 100%">
                <el-option
                  v-for="item in knowledgeBases"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="方案名称" required>
              <el-input v-model="editForm.title" placeholder="请输入方案名称" />
            </el-form-item>
          </el-col>
          
          <el-col :span="12">
            <el-form-item label="项目名称">
              <el-input v-model="editForm.project_name" placeholder="请输入项目名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目标段">
              <el-input v-model="editForm.project_section" placeholder="请输入项目标段" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="编制单位">
              <el-input v-model="editForm.issuing_authority" placeholder="请输入编制单位" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="编制日期">
              <el-date-picker v-model="editForm.release_date" type="date" placeholder="选择日期" value-format="YYYY-MM-DD" style="width: 100%" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="方案类别">
              <el-select v-model="editForm.plan_category" placeholder="请选择方案类别" style="width: 100%">
                <el-option v-for="item in planCategoryOptions" :key="item" :label="item" :value="item" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="一级分类">
              <el-input v-model="editForm.level_1_classification" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="二级分类">
              <el-select v-model="editForm.level_2_classification" placeholder="请选择二级分类" style="width: 100%">
                <el-option v-for="item in level2Options" :key="item" :label="item" :value="item" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="三级分类">
              <el-select v-model="editForm.level_3_classification" placeholder="请选择三级分类" style="width: 100%">
                <el-option v-for="item in level3Options" :key="item" :label="item" :value="item" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="四级分类">
              <el-select v-model="editForm.level_4_classification" placeholder="请选择四级分类" style="width: 100%">
                <el-option v-for="item in level4Options" :key="item" :label="item" :value="item" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="方案摘要">
              <el-input v-model="editForm.plan_summary" type="textarea" :rows="3" placeholder="请输入方案摘要" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="编制依据">
              <div v-for="(planStd, index) in compilationBasisList" :key="index" class="dynamic-input-row">
                <el-input v-model="compilationBasisList[index]" placeholder="请输入编制依据" />
                <div class="row-actions">
                  <el-button :icon="Plus" circle size="small" @click="addListItem(compilationBasisList)" />
                  <el-button :icon="Minus" circle size="small" type="danger" @click="removeListItem(compilationBasisList, index)" v-if="compilationBasisList.length > 1" />
                </div>
              </div>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="editForm.note" type="textarea" :rows="2" placeholder="请输入备注" />
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="上传文件" :required="!editForm.id">
              <el-upload
                v-if="!editForm.id"
                class="upload-demo"
                action="#"
                :auto-upload="false"
                :on-change="handleFileChange"
                :on-remove="handleFileRemove"
                :file-list="fileList"
                :limit="1"
                accept=".pdf,.doc,.docx,.ppt,.pptx,.png,.jpg,.jpeg,.txt"
              >
                <el-button type="primary">选择文件</el-button>
                <template #tip>
                  <div class="el-upload__tip">
                    支持 PDF, Word, PPT, 图片, TXT 等格式
                  </div>
                </template>
              </el-upload>
              <div v-else class="file-info-edit">
                <el-tag type="info" size="large" class="file-tag">
                  <el-icon><Document /></el-icon>
                  <span>文件已锁定（编辑模式不可更改）</span>
                </el-tag>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <el-button @click="formDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm" :loading="submitting">确定</el-button>
      </template>
    </el-dialog>

    <!-- 查看详情对话框 -->
    <el-dialog v-model="detailDialogVisible" title="详情信息" width="600px">
      <el-descriptions :column="1" border>
        <el-descriptions-item label="方案名称">{{ currentItem?.title }}</el-descriptions-item>
        <el-descriptions-item label="所属知识库" v-if="currentItem?.kb_name">
          <span class="kb-name-tag">
            <el-icon><Tickets /></el-icon> {{ currentItem.kb_name }}
          </span>
        </el-descriptions-item>
        <el-descriptions-item label="编制单位">{{ currentItem?.issuing_authority || '-' }}</el-descriptions-item>
        <el-descriptions-item label="编制日期">{{ formatDate(currentItem?.release_date) }}</el-descriptions-item>
        <el-descriptions-item label="项目名称">{{ currentItem?.project_name || '-' }}</el-descriptions-item>
        <el-descriptions-item label="项目标段">{{ currentItem?.project_section || '-' }}</el-descriptions-item>
        <el-descriptions-item label="方案类别">{{ currentItem?.plan_category || '-' }}</el-descriptions-item>
        <el-descriptions-item label="一级分类">{{ currentItem?.level_1_classification || '-' }}</el-descriptions-item>
        <el-descriptions-item label="二级分类">{{ currentItem?.level_2_classification || '-' }}</el-descriptions-item>
        <el-descriptions-item label="三级分类">{{ currentItem?.level_3_classification || '-' }}</el-descriptions-item>
        <el-descriptions-item label="四级分类">{{ currentItem?.level_4_classification || '-' }}</el-descriptions-item>
        <el-descriptions-item label="方案摘要">{{ currentItem?.plan_summary || '-' }}</el-descriptions-item>
        <el-descriptions-item label="编制依据">
          <div v-if="currentItem?.compilation_basis">
            <div v-for="(planStd, idx) in currentItem.compilation_basis.split(';')" :key="idx">{{ planStd }}</div>
          </div>
          <span v-else>-</span>
        </el-descriptions-item>
        <el-descriptions-item label="创建人">{{ currentItem?.creator_name || '-' }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ formatDateTime(currentItem?.created_time) }}</el-descriptions-item>
        <el-descriptions-item label="修改人">{{ currentItem?.updater_name || '-' }}</el-descriptions-item>
        <el-descriptions-item label="修改时间">{{ formatDateTime(currentItem?.updated_time) }}</el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <el-button @click="detailDialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="handleAction('edit', currentItem)">编辑</el-button>
        <el-button type="success" @click="handleAction('preview', currentItem)" v-if="currentItem?.file_url">预览文件</el-button>
      </template>
    </el-dialog>

    <!-- 预览对话框 -->
    <el-dialog v-model="previewVisible" :title="previewTitle" width="85%" top="5vh" custom-class="preview-dialog" @closed="previewUrl = ''">
      <template #header>
        <div class="dialog-header-custom">
          <span>{{ previewTitle }}</span>
          <div class="header-actions">
            <el-button type="primary" link @click="openInNewWindow">
              <el-icon><Monitor /></el-icon> 在新窗口打开
            </el-button>
          </div>
        </div>
      </template>
      <div v-loading="previewLoading" class="preview-content" style="height: 70vh;">
        <iframe 
          v-if="previewUrl" 
          :src="proxyPreviewUrl" 
          width="100%" 
          height="100%" 
          frameborder="0"
          @load="previewLoading = false"
        ></iframe>
        <div v-else-if="!previewLoading" class="no-preview">
          <el-empty description="无法预览该文件" />
        </div>
      </div>
    </el-dialog>

    <!-- 入库确认对话框 -->
    <el-dialog v-model="ingestDialogVisible" title="文档入库" width="450px">
      <el-form :model="ingestForm" label-width="100px">
        <el-form-item label="选择知识库" required>
          <el-select v-model="ingestForm.kb_id" placeholder="请选择知识库" style="width: 100%">
            <el-option
              v-for="item in knowledgeBases"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="切分方式" required>
          <el-radio-group v-model="ingestForm.kb_method">
            <el-radio label="length">按长度切分</el-radio>
            <el-radio label="symbol">按符号切分</el-radio>
            <el-radio label="parent_child">父子段切分</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="ingestDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmIngest" :loading="ingesting">确认入库</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, computed, reactive } from 'vue'
import { Search, View, Monitor, Download, Edit, Delete, Refresh, Plus, Minus, Document, Tickets } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import request from '@/api/request'
import type { ApiResponse } from '@/types/auth'
import dayjs from 'dayjs'
import { downloadFile } from '@/utils/download'
import { getFileExtension } from '@/utils/file'
import { useAuthStore } from '@/stores/auth'

const authStore = useAuthStore()
const loading = ref(false)
const submitting = ref(false)
const tableData = ref([])
const total = ref(0)
const currentPage = ref(1)
const pageSize = ref(20)

// 对话框状态
const formDialogVisible = ref(false)
const detailDialogVisible = ref(false)
const previewVisible = ref(false)
const previewLoading = ref(false)
const previewUrl = ref('')
const previewTitle = ref('')

// 入库相关状态
const ingestDialogVisible = ref(false)
const ingesting = ref(false)
const knowledgeBases = ref<any[]>([])
const ingestForm = reactive({
  kb_id: '',
  kb_method: 'length'
})

// 动态列表字段存储
const compilationBasisList = ref<string[]>([''])
const fileList = ref<any[]>([])

const handleFileChange = (file: any, files: any[]) => {
  fileList.value = files
  if (file.name && !editForm.title) {
    const nameParts = file.name.split('.')
    if (nameParts.length > 1) {
      nameParts.pop()
    }
    editForm.title = nameParts.join('.')
  }
}

const handleFileRemove = () => {
  fileList.value = []
}

const addListItem = (list: string[]) => {
  list.push('')
}

const removeListItem = (list: string[], index: number) => {
  if (list.length > 1) {
    list.splice(index, 1)
  } else {
    list[0] = ''
  }
}

// 当前操作的数据
const currentItem = ref<any>(null)
const editForm = reactive<any>({
  id: null,
  title: '',
  issuing_authority: '',
  release_date: '',
  project_name: '',
  project_section: '',
  note: '',
  file_url: '',
  plan_summary: '',
  compilation_basis: '',
  plan_category: '',
  level_1_classification: '施工方案',
  level_2_classification: '',
  level_3_classification: '',
  level_4_classification: '',
  kb_id: ''
})

const formTitle = computed(() => editForm.id ? '编辑施工方案' : '新增施工方案')

// 检索表单
const searchForm = reactive<any>({
  title: '',
  issuing_authority: '',
  release_date_start: '',
  release_date_end: '',
  plan_category: '',
  level_1_classification: '',
  level_2_classification: '',
  level_3_classification: '',
  level_4_classification: ''
})

// 选项配置
const planCategoryOptions = ['超危大方案', '超危大方案较大II级', '超危大方案特大IV级', '超危大方案一般I级', '超危大方案重大III级', '危大方案', '一般方案']
const level2Options = ['临建工程', '路基工程', '其他', '桥梁工程', '隧道工程']
const level3Options = ['/', 'TBM施工', '拌和站安、拆施工', '不良地质隧道施工', '常规桥梁', '挡土墙工程类', '辅助坑道施工', '复杂洞口工程施工', '钢筋加工场安、拆', '钢栈桥施工', '拱桥', '涵洞工程类', '滑坡体处理类', '路堤', '路堑', '其他', '深基坑', '隧道总体施工', '特殊结构隧道', '斜拉桥', '悬索桥']
const level4Options = ['/', '挡土墙', '顶管', '断层破碎带及软弱围岩', '钢筋砼箱涵', '高填路堤', '抗滑桩', '其他', '软岩大变形隧道', '上部结构', '深基坑开挖与支护', '深挖路堑', '隧道TBM', '隧道进洞', '隧道竖井', '隧道斜井', '特种设备', '瓦斯隧道', '下部结构', '小净距隧道', '岩爆隧道', '岩溶隧道', '涌水突泥隧道', '桩基础']

// 获取列表数据
const loadData = async () => {
  loading.value = true
  try {
    const response = await request.get<ApiResponse<{
      items: any[]
      total: number
      page: number
      size: number
    }>>('/api/v1/sample/basic-info/list', {
      params: {
        type: 'construction_plan',
        page: currentPage.value,
        size: pageSize.value,
        ...searchForm
      }
    })
    
    const resData = (response as unknown) as ApiResponse<{
      items: any[]
      total: number
      page: number
      size: number
    }>

    if (resData.code === 0) {
      tableData.value = resData.data.items as any
      total.value = resData.data.total
    } else {
      ElMessage.error(resData.message || '获取数据失败')
    }
  } catch (error) {
    console.error('获取数据失败:', error)
    ElMessage.error('网络错误，请稍后重试')
  } finally {
    loading.value = false
  }
}

const handleSearch = () => {
  currentPage.value = 1
  loadData()
}

const resetSearch = () => {
  Object.keys(searchForm).forEach(key => {
    searchForm[key] = ''
  })
  handleSearch()
}

const handleSizeChange = (val: number) => {
  pageSize.value = val
  loadData()
}

const handleCurrentChange = (val: number) => {
  currentPage.value = val
  loadData()
}

const formatDate = (date: string | undefined | null) => {
  if (!date) return '-'
  return dayjs(date).format('YYYY-MM-DD')
}

const formatDateTime = (date: string | undefined | null) => {
  if (!date) return '-'
  return dayjs(date).format('YYYY-MM-DD HH:mm:ss')
}

const proxyPreviewUrl = computed(() => {
  if (!previewUrl.value) return ''
  let baseUrl = import.meta.env.VITE_API_BASE_URL || 'http://localhost:8000'
  if (baseUrl.includes('localhost') && window.location.hostname !== 'localhost' && window.location.hostname !== '127.0.0.1') {
    baseUrl = baseUrl.replace('localhost', window.location.hostname)
  }
  return `${baseUrl}/api/v1/sample/documents/proxy-view?url=${encodeURIComponent(previewUrl.value)}&token=${authStore.token}`
})

const handleAction = async (action: string, row: any) => {
  currentItem.value = row
  switch (action) {
    case 'view':
      if (row.file_url) {
        previewTitle.value = row.title
        previewUrl.value = row.file_url
        previewVisible.value = true
        previewLoading.value = true
      } else {
        detailDialogVisible.value = true
      }
      break
    case 'preview':
      if (row.file_url) {
        previewTitle.value = row.title
        previewUrl.value = row.file_url
        previewVisible.value = true
        previewLoading.value = true
      } else {
        ElMessage.warning('该文档暂无预览链接')
      }
      break
    case 'download':
      if (row.file_url) {
        const ext = getFileExtension(row)
        const filename = row.title.endsWith(ext) ? row.title : `${row.title}${ext}`
        downloadFile(row.file_url, filename)
      } else {
        ElMessage.warning('该文档暂无下载链接')
      }
      break
    case 'edit':
      loadKnowledgeBases()
      Object.keys(editForm).forEach(key => {
        editForm[key] = row[key] || ''
      })
      compilationBasisList.value = row.compilation_basis ? row.compilation_basis.split(';') : ['']
      editForm.id = row.id
      fileList.value = []
      formDialogVisible.value = true
      break
    case 'delete':
      ElMessageBox.confirm('确定要删除该条信息吗？', '提示', {
        type: 'warning'
      }).then(async () => {
        try {
          const res = await request.post<ApiResponse>(`/api/v1/sample/basic-info/delete?type=construction_plan&id=${row.id}`)
          if (res.code === 0) {
            ElMessage.success('删除成功')
            loadData()
          } else {
            ElMessage.error(res.message || '删除失败')
          }
        } catch (error) {
          console.error('删除失败:', error)
          ElMessage.error('网络错误，请稍后重试')
        }
      }).catch(() => {})
      break
    case 'ingest':
      currentItem.value = row
      loadKnowledgeBases()
      ingestDialogVisible.value = true
      break
  }
}

const loadKnowledgeBases = async () => {
  try {
    const res = await request.get<ApiResponse<any[]>>('/api/v1/sample/knowledge-base/list', {
      params: {
        page: 1,
        page_size: 1000
      }
    })
    if (res.code === 0) {
      knowledgeBases.value = res.data
      if (knowledgeBases.value.length > 0 && !ingestForm.kb_id) {
        ingestForm.kb_id = knowledgeBases.value[0].id
      }
    }
  } catch (error) {
    console.error('获取知识库失败:', error)
  }
}

const confirmIngest = async () => {
  if (!ingestForm.kb_id) {
    ElMessage.warning('请选择知识库')
    return
  }
  if (!currentItem.value || !currentItem.value.id) {
    ElMessage.warning('未选中有效文档')
    return
  }
  ingesting.value = true
  try {
    const res = await request.post<ApiResponse>('/api/v1/sample/documents/batch-enter', {
      ids: [currentItem.value.id],
      table_type: 'construction_plan',
      kb_id: ingestForm.kb_id,
      kb_method: ingestForm.kb_method
    })
    if (res.code === 0) {
      ElMessage.success(res.message || '已加入入库队列')
      ingestDialogVisible.value = false
      loadData()
    } else {
      ElMessage.error(res.message || '入库失败')
    }
  } catch (error) {
    console.error('入库失败:', error)
    ElMessage.error('网络错误，请稍后重试')
  } finally {
    ingesting.value = false
  }
}

const handleAdd = () => {
  loadKnowledgeBases()
  Object.keys(editForm).forEach(key => {
    editForm[key] = ''
  })
  editForm.id = null
  editForm.level_1_classification = '施工方案'
  compilationBasisList.value = ['']
  fileList.value = []
  formDialogVisible.value = true
}

const submitForm = async () => {
  if (!editForm.title) {
    ElMessage.warning('请输入标题')
    return
  }
  submitting.value = true
  try {
    if (fileList.value.length > 0 && fileList.value[0].raw) {
      const file = fileList.value[0].raw
      const urlRes = await request.post<ApiResponse<any>>('/api/v1/sample/documents/upload-url', {
        filename: file.name,
        content_type: file.type,
        prefix: 'basic-info/construction_plan'
      })
      if (urlRes.code === 0) {
        const { upload_url, file_url } = urlRes.data
        await fetch(upload_url, {
          method: 'PUT',
          body: file,
          headers: {
            'Content-Type': file.type
          }
        })
        editForm.file_url = file_url
      } else {
        throw new Error(urlRes.message || '获取上传链接失败')
      }
    } else if (!editForm.id && fileList.value.length === 0) {
      ElMessage.warning('请选择要上传的文件')
      submitting.value = false
      return
    }

    editForm.compilation_basis = compilationBasisList.value.filter(item => item.trim() !== '').join(';')
    
    const url = editForm.id 
      ? `/api/v1/sample/basic-info/edit?type=construction_plan&id=${editForm.id}`
      : '/api/v1/sample/basic-info/add?type=construction_plan'
    
    const res = await request.post<ApiResponse>(url, editForm)
    if (res.code === 0) {
      ElMessage.success(editForm.id ? '修改成功' : '新增成功')
      formDialogVisible.value = false
      fileList.value = []
      loadData()
    } else {
      ElMessage.error(res.message || '提交失败')
    }
  } catch (error: any) {
    console.error('提交失败:', error)
    ElMessage.error(error.message || '网络错误，请稍后重试')
  } finally {
    submitting.value = false
  }
}

const openInNewWindow = () => {
  if (previewUrl.value) {
    window.open(proxyPreviewUrl.value, '_blank')
  }
}

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

<style scoped>
.basic-info-container {
  padding: 20px;
}
.search-card {
  margin-bottom: 20px;
}
.search-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
.search-header .title {
  font-size: 18px;
  font-weight: bold;
}
.search-form {
  margin-bottom: 0;
}
.search-buttons {
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
}
.table-card {
  margin-bottom: 20px;
}
.pagination-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
.action-buttons {
  display: flex;
  justify-content: center;
  gap: 5px;
}
.file-info-edit {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 100%;
}
.file-tag {
  display: flex;
  align-items: center;
  gap: 5px;
  width: fit-content;
}
.dynamic-input-row {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
  width: 100%;
}
.dynamic-input-row :deep(.el-input) {
  flex: 1;
}
.dynamic-input-row:last-child {
  margin-bottom: 0;
}
.row-actions {
  display: flex;
  gap: 5px;
  margin-left: 10px;
  flex-shrink: 0;
}
.tag-group {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}
.info-tag {
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.popover-tag-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 4px;
}
.popover-info-tag {
  margin-bottom: 4px;
}
.dialog-header-custom {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-right: 30px;
}
.header-actions {
  display: flex;
  align-items: center;
}
.no-preview {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
}
:deep(.preview-dialog .el-dialog__body) {
  padding: 0;
}
</style>
