<template>
  <view class="container resear-container">
    <view class="theme">
      <view class="theme-top">
        <text class="title"> 近期更新主题： </text>
        <view class="" @click="goRecent">
          更多
          <u-icon name="arrow-right" color="#BDBDBD" size="28"></u-icon>
        </view>
      </view>
      <view class="theme-content">
        <view class="theme-ul" v-for="item in themeNewList" :key="item.IndustrialManagementId">
          <text class="text_oneLine" @click="themeDetails(item)"> # {{ item.IndustryName }}</text>
          <image v-if="item.IsHot" src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/host_report.png"></image>
        </view>
      </view>
    </view>
    <view class="announcement">
      <view class="tab-cont">
        <scroll-view scroll-x="true" @scroll="scrollMove" :scroll-left="scrollLeft" scroll-with-animation class="scroll-tab" :scroll-into-view="'_' + tabIndex">
          <block v-for="(item, index) in tabBars" :key="index">
            <view class="scroll-tab-item" :class="{ active: tagActive == item.name }" @click.stop="typeChange(item)">
              {{ item.titel }}
              <image src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/border_act.png" mode="" class="border_act" v-if="tagActive == item.name"></image>
            </view>
          </block>
        </scroll-view>
      </view>
      <view class="content-ul" v-if="collectionList.length && tagActive == '用户'">
        <view class="user-li" v-for="(item, index) in collectionList" :key="item.ArticleId">
          <view class="li-item">
            <text class="li-serial" :style="{ background: serialBackground(index) }">
              {{ index + 1 }}
            </text>
            <view>
              <text @click="goDetail(item)" style="display: inline">{{ item.Title }}</text>
              <text class="li-industry" @click="themeDetails(item)"> # {{ item.IndustryName }} </text>
            </view>
          </view>
          <view class="li-item li-bottom" style="color: #999999">
            <view class="li-user text_oneLine" @click="authorDetails(item)">
              <image src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/user_report.png"></image>
              {{ item.NickName }}
            </view>
            <view class="li-item user-img">
              <view class="img-box">
                <image class="user-pv" src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/examine_icon.png"></image>
                <text>
                  {{ item.Pv }}
                </text>
              </view>
              <view class="img-box" @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>
                <text class=""> {{ item.CollectNum }}人收藏 </text>
              </view>
            </view>
          </view>
        </view>
      </view>
      <view class="content-ul" v-if="hotList.length && tagActive == '主题'">
        <view class="hot-li" v-for="(item, index) in hotList" :key="item.IndustrialManagementId">
          <view class="li-item hot-item">
            <view style="display: flex" class="text_oneLine hot-new" @click="themeDetails(item)">
              <text class="li-serial serial-number" :style="{ background: serialBackground(index) }">
                {{ index + 1 }}
              </text>
              <text class="text_oneLine" style="display: inline"> # {{ item.IndustryName }} </text>
              <image class="new-img" v-if="item.IsNew" src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/new_icon.png"></image>
            </view>
            <text :class="item.IsFollw ? 'cancel-attention' : 'attention'" @click="isAttention(item, '主题')">{{ item.IsFollw ? "取消关注" : "+ 关注" }}</text>
          </view>
          <view class="li-item read-more" @click="themeDetails(item)">
            <text class="text-box text_oneLine" v-for="val in item.IndustrialSubjectList" :key="val.IndustrialSubjectId">
              {{ val.SubjectName }}
            </text>
          </view>
        </view>
      </view>
      <view class="content-ul" v-if="kolList.length && tagActive == '专家'">
        <view class="kol-li" v-for="(item, index) in kolList" :key="item.DepartmentId">
          <text class="li-serial serial-number" :style="{ background: serialBackground(index) }">
            {{ index + 1 }}
          </text>
          <image @click="authorDetails(item)" :src="item.ImgUrl" v-if="item.ImgUrl"></image>
          <view class="li-item kol-item">
            <view class="item-top">
              <text @click="authorDetails(item)">
                {{ item.NickName }}
              </text>
              <text :class="item.IsFollw ? 'cancel-attention' : 'attention'" @click="isAttention(item, '专家')">{{ item.IsFollw ? "取消关注" : "+ 关注" }}</text>
            </view>
            <text @click="themeDetails(val)" class="kol-text text_oneLine" v-for="val in item.List" :key="val.IndustrialManagementId"> # {{ val.IndustryName }} </text>
          </view>
        </view>
      </view>
    </view>
    <view class="content-bottom">最新榜单您已掌握，明天再刷刷看~</view>
  </view>
</template>

