<template>
  <view class="container research-list-container">
    <view class="top-content">
      <view class="global_one_tabs">
        <view v-for="(item, index) in researchTabBars" :key="index" :class="['item', tagActive == item.name && 'tabs-active']" @click="typeChange(item)">
          {{ item.titel }}
        </view>
      </view>
    </view>
    <view class="content-ul" v-if="tagActive == '用户'">
      <view class="global_card_content user-li" v-for="(item, index) in collectionList" :key="item.ArticleId">
        <view class="li-item">
          <view class="li-serial" :style="{ background: serialBackground(index) }">
            {{ index + 1 }}
          </view>
          <view>
            <text class="global_title" @click="goDetail(item)" :style="{ display: 'inline', marginLeft: item.Title.substr(0, 1) != '【' ? '14rpx' : 0 }">{{ item.Title }}</text>
            <text class="li-industry" @click="themeDetails(key, '月度收藏榜')" v-for="key in item.List" :key="key.IndustrialManagementId"> # {{ key.IndustryName }} </text>
          </view>
        </view>
        <view class="li-bottom" style="color: #999999">
          <viwe class="time-user">
            <view class="time">
              {{ item.PublishDate }}
            </view>
            <view class="li-user" @click="authorDetails(item)">
              <image class="user_report-img" src="https://hzchart.oss-cn-shanghai.aliyuncs.com/new_cygx/user_report.png"></image>
              <text class="text_oneLine"> {{ item.NickName }}</text>
            </view>
          </viwe>

          <view class="user-img">
            <view class="img-box">
              <image class="user-pv" src="https://hzchart.oss-cn-shanghai.aliyuncs.com/new_cygx/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/new_cygx/collect_act.png"></image>
              <image v-else src="https://hzchart.oss-cn-shanghai.aliyuncs.com/new_cygx/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="global_card_content 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, '主题热度榜')">
            <view class="li-serial serial-number" :style="{ background: serialBackground(index) }">
              {{ index + 1 }}
            </view>
            <text class="global_title text_oneLine" style="display: inline"> # {{ item.IndustryName }} </text>
            <image class="new-img" v-if="item.IsNew" src="https://hzstatic.hzinsights.com/cygx/icon/new_report.png"></image>
          </view>
          <view :class="['follw', 'global_content_center', item.IsFollw && 'cancel-follw']" @click="isAttention(item, '主题')">
            {{ item.IsFollw ? "取消关注" : "+ 关注" }}
          </view>
        </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 class="content-button" @click="goThemeAuthor('主题')">查看全部主题>></view>
    </view>
    <view class="content-ul" v-if="kolList.length && tagActive == '专家'">
      <view class="global_card_content kol-li" v-for="(item, index) in kolList" :key="item.DepartmentId">
        <view class="li-serial serial-number" :style="{ background: serialBackground(index) }">
          {{ index + 1 }}
        </view>
        <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>
            <view :class="['follw', 'global_content_center', item.IsFollw && 'cancel-follw']" @click="isAttention(item, '专家')">
              {{ item.IsFollw ? "取消关注" : "+ 关注" }}
            </view>
          </view>
          <text @click="themeDetails(val, 'KOL榜')" class="kol-text text_oneLine" v-for="val in item.List" :key="val.IndustrialManagementId"> # {{ val.IndustryName }} </text>
        </view>
      </view>
      <view class="content-button" @click="goThemeAuthor('作者')">查看全部作者>></view>
    </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>
    <Loading />
  </view>
</template>

