<template>
	<view class="page">
		<u-navbar title="设备管理" :border-bottom="false" :background="bgc" back-icon-color="#262B37" title-color='#262B37'
			title-size='36' height='40' id="navbar" :custom-back="btns">
		</u-navbar>
		<view class="serch">
			<view class="lt">
				<image src="https://api.ccttiot.com/smartmeter/img/static/uaG7R4JLfYOzBhWfDN0j" mode=""></image><input type="text" v-model="keyword" placeholder="搜索设备编号或门店名称"/> <view class="sousuo" @click="btnsousuo">搜索</view>
			</view>
			<view class="rt" @click="btnsbs">
				<image src="https://api.ccttiot.com/smartmeter/img/static/uRxcU7Ae5GZRoHaYyoPr" mode=""></image>
			</view>
		</view>
        <view class="shaixuan">
        	<view class="lt" @click="tfshow = true">
        		{{sbtf}} <image src="https://api.ccttiot.com/smartmeter/img/static/uhbBClU5P2TBMcIan9VV" mode=""></image>
        	</view>
			<view class="rt" @click="ztshow = true">
				{{sbzx}} <image src="https://api.ccttiot.com/smartmeter/img/static/uhbBClU5P2TBMcIan9VV" mode=""></image>
			</view>
        </view>
		<view class="list" 
		  @touchstart="onTouchStart"
		  @touchend="onTouchEnd">
			<view class="list_item" v-for="(item,index) in list" :key="index" @click="btnedit(item.deviceId,item.sn)">
				<view class="top">
					<view class="sn">SN:{{item.sn}}</view>
					<view class="zt" style="color: #48893B;" v-if="item.onlineStatus == 1">
						<text></text>在线
					</view>
					<view class="zt" style="color: #808080;" v-else>
						<text style="background-color: #808080;"></text>离线
					</view>
				</view>
				<view class="cen">
					<image :src="item.picture" mode=""></image>
					<view class="sbxx">
						<view class="">
							门店名称：{{item.storeName == null ? '--' : item.storeName}}
						</view>
						<view class="">
							设备名称：{{item.deviceName == null ? '--' : item.deviceName}}
						</view>
						<view class="">
							设备状态：<text style="color: #48893B;" v-if="item.placementStatus == 1">已投放</text>
							 <text style="color: #F39B18;" v-if="item.placementStatus == 0">待投放</text>
						</view>
						<view class="" style="margin-bottom: 0;">
							使用状态：
							<text style="color: #48893B;" v-if="item.status == 1">空闲</text>
							<text style="color: #FF1818;" v-if="item.status == 2">使用中</text>
							<text style="color: #FF1818;" v-if="item.status == 3">维护中</text>
						</view>
					</view>
				</view>
				<view class="sbm" @click.stop="btnewm(item.deviceId)">
					<text></text>
					<view class="">
						设备码
					</view>
				</view>
			</view>
			<view class="" style="width: 100%;text-align: center;margin-top: 100rpx;font-size: 34rpx;color: #ccc;">
				没有更多设备啦...
			</view>
		</view>
        <u-select v-model="tfshow" :list="toufanglist" @confirm="confirmtf"></u-select>
		<u-select v-model="ztshow" :list="zhuangtailist" @confirm="confirmzx"></u-select>
	</view>
</template>

