<template>
	<view class="page">
		<view class="status-bar"></view>
		<view class="content">
			<!-- Banner -->
			<scroller @init="initScroller" @down="refreshData" @up="getData" :up="optUp"
				@scroll="navFloatShow(scroller)" :fixed="false">
				<view class="search">
					<view class="search-content">
						<image src="../../static/images/ic_search.png" class="icon" mode="scaleToFill"></image>
						<input @input="onSearchInput" placeholder="请输入关键字搜索" />
					</view>
					<view class="btn" @click="doSearch">搜索</view>
				</view>
				<swiper v-if="slider.length > 0" class="swiper" :indicator-dots="true" :autoplay="true"
					:circular="true">
					<swiper-item v-for="(item, index) in slider" :key="index">
						<navigator class="item" hover-class="none"
							:url="'/pages/news/newsdetail?url='+baseurl+item.templateRecordUrl">
							<image :lazy-load="true" :src="item.imageUrl" mode="scaleToFill"></image>

						</navigator>
					</swiper-item>
				</swiper>
				<!-- 新闻列表 -->
				<articleList :list="list" />
			</scroller>
		</view>
		<pageLoading v-if="showPageLoading"></pageLoading>
	</view>
</template>

<script>
	import scroller from '@/components/scroller/scroller.vue';
	import articleList from '@/components/article/list.vue';
	import pageLoading from '@/components/loading/pageLoading.vue';
	import iconfont from '@/components/iconfont/iconfont.vue';
	import util from '@/common/util.js';
	export default {
		components: {
			articleList,
			pageLoading,
			scroller,
			iconfont
		},
		data() {
			return {
				scroller: {},
				optUp: {
					auto: true,
					onScroll: true,
					page: {
						num: 0, //当前页 默认0,回调之前会加1; 即callback(page)会从1开始
						size: 10 //每页数据条数,默认10
					},
					empty: {
						tip: '暂无文章~'
					}
				},
				category_id: 1,
				category_index: 0,
				scroll_category_id: 'scroll_category_id_0',
				currentSliderIndex: 0,
				category: [],
				showMenu: false,
				slider: [],
				list: [],
				showNoData: false,
				showPageLoading: true,
				showNavFloat: false,
				navBarHeight: '',
				token: "",
				searchContent: "",
				baseurl: this.$api.common.baseUrl,

			};
		},
		onShow() {

			this.$initPageTitle(); //初始化页面标题
			console.log("page=onShow")
			/*导航栏高度*/
			if (this.navBarHeight == '') {
				this.navBarHeight = this.$app.getNaviBarHeight();
			}

			// /*来源是登录时更新*/
			// let source = uni.getStorageSync('source');
			// console.log(source)
			// if (source == 'login') {
			// 	uni.removeStorageSync('source');
			// 	this.loadData();
			// }
			this.token = getApp().globalData.token
			let userInfo = getApp().globalData.userInfo
			if (this.token && this.token != '' && userInfo.name == undefined) {
				this.getUserInfo()
			}
		},

		onShareAppMessage() {
			return {
				path: '/pages/article/index',
				success: function(e) {},
				title: '全民植树'
			};
		},
		onLoad(e) {
			// #ifdef APP-PLUS
			// this.getAppVersion(plus.runtime.versionCode)
			this.startDownloadApk()
			// #endif
			this.getBannerList();
		},
		onPullDownRefresh() {
			uni.showLoading({
				title: '刷新中'
			});
			this.loadData();
		},
		methods: {
			onSearchInput: function(event) {
				this.searchContent = event.target.value
				console.log(event.target.value)
				if (event.target.value == "") {
					this.refreshData()
				}
			},
			openLogin() {
				uni.navigateTo({
					url: '/pages/user/login'
				})
			},
			/*初始化滚动*/
			initScroller(scroller) {
				this.scroller = scroller;
				this.loadData();
			},

			/*刷新数据*/
			refreshData() {
				uni.showLoading({
					title: '刷新中'
				});
				this.scroller.resetUpScroll();
				this.getBannerList();
			},

			/*加载数据*/
			loadData() {
				this.slider = [];
				this.list = [];
				this.currentSliderIndex = 0;
				this.scroller.resetUpScroll();
			},

			/*获取子类别数据*/
			getCategory() {
				console.log("获取类别")
				this.$app.request({
					url: this.$api.news.getMainFun,
					method: 'GET',
					data: {
						'newsDirectoriesId': this.$api.news.NEWS_MAIN_TAB
					},
					dataType: 'json',
					success: res => {
						if (res != undefined && res.length > 0) {
							this.category = res;
							if (this.category_index > -1) {
								let nextIndex = this.category_index - 1;
								nextIndex = nextIndex <= 0 ? 0 : nextIndex;
								this.scroll_category_id = `category_id-${nextIndex}`; //动画滚动,滚动至中心位置
							}
						} else {
							this.$alert(res.msg);
						}
					},
					complete: res => {}
				});
			},

			/*获取数据*/
			getData() {
				this.$app.request({
					url: this.$api.news.getNewsList,
					data: {
						newsDirectoriesId: this.$api.news.NEWS_MAIN_ID,
						page: this.scroller.num,
						rows: this.scroller.size,
						keywords: this.searchContent
					},
					method: 'GET',
					dataType: 'json',
					success: res => {
						if (res.rows != undefined) {
							if (this.scroller.num == 1) {
								this.list = [];
							}
							this.list = this.list.concat(res.rows);
							this.scroller.endBySize(res.rows.length, res.rows.total);
							this.showPageLoading = false;
						} else {
							this.scroller.endSuccess();
							this.$alert(res.msg);
						}
					},
					fail: res => {
						this.scroller.endErr();
					},
					complete: res => {
						uni.stopPullDownRefresh();
						uni.hideLoading();
					}
				});
			},

			/*切换导航*/
			categoryChange(category_id, index) {
				this.showMenu = false;
				this.category_index = index;
				this.category_id = category_id;
				var nextIndex = index - 1;
				nextIndex = nextIndex <= 0 ? 0 : nextIndex;
				this.scroll_category_id = `category_id-${nextIndex}`; //动画滚动,滚动至中心位置
				this.loadData();

				// #ifdef H5
				// uni.navigateTo({
				// 	url: '/pages/article/list?category_id=' + this.category_id + '&category_index=' + this.category_index
				// });
				// #endif
			},

			/*广告切换*/
			sliderChange: function(e) {
				this.currentSliderIndex = e.detail.current;
			},

			/*滚动时导航栏浮动*/
			navFloatShow(scroller) {
				if (scroller) {
					if (scroller.scrollTop > 60) {
						if (!this.showNavFloat) {
							this.showNavFloat = true;
						}
					} else {
						if (this.showNavFloat) {
							this.showNavFloat = false;
						}
					}
				}
			},
			/*菜单框展示*/
			menuShow(value) {
				this.showMenu = value;
			},
			/*隐藏导航浮动*/
			navFloatHide() {
				this.showNavFloat = false;
			},
			/*获取banner*/
			getBannerList() {
				this.$app.request({
					url: this.$api.news.getNewsList,
					method: 'GET',
					data: {
						'newsDirectoriesId': this.$api.news.NEWS_BANNER_ID,
						'rows': '5',
						'page': '1'
					},
					dataType: 'json',
					success: res => {
						if (res.rows != undefined && res.rows.length > 0) {
							for (var i = 0; i < res.rows.length; i++) {
								res.rows[i].imageUrl = this.$api.news.imgUrl + res.rows[i]
									.newsContentCoverList[0]
									.contentCoverId
							}
							this.showPageLoading = false;
							this.scroller.endSuccess();
							this.slider = res.rows
						}
					},
					complete: res => {
						this.scroller.endBySize(1, 1);
						uni.stopPullDownRefresh();
						uni.hideLoading();
						this.showPageLoading = false;
					}
				});
			},
			getUserInfo() {
				this.$app.request({
					url: this.$api.user.getUserInfo,
					method: 'GET',
					header: {
						token: this.token
					},
					success: res => {
						if (res) {
							getApp().globalData.userInfo = res
						}
						console.log(getApp().globalData.userInfo)
					},
					complete: res => {

					}
				})
			},
			getAppVersion(version) {
				this.$app.request({
					url: this.$api.common.getAppVersion,
					method: "GET",
					success: res => {
						if (res) {
							if (res.data > version) {
								this.startDownloadApk()
							}
						}
					},
					complete: res => {

					}
				});
			},
			startDownloadApk() {
				//开始下载Apk
				console.log("开始下载Apk")
				// #ifdef APP-PLUS
				const dtask = plus.downloader.createDownload(this.$api.common.appDownloadUrl, {
					force: true
				}, function(d, status) {
					console.log(status)
					// 下载完成
					if (status == 200) {
						var path = (d.filename); //文件安装路径
						plus.runtime.install(path, {}, function() {
							plus.nativeUI.closeWaiting(); //关闭系统等待对话框
							if (name == 'wgt') {
								console.log("安装wgt文件成功！");
							} else {
								console.log("安装apk文件成功！")
							}

							plus.nativeUI.alert("引用资源更新完成！", function() {
								plus.runtime.restart();
							})
						}, function(e) {
							plus.nativeUI.closeWaiting(); //关闭系统等待对话框
							if (name == 'wgt') {
								console.log("安装wgt文件失败[" + e.code + "]：" + e.message);
								plus.nativeUI.alert("安装wgt文件失败[" + e.code + "]：" + e.message);
							} else {
								console.log("安装apk文件失败[" + e.code + "]：" + e.message);
								plus.nativeUI.alert("安装apk文件失败[" + e.code + "]：" + e.message);
							}

						})

					} else {
						alert("Download failed：" + status);
					}
				})
				// #endif
			},
			doSearch() {
				uni.hideKeyboard()
				if (this.searchContent == "") {
					this.$alert("请输入关键字");
				}
				this.refreshData()
			}
		}
	};