<script>
import { Reports, Research } from "@/config/api.js";
import purchaserReportListMixins from "../components/purchaser-report-list-mixins.js";
export default {
  data() {
    return {
      accounts: "",
      tagActive: "用户",
      collectionList: [], //用户收藏
      hotList: [],
      kolList: [],
      researchTabBars: [
        { titel: "月度收藏榜", name: "用户" },
        { titel: "主题热度榜", name: "主题" },
        { titel: "KOL榜", name: "专家" },
      ],
      confirmText: "知道了",
      isCancelBtn: false,
      goFollowShow: false,
    };
  },
  // purchaserReportListMixins 混入了 收藏、作者详情、文章详情、主题详情-method
  mixins: [purchaserReportListMixins],
  watch: {
    tagActive: {
      handler(val) {
        val == "用户" ? this.researchCollectionList() : val == "主题" ? this.researchHotList() : this.researchKolList();
      },
      immediate: true,
    },
  },
  mounted() {
    // 处理数据 列表同步详情
    uni.$on("updateFllowTheme", (data) => {
      this.hotList.forEach((item) => {
        if (item.IndustrialManagementId == data.id) {
          item.IsFollw = data.isFollw;
        }
      });
    });
    // 处理数据 列表同步详情
    uni.$on("updateFllowAuthor", (data) => {
      this.kolList.forEach((item) => {
        if (item.DepartmentId == data.id) {
          item.IsFollw = data.isFollw;
        }
      });
    });
  },
  methods: {
    //tabs切换
    typeChange(item) {
      this.tagActive = item.name;
    },
    //计算遍历的颜色
    serialBackground(index) {
      index += 1;
      return index == 1 ? "#D7584F" : index == 2 ? "#E98033" : index == 3 ? "#FDD367" : "#D3D3D3";
    },
    //用户收藏榜
    async researchCollectionList() {
      const res = await Research.researchCollectionList({ ChartPermissionId: 31 });
      if (res.Ret === 200) {
        this.collectionList = res.Data.List || [];
      }
    },
    //主题热度/近期更新更多
    async researchHotList() {
      const res = await Research.researchHotList({ ChartPermissionId: 31, ThemeType: 1 });
      if (res.Ret === 200) {
        this.hotList = res.Data.List || [];
      }
    },
    //KOL榜列表接口
    async researchKolList() {
      const res = await Research.researchKolList({ ChartPermissionId: 31 });
      if (res.Ret === 200) {
        this.kolList = res.Data.List || [];
      }
    },
    //关注
    async isAttention(item, val) {
      await this.$store.dispatch("showLoginModal");
      const res =
        val === "主题"
          ? await Reports.reportFllow({ IndustrialManagementId: item.IndustrialManagementId, PageRouter: this.$store.state.pageRouterReport })
          : await Reports.reportFllowDepartment({ DepartmentId: item.DepartmentId, PageRouter: this.$store.state.pageRouterReport });
      if (res.Ret === 200) {
        item.IsFollw = !item.IsFollw;
        if (res.Data.Status == 1) {
          this.goFollowShow = true;
          this.confirmText = res.Data.GoFollow ? "去关注" : "知道了";
          if (res.Data.GoFollow) {
            this.accounts = `${val == "主题" ? "产业" : "作者"}关注成功 <br> 想要及时获取该${val === "主题" ? "产业内容的更新推送" : "作者的报告更新提示"}，请关注【查研观向小助手】公众号`;
            this.isCancelBtn = true;
          } else {
            this.accounts = `${val == "主题" ? "产业" : "作者"}` + `关注成功<br>请关注【查研观向小助手】公众号，及时获取${val === "主题" ? "产业" : "作者"}报告更新提醒`;
          }
        } else {
          uni.showToast({
            title: "已取消关注",
            icon: "none",
            duration: 2000,
          });
        }
      }
    },
    //去关注
    goFollowShowBtn() {
      if (this.confirmText == "去关注") {
        uni.navigateTo({
          url: "/activityPages/accountsOfficial/accountsOfficial",
        });
      }
      this.goFollowShow = false;
    },
    // 跳转主题 of 作者
    goThemeAuthor(type) {
      type === "主题" ? uni.navigateTo({ url: "/pages-purchaser/researchTheme/researchTheme" }) : uni.navigateTo({ url: "/pages-purchaser/researchAuthor/researchAuthor" });
    },
    loadShare(option) {
      this.tagActive = option.tagActive || "用户";
    },
  },
  /** 用户点击分享 */
  onShareAppMessage: function (res) {
    return {
      title: "研选榜单",
      path: "/pages-purchaser/researchList/researchList?tagActive=" + this.tagActive,
    };
  },
  onLoad(option) {
    this.loadShare(option);
  },
};
</script>

