<template>
  <div class="search-page wrap">
    <!-- <Header></Header> -->

    <!-- 头部开始 -->
    <div class="header pc">
    <div class="inner">
        <div class="fl header_l">
            <a target="_blank"><img src="../../assets/img/logo.png" alt=""></a>
        </div>
        <div class="header_r fl">
            <ul style="overflow:hidden">

                <li :class="{ active: activeIndex === 0 }">
                  <input type="radio" id="label1" value="" v-model="queryParams.navigation.doc_lable" @change="setActive(0)" class="hidden-radio" />
                  <label for="label1" class="custom-radio">全部</label>&nbsp;
                </li>
                <li :class="{ active: activeIndex === 1 }">
                  <input type="radio" id="label2" value="财经" v-model="queryParams.navigation.doc_lable" @change="setActive(1)" class="hidden-radio" />
                  <label for="label2" class="custom-radio">财经</label>&nbsp;
                </li>
                <li :class="{ active: activeIndex === 2 }">
                  <input type="radio" id="label3" value="法制" v-model="queryParams.navigation.doc_lable" @change="setActive(2)" class="hidden-radio" />
                  <label for="label3" class="custom-radio">法制</label>&nbsp;
                </li>
                <li :class="{ active: activeIndex === 3 }">
                  <input type="radio" id="label4" value="国际" v-model="queryParams.navigation.doc_lable" @change="setActive(3)" class="hidden-radio" />
                  <label for="label4" class="custom-radio">国际</label>&nbsp;
                </li>
                <li :class="{ active: activeIndex === 4 }">
                  <input type="radio" id="label5" value="健康" v-model="queryParams.navigation.doc_lable" @change="setActive(4)" class="hidden-radio" />
                  <label for="label5" class="custom-radio">健康</label>&nbsp;
                </li>
                <li :class="{ active: activeIndex === 5 }">
                  <input type="radio" id="label6" value="军事" v-model="queryParams.navigation.doc_lable" @change="setActive(5)" class="hidden-radio" />
                  <label for="label6" class="custom-radio">军事</label>&nbsp;
                </li>
              <li :class="{ active: activeIndex === 6 }">
                <input type="radio" id="label7" value="科技" v-model="queryParams.navigation.doc_lable" @change="setActive(6)" class="hidden-radio" />
                <label for="label7" class="custom-radio">科技</label>&nbsp;
              </li>
              <li :class="{ active: activeIndex === 7 }">
                <input type="radio" id="label8" value="时政" v-model="queryParams.navigation.doc_lable" @change="setActive(7)" class="hidden-radio" />
                <label for="label8" class="custom-radio">时政</label>&nbsp;
              </li>
              <li :class="{ active: activeIndex === 8 }">
                <input type="radio" id="label9" value="体育" v-model="queryParams.navigation.doc_lable" @change="setActive(8)" class="hidden-radio" />
                <label for="label9" class="custom-radio">体育</label>&nbsp;
              </li>
              <li :class="{ active: activeIndex === 9 }">
                <input type="radio" id="label10" value="文教" v-model="queryParams.navigation.doc_lable" @change="setActive(9)" class="hidden-radio" />
                <label for="label10" class="custom-radio">文教</label>&nbsp;
              </li>
              <li :class="{ active: activeIndex === 10 }">
                <input type="radio" id="label11" value="娱乐" v-model="queryParams.navigation.doc_lable" @change="setActive(10)" class="hidden-radio" />
                <label for="label11" class="custom-radio">娱乐</label>&nbsp;
              </li>
                <!-- <li>
                  <input type="radio" id="label7" value="国家" v-model="queryParams.navigation.doc_lable" @change="performSearch" class="hidden-radio" />
                  <label for="label7" class="custom-radio">国家</label>
                </li> -->


            </ul>
            <!-- 搜索开始 -->
            <div class="search_wrap">
              <div class="search_wrap_l fl">
                  <input type="text" placeholder="请输入标题或关键字" v-model="queryParams.keyword" @input="changeVersion">
                  <button @click="performSearch_s"></button>
                  <!-- 输入推荐 -->
                  <div class="inputtj" id="search-form">
                    <ul>
                      <li @click="showValue($event)" v-for="(item,index) in tjListsData" v-if='index<5' v-html="item.doc_title"></li>
                    </ul>
                  </div>
              </div>

              <div class="search_wrap_r fr" @click="toggleAdvancedSearch" style="cursor:pointer">
                {{ showAdvancedSearch ? '高级检索' : '高级检索+' }}
              </div>
            </div>
            <!-- 搜索结束 -->
        </div>

    </div>

    </div>

    <!-- 头部结束 -->
    <div class="pc pcwrap">
      <div class="inner">
        <div class="contain">

          <!-- 高级搜索弹窗开始 -->
          <div v-if="showAdvancedSearch" class="gaojijiansuo">
            <div class="gaojijiansuo_left fl">
                <h1>高级检索</h1>
                <div class="advanced-options">
                  <div>
                      <label>关键词：</label>
                      <input v-model="advancedQuery.keyword" placeholder="请输入关键词" />
                  </div>

                  <div>
                      <label>包含所有以下词语：</label>
                      <input v-model="advancedQuery.and" placeholder="多个关键词用空格分隔" />
                  </div>
                  <div>
                      <label>包含任意以下词语：</label>
                      <input v-model="advancedQuery.or" placeholder="多个关键词用空格分隔" />
                  </div>

                  <div>
                      <label>不包含以下词语：</label>
                      <input v-model="advancedQuery.not" placeholder="多个关键词用空格分隔" />
                  </div>

                  <div class="btns_wrap">
                      <button @click="gaojiSearch">搜索</button>
                      <button @click="toggleAdvancedSearch" style="background-color: rgb(228, 228, 228);margin-left:30px">取消</button>
                  </div>


                </div>

            </div>
            <div class="gaojijiansuo_right fr">
                <h1>温馨提示</h1>
                <p>1. 高级检索支持选择检索词精确或模糊匹配。</p>
                <p>2. 运算符含义：</p>
                <p style="text-indent:2em;">AND：逻辑与, 所有词同时出现在文献中。</p>
                <p style="text-indent:2em;">OR：逻辑或, 至少一个词出现在文献中。</p>
                <p style="text-indent:2em;">NOT：逻辑非, 后面的词不出现在文献中。</p>
                <p style="text-indent:2em;">“ "：精确匹配, 引号内容作为整体进行检索。</p>
                <p style="text-indent:2em;">( )：限定检索顺序, 括号内容作为一个子查询。</p>
                <p style="text-indent:2em;">?：通配符, 一个问号代表一个字符。</p>
                <p>3. 逻辑运算优先级顺序：</p>
                <p style="text-indent:2em;">( ) > NOT > AND > OR。</p>
                <p>4. 运算符建议使用英文半角输入形式。</p>
            </div>

          </div>
          <!-- 高级搜索弹窗结束 -->




            <div class="cont_wrap">
                <div class="left_nav fl">
                    <h2 @click="getXzgf()" :class="xzgfListShow==true ? 'active':''">
                    资源类型
                    <img src="../../assets/img/xiajiantou.png" alt="">
                    </h2>
                    <ul v-if="xzgfListShow==true">
                      <li>
                        <input type="checkbox" value="pdf" v-model="queryParams.navigation.file_type" /> PDF
                      </li>
                      <li>
                        <input type="checkbox" value="word" v-model="queryParams.navigation.file_type" /> Word
                      </li>
                      <li>
                        <input type="checkbox" value="ppt" v-model="queryParams.navigation.file_type" /> PPT
                      </li>
                      <li>
                        <input type="checkbox" value="txt" v-model="queryParams.navigation.file_type" /> TXT
                      </li>
                      <!-- <li v-if="item.total!=0" @click="searchXzgf(index,item.id)" :class="showIndex==index ? 'active':''" v-for="(item,index) in xzgfListArr" :key="index">{{item.name}}<span class="fr">({{item.total}})</span></li> -->
                    </ul>
                    <h2 @click="getDocnotype()" :class="docnotypeListShow==true ? 'active':''">年份<img src="../../assets/img/xiajiantou.png" alt=""></h2>
                    <ul v-if="docnotypeListShow==true">
                      <li>
                        <input type="checkbox" value="2024" v-model="queryParams.navigation.file_year" /> 2024
                      </li>
                      <li>
                        <input type="checkbox" value="2023" v-model="queryParams.navigation.file_year" /> 2023
                      </li>
                      <li>
                        <input type="checkbox" value="2022" v-model="queryParams.navigation.file_year" /> 2022
                      </li>
                      <li>
                        <input type="checkbox" value="2021" v-model="queryParams.navigation.file_year" /> 2021
                      </li>
                      <!-- <li v-if="item.total!=0" @click="searchDocnotype(index,item.name)" :class="showIndex==index ? 'active':''" v-for="(item,index) in docnotypeListArr" :key="index">{{item.name}}<span class="fr">({{item.value}})</span></li> -->
                    </ul>


                    <h2 @click="getPublisher()" :class="publisherListShow==true ? 'active':''">语种<img src="../../assets/img/xiajiantou.png" alt=""></h2>
                    <ul v-if="publisherListShow==true">
                      <li>
                        <input type="checkbox" value="中文" v-model="queryParams.navigation.language" /> 中文
                      </li>
                      <li>
                        <input type="checkbox" value="俄语" v-model="queryParams.navigation.language" /> 俄文
                      </li>
                      <li>
                        <input type="checkbox" value="蒙语" v-model="queryParams.navigation.language" /> 蒙文
                      </li>
                      <li>
                        <input type="checkbox" value="英语" v-model="queryParams.navigation.language" /> 英文
                      </li>
                    </ul>
                    <h2 @click="getCity()" :class="cityListShow==true ? 'active':''">文件来源<img src="../../assets/img/xiajiantou.png" alt=""></h2>
                    <ul v-if="cityListShow==true">
                      <li>
                        <input type="checkbox" id="ftp" value="ftp" v-model="queryParams.navigation.sourcename"/>
                        <label for="ftp">FTP1</label>
                      </li>
                      <li>
                        <input type="checkbox" id="ftp2" value="ftp2" v-model="queryParams.navigation.sourcename" v-hasPermi="['adv/search:ftp2']"/>
                        <label for="ftp2" v-hasPermi="['adv/search:ftp2']">FTP2</label>
                      </li>
                      <li>
                        <input type="checkbox" id="oracle" value="oracle" v-model="queryParams.navigation.sourcename"/>
                        <label for="oracle">ORACLE</label>
                      </li>
                      <!-- <li v-if="item.total!=0" @click="searchCity(index,item.name)" :class="showIndex==index ? 'active':''" v-for="(item,index) in cityListArr" :key="index">{{item.name}}<span class="fr">({{item.value}})</span></li> -->
                    </ul>
                </div>
                <div class="right_cont fr">
                    <div class="data_wrap">
                      <h1 class="total_wrap">
                        <div class="search_types fl">
                          排序方式：
                          <span :class="RELEVANCEShow==true ? 'active':''" @click="searchRelevance()">按相关度</span>
                          <span :class="crtimeShow==true ? 'active':''" @click="searchCrtime()">按文件创建时间</span>
                          <span :class="visitsShow==true ? 'active':''" @click="searchVisits()">按访问量</span>
                        </div>
                        <h2 class="fr">找到<span>{{totalResultsCount}}</span>篇文献</h2>
                      </h1>
                      <!-- 数据列表 -->
                      <div class="data_list" style="position:relative">
                        <ul style="padding-left:15px">
                          <div v-loading="loading" style="position:absolute;top:85px;left:29%"></div>
                          <li v-for="(result, index) in results" :key="index">
                            <a @click="openDetailPage(result)" target="_blank" v-html="result.doc_title"></a>
                            <!-- <div>
                                关键词：<span v-html="result.doc_content"></span>
                            </div> -->
                            <div>
                                摘要：<span v-html="decodeHtmlEntity(result.doc_content)"></span>
                            </div>
                            <div>
                              <h2 class="fl">
                                时间：{{result.file_updatetime}}
                              </h2>
                              <h2 class="fl">
                                来源：{{result.sourcename}}
                              </h2>
                              <h2 class="fl">
                                文档类型：{{result.file_type}}
                              </h2>
                              <h2 class="fl">
                                语种：{{result.language}}
                              </h2>
                              <h2 class="fl">
                                标签：{{result.doc_lable}}
                              </h2>
                              <el-button class="fl el-button-tk" size="mini" type="text" icon="el-icon-edit" @click="openForm(result.docid,result.uid,result.doc_lable,result.doc_isindex,result.sourcename)" v-hasPermi="['manager:dig-ftp-source:update']">修改标签</el-button>
                            </div>
                          </li>
                        </ul>
                        <!-- 分页 -->
                        <!-- 分页 -->
                        <el-pagination
                          v-if="totalResultsCount != 0"
                          @current-change="handleCurrentChange"
                          :current-page="currentPage"
                          :page-size="10"
                          layout="total,prev, pager,next, jumper"
                          :total="totalResultsCount"
                          prev-text="上一页"
                          next-text="下一页"
                        >
                        </el-pagination>
                        <div class="noResult" v-if="totalResultsCount == 0" v-show="!loading" style="padding-bottom:20px;font-size: 16px;line-height: 30px;">
                          <strong>很抱歉！未能搜到您想要的内容</strong>
                          <br />
                          <strong>建议您</strong>
                          <br />
                          <strong>1.看看输入的文字是否有错误</strong>
                          <br />
                          <strong>2.去掉可能不必要的词或条件</strong>
                        </div>
                      </div>
                    </div>
                </div>
            </div>
        </div>
      </div>
    </div>



    <!-- 搜索栏 -->
    <!-- <div class="search-bar">
      <input
        type="text"
        v-model="queryParams.keyword"
        placeholder="请输入关键词"
      />
      <button @click="performSearch">搜索</button>
      <button class="advanced-search-toggle" @click="toggleAdvancedSearch">
        {{ showAdvancedSearch ? '收起高级搜索' : '展开高级搜索' }}
      </button>
    </div> -->

    <!-- 高级搜索 -->
    <!-- <transition name="fade">
      <div class="filters">

        <div class="filter">
          <label>文档标签：</label>
          <div>
            <input type="radio" id="label1" value="" v-model="queryParams.navigation.doc_lable" @change="performSearch" class="hidden-radio" />
            <label for="label1" class="custom-radio">全部</label>&nbsp;

            <input type="radio" id="label2" value="政治" v-model="queryParams.navigation.doc_lable" @change="performSearch" class="hidden-radio" />
            <label for="label2" class="custom-radio">政治</label>&nbsp;

            <input type="radio" id="label3" value="经济" v-model="queryParams.navigation.doc_lable" @change="performSearch" class="hidden-radio" />
            <label for="label3" class="custom-radio">经济</label>&nbsp;

            <input type="radio" id="label4" value="社会" v-model="queryParams.navigation.doc_lable" @change="performSearch" class="hidden-radio" />
            <label for="label4" class="custom-radio">社会</label>&nbsp;

            <input type="radio" id="label5" value="科学" v-model="queryParams.navigation.doc_lable" @change="performSearch" class="hidden-radio" />
            <label for="label5" class="custom-radio">科学</label>&nbsp;

            <input type="radio" id="label6" value="宗教" v-model="queryParams.navigation.doc_lable" @change="performSearch" class="hidden-radio" />
            <label for="label6" class="custom-radio">宗教</label>&nbsp;

            <input type="radio" id="label7" value="国家" v-model="queryParams.navigation.doc_lable" @change="performSearch" class="hidden-radio" />
            <label for="label7" class="custom-radio">国家</label>
          </div>
        </div>


        <div class="filter">
          <label>文件来源：</label>
          <div>
            <input type="checkbox" id="ftp" value="ftp" v-model="queryParams.navigation.sourcename"/>
            <label for="ftp">FTP1</label>

            <input type="checkbox" id="ftp2" value="ftp2" v-model="queryParams.navigation.sourcename" v-hasPermi="['adv/search:ftp2']"/>
            <label for="ftp2" v-hasPermi="['adv/search:ftp2']">FTP2</label>

            <input type="checkbox" id="oracle" value="oracle" v-model="queryParams.navigation.sourcename"/>
            <label for="oracle">ORACLE</label>
          </div>
        </div>

        <div class="filter">
          <label>资源类型：</label>
          <div>
            <input type="checkbox" value="pdf" v-model="queryParams.navigation.file_type" /> PDF
            <input type="checkbox" value="word" v-model="queryParams.navigation.file_type" /> Word
            <input type="checkbox" value="ppt" v-model="queryParams.navigation.file_type" /> PPT
            <input type="checkbox" value="txt" v-model="queryParams.navigation.file_type" /> TXT
          </div>
        </div>

        <div class="filter">
          <label>语种：</label>
          <div>
            <input type="checkbox" value="中文" v-model="queryParams.navigation.language" /> 中文
            <input type="checkbox" value="俄语" v-model="queryParams.navigation.language" /> 俄文
            <input type="checkbox" value="蒙语" v-model="queryParams.navigation.language" /> 蒙文
            <input type="checkbox" value="英语" v-model="queryParams.navigation.language" /> 英文
          </div>
        </div>

      </div>
    </transition> -->




    <!-- 搜索结果列表 -->
    <!-- <div v-if="loading" class="loading">正在加载...</div>
    <div v-else class="search-results">
      <h3>搜索结果：</h3>
      <ul v-if="results">
        <li v-for="(result, index) in results" :key="index">
          <div class="result-title">{{ result.title }}</div>
          <div class="result-info" @click="openDetailPage(result)">
            <span>类型: {{ result.doc_title }}</span> |
            <span>年份: {{ result.file_path }}</span>
          </div>
        </li>
      </ul>
      <p v-else>未找到匹配的结果</p> -->

      <!-- 分页控制 -->
      <!-- <div class="pagination" v-if="totalPages > 1">
        <button @click="goToPage(1)" :disabled="currentPage === 1">首页</button>
        <button @click="goToPage(currentPage - 1)" :disabled="currentPage === 1">上一页</button>
        <span v-for="page in visiblePages" :key="page" @click="goToPage(page)" :class="{ active: page === currentPage }">{{ page }}</span>
        <button @click="goToPage(currentPage + 1)" :disabled="currentPage === totalPages">下一页</button>
        <button @click="goToPage(totalPages)" :disabled="currentPage === totalPages">尾页</button>
      </div> -->

    <Footer></Footer>
        <DigFtpSourceForm ref="formRef" @success="getList" />
  </div>
