<template>
  <div class="search-result-page" v-loading="searchLoading">
    <!-- 搜索结果头部 -->
    <div class="result-header">
      <div class="header-container">
        <div class="header-left">
          <h1 class="page-title">{{ $t(`backendSystem.searchResult`) || `搜索结果` }} </h1>
          <p class="search-info" v-if="searchResults.length > 0">
            {{ $t(`backendSystem.keyword`) || `关键词` }}  "<strong>{{ searchKeyword }}</strong>" {{ $t(`backendSystem.find`) || `找到` }} <strong>{{ totalCount }}</strong> {{ $t(`backendSystem.relatedResults`) || `条相关结果` }}
          </p>
        </div>
        <div class="header-right">
          <el-button type="primary" @click="goBack">
            <i class="el-icon-arrow-left"></i>
            {{ $t(`backendSystem.returnSearch`) || `返回搜索` }}
          </el-button>
        </div>
      </div>
    </div>

    <!-- 搜索结果区域 -->
    <div class="search-content">
      <div class="result-container">
        <!-- 搜索结果列表 -->
        <div class="result-list" v-if="searchResults.length > 0">
          <div 
            v-for="item in searchResults" 
            :key="item.id"
            class="result-item"
            @click="handleItemClick(item)"
          >
            <!-- 文档图标 -->
            <div class="item-icon">
              <img 
                v-if="item.titleUrl" 
                :src="item.titleUrl" 
                alt="fail"
                class="doc-icon"
              />
              <i v-else class="el-icon-document doc-icon-placeholder"></i>
            </div>

            <!-- 文档信息 -->
            <div class="item-content">
              <h3 class="item-title" v-html="highlightKeyword(item.title)"></h3>
              <p class="item-subtitle" v-if="item.subTitle">
                {{ item.subTitle }}
              </p>
              <div class="item-meta">
                <span class="meta-item">
                  <i class="el-icon-time"></i>
                  {{ formatDate(item.createTime) }}
                </span>
                <span class="meta-item">
                  <i class="el-icon-user"></i>
                  {{ item.createUser }}
                </span>
                <span class="meta-item">
                  <i class="el-icon-collection-tag"></i>
                  {{ getDocumentTypeName(item.type) }}
                </span>
              </div>
              <div class="item-preview" v-if="item.content">
                <span v-html="getContentPreview(item.content)"></span>
              </div>
            </div>

            <!-- 操作按钮 -->
            <div class="item-actions">
              <el-button 
                type="text" 
                size="small"
                @click.stop="handlePreview(item)"
              >
                <i class="el-icon-view"></i>
                {{ $t(`backendSystem.preview`) || `预览` }}
              </el-button>
       
            </div>
          </div>
        </div>

        <!-- 无搜索结果 -->
        <div class="no-result" v-else>
          <div class="no-result-icon">
            <i class="el-icon-search"></i>
          </div>
          <h3>{{ $t(`backendSystem.noResult`) || `未找到相关结果` }}</h3>
          <p>{{ $t(`backendSystem.keyword`) || `关键词` }} "<strong>{{ searchKeyword }}</strong>" {{ $t(`backendSystem.noMatch`) || `没有找到匹配的文档` }}</p>
          <el-button type="primary" @click="goBack">{{ $t(`backendSystem.resetSearch`) || `重新搜索` }}</el-button>
        </div>

        <!-- 分页组件 -->
        <div class="pagination-container" v-if="searchResults.length > 0">
            <el-pagination        
                @current-change="handleCurrentChange"
                @size-change="handleSizeChange"    
                :current-page="currentPage"
                :page-sizes="[10, 50, 100, 400]"
                :total="totalCount"
                layout="total, sizes, prev, pager, next, jumper"
                class="pagination"
                />
        </div>
      </div>
    </div>

    <!-- 预览对话框 -->
    <el-dialog
      :title="$t(`backendSystem.documentPreview`) || `文档预览`"
      :visible.sync="previewVisible"
      width="80%"
      :before-close="handlePreviewClose"
      class="preview-dialog"
    >
      <div class="preview-content" v-if="previewData">
        <h2>{{ previewData.title }}</h2>
        <div class="preview-meta">
          <span>{{ $t(`backendSystem.type`) || `类型` }}：{{ getDocumentTypeName(previewData.type) }}</span>
          <span>{{ $t(`backendSystem.createTime`) || `创建时间` }}：{{ formatDate(previewData.createTime) }}</span>
          <span>{{ $t(`backendSystem.createUser`) || `创建者` }}：{{ previewData.createUser }}</span>
        </div>
        <div class="preview-body" v-html="previewData.content"></div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import IndexJs from "./index.js";
export default {
  mixins: [IndexJs],
  methods: {
    /**
     * 返回搜索页面
     */
    goBack() {
      this.$router.push(`/${this.$i18n.locale}/helpCenter`)
    }
  }
}
</script>

