<template>
	<view>
		<u-navbar
		 :title="title" 
		 title-color="#fff" 
		 :is-back="true"
		 :custom-back="customBack" 
		 :border-bottom="false" 
		 back-icon-color="#CCE8FF" 
		 :background="{background: 'linear-gradient(99deg, #7A4398 0%, #5A5DB9 100%)' }"></u-navbar>
		 <u-cell-item class="road-select-cell" icon="map-fill" bg-color="#fff" :border-bottom="false" :title="roadName" :arrow="true" @click="roadSelectShow = true" arrow-direction="right"></u-cell-item>
		 <u-select v-model="roadSelectShow" :list="roadListData" value-name="roadNo" label-name="roadName" @confirm="roadSelectConfirm"></u-select>
		 <u-gap height="12" bg-color="#e7e7e7"></u-gap>
		<!-- <u-tabs :list="tabList" :is-scroll="false" height="108" bar-width="100" :current="tabCurrent" @change="tabChange"></u-tabs> -->
		<!-- <u-gap height="20" bg-color="#F6F6F6"></u-gap> -->
		<view class="park-num u-flex">
			<view class="park-num-item">
				已停车位：<text>{{(parkNum.total-parkNum.remaeNum)||'-'}}个</text>
			</view>
			<view class="park-num-item">
				空余车位：<text>{{parkNum.remaeNum||'-'}}个</text>
			</view>
		</view>
		<mescroll-uni ref="mescrollRef01" @init="mescrollInit" :top="config.platform=='h5'?300:400" :down="downOption" @down="downCallback" :up="upOption" @up="upCallback" @emptyclick="emptyClick">
			<view class="device-list u-flex u-flex-wrap u-row-start">
				<view class="device-list-item u-m-b-24" v-for="item in dataList" :key="item.id"
				@click="goDetails(item.id)">
					<view class="top u-text-center">
						<view class="space-name">{{item.entranceName}}</view>
						<view class="signal">
							<text>{{item.roadwayName}}</text>
						</view>
					</view>
					<image class="carImg" src="../../../static/img/daozha-y.png" mode="heightFix"></image>
					<!-- <view class="deviceNo">{{item.deviceNo}}</view> -->
				</view>
			</view>
		</mescroll-uni>
	</view>
</template>