<style lang="scss" scoped>
.research-list-container {
  background-color: $uni-bg-color;
  padding: 100rpx 30rpx 30rpx;
  .top-content {
    width: 100%;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 99;
    background-color: #fff;
    padding-bottom: 10rpx;
    .item {
      flex: 1 !important;
    }
  }
  .content-ul {
    margin: 20rpx 0 0;
    border-top: 1px solid #f6f6f6;
    .user-li,
    .kol-li,
    .hot-li {
      margin-bottom: 20rpx;
      background-color: #fff;
    }
    .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: $uni-color-new;
        margin-left: 20rpx;
        display: inline-block;
        font-size: 32rpx;
        font-weight: 600;
      }

      .li-title {
        display: flex;
      }
    }
    .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;
        font-size: 24rpx;
        font-weight: 400;
        color: #333;
      }
    }
    .li-bottom {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-top: 30rpx;
      width: 100%;
      font-size: 24rpx;
      .user-img {
        display: flex;
        align-items: center;
        justify-content: flex-end;
        width: 39%;
        image {
          width: 26rpx;
          height: 26rpx;
          margin-right: 10rpx;
        }
        view {
          display: flex;
          align-items: center;
          flex-shrink: 0;
          margin-left: 20rpx;
        }
        .user-pv {
          width: 30rpx;
          height: 30rpx;
        }
      }
      .time-user {
        display: flex;
        align-items: center;
        flex: 1;
        .time {
          width: 200rpx;
        }
        view {
          display: flex;
          align-items: center;
          flex-shrink: 0;
        }
        image {
          width: 24rpx;
          height: 24rpx;
          margin-right: 10rpx;
        }
      }
    }
    .read-more {
      margin-top: 10rpx;
      display: flex;
      flex-wrap: wrap;
      .industry-video-item {
        height: 106rpx;
        width: 150rpx;
        margin: 0 10rpx 10rpx 0;
        border-radius: 8rpx;
        background-size: cover;
        background-position: center;
        background-repeat: no-repeat;
        font-weight: 500;
        font-size: 24rpx;
        color: white;
        line-height: 45rpx;
        padding: 20rpx;
        position: relative;
        overflow: hidden;

        .industry-video-icon {
          height: 0rpx;
          width: 0rpx;
          border-style: solid;
          border-color: #376cbb #376cbb transparent transparent;
          border-width: 28rpx;
          position: absolute;
          right: 0;
          top: 0;
          image {
            position: absolute;
            right: -24rpx;
            bottom: 0;
            height: 20rpx;
            width: 20rpx;
          }
        }
      }
      .text-box {
        padding: 0;
        margin: 0 10rpx 10rpx 0;
        font-size: 24rpx;
        color: $uni-color-new;
        width: 150rpx;
        height: 48rpx;
        text-indent: 0em;
        line-height: 48rpx;
        text-align: center;
        background-color: #f8f8fa;
        border-radius: 200rpx;
      }
    }
    .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;
      }
    }
  }

  .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;
    }
  }
  .content-bottom {
    background-color: $uni-bg-color;
    text-align: center;
    line-height: 100rpx;
    color: #c4c4c4;
    font-size: 20rpx;
  }
  .industry-color {
    font-size: 26rpx;
    padding: 0rpx 20rpx;
    border-radius: 4rpx;
    margin-left: 20rpx;
  }
  .follw {
    color: #fff;
    border-radius: 4rpx;
    font-size: 24rpx;
    width: 110rpx;
    height: 42rpx;
    background-color: $uni-color-new;
  }
  .cancel-follw {
    background-color: #e5efff;
    color: $uni-color-new;
  }
}
</style>