<style lang="scss" scoped>
.search-result-page {
  min-height: 100vh;
  background: #f5f7fa;
}

/* 搜索结果头部 */
.result-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 18px 0;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.header-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-left {
  color: white;
}

.page-title {
  margin: 0 0 8px 0;
  font-size: 28px;
  font-weight: 600;
}

.search-info {
  margin: 0;
  font-size: 16px;
  opacity: 0.9;

  strong {
    color: #ffd700;
  }
}

.header-right {
  .el-button {
    background: rgba(255, 255, 255, 0.2);
    border: 1px solid rgba(255, 255, 255, 0.3);
    color: white;

    &:hover {
      background: rgba(255, 255, 255, 0.3);
    }
  }
}

/* 搜索内容区域 */
.search-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 40px 20px;
}

.result-container {
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  overflow: hidden;
}

/* 搜索结果列表 */
.result-list {
  padding: 0;
}

.result-item {
  display: flex;
  align-items: flex-start;
  padding: 24px 30px;
  border-bottom: 1px solid #f5f5f5;
  cursor: pointer;
  transition: all 0.3s ease;

  &:hover {
    background: #f8f9ff;
    transform: translateX(4px);
  }

  &:last-child {
    border-bottom: none;
  }
}

.item-icon {
  margin-right: 16px;
  flex-shrink: 0;
}

.doc-icon {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  object-fit: cover;
}

.doc-icon-placeholder {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  background: #f0f0f0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #999;
  font-size: 20px;
}

.item-content {
  flex: 1;
  min-width: 0;
}

.item-title {
  margin: 0 0 8px 0;
  font-size: 18px;
  font-weight: 600;
  color: #333;
  line-height: 1.4;

  mark {
    background: #fff3cd;
    color: #856404;
    padding: 2px 4px;
    border-radius: 3px;
  }
}

.item-subtitle {
  margin: 0 0 8px 0;
  font-size: 14px;
  color: #666;
  line-height: 1.4;
}

.item-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  margin-bottom: 12px;
}

.meta-item {
  display: flex;
  align-items: center;
  font-size: 12px;
  color: #999;

  i {
    margin-right: 4px;
    font-size: 14px;
  }
}

.item-preview {
  font-size: 14px;
  color: #666;
  line-height: 1.6;
  background: #f8f9fa;
  padding: 12px;
  border-radius: 6px;
  border-left: 3px solid #667eea;
}

.item-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-left: 16px;
  flex-shrink: 0;
}

/* 无搜索结果 */
.no-result {
  text-align: center;
  padding: 80px 20px;
  color: #666;

  strong {
    color: #667eea;
  }
}

.no-result-icon {
  font-size: 64px;
  color: #ddd;
  margin-bottom: 20px;
}

.no-result h3 {
  margin: 0 0 12px 0;
  font-size: 24px;
  font-weight: 500;
  color: #333;
}

.no-result p {
  margin: 0 0 24px 0;
  font-size: 16px;
  color: #999;
}

/* 分页容器 */
.pagination-container {
  padding: 30px;
  text-align: center;
  border-top: 1px solid #f0f0f0;
  background: #fafbfc;
}

/* 预览对话框 */
.preview-dialog {
  .preview-content {
    h2 {
      margin: 0 0 16px 0;
      color: #333;
      font-size: 24px;
    }
  }

  .preview-meta {
    display: flex;
    gap: 20px;
    margin-bottom: 24px;
    padding: 16px;
    background: #f8f9fa;
    border-radius: 6px;
    font-size: 14px;
    color: #666;
  }

  .preview-body {
    line-height: 1.8;
    color: #333;

    :deep(h1), :deep(h2), :deep(h3) {
      color: #333;
      margin: 20px 0 12px 0;
    }

    :deep(p) {
      margin: 12px 0;
    }

    :deep(img) {
      max-width: 100%;
      height: auto;
      border-radius: 6px;
    }

    :deep(table) {
      border-collapse: collapse;
      width: 100%;
      margin: 16px 0;
    }

    :deep(th), :deep(td) {
      border: 1px solid #ddd;
      padding: 8px 12px;
      text-align: left;
    }

    :deep(th) {
      background: #f5f5f5;
      font-weight: bold;
    }
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-container {
    padding: 0 15px;
    flex-direction: column;
    gap: 20px;
    text-align: center;
  }

  .page-title {
    font-size: 24px;
  }

  .search-content {
    padding: 20px 15px;
  }

  .result-item {
    padding: 16px 20px;
    flex-direction: column;
  }

  .item-icon {
    margin-right: 0;
    margin-bottom: 12px;
  }

  .item-actions {
    margin-left: 0;
    margin-top: 12px;
    flex-direction: row;
    justify-content: flex-end;
  }

  .item-meta {
    gap: 12px;
  }

  .preview-meta {
    flex-direction: column;
    gap: 8px;
  }
}
</style>