<template>
  <div class="container-htgj-search" :class="isResult ? 'bg-content' : ''">
    <div :class="isResult ? 'top-box' : ''">
      <topbar class="top-box-bar" showText="搜索" />
      <Search v-model="searchTxt" shape="round" clearable placeholder="请输入关键词" @clear="clearIpt" @search="searchHandle">
        <template #right-icon>
          <div class="search-p" @click="searchHandle"><span></span>搜索</div>
        </template>
      </Search>
      <template v-if="isResult">
        <Tabs v-model:active="orderColumn" title-active-color="#2C83FF" title-inactive-color="#707070" @click-tab="onClickTabs" line-width="85">
          <Tab v-for="item in tabBars" :key="item.ChartPermissionId" :name="item.ChartPermissionId" :title="item.PermissionName"></Tab>
        </Tabs>
      </template>
    </div>
    <div class="search-cont">
      <template v-if="!isResult">
        <div class="search-cont-top" v-if="historySearchList.length">
          <div class="cont-tit">
            <p>搜索历史</p>
            <img src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/empty_ico.png" class="empty_ico" @click="clearHistory" />
          </div>
          <div class="targetList">
            <div class="target-item" v-for="(item, index) in historySearchList" :key="index" @click="chooseTarget(item)">{{ item }}</div>
          </div>
        </div>
        <div class="search-cont-top">
          <div class="cont-tit">
            <p>弘则推荐</p>
          </div>
          <div class="targetList">
            <div class="target-item" v-for="(item, index) in keywordList" :key="index" @click="chooseTarget(item)">{{ item }}</div>
          </div>
        </div>
      </template>
      <template v-else>
        <PullRefresh v-model="refreshing" @refresh="onRefresh" v-if="haveResult">
          <div class="data-cont">
            <div class="report-ul">
              <template v-for="(report, index) in resultList" :key="index">
                <div class="report-item" v-if="index % 2 === 0" @click="goDetail(report)">
                  <div class="item-title">
                    <div class="text-Line" v-html="report.Title"></div>
                  </div>
                  <div class="item-content-img" v-if="report.BodyHtml">
                    <img :src="report.BodyHtml" mode="" />
                  </div>
                  <div class="item-content" v-else>
                    <p v-html="report.Body[0]"></p>
                  </div>
                  <div class="item-createtime">
                    <span>{{ report.PublishDate }}</span>
                    <div class="item-examine" v-if="report.IsResearch">
                      <img src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/examine_icon.png" />
                      <span>{{ report.Pv }}</span>
                    </div>
                  </div>
                </div>
              </template>
            </div>
            <div class="report-ul">
              <template v-for="(report, index) in resultList" :key="index">
                <div class="report-item" v-if="index % 2 != 0" @click="goDetail(report)">
                  <div class="item-title">
                    <div class="text-Line" v-html="report.Title"></div>
                  </div>
                  <div class="item-content-img" v-if="report.BodyHtml">
                    <img :src="report.BodyHtml" mode="" />
                  </div>
                  <div class="item-content" v-else>
                    <p v-html="report.Body[0]"></p>
                  </div>
                  <div class="item-createtime">
                    <span>{{ report.PublishDate }}</span>
                    <div class="item-examine" v-if="report.IsResearch">
                      <img src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/examine_icon.png" />
                      <span>{{ report.Pv }}</span>
                    </div>
                  </div>
                </div>
              </template>
            </div>
          </div>
          <div class="finished-text" v-if="resultList.length">没有更多了</div>
        </PullRefresh>
        <div class="nodata" v-else>
          <img src="@/assets/cygx/noauth.png" mode="" class="nodata_ico" />
          <p>未找到您想搜索的内容</p>
        </div>
      </template>
    </div>
  </div>
</template>

<script setup>
import { reactive, onMounted, toRefs, ref, watch } from "vue";
import { Search, Toast, Tab, Tabs, PullRefresh } from "vant";
import { report } from "@/api/htgj/api.js";
import { useRoute, useRouter } from "vue-router";
import topbar from "./topbar.vue";
const router = useRouter();
const searchState = reactive({
  searchTxt: "", //搜索关键字
  isResult: false, //显示搜索结果
  haveResult: true, //是否有搜索数据
  // 历史搜索列表
  historySearchList: [],
  // 关键字列表
  keywordList: [],
  targetList: [], //所有指标列表
});
/* 获取关键词 */
const getKeyWord = async () => {
  const res = await report.getKeys({});
  if (res.Ret === 200) {
    searchState.keywordList = res.Data.Item.ConfigValue ? res.Data.Item.ConfigValue.split(",") : [];
  }
};
// 选择历史搜索 和推荐
const chooseTarget = (item) => {
  searchState.searchTxt = item;
  getDataList();
};