</script>

<style scoped lang="scss">
	page {
		height: 100%;
	}

	.page {
		display: flex;
		flex: 1;
		flex-direction: column;
		overflow: hidden;
		height: 100%;
	}

	.content {
		flex: 1;
		width: 100%;
		height: 100rpx;
	}

	/*头部*/
	.top {

		/*页面标题*/
		.page-title {
			display: flex;
			justify-content: center;
			align-items: center;
			height: 90rpx;
			text-align: center;
			border-bottom: 1rpx solid #efefef;
			z-index: 9999;
			line-height: 1;

			image {
				height: 50rpx;
				width: 280rpx;
				margin-left: -35rpx;
			}
		}



		/* 顶部navbar */
		.navbar {

			/*分类*/
			.menu {
				position: relative;
				height: 80rpx;
				white-space: nowrap;
				padding: 15rpx 0 6rpx;
				z-index: 10;
				display: flex;
				align-items: center;
				justify-content: space-between;

				/*分类*/
				.category {
					width: 650rpx;
					margin-left: 30rpx;
					white-space: nowrap;
					position: relative;

					scroll-view {
						width: auto;

						.item {
							position: relative;
							display: inline-block;
							margin: 0 10rpx 0;
							height: 80rpx;
							text-align: left;
							padding-top: 7rpx;

							//line-height: 80rpx;
							&:first-child {
								margin-left: 10rpx;
							}

							&:after {
								content: '';
								width: 0;
								height: 0;
								position: absolute;
								left: 50%;
								bottom: 0;
								transform: translateX(-50%);
								transition: 0.3s;
							}

							.text {
								position: relative;
								width: auto;
								height: auto;
								line-height: auto;
								display: inline-block;

								text {
									font-size: 36rpx;
									font-weight: bold;
									color: #555;
								}

								image {
									position: absolute;
									top: 16rpx;
									right: -14rpx;
									width: 50rpx;
									height: 50rpx;
									display: none;
								}
							}
						}

						.current {
							&:after {
								width: 50%;
							}

							.text {
								text {
									font-size: 40rpx;
									font-weight: bold;
									color: #262626;
								}

								image {
									display: block;
								}

								border-bottom: 6rpx solid #8cc7b5;
							}
						}
					}
				}

				.list {
					width: 70rpx;
					display: flex;
					align-items: center;
					justify-content: center;

					//box-shadow: -4rpx 0 0 #e9ebee;
					//box-shadow: -2px 0 0 #262626;
					/deep/ .icon {
						font-size: 36rpx;
						margin-top: -6rpx;
					}
				}
			}
		}
	}

	.menu-block {
		position: absolute;
		top: 0;
		left: 0;
		background: #fff;
		border-bottom: 1rpx solid #f5f5f5;
		padding: 20rpx 0 50rpx;
		z-index: 100;
		width: 100%;

		//box-shadow: 0 15rpx 10rpx -15rpx #e9ebee;
		.list {
			padding-left: 10rpx;

			text {
				background: #f5f7fa;
				border-radius: 8rpx;
				font-size: 32rpx;
				margin: 12rpx 20rpx;
				display: inline-block;
				height: 72rpx;
				width: 144rpx;
				line-height: 72rpx;
				text-align: center;
			}

			.current {
				color: #fff;
				background: #8cc7b5;
				//font-size: 32rpx;
			}
		}
	}

	.scroll {
		height: 100%;
	}

	/*幻灯片广告 */
	.swiper {
		height: 350rpx;
		margin: 14rpx 24rpx 0;

		image {
			height: 350rpx;
			width: 100%;
			border-radius: 15rpx;
			//box-shadow: 0 0 12px #dddee1;
		}

		.current {
			width: 98%;
			height: 350rpx;
			margin: 1.0% 1.0%;
			transition: all 0.2s ease-in 0s;
		}

		.item {
			position: relative;

			.title {
				position: absolute;
				left: 0;
				bottom: 0;
				display: flex;
				justify-content: center;
				width: 100%;

				text {
					margin: 7% 6%;
					display: flex;
					align-items: center;
					//background-image: linear-gradient(to right, rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3));
					//background-image: linear-gradient(90deg,rgba(255, 181, 19,.8),rgba(255, 181, 19,.2));
					color: #fff;
					display: -webkit-box;
					text-overflow: ellipsis;
					word-break: break-all;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
					overflow: hidden;
					font-size: 34rpx;
					padding: 14rpx 20rpx;
					max-width: 610rpx;
					line-height: 1.3;
					border-radius: 5rpx;
				}
			}
		}
	}

	/*浮动navbar*/
	.floatbar {
		//border-bottom: 1rpx solid #e8e8e8;
		box-shadow: 0px 2px 2px -2px #e8e8e8;
		padding-bottom: 12rpx;
	}

	/deep/ .no-data {
		margin-top: 200rpx;
	}

	/*搜索*/
	.search {
		padding: 10rpx 24rpx 10rpx 24rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;

		.search-content {
			background: #f2f2f2;
			border-radius: 15px;
			padding: 10rpx 24rpx 10rpx 24rpx;
			margin-right: 15rpx;
			flex: 1;
			display: flex;
			align-items: center;
		}

		/deep/.input {
			color: #8cc7b5;
		}

		.icon {
			width: 30rpx;
			height: 30rpx;
			margin-right: 15rpx;
		}

		.btn {
			color: #b3b3b3;
		}
	}
</style>