<script>
	export default {
		data() {
			return {
                bgc: {
                	backgroundColor: "#fff",
                },
				list:[],
				tfshow: false,
				ztshow:false,
				toufanglist: [
					{
						value: '',
						label: '全部'
					},
					{
						value: '0',
						label: '仓库中'
					},
					{
						value: '1',
						label: '空闲'
					},
					{
						value: '2',
						label: '使用中'
					},
					{
						value: '3',
						label: '调试中'
					}
				],
				zhuangtailist: [
					{
						value: '',
						label: '全部'
					},
					{
						value: '1',
						label: '在线'
					},
					{
						value: '0',
						label: '离线'
					}
				],
				sbtf:'设备状态',
				sbzx:'在线状态',
				status:'',
				keyword:'',
				pagesum:1,
				pagesize:10,
				touchStartX: 0, // 触屏起始点x
				touchStartY: 0, // 触屏起始点y  
				xiaflag:true,
				total:'',
				qingqiuflag:true,
			}
		},
		onLoad() {
            
		},
		onShow() {
			this.sbtf ='设备状态',
			this.sbzx ='在线状态',
			this.status ='',
			this.keyword ='',
			this.pagesum = 1
			this.list  = []
			this.getsb()
		},
		methods: {
			// 自定义返回
			btns(){
			   uni.reLaunch({
			   	url:'/page_shanghu/index'
			   })
			},
			// 点击跳转到二维码
			btnewm(sn){
				uni.navigateTo({
					url:'/page_shanghu/erwm?deviceId=' + deviceId
				})
			},
			// 点击进行搜索
			btnsousuo(){
				this.pagesum = 1
				this.list = []
				this.getsb()
			},
			// 点击跳转到设备信息页
			btnedit(deviceId,sn){
				uni.navigateTo({
					url:'/page_shanghu/shebeixq?deviceId=' + deviceId + '&sn=' + sn
				})
			},
			// 确定选择投放状态
			confirmtf(e){
				this.sbtf = e[0].label
				this.status = e[0].value
				this.pagesum = 1
                this.list = []
				this.getsb()
			},
			// 确定选择在线状态
			confirmzx(e){
				this.sbzx = e[0].label
				this.status = e[0].value
				this.pagesum = 1
				this.list = []
				this.getsb()
			},
			// 请求所有设备列表
			getsb(){
				this.$u.get(`/app/device/list?status=${this.status}&pageNum=${this.pagesum}&pageSize=${this.pagesize}&keyword=${this.keyword}`).then(res => {
					if (res.code == 200) {
						this.xiaflag = true
						this.total = res.total
						if (this.pagesum > 1) {
							this.pagesum++
							this.list = this.list.concat(res.rows)
							this.qingqiuflag = true
						} else {
							this.pagesum++
							this.list = res.rows  
							this.qingqiuflag = true
						} 
					}
				})
			},
			// 按下滑动
			onTouchStart(e) {
			    this.imgflag = false
				this.touchStartX = e.touches[0].clientX;
				this.touchStartY = e.touches[0].clientY;
			},
			// 松开滑动
			onTouchEnd(e) {
			  this.imgflag = true
			  let deltaX = e.changedTouches[0].clientX - this.touchStartX;
			  let deltaY = e.changedTouches[0].clientY - this.touchStartY;
			  if (Math.abs(deltaX) > 50 && Math.abs(deltaX) > Math.abs(deltaY)) {
			  	if (deltaX >= 0) {
			  		// console.log("左滑")  
			  	} else {
			  		// console.log("右滑")  
			  	}
			  } else if (Math.abs(deltaY) > 50 && Math.abs(deltaX) < Math.abs(deltaY)) {
			  	if (deltaY < 100) {
			  		if(this.qingqiuflag == true){
			  			let sum = (this.pagesum - 1) * this.pagesize
			  			console.log(sum,this.total);
			  			if (this.total > sum) {
			  				this.qingqiuflag = false
			  				this.getsb() 
			  			}
			  		}
			  	} else {
			  		console.log("下滑") 
			  	}
			  }
			},
			// 点击绑定设备到名下
			btnsbs() {
				uni.scanCode({
					onlyFromCamera: true,
					scanType: ['qrCode'],
					success: res => {
						function getQueryParam(url, paramName) {
							let regex = new RegExp(`[?&]${paramName}=([^&]*)`)
							let results = regex.exec(url);
							return results ? decodeURIComponent(results[1].replace(/\+/g, ' ')) : null
						}
						console.log(res);
						let sceneValue = res.result
						let decodedValue = decodeURIComponent(sceneValue)
						let id = getQueryParam(decodedValue, 'sn')
						let that = this
						let data = {
							sn: id,
						}
						that.$u.get(`/app/device/isBind?deviceNo=${id}`).then(res => {
							if (res.data == 2) {
								uni.showToast({
									title: '该设备已被绑定',
									icon: 'none',
									duration: 2000
								})
							} else if (res.data == 1) {
								uni.showModal({
									title: '提示',
									content: '该设备未绑定,你需进行绑定吗？',
									success: function(res) {
										if (res.confirm) {
											that.$u.put("/app/device/bind", data).then(res => {
													if (res.code == 200) {
														uni.showToast({
															title: '绑定成功',
															icon: 'success',
															duration: 2000
														})
														that.pagesum = 1
														this.list = []
														that.getsb()
													}
												})
										}
									}
								})
							}else if(res.data == 0){
								uni.showToast({
									title: '该设备为录入，请先录入',
									icon: 'none',
									duration: 2000
								})
							}
						})
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	page {
		background: #F6F6F6;
	}
	.list{
		width: 100%;
		height: 1230rpx;
		overflow: scroll;
		padding-bottom: 50rpx;
		box-sizing: border-box;
		.list_item{
			width: 750rpx;
			height: 422rpx;
			background: #FFFFFF;
			margin-top: 20rpx;
			.sbm{
				margin-top: 18rpx;
				display: flex;
				justify-content: space-between;
				padding-right: 38rpx;
				box-sizing: border-box;
				view{
					width: 152rpx;
					height: 64rpx;
					border-radius: 14rpx 14rpx 14rpx 14rpx;
					border: 2rpx solid #48893B;
					text-align: center;
					line-height: 64rpx;
					font-size: 32rpx;
					color: #48893B;
				}
			}
			.cen{
				display: flex;
				padding-left: 30rpx;
				box-sizing: border-box;
				margin-top: 28rpx;
				.sbxx{
					view{
						margin-bottom: 16rpx;
					}
				}
				image{
					width: 196rpx;
					height: 196rpx;
					margin-right: 40rpx;
				}
			}
			.top{
				display: flex;
				justify-content: space-between;
				border-bottom: 1px solid #D8D8D8;
				padding: 0 32rpx;
				height: 94rpx;
				line-height: 94rpx;
				.zt{
					display: flex;
					align-items: center;
					text{
						width: 14rpx;
						height: 14rpx;
						background: #48893B;
						margin-right: 12rpx;
						border-radius: 50%;
					}
				}
				.sn{
					font-weight: 600;
					font-size: 30rpx;
					color: #3D3D3D;
				}
			}
		}
	}
	.shaixuan{
		width: 100%;
		padding: 0 112rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
		align-items: center;
		background-color: #fff;
		padding-top: 32rpx;
		padding-bottom: 24rpx;
		box-sizing: border-box;
		.lt{
			display: flex;
			align-items: center;
			font-size: 32rpx;
			color: #3D3D3D;
		}
		.rt{
			display: flex;
			align-items: center;
			font-size: 32rpx;
			color: #3D3D3D;
		}
		image{
			width: 32rpx;
			height: 32rpx;
			margin-left: 10rpx;
		}
	}
    .serch{
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 0 40rpx;
		box-sizing: border-box;
		padding-top: 40rpx;
		background-color: #fff;
		.rt{
			width: 56rpx;
			height: 56rpx;
			margin-top: 4rpx;
		}
		.lt{
			display: flex;
			align-items: center;
			width: 596rpx;
			height: 68rpx;
			border: 2rpx solid #48893B;
			border-radius: 24rpx;
			padding-left: 30rpx;
			box-sizing: border-box;
			.sousuo{
				width: 140rpx;
				height: 66rpx;
				text-align: center;
				line-height: 66rpx;
				font-size: 32rpx;
				color: #FFFFFF;
				background: #48893B;
				border: 2rpx solid #48893B;
				border-radius: 0 20rpx 20rpx 0;
			}
			image{
				width: 46rpx;
				height: 46rpx;
			}
			input{
				width: 380rpx;
				margin-left: 30rpx;
				height: 68rpx;
				line-height: 68rpx;
			}
		}
	}

</style>