const searchHandle = () => {
  if (searchState.searchTxt) {
    //添加搜索记录
    if (!searchState.historySearchList.includes(searchState.searchTxt)) {
      searchState.historySearchList.unshift(searchState.searchTxt);
      localStorage.setItem("historySearchList", JSON.stringify(searchState.historySearchList));
      dataState.resultList = [];
      getDataList();
    }
  } else {
    Toast("请输入关键字");
  }
};
/* 表单清空 */
const clearIpt = () => {
  searchState.searchTxt = "";
  dataState.resultList = [];
  dataState.orderColumn = "Matching";
  console.log(searchState.searchTxt);
};
/* 历史搜索清空 */
const clearHistory = () => {
  searchState.historySearchList = [];
  localStorage.removeItem("historySearchList");
};

getKeyWord();
const dataState = reactive({
  resultList: [],
  orderColumn: "Matching",
  refreshing: false,
  tabBars: [
    {
      PermissionName: "匹配度排序",
      ChartPermissionId: "Matching",
    },
    {
      PermissionName: "综合排序",
      ChartPermissionId: "Comprehensive",
    },
    {
      PermissionName: "发布时间排序",
      ChartPermissionId: "PublishDate",
    },
  ],
});

// 查找数据
const getDataList = async () => {
  searchState.isResult = true;
  const res = await report.getResult({
    KeyWord: searchState.searchTxt,
    OrderColumn: dataState.orderColumn,
  });
  if (res.Ret === 200) {
    dataState.refreshing = false;
    dataState.resultList = res.Data.List || [];
    searchState.haveResult = dataState.resultList.length ? true : false;
  }
};

/* 下拉刷新 */
const onRefresh = async () => {
  dataState.resultList = [];
  getDataList();
};

/* tab点击 事件 */
const onClickTabs = () => {
  dataState.resultList = [];
  getDataList();
};

/* 去往详情 */
const goDetail = (item) => {
  if (item.IsNeedJump) {
    router.push({
      path: "/htgj/strategyDetail",
      query: {
        id: item.ArticleId,
      },
    });
  } else {
    router.push({
      path: "/htgj/detail",
      query: {
        id: item.ArticleId,
      },
    });
  }
};

watch(
  () => searchState.searchTxt,
  (newValue) => {
    if (newValue.length <= 0) {
      searchState.isResult = false;
    }
  }
);
onMounted(() => {
  // 获取历史搜索记录
  if (localStorage.getItem("historySearchList")) {
    let historyList = JSON.parse(localStorage.getItem("historySearchList"));
    searchState.historySearchList = historyList;
  }
});
const { searchTxt, isResult, haveResult, historySearchList, keywordList, targetList } = toRefs(searchState);
const { resultList, tabBars, orderColumn, refreshing } = toRefs(dataState);
</script>

<style lang="scss">
.container-htgj-search {
  .top-box-bar {
    margin: 15px 0 0 0;
  }
  @import "./index.scss";
  .search-p {
    display: flex;
    align-items: center;
    color: #3385ff;
    span {
      width: 0px;
      height: 17px;
      border: 1px solid #e0e0e0;
      opacity: 1;
      margin-right: 10px;
    }
  }
  .search-cont-top {
    padding: 0 34px 0;
    margin-bottom: 10px;
    padding-top: 20px;
    &:last-child {
      margin-bottom: 0;
    }
    .cont-tit {
      color: #666;
      font-size: 28px;
      margin-bottom: 30px;
      display: flex;
      justify-content: space-between;
      .empty_ico {
        width: 32px;
        height: 33px;
      }
    }
    .targetList {
      display: flex;
      flex-wrap: wrap;
      // justify-content: space-between;
      .target-item {
        padding: 4px 18px;
        color: #4a4a4a;
        font-size: 26px;
        // border: 1px solid #3385ff;
        background-color: #f7f7f7;
        margin-bottom: 30px;
        margin-right: 30px;
        border-radius: 20px;
      }
    }
  }
  .finished-text {
    width: 100%;
    text-align: center;
    padding: 30px;
    color: #666;
  }
  .top-box {
    padding: 0 0 10px !important;
  }
}
.bg-content {
  background: #f7f7f7 !important;
}
</style>
