<template>
  <view class="searchTarget-container container">
    <view class="searchTarget-header">
      <input
        type="text"
        :placeholder="radioSelect == 1 ? '请输入报告标题/报告内容' : '请输入产业名/公司名'"
        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-group>
    </view>
    <view class="search-cont">
      <template 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>
      </template>
      <template v-else>
        <block v-if="haveResult">
          <view class="content-list" v-if="radioSelect == '2'">
            <block v-if="ListHz.length">
              <view class="resource">
                <image src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/zybico.jpg"></image>
                <text>弘则资源包</text>
              </view>
              <text style="height: 1rpx; background: #ececec"></text>
              <view class="list-ui" v-for="item in ListHz" :key="item.IndustrialManagementId" @click="themeDetails(item)">
                <view class="item-title">
                  <text>
                    {{ item.IndustryName }}
                  </text>
                  <view class="publish">
                    {{ item.PublishDate }}更新
                    <u-icon name="arrow-right" color="#BDBDBD" size="34"></u-icon>
                  </view>
                </view>
                <view class="read-more" v-if="item.IndustrialSubjectList">
                  <text class="text-box text_oneLine" v-for="val in item.IndustrialSubjectList" :key="val.IndustrialSubjectId">
                    {{ val.SubjectName }}
                  </text>
                </view>
              </view>
            </block>
            <block v-if="ListYx.length">
              <view class="resource">
                <image src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/zybico.jpg"></image>
                <text>买方研选主题</text>
              </view>
              <text style="height: 1rpx; background: #ececec"></text>
              <view class="list-ui" v-for="item in ListYx" :key="item.IndustrialManagementId" @click="themeDetails(item)">
                <view class="item-title">
                  <text>
                    {{ item.IndustryName }}
                  </text>
                  <view class="publish">
                    {{ item.PublishDate }}更新
                    <u-icon name="arrow-right" color="#BDBDBD" size="34"></u-icon>
                  </view>
                </view>
                <view class="read-more" @click="themeDetails(item)" v-if="item.IndustrialSubjectList">
                  <text class="text-box text_oneLine" v-for="val in item.IndustrialSubjectList" :key="val.IndustrialSubjectId">
                    {{ val.SubjectName }}
                  </text>
                </view>
              </view>
            </block>
          </view>
          <view class="content-report" v-if="radioSelect == '1'">
            <block v-if="ListYx.length">
              <view class="resource">
                <image src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/report_Yx.png"></image>
                <text>买方研选报告</text>
              </view>
              <text style="height: 1rpx; background: #ececec"></text>
              <view class="content-item" v-for="item in ListYx" :key="item.ArticleId">
                <view class="item-user">
                  <image src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/user_report.png"></image>
                  <text> {{ item.NickName }}</text>
                </view>
                <view class="item-title">
                  <text style="display: inline" @click="goDetailReport(item)">
                    {{ item.Title }}
                  </text>
                  <text class="item-industry" v-if="item.IndustryName" @click="themeDetails(item)">&nbsp;&nbsp;&nbsp;#{{ item.IndustryName }}</text>
                </view>
                <view class="item-more">
                  <text>{{ item.PublishDate }}</text>
                  <view class="pv-ollect">
                    <view>
                      <image class="pv" src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/examine_icon.png"></image>
                      {{ item.Pv }}
                    </view>
                    <view @click="collectClick(item)">
                      <image v-if="item.IsCollect" src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/collect_act.png"></image>
                      <image v-else src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/collect_ico.png"></image>
                      {{ item.CollectNum }}人收藏
                    </view>
                  </view>
                </view>
              </view>
            </block>
            <block v-if="ListHz.length">
              <text style="height: 10rpx; background: #ececec"></text>
              <view class="resource">
                <image src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/report_Hz.png"></image>
                <text>弘则报告</text>
              </view>
              <text style="height: 1rpx; background: #ececec"></text>
              <view class="content-item" v-for="item in ListHz" :key="item.ArticleId">
                <view class="item-title">
                  <text style="display: inline" @click="goDetailReport(item)">
                    {{ item.Title }}
                  </text>
                  <text class="item-industry" @click="themeDetails(item)" v-if="item.IndustryName">&nbsp;&nbsp;&nbsp;#{{ item.IndustryName }}</text>
                </view>
                <view class="item-more">
                  <text>{{ item.PublishDate }}</text>
                </view>
              </view>
            </block>
          </view>
        </block>
        <view class="nodata" v-else>
          <image src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/act_search.png" mode="" class="nodata_img"></image>
          <text>未找到您想搜索的内容</text>
          <text> 正式客户可联系销售免费发起新调研主题 </text>
        </view>
      </template>
    </view>
    <u-modal
      v-model="goFollowShow"
      :content-style="{ fontSize: '32rpx' }"
      @confirm="goFollowShowBtn"
      :show-cancel-button="isCancelBtn"
      :confirm-text="confirmText"
      @cancel="isCancelBtn = false"
      :show-title="false"
      :cancel-style="{ borderRight: '1rpx solid #EBEBEB' }"
      :confirm-style="{ fontWeight: '700' }"
    >
      <view class="slot-content">
        <rich-text :nodes="accounts"></rich-text>
      </view>
    </u-modal>
    <freeCharge class="free-charge" :isShowFreeBtn="isShowFree" />
  </view>
