<template>
  <view class="container container-hot-list">
    <view class="top-content">
      <view class="tab-content">
        <view v-for="item in tabList" :key="item.Source" :class="['item', tabsActive == item.Source && 'tab-active']" @click="tabsHandler(item)">
          {{ item.Name }}
          <image v-if="tabsActive == item.Source" src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/border_act.png"></image>
        </view>
      </view>
      <block v-for="item in tabList" :key="item.Source">
        <view class="children-tabs" v-if="tabsActive == item.Source">
          <view
            :class="['children-item', childrenTabsActive == key.ChartPermissionId && 'children-tab-active']"
            v-for="(key, index) in item.List"
            :key="key.ChartPermissionId"
            @click="childrenTabsHandel(key, index)"
          >
            {{ key.PermissionName }}
          </view>
        </view>
      </block>
    </view>
    <view class="content">
      <view class="content-ul" v-if="hotList.length && tabsActive == 1">
        <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="title-text text_oneLine" style="display: inline"> # {{ item.IndustryName }} </text>
              <view class="industry-color" :style="[classifyColor(item.PermissionName)]">{{ item.PermissionName }}</view>
              <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.IsFollow ? 'cancel-attention' : 'attention'" @click="isAttention(item, '主题')">{{ item.IsFollow ? "取消关注" : "+ 关注" }}</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 && tabsActive != 1">
        <view class="user-li" v-for="(item, index) in kolList" :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; margin-right: 20rpx">{{ 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-item li-bottom" style="color: #999999">
            <view class="li-user text_oneLine">
              {{ item.PublishDate }}
              <view class="industry-color" :style="[classifyColor(item.PermissionName)]">{{ item.PermissionName }}</view>
            </view>
          </view>
        </view>
      </view>
    </view>
    <view class="content-bottom">最新榜单您已掌握，明天再刷刷看~</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>
  </view>
</template>

<script>
import { Reports } from "@/config/api.js";

export default {
  data() {
    return {
      tabList: [], //tab
      tabsActive: 1,
      childrenTabsActive: 0,
      hotList: [],
      kolList: [],

      confirmText: "知道了",
      accounts: "",
      isCancelBtn: false,
      goFollowShow: false,
    };
  },
  methods: {
    /* 获取tab*/
    async getTabList() {
      const res = await Reports.getHotTableList();
      if (res.Ret === 200) {
        this.tabList = res.Data.List || [];
        this.tabsActive = this.tabList.length ? this.tabList[0].Source : 1;
        this.childrenTabsActive = this.tabList.length ? this.tabList[0].List[0].ChartPermissionId : 1;
        this.researchHotList();
      }
    },
    /* 点击一级的tabs*/
    tabsHandler(item) {
      this.tabsActive = item.Source;
      this.childrenTabsActive = item.List[0].ChartPermissionId;
      this.listInit();
      this.researchHotList();
    },
    /* 点击二级的tabs*/
    childrenTabsHandel(item, index) {
      this.childrenTabsActive = item.ChartPermissionId;
      this.listInit();
      this.researchHotList();
    },
    //产业关注 阅读、报告收藏
    async researchHotList() {
      const res =
        this.tabsActive == 1
          ? await Reports.getIndustryFllowList({ ChartPermissionId: this.childrenTabsActive })
          : await Reports.getIndustryReadList({ ChartPermissionId: this.childrenTabsActive, Source: this.tabsActive });
      if (res.Ret === 200) {
        if (this.tabsActive == 1) {
          this.hotList = res.Data.List || [];
        } else {
          this.kolList = res.Data.List || [];
        }
      }
    },
    listInit() {
      if (this.tabsActive == 1) {
        this.hotList = [];
      } else {
        this.kolList = [];
      }
    },
    //去往作者详情
    authorDetails(item) {
      uni.navigateTo({
        url: "/reportPages/authorPages/authorPages?id=" + item.DepartmentId,
      });
    },
    //去往主题详情
    themeDetails(item) {
      this.$store.dispatch("checkHandle", "/reportPages/IndustryReport/IndustryReport?id=" + item.IndustrialManagementId);
    },
    //计算遍历的颜色
    serialBackground(index) {
      index += 1;
      return index == 1 ? "#D7584F" : index == 2 ? "#E98033" : index == 3 ? "#FDD367" : "#D3D3D3";
    },
    //关注
    async isAttention(item, val) {
      const res = val === "主题" ? await Reports.reportFllow({ IndustrialManagementId: item.IndustrialManagementId }) : await Reports.reportFllowDepartment({ DepartmentId: item.DepartmentId });
      if (res.Ret === 200) {
        item.IsFollow = !item.IsFollow;
        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;
    },
    goDetail(item) {
      uni.navigateTo({
        url: "/pageMy/reportDetail/reportDetail?id=" + item.ArticleId,
      });
    },
    classifyColor(item) {
      let color_word = item == "医药" ? "#04CE3D" : item == "科技" ? "#3385FF" : item == "消费" ? "#F1BA70" : item == "智造" ? "#8F72E1" : "#F1925C";
      let color_bg = item == "医药" ? "#EEFFF2" : item == "科技" ? "#EEF5FF" : item == "消费" ? "#FFFAF3" : item == "智造" ? "#F4F0FF" : "#FFFAF2";
      let _isColor = { color: color_word, border: ` 1px solid ${color_word}`, "background-color": color_bg };
      return _isColor;
    },
  },
  onLoad() {
    this.getTabList();
  },
};
</script>

<style lang="scss" scoped>
.container-hot-list {
  padding: 30rpx;
  .top-content {
    position: sticky;
    top: 0;
    left: 0;
    z-index: 99;
    background-color: #fff;
    padding-bottom: 10rpx;
  }
  .tab-content {
    display: flex;
    .item {
      position: relative;
      display: inline-block;
      padding-bottom: 10rpx;
      font-size: 32rpx;
      color: #999999;
      margin-right: 50rpx;
      font-weight: 400;
      image {
        position: absolute;
        bottom: 0;
        left: 50%;
        transform: translateX(-50%);
        width: 100rpx;
        height: 4rpx;
      }
    }
    .tab-active {
      color: #333333;
      font-weight: 500;
    }
  }
  .children-tabs {
    margin-top: 30rpx;
    width: 100%;
    display: flex;
    overflow: hidden;
    overflow-x: auto;
    .children-item {
      padding: 10rpx 20rpx;
      background: #f9f9f9;
      border-radius: 4px;
      margin-right: 40rpx;
    }
    .children-tab-active {
      background-color: #3385ff;
      color: #fff;
    }
  }
  .content-ul {
    margin: 20rpx 0 0;
    border-top: 1px solid #f6f6f6;
    .user-li,
    .hot-li {
      padding: 30rpx 0 20rpx;
      border-bottom: 1px solid #f6f6f6;
    }
    .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-right: 20rpx;
        display: inline-block;
      }
      .li-user {
        margin-left: 46rpx;
        display: flex;
        align-items: center;
      }
      .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;
      }
    }
  }
  .content-bottom {
    background-color: #fff;
    text-align: center;
    line-height: 100rpx;
    color: #c4c4c4;
    font-size: 20rpx;
  }
  .industry-color {
    padding: 3rpx 20rpx;
    border-radius: 4rpx;
    margin-left: 20rpx;
  }
  .title-text {
    font-size: 28rpx;
    font-weight: 500;
  }
}
</style>
