<template>
  <div :class="$style.fullbox">
    <a-layout :class="$style.fullsearch">
      <!-- header -->
      <a-layout-header :class="$style.searchheader">
        <oaFullsearchHeader
          :class="$style.searchbg"
          :search-val="searchVal"
          @onSearch="onSearch"
          @handleSelect="handleSelect"
        ></oaFullsearchHeader>
      </a-layout-header>
      <!-- content -->
      <a-layout-content :class="[$style.searchcontent, { [$style.hiddentab]: isOaSearch }]">
        <!-- tab页 -->
        <a-tabs v-model="activeKey" :class="$style.fulltabs">
          <a-tab-pane key="oaSearch" tab="办公系统">
            <oaFullContent
              ref="oaSearch"
              :is-apply-flag="isApplyFlag"
              :apply-list="applyList"
              :search-val="searchVal"
              :search-list="searchList"
              :total="total"
              :params="params"
              @onSearch="onSearch"
            ></oaFullContent>
          </a-tab-pane>
          <!-- 扩展插槽 1、a-tab-pane的key值与ref值必须一致; 2、搜索的方法名必须为onSearch。-->
          <slot name="searchtabs" />
        </a-tabs>
      </a-layout-content>
    </a-layout>
  </div>
</template>

<script>
import moment from 'moment'
import components from './_import-components/oa-full-search-import'
import oaFullsearchHeader from './oa-fullsearch-header.vue'
import FullSearchService from './fullsearch-service'
import oaFullContent from './oa-full-content.vue'

export default {
  name: 'OaFullSearch',
  metaInfo: {
    title: '全文检索',
  },
  components: {
    ...components,
    oaFullsearchHeader,
    oaFullContent,
  },
  data() {
    return {
      activeKey: 'oaSearch',
      isApplyFlag: false,
      searchList: [],
      globalVal: '',
      searchVal: this.$route.query.searchVal || '请输入搜索内容',
      total: 0,
      params: [
        {
          createDeptId: ['all'], // 发起部门
          creatorUserAccount: ['all'], // 发起人
          docTypeId: ['all'], // 业务类型
          searchDateType: 'all', // 搜索时间
          flowStatus: ['all'], // 流转状态
          selField: ['all'], // 搜索范围
          startDate: '', // 开始时间， extra
          endDate: '', // 结束时间，extra
          searchSort: 'score', // 排序，对应this.sort
          searchText: '', // 关键词，对应searchVal
          startPosition: '0', // 分页，默认0
          maxResults: '10', // 分页条数
          // fileType: 'all', // 文件类型 （预留）
          searchType: 'splitWrod', // 搜索类型
          // oaNumber: '财务[2020]001号', // 预留
        },
      ],
      applyList: [],
      isOaSearch: true,
    }
  },
  created() {
    if (this.$route.name === 'fullSearch') {
      this.isOaSearch = true
    } else {
      this.isOaSearch = false
    }
    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 (this.activeKey === 'oaSearch') {
        if (this.isApplyFlag || type === 'fileType') {
          this.isApplyFlag = true
          this.getApplyList(val)
          return
        }
        this.params.splice(1, 1)
        if (type !== 'startPosition' && type !== 'maxResults') {
          this.params[0].startPosition = 0
        }
        if (type === 'searchResult') {
          // 按结果搜索
          const resultparams = Object.assign({}, this.params[0], { searchText: val })
          this.params.push(resultparams)
          this.params[0].searchText = this.globalVal
        } else if (type === 'reset') {
          // 重置
          this.params[0].searchDateType = 'all'
          this.params[0].flowStatus = ['all']
          this.params[0].selField = ['all']
          this.params[0].creatorUserAccount = ['all']
          this.params[0].createDeptId = ['all']
        } else if (type === 'searchDateType') {
          // 创建时间
          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().format('YYYY-MM-DD HH:mm:ss') // 结束时间
            if (val === 'week') {
              this.params[0].startDate = moment(new Date())
                .subtract(1, 'weeks')
                .format('YYYY-MM-DD HH:mm:ss') // 开始时间
            } else if (val === 'month') {
              this.params[0].startDate = moment(new Date())
                .subtract(1, 'months')
                .format('YYYY-MM-DD HH:mm:ss')
            } else if (val === '3') {
              this.params[0].startDate = moment(new Date())
                .subtract(3, 'months')
                .format('YYYY-MM-DD HH:mm:ss')
            } else if (val === '6') {
              this.params[0].startDate = moment(new Date())
                .subtract(6, 'months')
                .format('YYYY-MM-DD HH:mm:ss')
            } else if (val === 'years') {
              this.params[0].startDate = moment(new Date())
                .subtract(1, 'years')
                .format('YYYY-MM-DD HH:mm:ss')
            } else {
              this.params[0].startDate = val[0]
              this.params[0].endDate = val[1]
            }
          }
        } else {
          if (type === 'searchText') {
            this.globalVal = val
            this.searchVal = val
          }
          if (Object.keys(this.params[0]).indexOf(type) !== -1) {
            this.params[0][type] = val
          }
        }
        this.getSearchList()
      } else {
        this.$slots.searchtabs[0]?.context?.onSearch()
      }
    },
    // 获取搜索结果
    getSearchList() {
      this.searchList = []
      if (this.params[0].searchText === '请输入搜索内容') this.params[0].searchText = ''
      FullSearchService.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)
          })
        }
        if (this.searchList) {
          this.loading = false
          this.searchList.forEach((item, index) => {
            if (item.status) {
              item.status.forEach((i, key) => {
                if (i[1] === '1') {
                  i[1] = '待处理'
                } else if (i[1] === '2') {
                  i[1] = '已处理'
                } else {
                  i[1] = '已办结'
                }
              })
            }
            const tags = {
              belong: item.docTypeName,
              sender: item.creatorName,
              department: item.createDeptName,
              creationtime: item.creationTime,
              status: item.status,
            }
            item.tags = tags
          })
        }
      })
    },
    // 获取应用搜索结果
    getApplyList(text) {
      FullSearchService.applySearch(text).then((res) => {
        this.applyList = res.data
      })
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.fullbox {
  height: 100%;
  overflow: hidden;
  background: #f0f2f5;
}
.fullsearch {
  width: 100%;
  height: 100%;
  .searchheader {
    width: 100%;
    height: 286px;
    padding: 0;
    // background-image: linear-gradient(90deg, #1890ff, #0162eb);
    background: url('./bj.png') no-repeat center center;
    // background-size: 100%;
  }
}
.searchcontent {
  width: 100%;
  height: calc(100% - 156px);
  .fulltabs {
    height: 100%;
  }
  :global(.ant-tabs-bar) {
    display: flex;
    justify-content: center;
    margin-bottom: 0;
    background: #fff;
  }
  :global(.ant-tabs-content) {
    height: 100%;
    overflow: scroll;
  }
}
.hiddentab {
  :global(.ant-tabs-bar) {
    display: none;
  }
}
</style>
