<template>
	<view class="device-list-box">
		<!-- 菜单 -->
		<view class="device-search">
			<u-dropdown class="sl-filter" border-bottom	>
				<u-dropdown-item v-model="productId" title="所属产品" height="750" :options="productList" @change="searchChange"></u-dropdown-item>
				<u-dropdown-item v-model="deviceType" title="设备类型" :options="deviceTypeList" @change="searchChange"></u-dropdown-item>
			</u-dropdown>
			<u-search placeholder="请输入设备名称/编号搜索" class="uni-search-bar" v-model="searchVal" @custom="searchChange" @clear="searchChange" @search="searchChange"></u-search>
			<u-tabs :list="tabsList" bar-width="187" :is-scroll="false" :current="tabIndex" @change="tabChange"></u-tabs>
		</view>

		<mescroll-body ref="mescrollRef" top="260"  @init="mescrollInit" @down="downCallback" :up="upOption" @up="upCallback">
			<view class="device-box">
				<view class="device-info">
					<view class="name">设备总数: <text style="font-size: 24rpx;">{{total}}</text></view>
				</view>
				<!-- 设备列表 -->
				<view class="device-list">
					<view class="device-item" v-for="(item, index) in dataList" :key="index" @click="goDetail(item)">
						<view class="device-name">
							{{item.devName}}
						</view>
						<view class="device-status">
							<view class="device-status-item">
								<u-icon name="wifi" :color="item.devState==1?'#10CC70':item.devState==2?'#FF2B38':'#999999'"
									size="26"></u-icon>
								<text
									:style="{marginLeft:'10rpx',color:item.devState==1?'#10CC70':item.devState==2?'#FF2B38':'#999999'}">{{item.devState==1?'在线':item.devState==2?'离线':'未激活'}}</text>
							</view>
							<!-- <view class="device-status-item notice-tag">
								<u-icon name="warning" color="#F6A582" size="26"></u-icon>
								<text
									:style="{marginLeft:'10rpx',color:'#F6A582'}">告警未处理</text>
							</view> -->
						</view>
						<view class="device-img-box">
							<image v-if="item.devImage" :src="imgPath + item.devImage" mode=""></image>
							<!-- #ifdef MP-WEIXIN -->
							<image v-else src="https://file.iot-fast.com/wxapp/static/image/device/device-default.png" mode=""></image>
							<!-- #endif -->
							<!-- #ifndef MP-WEIXIN -->
							<image v-else src="/static/app-plus/image/device/device-default.png" mode=""></image>
							<!-- #endif -->
						</view>
					</view>
				</view>
			</view>
		</mescroll-body>
		<u-tabbar class="tabbar-box" v-model="tabbarIndex" active-color="#1890ff" :list="tabbarList" @change="tabbarChange"></u-tabbar>
	</view>
</template>

<script>
	import IotsTabbarMixin from "../iotsTabbarMixins.js"
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js"
	export default {
		mixins: [IotsTabbarMixin,MescrollMixin],
		components: {
		},
		data() {
			return {
				upOption:{
					noMoreSize: 4,
					textNoMore: '----  已经到底啦  ----',
					empty:{
						tip: '~ 搜索无数据 ~' // 提示
					}
				},
				tabbarIndex:1,
				productId:null,
				deviceType:null,
				searchVal: '',
				tabIndex: 0, // tab下标

				productList:[],
				deviceTypeList:[
					{
						label: '全部设备',
						value: null,
					},
					{
						label: '直连设备',
						value: 'dev',
					},
					{
						label: '网关子设备',
						value: 'childrenDevice',
					},
					{
						label: '网关设备',
						value: 'gateway',
					}
				],

				tabsList:[{
					name: '全部',
					value: null
				}, {
					name: '未激活',
					value: 0
				}, {
					name: '在线',
					value: 1
				}, {
					name: '离线',
					value: 2
				}],
				dataList: [],
				total: 0,
				imgPath:'',
			}
		},
		onLoad() {
			this.getConfigPath();
			this.getProductList();
		},
		methods: {
			goDetail(obj){
				// console.log("跳转设备详情",obj)
				uni.navigateTo({
					url:'./device-detail?id=' + obj.id + '&devName=' + obj.devName
				})
			},
			getConfigPath(){
				let configList = uni.getStorageSync('configList');
				let configIndex = uni.getStorageSync('configIndex');
				this.imgPath = configList[configIndex].protocol + configList[configIndex].address;
			},
			getDeviveTypeName(e){
				if(e == 'dev'){
					return '直连设备'
				}else if(e == 'childrenDevice'){
					return '网关子设备'
				}else if(e == 'gateway'){
					return '网关设备'
				}else{
					return '-'
				}
			},
			// 切换菜单
			tabChange(e) {
				this.tabIndex = e;
				this.mescroll.resetUpScroll() // 再刷新列表数据
			},
			searchChange(e) {
				// this.searchVal = res.value
				this.dataList = []// 先置空列表,显示加载进度
				this.mescroll.resetUpScroll() // 再刷新列表数据
			},
			getProductList(){
				this.$api.iotsApi.getProductList({page:1,pageSize:999}).then(res => {
					if(res.code == 0 && res.data.list.length>0){
						let productList = res.data.list.map((item)=>{
							return {
								label:item.prodName,
								value:item.pk
							}
						})
						this.productList = [{
							label: '全部产品',
							value: null,
						},...productList];
					}else{
						this.productList = [{
							label: '全部产品',
							value: null,
						}];
					}
				}, error => {
				})
			},
			upCallback(page) {
				let params = {
					"pk": this.productId,
					"page": page.num,
					"pageSize": page.size,
					"Wheres": {
							"wheres": [
									{"field": "prodType","operator": "=","value": this.deviceType},
									{"field":"devState","operator":"=","value":this.tabsList[this.tabIndex].value},
									{"fields": ["devId","devName"],"operator": "keyword","value": this.searchVal}
							]
					},
				}
				this.$api.iotsApi.getDeviceList(params).then(res => {
					if(res.code === 0){
						let curPageData = res.data && res.data.list;
						let curPageLen = curPageData && curPageData.length || 0;
						let totalSize = res.data.totalCount;
						this.$nextTick(()=>{
							// 隐藏下拉刷新和上拉加载的状态;
							this.mescroll.endBySize(curPageLen, totalSize);
						})
						//设置列表数据
						if(page.num == 1) this.dataList = []; //如果是第一页需手动制空列表
						this.dataList = curPageLen > 0 &&  this.dataList.concat(curPageData); //追加新数据
						this.total = totalSize;
					}else{
						this.$u.toast(res.msg);
						this.mescroll.endErr();
					}
				}, error => {
				})
			}
		}
	}
