<template>
  <div :class="$style.oafullbox">
    <a-layout :class="$style.fullsearch">
      <!-- header -->
      <a-layout-header :class="$style.searchheader">
        <km-fullsearch-header
          :search-val="searchVal"
          @onSearch="onSearch"
          @handleSelect="handleSelect"
        ></km-fullsearch-header>
      </a-layout-header>
      <!-- content -->
      <a-layout-content :class="$style.searchcontent">
        <km-apply v-if="isApplyFlag" :apply-list="applyList"></km-apply>
        <div v-else :class="$style.file">
          <!-- 左侧的搜索条件 -->
          <km-fullsearch-classify
            :class="$style.leftcondition"
            @onSearch="onSearch"
          ></km-fullsearch-classify>
          <!-- 搜索内容及结果展示区 -->
          <km-fullsearch-cotent
            ref="content"
            :class="$style.middleresult"
            :search-list="searchList"
            :search-val="searchVal"
            :total="total"
            :params="params"
            @onSearch="onSearch"
          ></km-fullsearch-cotent>
          <!-- 右侧的搜索热度排名 -->
          <div :class="$style.rightrate">
            <km-fullsearch-rate :class="$style.rate" @onSearch="onSearch"></km-fullsearch-rate>
          </div>
        </div>
      </a-layout-content>
    </a-layout>
  </div>
</template>

<script>
import moment from 'moment'
import KmKnowledageService from '../km-knowledage-service'
import KmFullsearchHeader from './km-fullsearch-header'
import KmFullsearchClassify from './km-fullsearch-classify'
import KmFullsearchCotent from './km-fullsearch-cotent'
import KmFullsearchRate from './km-fullsearch-rate'
import components from './_import-components/km-knowledge-fullsearch-import'

export default {
  name: 'KmKnowledgeFullsearch',
  metaInfo: {
    title: '全文检索',
  },
  components: {
    ...components,
    'km-fullsearch-header': KmFullsearchHeader,
    'km-fullsearch-classify': KmFullsearchClassify,
    'km-fullsearch-cotent': KmFullsearchCotent,
    'km-fullsearch-rate': KmFullsearchRate,
  },
  data() {
    return {
      isApplyFlag: false,
      searchList: [],
      searchVal: this.$route.query.searchVal || '请输入搜索内容',
      total: 0,
      params: [
        {
          searchText: '', // 关键词，对应searchVal
          searchDateType: 'all', // 搜索时间
          startDate: '', // 开始时间， extra
          endDate: '', // 结束时间，extra
          typeIds: ['all'], // 知识分类编号
          tagNames: ['all'], // 知识标签名称
          domainIds: ['all'], // 业务领域
          searchFields: ['all'], // 搜索范围
          createDeptIds: ['all'], // 创建部门
          creatorAccounts: ['all'], // 创建人
          searchType: 'splitWrod', // 搜索类型
          searchSort: 'score', // 排序，对应this.sort
          startPosition: '0', // 分页，默认0
          size: 5, // 分页条数
        },
      ],
      applyList: [],
    }
  },
  mounted() {
    if (this.searchVal) {
      this.params[0].searchText = this.searchVal
    }
    this.getSearchList()
  },
  methods: {
    // 文件/应用选择
    handleSelect(val) {
      if (val === 'apply') {
        this.isApplyFlag = true
        this.getApplyList('')
      } else {
        this.isApplyFlag = false
      }
    },
    // 获取搜索条件 搜索
    onSearch(val, type) {
      if (type === 'fileType') {
        this.isApplyFlag = true
        this.getApplyList(val)
        return
      }
      if (type === 'startDate') {
        this.params[0].startDate = val
        return
      }

      this.params.splice(1, 1)
      if (type !== 'startPosition' && type !== 'maxResults') {
        this.params[0].startPosition = 0
      }
      if (type === 'searchResult') {
        // 按结果搜索
        this.params.push(this.params[0])
      } else if (type === 'reset') {
        // 重置
        this.params[0].searchDateType = 'all'
        this.params[0].typeIds = ['all']
        this.params[0].tagNames = ['all']
        this.params[0].domainIds = ['all']
        this.params[0].searchFields = ['all']
        this.params[0].creatorAccounts = ['all']
        this.params[0].createDeptIds = ['all']
      } else if (type === 'searchDateType') {
        // 创建时间
        if (val === 'yourself') {
          // 自定义
          this.params[0].searchDateType = 'part'
          return
        } else if (val === 'all') {
          // all
          this.params[0].searchDateType = 'all'
          this.params[0].startDate = ''
          this.params[0].endDate = ''
        } else {
          this.params[0].searchDateType = 'part'
          this.params[0].endDate = moment(new Date()) // 结束时间
          if (val === 'week') {
            this.params[0].startDate = moment(new Date()).subtract(1, 'weeks')
            // 开始时间
          } else if (val === 'month') {
            this.params[0].startDate = moment(new Date()).subtract(1, 'months')
          } else if (val === '3') {
            this.params[0].startDate = moment(new Date()).subtract(3, 'months')
          } else if (val === '6') {
            this.params[0].startDate = moment(new Date()).subtract(6, 'months')
          } else {
            this.params[0].startDate = moment(new Date()).subtract(1, 'years')
          }
        }
      } else {
        if (type === 'searchText') {
          this.searchVal = val
        }
        if (Object.keys(this.params[0]).indexOf(type) !== -1) {
          this.params[0][type] = val
        }
      }
      if (this.params[0].startDate) {
        this.params[0].startDate = moment(this.params[0].startDate).format('YYYY-MM-DD HH:mm:ss')
      }
      if (this.params[0].endDate) {
        if (this.params[0].searchDateType === 'part') {
          this.params[0].endDate = moment(this.params[0].endDate)
            .add(1, 'days')
            .subtract(1, 'seconds')
        }
        this.params[0].endDate = moment(this.params[0].endDate).format('YYYY-MM-DD HH:mm:ss')
      }
      this.getSearchList()
    },
    // 获取搜索结果
    getSearchList() {
      this.searchList = []
      this.$refs.content.loading = true
      if (this.params[0].searchText === '请输入搜索内容') this.params[0].searchText = ''
      KmKnowledageService.getSearchList(this.params).then((res) => {
        this.total = res.data.total
        const data = res.data.docs
        if (data) {
          data.forEach((item, index) => {
            this.searchList.push(item.fieldValues)
          })
        }
        this.$refs.content.loading = false
      })
    },
    // 获取应用搜索结果
    getApplyList(text) {
      KmKnowledageService.applySearch(text).then((res) => {
        this.applyList = res.data
      })
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
$line-height: 30px;
.oafullbox {
  height: 100%;
  background: #f0f2f5;
}
.fullsearch {
  width: 100%;
  height: 100%;
  .searchheader {
    width: 100%;
    height: 156px;
    padding: 0;
    background-image: linear-gradient(90deg, #1890ff, #0162eb);
  }
  .searchcontent {
    width: 100%;
    height: calc(100% - 64px);
    margin-top: 1rem;
    overflow: scroll;
    .file {
      display: flex;
      .leftcondition {
        width: 300px;
      }
      .middleresult {
        width: calc(100% - 582px);
      }
      .rightrate {
        width: 250px;
        .rate {
          width: calc(100% - 32px);
        }
      }
    }
  }
  :global(.ant-tabs) {
    display: none;
  }
}
</style>