</template>

<script>
// import Header from './header.vue';
import Footer from './footer.vue';
import _ from "lodash";
import * as searchPageApi from '@/api/manager/digsearch';
import DigFtpSourceForm from './DigFtpSourceForm.vue';
import $ from 'jquery';

export default {
  name: "SearchPage",
  components: {
    Footer,
    DigFtpSourceForm
    // Header
  },

  data() {
    return {
      RELEVANCEShow:true,//相关度排序
      crtimeShow:false,//时间排序
      visitsShow:false,//访问量排序
      activeIndex:0,
      xzgfListShow:true,
      publisherListShow:true,//语种
      cityListShow:true,
      docnotypeListShow:true,//年份
      tjListsData:[],//输入推荐
      searchQuery: "",
      results: [],
      queryParams: {
        keyword: "",
        navigation: {
          sourcename: [],
          file_type: [],
          language: [],
          doc_lable: [],
          file_year:[]
        },
        sort:{
          // RELEVANCE:'desc'
        },
        sourcename: [],
        language: "",
        selectedYear: "",
        pageNo: 1,
        pageSize: 10,
        advField: [], // 搜索字段
        fieldType: [], // 字段类型
        connect: [], // 逻辑运算符
        advValue: [], // 搜索值
        type: 1,
      },
      advancedQuery: {
        keyword: "",
        and: "", // 包含所有以下词语（AND）条件
        or: "", // 包含任意以下词语（OR）条件
        not: "", // 不包含以下词语（NOT）条件
      },
      availableYears: Array.from({ length: 20 }, (_, i) => new Date().getFullYear() - i),
      loading: false,
      showAdvancedSearch: false,
      totalResultsCount: 0,      // 总结果数，用于计算分页
      currentPage: 1,            // 当前页码
      itemsPerPage: 10,          // 每页显示条目数
    };
  },
  computed: {
    /*paginatedResults() {
      const start = (this.currentPage - 1) * this.itemsPerPage;
      return this.results.slice(start, start + this.itemsPerPage);
    },*/
    totalPages() {
      return Math.ceil(this.totalResultsCount / this.itemsPerPage);
    },
    visiblePages() {
      const range = 5;
      const start = Math.max(1, this.currentPage - Math.floor(range / 2));
      const end = Math.min(this.totalPages, start + range - 1);
      return Array.from({ length: end - start + 1 }, (_, i) => start + i);
    },
    hasFtp2Perm() {
      return this.$store.getters.permissions.includes("adv/search:ftp2");
    }
  },
  methods: {
    decodeHtmlEntity(encodedStr) {
      const doc = new DOMParser().parseFromString(encodedStr, "text/html");
      return doc.body.textContent || doc.body.innerText;
    },
     /** 添加/修改操作 */
    openForm(docid,uid,doc_lable,doc_isindex,sourcename) {
      this.$refs["formRef"].open(docid,uid,doc_lable,doc_isindex,sourcename);
    },
      /** 查询列表 */
    async getList() {
      this.performSearch();

    },

    // 文档标签
    setActive(index){
      this.activeIndex = index;
      this.performSearch();
    },
    // 资源类型
    getXzgf(){
      this.xzgfListShow=!this.xzgfListShow
    },
    // 语种
    getPublisher(){
      this.publisherListShow=!this.publisherListShow
    },
    // 文件来源
    getCity(){
      this.cityListShow=!this.cityListShow
    },
    // 年份
    getDocnotype(){
      this.docnotypeListShow=!this.docnotypeListShow
    },
    performSearch_s(){
      this.queryParams.pageNo = 1;
      this.performSearch();
    },
    // 搜索功能
    async performSearch() {
      this.queryParams.type = 1;
      this.loading = true;
      // 拼接查询参数
      const searchParams = this.buildSearchParams();
      // 发送请求到后端
      try {
        // 检查是否有“adv/search:ftp2”权限
        const res = this.hasFtp2Perm
          ? await searchPageApi.getSearchPageJUEM(searchParams)
          : await searchPageApi.getSearchPageJM(searchParams);
        this.results = res.data.list; // 假设返回的数据包含 results
        this.totalResultsCount = res.data.total;
        this.loading = false;
      } catch (error) {
        console.error("搜索失败:", error);
        this.loading = false;
      }
    },

    // 拼接查询参数
    buildSearchParams() {
      const { keyword, navigation, advancedQuery,sort } = this.queryParams;
      const searchParams = {
        condition: 1,
        type: this.queryParams.type,
        keyword: keyword.trim(),
        metaId: this.queryParams.metaId,
        navigation: {

        },
        sort:{

        },
        pageNo: this.queryParams.pageNo,
        pageSize: this.queryParams.pageSize,
        advField: this.queryParams.advField, // 搜索字段
        fieldType: this.queryParams.fieldType, // 字段类型
        connect: this.queryParams.connect, // 逻辑运算符
        advValue: this.queryParams.advValue, // 搜索值
      };
      // console.log(navigation)
      if(navigation.sourcename.length){
        searchParams.navigation.sourcename = navigation.sourcename;
      }

      if(navigation.doc_lable.length){
        searchParams.navigation.doc_lable = [navigation.doc_lable];
      }

      if(navigation.file_type.length){
        searchParams.navigation.file_type = navigation.file_type;
      }
      if (navigation.file_type.length) {
        if (navigation.file_type.includes('word')) {
          if (!searchParams.navigation.file_type.includes("doc")) {
            searchParams.navigation.file_type.push("doc");
          }
          if (!searchParams.navigation.file_type.includes("docx")) {
            searchParams.navigation.file_type.push("docx");
          }
        } else {
          // 移除 'doc' 和 'docx'
          searchParams.navigation.file_type = searchParams.navigation.file_type.filter(type => type !== "doc" && type !== "docx");
        }

        if (navigation.file_type.includes('ppt')) {
          if (!searchParams.navigation.file_type.includes("ppt")) {
            searchParams.navigation.file_type.push("ppt");
          }
          if (!searchParams.navigation.file_type.includes("pptx")) {
            searchParams.navigation.file_type.push("pptx");
          }
        } else {
          // 移除 'ppt' 和 'pptx'
          searchParams.navigation.file_type = searchParams.navigation.file_type.filter(type => type !== "ppt" && type !== "pptx");
        }
      }

      if(navigation.language.length){
        searchParams.navigation.language = navigation.language;
      }

      if(navigation.file_year.length){
        searchParams.navigation.file_year = navigation.file_year;
      }
      if(this.RELEVANCEShow==true){
        searchParams.sort.RELEVANCE = 'desc';
      }
      if(this.crtimeShow==true){
        searchParams.sort.file_crtime = 'desc';
      }
      if(this.visitsShow==true){
        searchParams.sort.doc_visit = 'desc';
      }
      //searchParams.navigation.file_type = [navigation.file_type];
      return searchParams;
    },
    // 按相关度
    searchRelevance(){
      this.RELEVANCEShow=true;
      this.crtimeShow=false;
      this.visitsShow=false;
      // this.queryParams.sort.RELEVANCE='desc';
      // this.queryParams.sort.file_crtime='';
      this.performSearch();
    },
    // 按创建时间
    searchCrtime(){
      this.crtimeShow=true;
      this.RELEVANCEShow=false;
      this.visitsShow=false;
      // this.queryParams.sort.file_crtime='desc';
      // this.queryParams.sort.RELEVANCE='';
      this.performSearch();
    },
    // 按访问量
    searchVisits(){
      this.visitsShow=true;
      this.RELEVANCEShow=false;
      this.crtimeShow=false;
      // this.queryParams.sort.file_crtime='desc';
      // this.queryParams.sort.RELEVANCE='';
      this.performSearch();
    },
    toggleAdvancedSearch() {
      this.queryParams.advValue = [];
      this.advancedQuery.keyword='';
      this.advancedQuery.and='';
      this.advancedQuery.or='';
      this.advancedQuery.not='';

      this.showAdvancedSearch = !this.showAdvancedSearch;
    },
    // 添加新的条件输入框
    addCondition(type) {
      if (type === 'and') {
        this.advancedQuery.and.push(""); // 添加AND条件
      } else if (type === 'or') {
        this.advancedQuery.or.push(""); // 添加OR条件
      } else if (type === 'not') {
        this.advancedQuery.not.push(""); // 添加NOT条件
      }
    },
    gaojiSearch(){
      if(this.advancedQuery.keyword==''){
          alert('关键词不允许为空，请输入关键词')
      }else{
        this.applyAdvancedSearch()
      }

    },
    // 应用高级搜索
    async applyAdvancedSearch() {

      let advField = [];
      let fieldType = [];
      let connect = [];
      let advValue = [];

      // 处理关键词
      if (this.advancedQuery.keyword.trim()) {
        advField.push("doc_title");
        fieldType.push("string");
        //connect.push(""); // 关键词没有逻辑运算符
        advValue.push(this.advancedQuery.keyword.trim());
      }

      // 处理AND条件
      if (this.advancedQuery.and.trim()) {
        advField.push("doc_content");
        fieldType.push("string");
        connect.push("AND");
        //advValue.push(this.advancedQuery.and.trim().split(" ").map(word => word.trim())); // 按空格分隔并去掉空格
        advValue.push(this.advancedQuery.and.trim());
      }

      // 处理OR条件
      if (this.advancedQuery.or.trim()) {
        advField.push("doc_content");
        fieldType.push("string");
        connect.push("OR");
        //advValue.push(this.advancedQuery.or.trim().split(" ").map(word => word.trim())); // 按空格分隔并去掉空格
        advValue.push(this.advancedQuery.or.trim());
      }

      // 处理NOT条件
      if (this.advancedQuery.not.trim()) {
        advField.push("doc_content");
        fieldType.push("string");
        connect.push("NOT");
        //advValue.push(this.advancedQuery.not.trim().split(" ").map(word => word.trim())); // 按空格分隔并去掉空格
        advValue.push(this.advancedQuery.not.trim());
      }

      // 拼接最终的搜索请求参数
      this.queryParams.type = 2;
      this.queryParams.advField = advField;
      this.queryParams.fieldType = fieldType;
      this.queryParams.connect = connect;
      this.queryParams.advValue = advValue;

      // 调用搜索接口
      this.queryParams.type = 2;
      this.loading = true;
      // 拼接查询参数
      const searchParams = this.buildSearchParams();
      // 发送请求到后端
      try {
        // 检查是否有“adv/search:ftp2”权限
        const res = this.hasFtp2Perm
          ? await searchPageApi.getSearchPageJUEM(searchParams)
          : await searchPageApi.getSearchPageJM(searchParams);
        this.results = res.data.list; // 假设返回的数据包含 results
        this.totalResultsCount = res.data.total;
        this.loading = false;
      } catch (error) {
        // console.error("搜索失败:", error);
        this.loading = false;
      }
    },
    goToPage(page) {
      if (page > 0 && page <= this.totalPages) {
        this.currentPage = page;
        this.queryParams.pageNo = page; // 更新分页参数
        this.performSearch(); // 重新请求数据
      }
    },
    handleCurrentChange(newPage) {
      this.currentPage = newPage;
      this.queryParams.pageNo = newPage; // 更新分页参数
      this.performSearch();
    },
    openDetailPage(result) {
      localStorage.setItem('detailData', JSON.stringify(result));
      // 打开新标签页
      window.open('/#/search/detail', '_blank');

    },
        // 输入推荐接口
      async  changeVersion(){
            $('.inputtj').show();
            const searchParamsV = {
          condition: 1,
          type: 3,
          keyword: this.queryParams.keyword,
          navigation: {

          },
          sort: {},
          libType: 1,
          pageNo: 1,
          pageSize: 5
      };
      try {
          const res = this.hasFtp2Perm
          ? await searchPageApi.getSearchPageJUEM(searchParamsV)
          : await searchPageApi.getSearchPageJM(searchParamsV)
          this.tjListsData = res.data.list;
          console.log(tjListsData);
      } catch (error) {
        console.error("搜索失败:", error);
        // this.loading = false;
      }

        },
            // 点击输入推荐
        showValue(event){
            var e = event.currentTarget;
            // console.log('输入推荐：'+e.innerHTML);
            this.queryParams.keyword=e.innerText;
            $('.inputtj').hide();
            // this.askAnswer();
        },
    /*debouncedSearch: _.debounce(function () {
      this.performSearch();
    }, 300),*/
  },
  mounted:function(){
    if(this.$route.query.gaoji==1){
      this.showAdvancedSearch=true;
    }
    if(this.$route.query.id){
      // console.log('id值：'+this.$route.query.id);
      this.queryParams.navigation.doc_lable=this.$route.query.name;
      this.setActive(this.$route.query.id);
      // this.activeIndex = this.$route.query.id;
      // this.performSearch();

    }

    this.performSearch();
  }
};

