<template>
	<view class="content">
		
		<view class="top-content">
			<view class="search-box">
				<view class="inner">
					<image src="/static/images/icon_search.png" mode=""></image>
					<input type="text"  v-model="searchValue" @confirm="doSearch" placeholder="输入关键字搜索" placeholder-class="placeholder">
				</view>
			</view>
			<view class="tag-box">
				<scroll-view scroll-x="true" >
					<view class="tag-scroll">
						<view v-for="(item,index) in cates" :key="index" @click="choseCate(item)" :class="choseCateId == item.cateId?'tag active':'tag'"  >{{ item.cateName }}</view>
					</view>
				</scroll-view>
			</view>
		</view>
		<!-- 数据列表 -->
		
		<view class="prom-list">
			<mescroll-body   ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption">
			    <view class="item" v-for="(item,index) in dataList" :key="index">
				   <view class="info-box" @click="showDetail(item)">
					 <view class="info-text">
						<view class="title">{{ item.title }}</view>
						<view class="time">{{item.publishTime}}</view>
					</view>
				    	<image class="img" :src="item.imageUrl" mode="aspectFill"></image>
				   </view>
				   <view class="hand-box" >
					<view class="left" @click="showDetail(item)">
						<view class="head-box" v-if="item.viewsList!=null&&item.viewsList.length>0">
							<view class="head-item" v-for="(subitem,j) in item.viewsList" :key="j">
								<image class="img"   :src="getImgSrc(subitem)"   mode=""></image>
							</view>
						</view>
						<view class="hand-item">
							<image class="eyes" src="/static/images/icon_eyes.png" mode=""></image>
							<text class="text">{{item.views}}</text>
						</view>
						<view class="hand-item">
							<image class="share" src="/static/images/icon_share_gray.png" mode=""></image>
							<text class="text">0</text>
						</view>
					</view>
					<view class="share-btn" @click="openShare(item)">立即分享</view>
				   </view>
			    </view>
			</mescroll-body>
		</view>
		
		<!-- 分享弹窗 -->
		<u-popup :show="show" @close="closeShare" >
			<view class="share-content">
				<view class="share-inner">
					<view class="share-item" @click="wxShare">
						<image src="/static/images/icon_wx.png" mode=""></image>
						<text class="text">微信</text>
					</view>
					<view class="share-item" @click="wxLineShare">
						<image src="/static/images/icon_pyq.png" mode=""></image>
						<text class="text">朋友圈</text>
					</view>
		<!-- 			<view class="share-item">
						<image src="/static/images/icon_xcxhb.png" mode=""></image>
						<text class="text">小程序海报</text>
					</view>
					<view class="share-item">
						<image src="/static/images/icon_xccard.png" mode=""></image>
						<text class="text">小程序卡片</text>
					</view>
					<view class="share-item">
						<image src="/static/images/icon_code.png" mode=""></image>
						<text class="text">二维码</text>
					</view> -->
					<view class="share-item">
						<image src="/static/images/icon_copy_link.png" mode=""></image>
						<text class="text">复制链接</text>
					</view>
				</view>
				<view class="cancel-btn" @click="closeShare">取消</view>
			</view>
		</u-popup>
		
	</view>
</template>

