<template>
  <view class="search-container container">
    <view class="searchTarget-header">
      <input type="text" placeholder="请输入关键字" placeholder-class="sea_ipt_placeholder" class="sea_ipt" v-model="searchTxt" focus="true" confirm-type="search" @confirm="searchHandle" />
      <icon type="search" size="15" class="sea_ico" />
      <view class="ipt-right">
        <icon type="clear" size="16" color="#E0E0E0" v-show="searchTxt" @click="clearIpt" />
        <text class="line">|</text>
        <text @click="searchHandle" style="color: #3385ff">搜索</text>
      </view>
    </view>
    <view class="radio-content">
      <van-radio-group :value="radioSelect" @change="onChangeRadio" direction="horizontal">
        <van-radio icon-size="16" name="1">搜全部</van-radio>
        <van-radio icon-size="16" name="2">搜纪要</van-radio>
        <van-radio icon-size="16" name="3">搜图表</van-radio>
      </van-radio-group>
    </view>
    <view class="tab-cont">
      <scroll-view scroll-x="true" scroll-with-animation class="scroll-tab" :scroll-into-view="'_' + tabIndex">
        <block v-for="item in tabBars" :key="item.mode">
          <view class="scroll-tab-item" :class="{ active: orderColumn === item.mode }" @click.stop="toggleTab(item)">
            {{ item.PermissionName }}
          </view>
        </block>
      </scroll-view>
    </view>
    <view class="search-cont" v-if="!isResult">
      <view class="search-cont-top">
        <view class="cont-tit">
          <text>热搜关键词：</text>
        </view>
        <view class="targetList">
          <view class="target-item" v-for="(item, index) in hotKeyWord" :key="index" @click="chooseTarget(item.KeyWord)"># {{ item.KeyWord }}</view>
        </view>
      </view>
      <view class="search-cont-top">
        <view class="cont-tit">
          <text>弘则推荐：</text>
        </view>
        <view class="targetList">
          <view class="target-item" v-for="(item, index) in keywordList" :key="index" @click="chooseTarget(item)"># {{ item }}</view>
        </view>
      </view>
      <view class="search-cont-top" v-if="historySearchList.length">
        <view class="cont-tit">
          <text>搜索历史：</text>
          <image src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/empty_ico.png" class="empty_ico" @click="clearHistory"></image>
        </view>
        <view class="targetList">
          <block v-for="(item, index) in historySearchList" :key="index">
            <view v-if="index < 8" class="target-item" @click="chooseTarget(item)"># {{ item }}</view>
          </block>
        </view>
      </view>
    </view>
    <!-- 内容地方 -->
    <view class="search-cont" v-else>123 </view>
  </view>
</template>

<script>
import { Search } from "@/config/api.js";
import { Debounce, Throttle } from "@/config/util.js";
let app = getApp({ allowDefault: true });
export default {
  data() {
    return {
      searchTxt: "", //搜索关键字
      isResult: false, //显示搜索结果
      haveResult: true, //是否有搜索数据
      // 历史搜索列表
      historySearchList: [],
      // 关键字列表
      keywordList: [],
      // 搜索结果列表
      resultList: [],
      indList: [],
      page_no: 1,
      pageSize: 10,
      totalPage: 0,
      hotKeyWord: [],
      radioSelect: "",
      tabBars: [
        {
          PermissionName: "匹配度排序",
          mode: "Matching",
        },
        {
          PermissionName: "综合排序",
          mode: "Comprehensive",
        },
        {
          PermissionName: "发布时间排序",
          mode: "PublishDate",
        },
      ],
      orderColumn: "Matching",
    };
  },
  watch: {
    searchTxt(newVal) {
      if (newVal.length <= 0) {
        this.isResult = false;
      }
    },
  },
  methods: {
    //获取热搜关键词的请求
    async researchHotKeyWord() {
      const res = await Research.researchHotKeyWord();
      if (res.Ret === 200) {
        this.hotKeyWord = res.Data.List || [];
      }
    },
    //点击了搜索的变化
    onChangeRadio(value) {
      this.radioSelect = value.detail;
      this.clearIpt();
    },
    /* 获取关键词 */
    getKeyWord() {
      Search.getKeys().then((res) => {
        if (res.Ret === 200) {
          this.keywordList = res.Data.Item.ConfigValue ? res.Data.Item.ConfigValue.split(",") : [];
        }
      });
    },
    // 选择历史搜索
    chooseTarget(item) {
      this.searchTxt = item;
      this.SecName = item;
      this.resultList = [];
      this.indList = [];
      if (!this.historySearchList.includes(this.searchTxt)) {
        this.historySearchList.unshift(this.searchTxt);
        this.$db.set("historySearchList", JSON.stringify(this.historySearchList));
      }
      this.getDataList();
    },
    // 拼接
    join(str, key) {
      return str.replace(new RegExp(`${key}`, "g"), `%%${key}%%`).split("%%");
    },
    // 搜索数据
    searchHandle: Debounce(function () {
      if (this.searchTxt) {
        //添加搜索记录
        if (!this.historySearchList.includes(this.searchTxt)) {
          this.historySearchList.unshift(this.searchTxt);
          this.$db.set("historySearchList", JSON.stringify(this.historySearchList));
        }
        this.resultList = [];
        this.indList = [];
        this.getDataList();
      } else {
        this.$util.toast("请输入关键字");
      }
    }),
    // 查找数据
    async getDataList() {
      this.isResult = true;
    },
    /* 表单清空 */
    clearIpt() {
      this.searchTxt = "";
      this.isTabAct = false;
    },
    /* 历史搜索清空 */
    clearHistory() {
      this.historySearchList = [];
      this.$db.del("historySearchList");
    },
    //tabs切换事件
    toggleTab(item) {
     this.orderColumn = item.mode
    },
  },
  onLoad(options) {
    this.radioSelect = options.id == 31 ? "1" : "2";
    if (options.text) {
      this.searchTxt = options.text;
      this.getDataList();
    }
    // 获取历史搜索记录
    if (this.$db.get("historySearchList")) {
      let historyList = JSON.parse(this.$db.get("historySearchList"));
      this.historySearchList = historyList;
    }
  },
  onShow() {
    this.$store.dispatch("statistics", { PageType: "SummarySearch" });
    this.getKeyWord();
    // this.researchHotKeyWord();
  },
};
</script>

