<template>
	<view class="page">
		<view class="example-body" v-if="listCards.length>0" >
			<view v-for="(item,index) in listCards" :key="index" class="example-box" @click="selectDevices(item)">
				<dev-block :title="item.title" :titleNumber="item.titleNumber" 
				:contentList="item.contentList"></dev-block>
			</view>
			<uni-load-more  :loadingType="loadingType" :contentText="contentText" ></uni-load-more>
		</view>
		<view class="example-body" v-else>
			<no-record ></no-record>
		</view>
	</view>
</template>

<script>

import devBlock from '@/components/xinsoft-dev-block/xinsoft-dev-block.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: {
		devBlock,uniLoadMore,noRecord
	},
	data() {
		return {
	        title: 'list-triplex-row',
			listCards: [],
			//上拉刷新，下拉加载
			loadingText: '加载中...',
			loadingType: 0,//定义加载方式 0---contentdown  1---contentrefresh 2---contentnomore
			contentText: {
				contentdown:'上拉显示更多',
				contentrefresh: '正在加载...',
				contentnomore: '没有更多数据了'
			},
			pageSize:5,
			page:1,
			flag:'',
			repairid:''
	    }
	},
	onLoad(option) {		
		//初始化当前列表。
		this.deviceList();
		//从故障报修跳转过来还是从新增维修记录跳转过来
		this.flag = option.flag;
		this.repairid = option.repairid
		// if(this.flag == 'addRepair'){  //故障报修
		// }
		// else if(this.flag == 'repairRecordAddStep1'){  //新增维修记录
		// }
	},
	onPullDownRefresh: function() {
		//下拉刷新的时候请求一次数据
	    this.deviceList();
	},
	onReachBottom: function() {
		//触底的时候请求数据，即为上拉加载更多
		this.deviceListMore();
	},
	methods: {
		deviceList(){
			this.page = 1;
			this.loadingType = 0;		
			this.$http.request({
			  url: '/apis/device/list',
			  params:{
				  pageno:this.page,
				  pagesize:this.pageSize
			  },
			}).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=>{
			});
		},
		deviceListMore(filterRes){
			if (this.loadingType !== 0) {//loadingType!=0;直接返回
				return false;
			}
			this.loadingType = 1;
			uni.showNavigationBarLoading();//显示加载动画
			this.$http.request({
			  url: '/apis/device/list',
			  params:{
				  pageno:this.page,
				  pagesize:this.pageSize
			  },
			}).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=>{
			});
		},
		selectDevices(item){
			var _this = this;
			var devInfo = {
				name:item.myName,
				serialno:item.serialno,
				model:item.contentList[0].labelContent,
				type:item.contentList[1].labelContent,
				site:item.contentList[3].labelContent,
				id:item.id
			}
			
			if(this.flag == 'addRepair'){  //故障报修
				var repairInfo =  uni.getStorageSync('repairInfo');
				if(!repairInfo){
					repairInfo = {}
				}
				repairInfo.devInfo = devInfo;
				uni.setStorage({
				    key: 'repairInfo',
				    data: repairInfo,
				    success: function () {
						uni.navigateTo({
						    url: 'addRepair'
						});
				    }
				});
			}
			else if(this.flag == 'repairRecordAddStep1'){  //新增维修记录
				var addRepairInfo =  uni.getStorageSync('addRepairInfo');
				if(!addRepairInfo){
					addRepairInfo = {}
				}
				addRepairInfo.devInfo = devInfo;
				uni.setStorage({
				    key: 'addRepairInfo',
				    data: addRepairInfo,
				    success: function () {
						uni.navigateTo({
						    url: _this.flag+'?repairid='+(_this.repairid||'')
						});
				    }
				});
			}
		},
		formaterData(data){
			var resData = [];
			data.forEach((vals,index)=>{
				var combineJSON = {
					id: vals.id,
					title: '['+vals.serialno+']'+vals.name,
					contentList: [
						{
							id:1,
							isDouble:true,
							labelName:'规格型号',
							labelContent:vals.model
						},						
						{
							id:2,
							isDouble:true,
							labelName:'设备类型',
							labelContent:vals.type
						},
						{
							id:3,
							isDouble:true,
							labelName:'使用部门',
							labelContent:vals.deptName
						},
						{
							id:3,
							isDouble:false,
							labelName:'安装地点',
							labelContent:vals.site
						}
					],
					serialno:vals.serialno,
					myName:vals.name
				}
				resData.push(combineJSON);
			});
			return resData;
		}
	}
};
</script>