</script>

<style>
	page{
		background-color: #f5f5f5;
	}
</style>

<style lang="scss" scoped>
	/deep/.u-search{
		padding: 19rpx 20rpx;
		border-bottom: 1px solid #f4f6f8;
	}
	.device-list-box{
		width: 100%;
		height: 100%;
		background-color: #f5f5f5;
		.device-search{
			position: fixed;
			top: 0;
			left: 0;
			box-sizing: border-box;
			height: 260rpx;
			background-color: #fff;
			width: 100%;
			z-index: 999;
		}

		.device-box{
			.device-info{
				padding: 20rpx 20rpx;
				padding-bottom: 0;
				.name{
					font-size: 28rpx;
					font-weight: bold;
					color: #333333;
				}
			}
			.device-list {
				display: flex;
				flex-wrap: wrap;
				padding: 20rpx;
				// background-color: #f5f5f5;
				.device-item {
					width: 345rpx;
					// height: 300rpx;
					background: #fff;
					border-radius: 20rpx;
					margin-bottom: 20rpx;
					font-weight: bold;
					padding: 20rpx;
					box-sizing: border-box;
			
					// overflow: hidden;
					&:nth-child(2n-1) {
						margin-right: 20rpx;
					}
			
					.device-name {
						font-size: 28rpx;
						line-height: 28rpx;
						display: inline-block;
						width: 310rpx;
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
					}
					.device-status{
						display: flex;
						align-items: center;
						margin-bottom: 10rpx;
						font-size: 22rpx;
						line-height: 22rpx;
						.device-status-item{
							display: flex;
							align-items: center;
							&.notice-tag{
								margin-left:auto;
							}
						}
					}
					.device-img-box {
						margin-left: 10rpx;
						width: 120rpx;
						height: 120rpx;
						// background: #f5f5f5;
						// display: flex;
						// justify-content: center;
						// align-items: center;
						image{
							width: 100%;
							height: 100%;
						}
						
					}
				}
			}
			// .device-list{
			// 	padding: 0 20rpx;
			// 	box-sizing: border-box;

			// 	.device-item{
			// 		width: 100%;
			// 		background-color: #fff;
			// 		box-shadow: 0px 0px 10px 0px rgba(192,192,192,0.5);
			// 		border-radius: 12rpx;
			// 		// padding: 20rpx 40rpx;
			// 		box-sizing: border-box;
			// 		margin-top: 20rpx;
			// 		.device-item-top{
			// 			display: flex;
			// 			align-items: center;
			// 			justify-content: space-between;
			// 			font-size: 28rpx;
			// 			color: #333333;
			// 			padding: 20rpx 30rpx;
			// 			// padding-bottom: 20rpx;
			// 			box-sizing: border-box;
			// 			border-bottom: 1rpx solid #f1f1f1;
			// 			.device-id{
			// 				flex: 1;
			// 				overflow: hidden;
			// 				text-overflow: ellipsis;
			// 				word-break: break-all;
			// 			}
			// 		}
			// 		.device-content{
			// 			display: flex;
			// 			justify-content: space-between;
			// 			align-items: center;
			// 			padding:20rpx 30rpx;
			// 			.device-content-left{
			// 				.device-centent-center{
			// 					display: flex;
			// 					align-items: center;
			// 					.device-img-box{
			// 						// padding: 20rpx;
			// 						margin-right: 20rpx;
			// 						width: 120rpx;
			// 						height: 120rpx;
			// 						// background: #f5f5f5;
			// 						display: flex;
			// 						justify-content: center;
			// 						align-items: center;
			// 						image{
			// 							width: 100%;
			// 							height: 100%;
			// 						}
			// 					}
			// 					.device-name{
			// 						font-size: 30rpx;
			// 						font-weight: bold;
			// 					}
			// 				}
			// 				.detail-centent-bottom{
			// 					.device-item-row{
			// 						display: flex;
			// 						// align-items: center;
			// 						line-height: 50rpx;
			// 						font-size: 28rpx;
			// 						color: #333333;
			// 						margin-top: 10rpx;
			// 						.name{
			// 							width: 140rpx;
			// 						}
			// 						.val{
			// 							flex: 1;
			// 							overflow: hidden;
			// 							text-overflow: ellipsis;
			// 							word-break: break-all;
			// 						}
			// 					}
			// 				}
			// 			}
			// 			.device-content-right{
			// 				font-size: 28rpx;
			// 				font-weight: bold;
			// 			}
			// 		}
			// 	}
			// }
		}
	}
</style>
