<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="base-info">
			<view class="park-num u-flex u-row-between">
				<view class="">
					停车情况:
					<text v-if="parkNum.total&&parkNum.remaeNum">已停{{parkNum.total-parkNum.remaeNum}}个;</text>
					<text v-if="parkNum.remaeNum">空余{{parkNum.remaeNum}}个</text>
				</view>
				<!-- <view class="">异常上报</view> -->
			</view>
			<view class="u-flex u-row-between">
				<view class="">
					道闸控制:
					<text @click="deviceSelectShow=true;deviceSelectConfirmType='close';deviceSelectTitle='关闸'" style="color: #5198ff;" class="u-m-r-30">关闸</text>
					<text @click="deviceSelectShow=true;deviceSelectConfirmType='open';deviceSelectTitle='开闸'" style="color: #dba80f;">开闸</text>
					<!-- <u-button type="success" size="mini">关闸</u-button>
					<u-button type="success" size="mini" :plain="true">开闸</u-button> -->
				</view>
				
				<!-- <text @click="goDeviceList" style="color: #0082FF;">
					出入口信息
					<u-icon name="arrow-right" color="#0082FF" size="32" style="vertical-align: middle;"></u-icon>
				</text> -->
				<text @click="deviceSelectShow=true;deviceSelectConfirmType='report';deviceSelectTitle='异常上报'" style="color: #6b79ff;">异常上报</text>
			</view>
		</view>
		
		<mescroll-uni ref="mescrollRef01" @init="mescrollInit" :top="config.platform=='h5'?350:450" :down="downOption" @down="downCallback" :up="upOption" @up="upCallback" @emptyclick="emptyClick">
			<view class="device-list u-flex u-flex-wrap u-row-start" v-if="dataList.length>1">
				<view class="device-list-item u-m-b-24" v-for="item in dataList" :key="item.id">
					<view class="top u-text-center">
						<view class="space-name">
							<text>{{item.vehicleNo}}</text>
							<image v-if="item.vehicleNoStatus" class="arrearage-img" src="../../static/img/arrearage.png" mode="heightFix"></image>
						</view>
						<view class="signal">
							<text>{{item.roadwayName}}</text>
						</view>
					</view>
					<view class="carImg-wrap">
						<image class="carImg" :src="item.inImage||'../../static/img/default-car.png'" mode="scaleToFill"></image>						
					</view>
					<view class="time">入场：{{item.inTime}}</view>
					<view class="time">时长：{{item.parkingTime}}</view>
				</view>
			</view>
			<view v-else class="empty">
				<image src="../../static/img/empty.png" mode="widthFix"></image>
				<view class="text">该停车场还没有车辆!</view>
			</view>
		</mescroll-uni>
		<u-select 
			:title="deviceSelectTitle"
			v-model="deviceSelectShow" 
			:list="parkDeviceList" 
			value-name="roadwayNo" 
			label-name="roadwayName" 
			@confirm="deviceSelectConfirm">
		</u-select>
	</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:"停车场",
				downOption:{
					auto:false // 不自动加载 (mixin已处理第一个tab触发downCallback)
				},
				upOption:{
					page: {
						size: 10 // 每页数据的数量
					},
					auto:true,
					// use:false,
					noMoreSize: 4, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
					empty:{
						tip: '~ 暂无数据 ~', // 提示
						// btnText: '去看看'
					}
				},
				dataList:[],
				parkNum:{
					remaeNum:null,
					total:null
				},
				roadSelectShow:false,
				roadListData:[],
				parkDeviceList:[],
				deviceSelectShow:false,
				deviceSelectConfirmType:'',
				deviceSelectTitle:'',
			}
		},
		onLoad(page){
			console.log('page',page);
			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,
					}
				})
			},
			goDeviceList(){
				this.$u.route({
				  url: 'pages/deviceList/deviceListType3/deviceListType3',
					params:{
						roadNo:this.roadNo,
						roadName:this.roadName,
						deviceType:this.deviceType,
					}
				})
			},
			/*上拉加载的回调: 其中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.selectParkVehicleList(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( );
			},
			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();
				})
			},
			async getParkDeviceList(){
				let params ={
					roadNo:this.roadNo,
					deviceType:this.deviceType,//设备类型: 1-地磁 2-车位锁
					pageNum:1,
					pageSize:100
				};
				let {code,data} = await this.$u.api.selectParkDeviceList(params);
				this.parkDeviceList = data.rows
				// this.parkDeviceList = data.rows.map(item=>{
				// 	return {
				// 		label: item.roadwayName,
				// 		value: item.roadwayNo
				// 	}
				// });
				// console.log('this.parkDeviceList',this.parkDeviceList);
			},
			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();
					this.getParkDeviceList()
					// 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();
				this.getParkDeviceList()
			},
			deviceSelectConfirm(e){
				console.log('deviceSelectConfirm',e);
				console.log('deviceSelectConfirmType',this.deviceSelectConfirmType);
				if(this.deviceSelectConfirmType=='close'||this.deviceSelectConfirmType=='open'){
					let param = {
						roadwayNo:e[0].value,
						command:this.deviceSelectConfirmType=='close'?0:1
					}
					this.$u.api.controlDevice(param).then(res=>{
						uni.showToast({
							icon:'none',
							title:res.msg
						})
					}).catch(()=>{
						console.log('catch');
						//联网失败, 结束加载
						this.mescroll.endErr();
					})
				}else if(this.deviceSelectConfirmType=='report'){
					let selectItem = this.parkDeviceList.filter(item => item.roadwayNo== e[0].value);
					this.$u.route({
					  url: 'pages/report/report',
						params:{
							id:selectItem[0].id,
							deviceId:selectItem[0].deviceId,
							roadNo:selectItem[0].roadNo,
							roadName:selectItem[0].roadName,
							deviceType:selectItem[0].deviceType,
						}
					})
				}
			},
			
		}
	}
</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: 24rpx 15rpx;
		border-radius: 13rpx;
		.top{
			margin-bottom: 20rpx;
			.space-name{
				color: #212121;
				font-size: 30rpx;
				line-height: 42rpx;
				.arrearage-img{
					width: 35rpx;
					height: 35rpx;
					vertical-align: middle;
					margin-left: 8rpx;
				}
			}
			.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-wrap{
			height: 229rpx;
			width: 100%;
			border-radius: 15rpx;
			overflow: hidden;
			margin: 16rpx auto 18rpx;
			text-align: center;
			.carImg{
				width: 100%;
				height: 100%;
				// display: block;
			}
		}
		.parkingTime{
			height: 37rpx;
			font-size: 26rpx;
			font-weight: 400;
			color: #5B5B5B;
			line-height: 37rpx;
			margin-bottom: 15rpx;
			text-align: center;
		}
		.time{
			text-align: center;
			color: #6a6a6a;
			font-size: 14rpx;
			margin-top: 8rpx;
		}
	}
	
}
.base-info{
	background-color: #fff;
	padding: 30rpx;
	.park-num{
		text{color: #767676;margin-left: 8rpx;}
		padding-bottom: 20rpx;
	}
}
.empty{
	padding-top: 20vh;
	text-align: center;
	image{
		width: 344rpx;
	}
	.text{
		margin-top: 60rpx;
		font-weight: 400;
		font-size: 40rpx;
	}
}
</style>