<script>
	
	import { getArticleCate,getArticleList } from '@/api/article'
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";	
	import { qconfig } from '@/utils/config';
	export default {
		mixins: [MescrollMixin], 
		data() {
			return {
				show:false,
				shareItem:null,
				cates:[],
				choseCateId:0,
				searchValue: '',
				mescroll:null,
				//下拉刷新
				downOption: {   
					use:true,
					auto: true // 不自动加载 (mixin已处理第一个tab触发downCallback)
				},
				// 上拉加载的配置
				upOption: {
					onScroll:true,
					use: true, // 是否启用上拉加载; 默认true
					page: {
						num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 10, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					empty: {
						icon:'/static/images/no_data.png',
						tip: '暂无数据'
					},
					textNoMore:'已经到底了'
				},
				// 列表数据
				dataList: [],
				defImg:"../../static/images/detault_head.jpg",
			}
		},
		onShow() {
			this.getArticleCate();
		},
		methods: {
			openShare(item) {
				this.show = true
				this.shareItem=item;
			},
			closeShare() {
			  this.show = false
			},
			doSearch(){
				this.mescroll.resetUpScroll()
			},
			getArticleCate(){
				var that=this;
				let data = {};
				getArticleCate(data).then(
					res => {
						if(res.code==200){
							this.cates=res.data;		
						}else{
							uni.showToast({
								icon:'none',
								title: "请求失败",
							});
						}
					},
					rej => {}
				);
			},
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			/*下拉刷新的回调 */
			downCallback(mescroll) {
				mescroll.resetUpScroll()
			},
			upCallback(page) {
				//联网加载数据
				var that = this;
				var data = {
					keyword:this.searchValue,
					cateId:this.choseCateId,
					page: page.num,
					pageSize: page.size
				};
				getArticleList(data).then(res => {
					if(res.code==200){
						//设置列表数据
						if (page.num == 1) {
							that.dataList = res.data.list; 
						} else {
							that.dataList = that.dataList.concat(res.data.list);
						}
						that.mescroll.endBySize(res.data.list.length, res.data.total);
					}else{
						uni.showToast({
							icon:'none',
							title: "请求失败",
						});
						that.dataList = null;
						that.mescroll.endErr();
					}
				});
			},
			
			// 关键词选择
			choseCate(item) {
				this.choseCateId = item.cateId;
				this.mescroll.resetUpScroll()
			},
			// 查看详情
			showDetail(item) {
				uni.navigateTo({
					url: './detail?articleId=' + item.articleId
				});
			},
			wxShare(){
				// #ifdef APP-PLUS
				   if(!plus.runtime.isAgreePrivacy()) {
					uni.showToast({
						title: "请同意隐私政策",
						icon: "none",
						position: 'top',
						duration: 2000
					})
				   	return;
				   }
				// #endif
				uni.share({
									provider: "weixin",
									scene: "WXSceneSession",
									type: 5,
									imageUrl: this.shareItem.imageUrl,    //分享封面图片
									title: this.shareItem.title,    // 分享标题
									//summary: "微信分享示例",    // 分享内容文字
									miniProgram:{ 
										id:qconfig.miniProgramId,
										path:"pages/healthy/detail?articleId="+this.shareItem.articleId,
										type:0,//0-正式版； 1-测试版； 2-体验版。 默认值为0。
									    webUrl:"https://uniapp.dcloud.io"
									},
									success: function(res) {
										 uni.showToast({
										 	title: "分享成功",
										 	icon: 'none'
										 });
									},
									fail: function(err) {
										// 此处是调起微信分享失败的回调
									}
								});
				
			},
			wxLineShare(){
				
			},
			getImgSrc(item){
				var imgSrc=this.defImg;
				if(item!=null && item.avatar!=null){
					imgSrc=item.avatar;
				}
				return imgSrc;
			}
			
		}
	}
</script>

<style lang="scss">
	page{
		background-color: #F7F7F7;
	}
</style>

<style scoped lang="scss">
	
	.top-content{
		width: 100%;
		position: fixed;
		top: 0;
		left: 0;
		z-index: 11;
		background: #FFFFFF;
		.search-box{
			height: 104upx;
			padding: 0 30upx;
			display: flex;
			align-items: center;
			.inner{
				box-sizing: border-box;
				width: 100%;
				height: 72upx;
				background: #F7F7F7;
				border-radius: 36upx;
				display: flex;
				align-items: center;
				padding: 0 30upx;
				image{
					width: 28upx;
					height: 28upx;
					margin-right: 20upx;
				}
				input{
					font-size: 28upx;
					font-family: PingFang SC;
					font-weight: 500;
					flex: 1;
				}
				.placeholder{
					font-size: 28upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #BBBBBB;
				}
			}
		}
		
		.tag-box{
			height: 106upx;
			padding: 0 30upx;
			.tag-scroll{
				height: 106upx;
				display: flex;
				align-items: center;
				.tag{
					flex-shrink: 0;
					height: 58upx;
					line-height: 56upx;
					padding: 0 24upx;
					background: #F5FFFE;
					border: 2upx solid #8AD5CE;
					border-radius: 14upx;
					font-size: 28upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #FF5C03;
					margin-right: 20upx;
					&:last-child{
						margin-right: 0;
					}
					&.active{
						color: #FFFFFF;
						background: #2BC7B9;
						border: 1px solid #2BC7B9;
					}
				}
			}
		}
	}
	.prom-list{
		padding: 230upx 20upx 20upx;
		.item{
			background: #FFFFFF;
			border-radius: 16upx;
			margin-bottom: 20upx;
			padding: 40upx 30upx;
			&:last-child{
				margin-bottom: 0;
			}
			.info-box{
				display: flex;
				align-items: center;
				justify-content: space-between;
				.info-text{
					width: calc(100% - 254upx);
					height: 170upx;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					.title{
						font-size: 32upx;
						font-family: PingFang SC;
						font-weight: bold;
						color: #111111;
						line-height: 50upx;
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-line-clamp: 2;
						-webkit-box-orient: vertical;
					}
					.time{
						font-size: 24upx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #999999;
						line-height: 1;
					}
				}
				.img{
					width: 224upx;
					height: 171upx;
					background: #E0E0E0;
					border-radius: 16upx;
					overflow: hidden;
				}
			}
			.hand-box{
				margin-top: 50upx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				.left{
					display: flex;
					align-items: center;
					.head-box{
						display: flex;
						margin-right: 26upx;
						.head-item{
							width: 48upx;
							height: 48upx;
							background: #F6F7FB;
							border: 2upx solid #FFFFFF;
							border-radius: 50%;
							overflow: hidden;
							margin-left: -10upx;
							&:first-child{
								margin-left: 0;
							}
							.img{
								width: 100%;
								height: 100%;
							}
						}
					}
					.hand-item{
						display: flex;
						align-items: center;
						margin-right: 40upx;
						&:last-child{
							margin-right: 0;
						}
						.eyes{
							width: 27upx;
							height: 22upx;
							margin-right: 18upx;
						}
						.share{
							width: 24upx;
							height: 24upx;
							margin-right: 12upx;
						}
						.text{
							font-size: 24upx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #999999;
							line-height: 1;
						}
					}
				}
				.share-btn{
					padding: 0 25upx;
					height: 60upx;
					line-height: 60upx;
					background: #FF5C03;
					border-radius: 12upx;
					font-size: 26upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #FFFFFF;
				}
			}
		}
	}
	
	
	.share-content{
		background-color: #FFF;
		border-radius: 40upx 40upx 0px 0px;
		.share-inner{
			padding: 70upx 0 0 0;
			display: flex;
			flex-wrap: wrap;
			.share-item{
				width: 25%;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				margin-bottom: 66upx;
				image{
					width: 80upx;
					height: 80upx;
					margin-bottom: 20upx;
				}
				.text{
					font-size: 28upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #111111;
					line-height: 1;
				}
			}
		}
		.cancel-btn{
			height: 96upx;
			line-height: 96upx;
			text-align: center;
			font-size: 32upx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #111111;
			border-top: 2upx solid #E8E8E8;
			background-color: #FFF;
		}
	}


</style>
