<template>
	<view class="page">
		<view class="mybox" v-if="listCards.length>0">
			<view v-for="(item,index) in listCards" :key="index" class="example-box" style="position: relative;"  @click="clickDetail(item)">
				<repair-block :title="item.title" :titleNumber="item.titleNumber" :note="item.note" 
				:extra="item.extra"  :tagType="item.tagType"
				:contentList="item.contentList"></repair-block>
			</view>
			<uni-load-more  :loadingType="loadingType" :contentText="contentText" ></uni-load-more>
		</view>
		<view class="mybox" v-else>
			<no-record ></no-record>
		</view>
		<view class="goHome" @click='goMain()'>
			<img src="../../static/img/gohome.png">
		</view>
	</view>
</template>

<script>
	import repairBlock from '@/components/xinsoft-dev-repair/xinsoft-dev-repair.vue'
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	import noRecord from '@/components/xinsoft-no-record/xinsoft-no-record';
	
	export default {
		components: {
			repairBlock,uniLoadMore,noRecord
		},
		data() {
			return {
				listCards: [],
				//上拉刷新，下拉加载
				loadingText: '加载中...',
				loadingType: 0,//定义加载方式 0---contentdown  1---contentrefresh 2---contentnomore
				contentText: {
					contentdown:'上拉显示更多',
					contentrefresh: '正在加载...',
					contentnomore: '没有更多数据了'
				},
				pageSize:5,
				page:1
			}
		},
		onLoad() {
			//初始化当前列表。
			this.dataList();
		},
		onPullDownRefresh: function() {
			//下拉刷新的时候请求一次数据
		    this.dataList();
		},
		onReachBottom: function() {
			//触底的时候请求数据，即为上拉加载更多
			this.dataListMore();
		},
		methods: {
			dataList(){
				this.page = 1;
				this.loadingType = 0;		
				var filterRes = {};
				filterRes.pageno = this.page;
				filterRes.pagesize = this.pageSize;
				this.$http.request({
				  url: '/apis/news/list',
				  method:'GET',
				  params:filterRes,
				}).then(res=>{
					if (!res.data.data || res.data.data.length ==0) {//没有数据
						this.listCards= [];
						uni.hideNavigationBarLoading();//关闭加载动画
						return;
					}
					if(res.data.code == 0){
						this.page++;//得到数据之后page+1
						uni.hideNavigationBarLoading();
						uni.stopPullDownRefresh();//得到数据后停止下拉刷新
						if(res.data.data.datas.length>0){
							this.listCards = this.formaterData(res.data.data.datas);
						}
					}
				}).catch(err=>{
					uni.showToast({
						title:'获取信息失败',
						duration: 2000,
						icon:"none"
					});
				});
			},
			dataListMore(){
				if (this.loadingType !== 0) {//loadingType!=0;直接返回
					return false;
				}
				this.loadingType = 1;
				uni.showNavigationBarLoading();//显示加载动画
				var filterRes = {};
				filterRes.pageno = this.page;
				filterRes.pagesize = this.pageSize;
				this.$http.request({
				  url: '/apis/news/list',
				  method:'GET',
				  params:filterRes,
				}).then(res=>{
					if (!res.data.data || res.data.data.length ==0) {//没有数据
						this.loadingType = 2;
						uni.hideNavigationBarLoading();//关闭加载动画
						return;
					}
					if(res.data.code == 0){
						this.page++;//每触底一次 page +1
						this.loadingType = 0;//将loadingType归0重置
						uni.hideNavigationBarLoading();//关闭加载动画
						var resultData = this.formaterData(res.data.data.datas);
						this.listCards = [...this.listCards,...resultData];
					}
				}).catch(err=>{
					uni.showToast({
						title: '获取信息失败',
						duration: 2000,
						icon:"none"
					});
				});
			},
			clickDetail(item){
				//点击详情
				// if(item.status == '148'){
					uni.navigateTo({
					    url: 'newsDetail?id='+item.id
					});
				// }else{
				// 	//再次编辑。
				// 	uni.navigateTo({
				// 	    url: 'addDaily?id='+item.id
				// 	});
				// }
			},
			formaterData(data){
				var resData = [];
				data.forEach((vals,index)=>{
					var combineJSON = {
						id: vals.id,
						title: vals.title,
						contentList: [
							{
								id:1,
								isDouble:false,
								labelName:'创建人',
								labelContent:vals.createbyname
							},						
							{
								id:2,
								isDouble:false,
								labelName:'创建时间',
								labelContent:vals.createttime||''
							}
						],
						note: this.setNotes(vals.status),
						status:vals.status
					}
					resData.push(combineJSON);
				});
				return resData;
			},
			setNotes(status){
				// if(status == '148'){
					return '查看详情'
				// }else{
				// 	return '编辑详情'
				// }
			}
		},
		onBackPress(options) { //取消默认的返回事件.
			uni.switchTab({
				url: "../main/main"
			});
			return true;
		}
	}
</script>

