<template>
  <div class="search-box">
    <div class="search">
      <input
        class="weui-input"
        placeholder="输入内容关键词"
        v-model="keywork"
        confirm-type="search"
        @focus='focus'
        @confirm='search'/>
      <div v-if="keywork" @click="clearText" class="clear"></div>
    </div>
    <div v-if="!isSearch">
      <!-- 热门搜索 -->
      <div class="log-box" v-if="hotLog.length">
        <div class="title-box clearfix">
          <div class="title fl">热门搜索</div>
        </div>
        <div class="log-list">
          <div class="log" v-for="(log, key) in hotLog" :key="key" @click="logSearch(log)">{{log}}</div>
        </div>
      </div>

      <!-- 搜索历史 -->
      <div class="log-box">
        <div class="title-box clearfix">
          <div v-if="localLog.length > 0" class="del fr" @click="delLocalLog">清空</div>
          <div class="title fl">搜索历史</div>
        </div>
        <div v-if="localLog.length > 0" class="log-list">
          <div class="log" v-for="(log, key) in localLog" :key="key" @click="logSearch(log)">{{log}}</div>
        </div>
        <div v-else class="log-none">
          <img class="img" src="../../assets/images/none_log_img.png">
          <div class="tips">暂无搜索历史</div>
        </div>
      </div>
    </div>
    <!-- 搜索结果 -->
    <scroll-view
      v-if="isSearch"
      scroll-y="true"
      @scrolltolower="getData"
      class="scroll-box">
      <div v-if="doctor.length" class="result-box">
        <div class="title">相关医生</div>
        <doctorList
          v-for="(doctorInfo, key) in doctor"
          :key="key"
          isHideWorks="true"
          :doctorInfo="doctorInfo"></doctorList>
      </div>

      <div v-if="article.length" class="result-box">
        <div class="title">关联内容</div>
        <div
          v-for="(item, key) in article"
          :key="key">
          <article-item
            v-if="item.contentType == 'text'"
            :articleInfo="item"></article-item>
          <media-item
            v-else
            isHideInfo="true"
            :mediaInfo="item"></media-item>
        </div>
      </div>

      <div v-if="doctor.length == 0 && article.length == 0" class="log-none search-none">
        <img class="img" src="../../assets/images/none_log_img.png">
        <div class="tips">暂无搜索结果</div>
      </div>
    </scroll-view>
  </div>
</template>

<script>
import { getHotLog, search } from '@/utils/api'

import doctorList from '@/components/doctorList'
import mediaItem from '@/components/mediaItem'
import articleItem from '@/components/articleItem'
export default {
  components: {
    doctorList,
    mediaItem,
    articleItem
  },
  data () {
    return {
      keywork: '',
      hotLog: [],
      localLog: [],
      doctor: [],
      article: [],
      isSearch: false,
      page: 1,
      limit: 10,
      nomore: false
    }
  },

  mounted () {
    this.getHotLog()
    this.getLocalLog()
  },

  methods: {
    // 历史搜索
    logSearch (log) {
      this.keywork = log
      this.search()
    },

    // 搜索
    search () {
      this.nomore = false
      this.page = 1
      this.getData()
      wx.hideLoading()

      /* add by 梁展鹏 20200630 故意延迟执行把搜索词塞到缓存内，优化页面体验 */
      let _that = this
      setTimeout(() => {
        _that.addLocalLog()
      }, 250)
    },

    async getData () {
      console.log(1)

      if (this.nomore) {
        return false
      }

      wx.showLoading({
        title: '加载中'
      })

      let res = await search(this.keywork, this.page, this.limit)

      if (this.doctor.length === 0) {
        this.doctor = res.doctorList
      }
      this.article = this.article.concat(res.articleList)

      if (res.articleList.length === 0) {
        this.nomore = true
      } else {
        this.page++
      }
      this.isSearch = true
      wx.hideLoading()
    },

    // 文本框获取焦点回到搜索历史
    focus () {
      this.isSearch = false
      this.doctor = []
      this.article = []
    },

    // 清空文本
    clearText () {
      this.keywork = ''
    },

    // 获取热门搜索记录
    async getHotLog () {
      this.hotLog = (await getHotLog()).sort((a, b) => {
        return a.sort - b.sort
      }).map(item => {
        return item.word
      })
    },

    // 获取本地搜索记录
    getLocalLog () {
      let log = wx.getStorageSync('searchLog')
      if (log) {
        this.localLog = log.split(',')
      }
    },

    // 添加本地搜索记录
    addLocalLog () {
      let log = this.keywork.replace(/(^\s*)|(\s*$)/g, '')
      let index = this.localLog.indexOf(log)
      if (log) {
        if (index > -1) {
          this.localLog.splice(index, 1)
        }
        this.localLog.unshift(log)
        wx.setStorageSync('searchLog', this.localLog.join(','))
      }
    },

    // 删除本地搜索记录
    delLocalLog () {
      this.localLog = []
      wx.removeStorageSync('searchLog')
    }
  }
}
</script>

<style scoped lang="less">
@import '~@/assets/less/common.less';
.search-box{
  .search{
    position: relative;
    margin: @spacing;
    input{
      padding: 5px 5px 5px 30px;
      height: 10px;
      line-height: 10px;
      border-radius: 15px;
      background: #fff;
      background: url('../../assets/images/icon_search.png') no-repeat 10px center #F5F5F5;
      background-size: auto 14px;
    }
    .clear{
      z-index: 99;
      position: absolute;
      top: 50%;
      right: 10px;
      padding: 0 0 0 10px;
      width: 20px;
      height: 20px;
      transform: translate(0, -50%);
      background: url('../../assets/images/icon_clear.png') no-repeat center center #F5F5F5;
      background-size: auto 16px;
    }
  }
  .log-box{
    margin: 0 0 5px;
    .title-box{
      margin: 0 @spacing @spacing;
      height: 20px;
      line-height: 20px;
      font-size: 15px;
      font-weight: 500;
      color: #1A2033;
      .del{
        padding-left: 20px;
        font-size: 11px;
        background: url('../../assets/images/icon_delete.png') no-repeat center left;
        background-size: auto 15px;
        color: #999999;
      }
    }
    .log-list{
      display: flex;
      flex-wrap: wrap;
      justify-content: flex-start;
      margin: 0 @spacing/2;
      .log{
        margin: 0 @spacing/2 @spacing;
        padding: 4px 10px;
        line-height: 18px;
        border-radius: 14px;
        font-size: 13px;
        background: #F5F5F5;
        color: #1A2033;
      }
    }
  }
  .log-none{
    position: absolute;
    bottom: 23%;
    width: 100%;
    .img{
      margin: 0 auto @spacing;
      width: 114px;
      height: 114px;
    }
    .tips{
      line-height: 20px;
      text-align: center;
      color: #CCCCCC;
    }
    &.search-none{
      top: 34%;
      bottom: auto;
    }
  }
  .scroll-box{
    .scroll-box();
    top: 60px;
    .result-box{
      padding-bottom: 5px;
      .title{
        margin: 0 @spacing @spacing;
        font-size: 18px;
        font-weight: 500;
        color: #000000;
      }
    }
  }
}
</style>