<style lang="scss">
.search-container {
  background-color: #fff;
  padding-bottom: 30rpx;
  .searchTarget-header {
    padding: 0 34rpx;
    width: 100%;
    background-color: #fff;
    position: sticky;
    top: 0;
    left: 0;
    z-index: 99;
    padding: 30rpx 0;
    display: flex;
    justify-content: center;
    align-items: center;
    .sea_ipt_placeholder {
      color: #8d8d8d;
    }
    .sea_ipt {
      width: 682rpx;
      height: 70rpx;
      line-height: 70rpx;
      box-sizing: border-box;
      border: 1rpx solid #e5e5e5;
      background-color: rgba(245, 245, 245, 0.2);
      font-size: 26rpx;
      color: #4a4a4a;
      padding: 0 180rpx 0 78rpx;
      border-radius: 70rpx;
    }
    .sea_ico {
      width: 31rpx;
      height: 31rpx;
      position: absolute;
      left: 68rpx;
      top: 50%;
      transform: translateY(-50%);
    }
    .ipt-right {
      display: flex;
      align-items: center;
      position: absolute;
      right: 59rpx;
      top: 50%;
      transform: translateY(-50%);
      color: #3385ff;
      .line {
        margin: 0 21rpx;
        color: #e0e0e0;
      }
    }
  }
  .radio-content {
    width: 100%;
    padding-left: 58rpx;
    background-color: #fff;
    position: sticky;
    height: 70rpx;
    top: 128rpx;
    left: 0;
    z-index: 99;
    display: flex;
  }
  .search-cont {
    .search-cont-top {
      padding: 0 34rpx 0;
      margin-bottom: 10rpx;
      padding-top: 20rpx;
      &:last-child {
        margin-bottom: 0;
      }
      .cont-tit {
        font-size: 32rpx;
        margin-bottom: 20rpx;
        font-weight: 500;
        display: flex;
        justify-content: space-between;
        .empty_ico {
          width: 32rpx;
          height: 33rpx;
        }
      }
      .targetList {
        display: flex;
        flex-wrap: wrap;
        font-size: 28rpx;
        .target-item {
          width: 50%;
          margin-bottom: 20rpx;
        }
      }
    }
    .result-cont {
      padding: 0 34rpx 0;
      padding-left: 21rpx;
      .result-list {
        display: flex;
        align-items: center;
        color: #333;
        padding-bottom: 30rpx;
        border-bottom: 1rpx solid #ebedf0;
        margin-bottom: 30rpx;
        .result_ico {
          width: 28rpx;
          height: 28rpx;
          margin-right: 20rpx;
        }
        text {
          display: inline;
        }
        .highlight {
          color: #3385ff;
        }
      }
    }
    .result-data {
      // margin-top: 80rpx;
      min-height: calc(100vh - 130rpx);
      padding: 20rpx 34rpx 40rpx;
      display: flex;
      background-color: #f7f7f7;
    }
  }
  .tab-cont {
    position: sticky;
    top: 128rpx;
    width: 100%;
    padding: 0 26rpx;
    background-color: #fff;
    font-size: 32rpx;
    .scroll-tab {
      width: 100%;
      white-space: nowrap;
    }
    .scroll-tab-item {
      text-align: center;
      display: inline-block;
      // padding: 0 8rpx 20rpx 8rpx;
      padding-bottom: 20rpx;
      margin-right: 40rpx;
      border-bottom: 8rpx solid transparent;
      position: relative;
      &:last-child {
        margin-right: 0;
      }
      &.active {
        border-bottom: none;
        color: #2c83ff;
        font-weight: 700;
        border-bottom: 2px solid;
        border-image: linear-gradient(to right, #2e85ff, #7eeaf6) 1;
      }
      .border_act {
        width: 100%;
        height: 8rpx;
        position: absolute;
        bottom: 0;
        left: 0;
      }
    }
  }
}
</style>