<script>
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default{
		mixins: [MescrollMixin], // 使用mixin (在main.js注册全局组件)
		components:{
			
		},
		data(){
			return{
				roadName:'全部停车场',
				roadNo:'',
				title:"设备列表",
				tabCurrent:0,
				breakStatus:'',
				tabList:[
					{name:'全部',status:''},
					{name:'异常',status:'0'},
					{name:'低电量',status:'3200_b'},
					{name:'低信号',status:'-100_s'}
				],
				downOption:{
					auto:false // 不自动加载 (mixin已处理第一个tab触发downCallback)
				},
				upOption:{
					page: {
						size: 10 // 每页数据的数量
					},
					auto:true,
					// use:false,
					noMoreSize: 4, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
					empty:{
						tip: '~ 暂无数据 ~', // 提示
						// btnText: '去看看'
					}
				},
				dataList:[],
				lowBattery:'',
				lowSignal:'',
				parkNum:{},
				roadSelectShow:false,
				roadListData:[]
				
			}
		},
		onLoad(page){
			console.log('page',page);
			this.getGeoDeviceStatust()
			if(page.roadName){
				this.roadName = page.roadName;
				this.title = page.roadName;
			}
			if(page.roadNo){
				this.roadNo = page.roadNo
			}
			if(page.deviceType){
				this.deviceType = page.deviceType
			}
			this.getRoadList()
		},
		onShow(){
			// this.selectParkNum();
		},
		methods:{
			customBack(){
				let canNavBack = getCurrentPages();
				 if(canNavBack && canNavBack.length>1) {  
				   uni.navigateBack({  
					 delta: 1  
				   });  
				 } else {  
				   history.back();  
				 }
			},
			openPage(path,id) {
			  this.$u.route({
			    url: path,
				params:{
					id:id
				}
			  })
			},
			goDetails(id){
				this.$u.route({
				  url: 'pages/deviceList/deviceListType3/deviceInfo',
					params:{
						id:id,
						roadNo:this.roadNo,
						roadName:this.roadName,
						deviceType:this.deviceType,
					}
				})
			},
			// 切换菜单
			tabChange(index) {
				this.tabCurrent = index;
				this.breakStatus = this.tabList[index].status;
				this.refresh()
			},
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			upCallback(page) {
				// console.log('page',page);
				let params ={
					roadNo:this.roadNo,
					deviceType:this.deviceType,//设备类型: 1-地磁 2-车位锁
					breakStatus:this.breakStatus,//设备状态：低电量 （3200_b ）、 低信号（ -100_s）、有故障（0）
					pageNum:page.num,
					pageSize:page.size
				};
				this.$u.api.selectParkDeviceList(params).then(curPageData=>{
					// console.log('word',word);
					 console.log('curPageData',curPageData);
					//联网成功的回调,隐藏下拉刷新和上拉加载的状态;
					this.mescroll.endSuccess(curPageData.data.total);
					this.mescroll.endBySize(curPageData.data.rows.length, curPageData.data.total);
					//设置列表数据
					if(page.num == 1) this.dataList = []; //如果是第一页需手动制空列表
					this.dataList=this.dataList.concat(curPageData.data.rows); //追加新数据
					console.log('this.dataList',this.dataList);
					this.selectParkNum();
				}).catch(()=>{
					console.log('catch');
					//联网失败, 结束加载
					this.mescroll.endErr();
				})
			},
			//点击空布局按钮的回调
			emptyClick(){
				uni.showToast({
					title:'点击了按钮,具体逻辑自行实现'
				})
			},
			refresh(){
				this.mescroll.resetUpScroll( );
			},
			getGeoDeviceStatust(){
				this.$u.api.geoDeviceStatust().then(res=>{
					let that = this;
					res.data.data.forEach(function(item){
						if(item.dictLabel=='低电量'){that.lowBattery = Number( item.dictValue.split('_')[0] )}
						if(item.dictLabel=='低信号'){that.lowSignal = Number( item.dictValue.split('_')[0] )}
					});
					console.log('this.lowBattery',this.lowBattery);
				}).catch(()=>{
					console.log('catch');
					//联网失败, 结束加载
					this.mescroll.endErr();
				})
			},
			selectParkNum(){
				if(!this.roadNo){return}
				this.$u.api.selectParkNum({roadNo:this.roadNo}).then(res=>{
					console.log('selectParkNum',res);
					this.parkNum = res.data;
				}).catch(()=>{
					console.log('catch');
					//联网失败, 结束加载
					this.mescroll.endErr();
				})
			},
			getRoadList(){
				let that = this;
				that.$u.api.getRoadList()
				.then(res=>{
					this.roadListData = res.data.filter(item => item.type== 3);
					console.log('this.roadListData111',this.roadListData);
					this.roadName = this.roadListData[0].roadName;
					this.roadNo = this.roadListData[0].roadNo;
					this.mescroll.resetUpScroll();
					// console.log('this.getRoadList',res)
				}).catch(err=>{
					this.$refs.uToast.show({
						title: err.msg,
						type: 'error',
					});
				});
			},
			roadSelectConfirm(e){
				// console.log('roadSelectConfirm',e[0]);
				this.roadName = e[0].label;
				this.roadNo = e[0].value;
				console.log('this.roadNo',this.roadNo);
				this.mescroll.resetUpScroll();
			},
			
		}
	}
</script>
<style>
	page{background: #F3F3F3;}
</style>
<style lang="scss" scoped>
.device-list{
	margin: 24rpx 0;
	.device-list-item{
		background-color: #fff;
		width: 45%;
		margin: 0 0 24rpx 3.3%;
		padding: 15rpx;
		border-radius: 13rpx;
		.top{
			margin-bottom: 20rpx;
			.space-name{
				color: #5B5B5B;
				font-size: 26rpx;
			}
			.signal{
				font-size: 26rpx;
				color: #5B5B5B;
				.img{
					display: inline-block;
					height: 26rpx;
					margin-left: 7rpx;
				}
			}
			.low{color: #f00;}
		}
		.vehicleNo{
			height: 42rpx;
			font-size: 30rpx;
			font-weight: 400;
			color: #212121;
			line-height: 42rpx;
			text-align: center;
		}
		.carImg{
			height: 118rpx;
			display: block;
			margin: 16rpx auto 18rpx;
		}
		.parkingTime{
			height: 37rpx;
			font-size: 26rpx;
			font-weight: 400;
			color: #5B5B5B;
			line-height: 37rpx;
			margin-bottom: 15rpx;
			text-align: center;
		}
		.deviceNo{text-align: center;}
	}
	
}
.park-num{
	background-color: #fff;
	padding: 30rpx;
	.park-num-item{
		&+ .park-num-item{margin-left: 34rpx;}
		text{color: #0082FF;}
	}
}
</style>
