<template>
	<view class="research-list-container">
		<view class="announcement">
		  <view class="tab-cont">
		    <scroll-view
		      scroll-x="true"
		      scroll-with-animation
		      class="scroll-tab"
		      :scroll-into-view="'_' + tabIndex"
		    >
		      <block v-for="(item, index) in researchTabBars" :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="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
								class="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-item li-bottom" style="color: #999999">
						<viwe class="time-user">
							<text>
								{{ item.PublishDate }}
							</text>
							<view class="li-user" @click="authorDetails(item)">
								<image class="user_report-img" src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/user_report.png"></image>
								<text class="text_oneLine"> {{ item.NickName }}</text>
							</view>
						</viwe>
	
						<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 class="content-bottom">最新榜单您已掌握，明天再刷刷看~</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 title" 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>
		        <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 class="content-button" @click="goThemeAuthor('主题')">查看全部主题>></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, '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>
		</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>
	@import "../components/purchaser-report-list.scss";
	.research-list-container{
		background-color: #f7f7f7;
		padding-top: 20rpx;
		box-sizing: border-box;
		.tab-cont {
			height: 115rpx;
			background-color: #fff;
			font-size: 32rpx;
			border-bottom: 1rpx solid #ececec;
			.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;
		// }
		.content-button {
			height: 100rpx;
			width: 100%;
			text-align: center;
			line-height: 100rpx;
			color: #3385ff;
		}
	}
</style>