</template>

<script>
import { Search, Reports, Research, Report } from "@/config/api.js";
import { Debounce, Throttle } from "@/config/util.js";
import freeCharge from "@/components/freeCharge";
let app = getApp({ allowDefault: true });
export default {
  data() {
    return {
      searchTxt: "", //搜索关键字
      isResult: false, //显示搜索结果
      haveResult: true, //是否有搜索数据
      // 历史搜索列表
      historySearchList: [],
      // 关键字列表
      keywordList: [],
      targetList: [], //所有指标列表
      // 搜索结果列表
      resultList: [],
      indList: [],
      hotKeyWord: [],
      radioSelect: "",
      ListHz: [],
      ListYx: [],
      goFollowShow: false,
      confirmText: "知道了",
      isCancelBtn: false,
      accounts: "",
    };
  },
  watch: {
    searchTxt(newVal) {
      if (newVal.length <= 0) {
        this.isResult = false;
      }
    },
  },
  components: {
    freeCharge,
  },
  methods: {
    async researchHotKeyWord() {
      const res = await Research.researchHotKeyWord();
      if (res.Ret === 200) {
        this.hotKeyWord = res.Data.List || [];
      }
    },
    onChangeRadio(value) {
      this.radioSelect = value.detail;
      let srt = this.searchTxt.replace(/^\s+|\s+$/g, "");
      if (!srt) return this.$util.toast(this.radioSelect == 1 ? "请输入报告标题/报告内容" : "请输入产业名/公司名");
      this.dataInit();
      this.getDataList();
    },
    /* 获取关键词 */
    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("historySearchListReport", JSON.stringify(this.historySearchList));
      }
      this.getDataList();
    },
    // 键盘输入过程中
    searchDoing() {
      this.isResult = false;
      //全部指标列表
      let arr = JSON.parse(JSON.stringify(this.targetList));
      let filterArr = [];
      // // 过滤出符合搜索条件的值
      arr.forEach((item, index) => {
        if (item.SecName.includes(this.searchTxt)) {
          item.SecName = this.join(item.SecName, this.searchTxt);
          filterArr.unshift(item);
        }
      });
      this.keywordList = filterArr;
    },
    // 拼接
    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("historySearchListReport", JSON.stringify(this.historySearchList));
        }
        this.resultList = [];
        this.indList = [];
        this.getDataList();
      } else {
        this.$util.toast(this.radioSelect == 1 ? "请输入报告标题/报告内容" : "请输入产业名/公司名");
      }
    }),
    // 查找数据
    async getDataList() {
      this.isResult = true;
      const res =
        this.radioSelect == 2
          ? await Reports.getIndustryAndArticle({
              KeyWord: this.searchTxt.replace(/^\s+|\s+$/g, ""),
            })
          : await Reports.getReportSearchReport({
              KeyWord: this.searchTxt.replace(/^\s+|\s+$/g, ""),
            });
      if (res.Ret === 200) {
        this.ListHz = res.Data.ListHz || [];
        this.ListYx = res.Data.ListYx || [];
        this.haveResult = this.ListHz.length > 0 || this.ListYx.length > 0;
      }
    },
    // 点击数据列表修改数据
    itemClick(item) {
      let data = encodeURIComponent(JSON.stringify(item));
      uni.navigateTo({
        url: "/pages/recordData/recordData?data=" + data,
      });
    },
    /* 表单清空 */
    clearIpt() {
      this.searchTxt = "";
      this.isTabAct = false;
    },
    /* 历史搜索清空 */
    clearHistory() {
      this.historySearchList = [];
      this.$db.del("historySearchListReport");
    },
    //去往文章详情页面
    goDetailReport(item) {
      this.$store.dispatch("checkHandle", "/pageMy/reportDetail/reportDetail?id=" + item.ArticleId);
    },
    /* 进入详情 校验是否有该品种权限 */
    goDetail(item) {
      uni.navigateTo({
        url: "/reportPages/IndustryReport/IndustryReport?id=" + item.IndustrialManagementId,
      });
    },
    goDetailIndust(id) {
      this.$store.dispatch("checkHandle", "/pageMy/reportDetail/reportDetail?id=" + id);
    },
    //去往主题详情
    themeDetails(item) {
      if (item.Source === 1) {
        //非严选
        this.$store.dispatch("checkHandle", "/reportPages/IndustryReport/IndustryReport?id=" + item.IndustrialManagementId);
      } else {
        //严选
        this.$store.dispatch("checkHandle", "/reportPages/researchTheme/researchTheme?id=" + item.IndustrialManagementId);
      }
    },
    //收藏
    async collectClick(item) {
      const res = await Report.collectRpt({ ArticleId: item.ArticleId });
      if (res.Ret === 200) {
        item.IsCollect = !item.IsCollect;
        item.IsCollect
          ? (item.CollectNum += 1) &&
            uni.showToast({
              title: "收藏成功",
              icon: "none",
              duration: 2000,
            })
          : (item.CollectNum -= 1);
        !item.IsCollect &&
          uni.showToast({
            title: "已取消收藏",
            icon: "none",
            duration: 2000,
          });
      }
    },
    //获取数据的初始值
    dataInit() {
      this.ListHz = [];
      this.ListYx = [];
      uni.pageScrollTo({
        scrollTop: 0,
        duration: 0,
      });
    },
    //点击了去关注
    goFollowShowBtn() {
      if (this.confirmText == "去关注") {
        uni.navigateTo({
          url: "/activityPages/accountsOfficial/accountsOfficial",
        });
      }
      this.goFollowShow = false;
    },
  },
  onLoad(options) {
    this.radioSelect = options.id == "研选" ? "1" : "2";
    if (options.text) {
      this.searchTxt = options.text;
      this.getDataList();
    }
    // 获取历史搜索记录
    if (this.$db.get("historySearchListReport")) {
      let historyList = JSON.parse(this.$db.get("historySearchListReport"));
      this.historySearchList = historyList;
    }
  },
  onShow() {
    this.$store.dispatch("statistics", { PageType: "ReportSearch" });
    this.getKeyWord();
    this.researchHotKeyWord();
  },
  /**
   * 用户点击分享
   */
  onShareAppMessage: function (res) {
    return {
      title: this.isHorzMobile ? "好友向您推荐此内容，上传名片享查研观向免费月卡！" : "报告",
      path: "/reportPages/reportSearch/reportSearch?text=" + this.searchTxt,
      success: (res) => {},
      fail: (err) => {},
    };
  },
};
</script>