<script>
import { Reports, Research, Report } from "@/config/api.js";
import { Throttle } from "@/config/util.js";
export default {
  props: {
    strategyIndex: {
      type: Number,
      required: true,
    },
    tabAct: {
      type: Number,
      required: true,
    },
    isNum: {
      type: Number,
      required: true,
    },
    pageNumFather: {
      type: Number,
      default: "",
      required: true,
    },
  },
  data() {
    return {
      themeNewList: [],
      tagActive: "用户",
      collectionList: [], //用户收藏
      hotList: [],
      kolList: [],
      tabBars: [
        { titel: "用户收藏榜", name: "用户" },
        { titel: "主题热度榜", name: "主题" },
        { titel: "KOL榜", name: "专家" },
      ],
    };
  },
  watch: {
    strategyIndex: {
      handler(val) {
        if (val == 5) {
          this.researchNewList();
        }
      },
      immediate: true,
    },
    isNum: {
      handler(val) {
        if (val > 1 && this.strategyIndex) {
          if (this.status === "nomore") return;
          this.status = "loading";
          this.page_no++;
          this.industryListByDepartment();
        }
      },
    },
    pageNumFather: {
      handler(val) {
        if (val == 1) {
          this.page_no = 1;
          (this.dataList = []), (this.listNew = []);
          this.industryListByDepartment();
          this.$parent.pageNumFather = "";
        }
      },
    },
    tagActive: {
      handler(val) {
        val == "用户" ? this.researchCollectionList() : val == "主题" ? this.researchHotList() : this.researchKolList();
      },
      immediate: true,
    },
  },
  methods: {
    //近期更新主题
    async researchNewList() {
      const res = await Research.researchNewList({ ChartPermissionId: this.tabAct });
      if (res.Ret === 200) {
        this.themeNewList = res.Data.List || [];
      }
    },
    //用户收藏榜
    async researchCollectionList() {
      const res = await Research.researchCollectionList({ ChartPermissionId: this.tabAct });
      if (res.Ret === 200) {
        this.collectionList = res.Data.List || [];
      }
    },
    //主题热度/近期更新更多
    async researchHotList() {
      const res = await Research.researchHotList({ ChartPermissionId: this.tabAct, ThemeType: 1 });
      if (res.Ret === 200) {
        this.hotList = res.Data.List || [];
      }
    },
    //KOL榜列表接口
    async researchKolList() {
      const res = await Research.researchKolList({ ChartPermissionId: this.tabAct });
      if (res.Ret === 200) {
        this.kolList = res.Data.List || [];
      }
    },
    //tabs切换
    typeChange(item) {
      this.tagActive = item.name;
    },
    //关注
    async isAttention(item, val) {
      const res = val === "主题" ? await Reports.reportFllow({ IndustrialManagementId: item.IndustrialManagementId }) : await Reports.reportFllowDepartment({ DepartmentId: item.DepartmentId });
      if (res.Ret === 200) {
        item.IsFollw = !item.IsFollw;
        if (res.Data.Status == 1) {
          this.$parent.goFollowShow = true;
          this.$parent.confirmText = res.Data.GoFollow ? "去关注" : "知道了";
          if (res.Data.GoFollow) {
            this.$parent.accounts = `${val == "主题" ? "产业" : "作者"}关注成功 <br> 想要及时获取该${val === "主题" ? "产业内容的更新推送" : "作者的报告更新提示"}，请关注【查研观向小助手】公众号`;
            this.$parent.isCancelBtn = true;
          } else {
            this.$parent.accounts = `${val == "主题" ? "产业" : "作者"}` + `关注成功<br>请关注【查研观向小助手】公众号，及时获取${val === "主题" ? "产业" : "作者"}报告更新提醒`;
          }
        } else {
          uni.showToast({
            title: "已取消关注",
            icon: "none",
            duration: 2000,
          });
        }
      }
    },
    //去往更多主题
    goRecent() {
      uni.navigateTo({
        url: "/reportPages/recentPages/recentPages?id=" + this.tabAct,
      });
    },
    //收藏
    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,
          });
      }
    },
    //去往作者详情
    authorDetails(item) {
      uni.navigateTo({
        url: "/reportPages/authorPages/authorPages?id=" + item.DepartmentId,
      });
    },
    //去往文章详情页面
    goDetail(item) {
      this.$store.dispatch("checkHandle", "/pageMy/reportDetail/reportDetail?id=" + item.ArticleId);
    },
    //去往主题详情
    themeDetails(item) {
      this.$store.dispatch("checkHandle", "/reportPages/researchTheme/researchTheme?id=" + item.IndustrialManagementId);
    },
    //计算遍历的颜色
    serialBackground(index) {
      index += 1;
      return index == 1 ? "#D7584F" : index == 2 ? "#E98033" : index == 3 ? "#FDD367" : "#D3D3D3";
    },
  },
};
</script>

