<template>
	<view class="pages">
		<!-- #ifndef H5 -->
		<statusBar></statusBar>
		<!-- #endif -->
		<!-- 搜索功能 -->
		
		<uni-search-bar @click="searchClick" class="uni-search-box" v-model="keyword" ref="searchBar" radius="100"
				cancelButton="none" disabled />
		<view class="cover-search-bar" @click="searchClick"></view>
		
		
		<unicloud-db ref='udb' v-slot:default="{data,pagination,hasMore, loading, error, options}" @error="onqueryerror"
			:where="where" collection="opendb-news-articles,uni-id-users" :page-size="10"
			field="avatar,title,last_modify_date,user_id.username">
			<!-- 基于 uni-list 的页面布局 -->
			<uni-list class="uni-list" :border="false" :bounce="true" :alwaysScrollableVertical="true"
				:style="{height:listHight}">
				<uni-list-item :to="'/pages/list/detail?id='+item._id+'&title='+item.title" v-for="(item,index) in data"
					:key="index">
					<!-- 通过header插槽定义列表左侧图片 -->
					<template v-slot:header>
						<image class="avatar" :src="item.avatar" mode="aspectFill"></image>
					</template>
					<!-- 通过body插槽定义布局 -->
					<template v-slot:body>
						<view class="main">
							<text class="title">{{item.title}}</text>
							<view class="info">
								<text class="author">{{item.user_id[0].username}}</text>
								<uni-dateformat class="last_modify_date" :date="item.last_modify_date" format="yyyy-MM-dd"
									:threshold="[60000, 2592000000]" />
							</view>
						</view>
					</template>
				</uni-list-item>
				<uni-list-item>
					<template v-slot:body>
						<uni-load-state @networkResume="refresh" :state="{data,pagination,hasMore, loading, error}">
						</uni-load-state>
					</template>
				</uni-list-item>
			</uni-list>
		</unicloud-db>
	</view>
</template>

<script>
	var cdbRef, currentWebview;
	import statusBar from "@/uni_modules/uni-nav-bar/components/uni-nav-bar/uni-status-bar";
	
	import Gps from '@/uni_modules/json-gps/js_sdk/gps.js';
	const gps = new Gps()
	
	export default {
		components: {
			statusBar
		},
		data() {
			return {
				where: "",
				keyword: "",
				refreshState: 0,
				listHight: 0
			}
		},
		watch: {
			keyword(keyword, oldValue) {
				if (keyword) {
					this.where = `/${keyword}/.test(title)`;
				} else {
					this.where = '';
				}
			}
		},
		async onReady() {
			// #ifdef APP-NVUE
			this.listHight = uni.getSystemInfoSync().windowHeight - 96 + 'px'
			// #endif
			// #ifndef APP-NVUE
			this.listHight = 'auto'
			// #endif
			cdbRef = this.$refs.udb
		},
		async onShow() {
			this.keyword = getApp().globalData.searchText
			getApp().globalData.searchText = ''
			//这里仅演示如何，在onShow生命周期获取设备位置，并在设备或者应用没有权限时自动引导。设置完毕自动重新获取。
			//你可以基于他做自己的业务，比如：根据距离由近到远排序列表数据等
			// uni.showLoading({
			// 	title:"获取定位中"
			// });
			let location = await gps.getLocation({geocode:true})
			console.log(location);
			// if(location){
			// 	uni.showToast({
			// 		title: JSON.stringify(location),
			// 		icon: 'none'
			// 	});
			// }
			// uni.hideLoading()
		},
		methods: {
			searchClick(e) { //点击搜索框
				uni.hideKeyboard();
				uni.navigateTo({
					url: '/pages/list/search/search',
					animationType: 'fade-in'
				});
			},
			retry() {
				this.refresh()
			},
			refresh() {
				console.log('----platform----');
				console.log(JSON.stringify(process.env.VUE_APP_PLATFORM));
				cdbRef.loadData({
					clear: true
				}, () => {
					uni.stopPullDownRefresh()
				})
				console.log('refresh');
			},
			onqueryerror(e){
				console.log(e);
			}
		},
		onPullDownRefresh() {
			this.refresh()
		},
		onReachBottom() {
			cdbRef.loadMore({
				clear: true
			})
		}
	}
</script>

<style scoped>
	/* #ifndef APP-NVUE */
	view {
		display: flex;
		box-sizing: border-box;
		flex-direction: column;
	}
	/* #endif */
	.pages {
		background-color: #FFFFFF;
	}

	.avatar {
		width: 200rpx;
		height: 200rpx;
		margin-right: 10rpx;
	}

	.main {
		justify-content: space-between;
	}

	.title {
		width: 480rpx;
		font-size: 32rpx;
	}

	.info {
		flex-direction: row;
		justify-content: space-between;
	}

	.author,
	.last_modify_date {
		font-size: 28rpx;
		color: #999999;
	}

	.refresh-tip {
		color: #67c23a;
		font-size: 14px;
		line-height: 40px;
		text-align: center;
		background-color: #f0f9eb;
		height: 0;
		opacity: 0;
		transform: translateY(-100%);
		transition: height 0.3s;
	}

	.uni-search-box {
		background-color: #FFFFFF;
		position: sticky;
		top: 0;
		left: 0;
		/* #ifndef APP-PLUS */
		z-index: 9;
		/* #endif */
		/* #ifdef MP-WEIXIN */
		width: 580rpx;
		/* #endif */
	}

	.show-refresh-tip {
		transform: translateY(0);
		height: 40px;
		opacity: 1;
	}

	.get-data-state {
		width: 750rpx;
		align-items: center;
	}

	.get-data-state-img {
		width: 500rpx;
	}

	.get-data-state-text {
		width: 32rpx;
		color: #999999;
		line-height: 50rpx;
		height: 50rpx;
		width: 750rpx;
		text-align: center;
	}

	.uni-list {}

	.f1 {
		flex: 1;
	}
	.cover-search-bar{
		height: 50px;
		position: relative;
		top: -50px;
		margin-bottom: -50px;
		/* #ifndef APP-NVUE */
		z-index:999;
		/* #endif */
	}
</style>