<style lang="scss">
.searchTarget-container {
  background-color: #f7f7f7;
  padding-bottom: 30rpx;
  .searchTarget-header {
    padding: 0 34rpx;
    width: 100%;
    background-color: #fff;
    position: fixed;
    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: fixed;
    height: 70rpx;
    top: 128rpx;
    left: 0;
    z-index: 99;
    display: flex;
  }
  .resource {
    padding: 0 30rpx;
    background-color: #fff;
    margin-top: 4rpx;
    height: 91rpx;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #333333;
    font-size: 30rpx;
    image {
      width: 34rpx;
      height: 36rpx;
      margin-right: 10rpx;
    }
  }
  .content-list {
    .list-ui {
      background-color: #fff;
      margin-top: 2rpx;
      margin-bottom: 10rpx;
    }
    .item-title {
      display: flex;
      align-items: center;
      justify-content: space-between;
      height: 100rpx;
      border-bottom: 1px solid #f6f6f6;
      padding: 0 30rpx;
      background-color: #fff;
      .publish {
        display: flex;
        align-items: center;
        color: #999999;
        font-style: 26rpx;
      }
    }
    .read-more {
      margin-top: 20rpx;
      display: flex;
      flex-wrap: wrap;
      margin-left: 30rpx;
      .text-box {
        margin-bottom: 27rpx;
        margin-right: 15rpx;
        padding: 0;
        font-size: 24rpx;
        color: #408fff;
        width: 216rpx;
        height: 46rpx;
        line-height: 46rpx;
        text-align: center !important;
        background: url(~@/static/img/report_bg.png) no-repeat;
        background-size: 100% 100%;
        text-indent: 0em;
      }
    }
  }
  .search-cont {
    padding-top: 180rpx;
    .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;
    }
  }
  .content-report {
    .content-item {
      background-color: #fff;
      padding: 35rpx 20rpx 0;
      .item-title {
        font-weight: 500;
        .item-industry {
          margin-left: 10rpx;
          color: #3385ff;
          display: inline-block;
        }
      }
      .item-user {
        display: flex;
        align-items: center;
        color: #999999;
        font-size: 28rpx;
        margin-bottom: 20rpx;
        image {
          width: 23rpx;
          height: 26rpx;
          margin-right: 20rpx;
        }
      }
      .item-more {
        display: flex;
        justify-content: space-between;
        color: #cecece;
        margin: 20rpx 0 0;
        padding-bottom: 30rpx;
        border-bottom: 1rpx solid #ececec;
        .pv-ollect {
          display: flex;
          align-items: center;
          width: 40%;
          justify-content: space-between;
          image {
            width: 22rpx;
            height: 21rpx;
            margin-right: 10rpx;
          }
          .pv {
            height: 16rpx;
          }
        }
      }
    }
  }
}
</style>