<style lang="scss">
.resear-container {
  padding: 220rpx 0 0;
  background-color: #f7f7f7;
  .theme {
    background-color: #fff;
    padding: 0 30rpx 15rpx;
    margin-bottom: 8rpx;
    .theme-top {
      display: flex;
      color: #999999;
      align-items: center;
      font-size: 28rpx;
      height: 95rpx;
      justify-content: space-between;
      .title {
        color: #333;
        font-size: 32rpx;
        font-weight: 500;
      }
    }
    .theme-content {
      display: flex;
      flex-wrap: wrap;
      .theme-ul {
        width: 50%;
        display: flex;
        align-items: center;
        padding-right: 15rpx;
        line-height: 59rpx;
        image {
          width: 26rpx;
          height: 28rpx;
          margin-left: 8rpx;
          flex-shrink: 0;
        }
      }
    }
  }

  .content-ul {
    margin: 20rpx 0 0;
    border-top: 1px solid #f6f6f6;
    .user-li,
    .hot-li,
    .kol-li {
      padding: 30rpx 0 20rpx;
      border-bottom: 1px solid #f6f6f6;
    }
    .user-img {
      width: 40%;
      justify-content: space-between;
      .img-box {
        display: flex;
        align-items: center;
      }
      .user-pv {
        width: 21rpx;
        height: 16rpx;
      }
    }
    .kol-li {
      display: flex;
      align-items: center;
      padding-bottom: 30rpx;
      image {
        width: 120rpx;
        height: 120rpx;
        border-radius: 8rpx;
        margin: 0 20rpx 0 15rpx;
        overflow: hidden;
        flex-shrink: 0;
      }
    }
    .kol-item {
      display: flex;
      flex-wrap: wrap;
      width: 480rpx;
      height: 120rpx;
      align-items: baseline;
      margin-bottom: 20rpx;
      .item-top {
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: space-between;
        font-size: 29rpx;
        font-weight: 700;
      }
      .kol-text {
        width: 50%;
        margin-top: 10rpx;
        flex-shrink: 0;
      }
    }

    .li-serial {
      width: 26rpx;
      height: 26rpx;
      line-height: 26rpx;
      color: #fff;
      font-size: 20rpx;
      text-align: center;
      border-radius: 4rpx 4rpx 4rpx 4rpx;
      margin: 7rpx 10rpx 0 20rpx;
      flex-shrink: 0;
    }
    .serial-number {
      margin-top: 0 !important;
    }
    .li-item {
      display: flex;
      .li-industry {
        color: #3385ff;
        margin-left: 20rpx;
        display: inline-block;
      }
      .li-user {
        margin-left: 46rpx;
      }
      .li-title {
        display: flex;
      }
    }
    .li-bottom {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-top: 30rpx;
      width: 100%;
      image {
        width: 23rpx;
        height: 26rpx;
        margin-right: 10rpx;
      }
    }
    .read-more {
      margin-left: 30rpx;
      margin-top: 20rpx;
      display: flex;
      flex-wrap: wrap;
      .text-box {
        margin-bottom: 27rpx;
        margin-right: 40rpx;
        padding: 0;
        font-size: 24rpx;
        color: #408fff;
        width: 170rpx;
        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;
      }
    }
    .attention {
      flex-shrink: 0;
      padding: 5rpx 28rpx;
      border-radius: 37rpx 37rpx 37rpx 37rpx;
      color: #fff;
      background: #3385ff;
      font-weight: 400;
      font-size: 24rpx;
    }
    .hot-item {
      justify-content: space-between;
      align-items: center;
      .hot-new {
        align-items: center;
        flex: 1;
        padding-right: 20rpx;
      }
      .new-img {
        width: 26rpx;
        height: 28rpx;
        margin-left: 15rpx;
        flex-shrink: 0;
      }
    }
  }
  .announcement {
    background: #fff;
    padding: 0 30rpx;
    .tab-cont {
      height: 115rpx;
      background-color: #fff;
      font-size: 32rpx;
      .scroll-tab {
        position: relative;
        width: 100%;
        white-space: nowrap;
        display: flex;
        justify-content: space-around;
      }
      .scroll-tab-item {
        text-align: center;
        display: inline-block;
        padding: 32rpx 8rpx 30rpx 8rpx;
        border-bottom: 8rpx solid transparent;
        margin-right: 60rpx;
        position: relative;
        color: #999999;
        font-size: 32rpx;
        &:last-child {
          margin-right: 0;
        }
        &.active {
          border-bottom: none;
          color: #333;
          font-weight: 500;
        }
        .border_act {
          width: 80%;
          height: 8rpx;
          position: absolute;
          bottom: 0;
          left: 50%;
          transform: translateX(-50%);
        }
      }
    }
  }
  .content-bottom {
    background-color: #fff;
    text-align: center;
    line-height: 100rpx;
    color: #c4c4c4;
    font-size: 20rpx;
  }
}
</style>