// 关闭输入推荐框
$(document).mouseup(function(e){
    var _con=$('#search-form');//设置目标区域
    if(!_con.is(e.target)&&_con.has(e.target).length===0){
        $('#search-form').hide()
    }
});

</script>

<style scoped>
@import '../../assets/css/css.css';
@import '../../assets/css/index.css';
/* .search-page {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
} */
.search-bar {
  display: flex;
  align-items: center;
  gap: 8px;
}
.search-bar input {
  flex: 1;
  padding: 8px;
  border: 1px solid #ccc;
}
.search-bar button {
  padding: 8px 16px;
}
.advanced-search-toggle {
  margin-left: auto;
  background: none;
  border: none;
  color: #007bff;
  cursor: pointer;
}
.filters {
  display: flex;
  gap: 20px;
  margin-top: 10px;
}
.search-results {
  border-top: 1px solid #eee;
  padding-top: 20px;
}
.loading {
  text-align: center;
  padding: 20px;
  color: #666;
}
.pagination {
  display: flex;
  justify-content: center;
  gap: 5px;
  margin-top: 20px;
}
.pagination button,
.pagination span {
  padding: 5px 10px;
  cursor: pointer;
}
.pagination .active {
  font-weight: bold;
  color: #007bff;
}

/* 隐藏原生单选框 */
.hidden-radio {
  display: none;
}

/* 默认样式 */
.custom-radio {
  font-weight: normal;
  cursor: pointer;
}

/* 选中时加粗 */
.hidden-radio:checked + .custom-radio {
  font-weight: bold;
}

.el-button-tk{
    margin-top: 26px;
    margin-bottom: 10px;
}
/* 输入推荐 */
.inputtj{
    /* width: 300px; */
    height: auto;
    background: #fff;
    box-shadow: 0 0 3px rgba(16,115,183,.2);
    /* border: 1px solid #ffffff; */
    border: none;
    position: absolute;
    top: 126px;

    /* left: 0; */
    z-index: 1000;
    padding-right: 20px;
    max-width: 1100px;
}
.inputtj ul{
    padding-left: 10px;
}
.inputtj li{
    cursor: pointer;
    font-size: 14px;
    line-height: 30px;
    color: #333;
    margin: 0;
    width: 100%;
}
.inputtj li:hover{
    color: #0f5de5
}

</style>
