<template>
	<view>
		<view class="top-box">
			<view class="top-title" :style="'height: '+ systems.navigationHeight + 'rpx;padding-top:' + systems.ktxStatusHeight + 'rpx;'+'line-height: '+ systems.navigationHeight + 'rpx;'">
				<view v-if="!userInfo.storeId&&actMerchant" @click="showPop=true" class="ddflex">
					<view>{{actMerchant.name}}</view>
					<image class="dow-ssicon" src="../../static/pages/images/dow-ssicon.png"></image>
				</view>
			</view>
			<view class="search-box ddflex">
				<image class="search-saoma" src="../../static/pages/images/saoma.png" @click="saomaFn()"></image>
				<view class="search-input ddflex fflex">
					<image class="search-input-icon" src="../../static/pages/images/search.png"></image>
					<input class="fflex" v-model="searchValue" confirm-type="search" @confirm="searchFn"  placeholder="请输入编号查询"/>
					<view class="search-btn" @click="searchFn">搜索</view>
				</view>
			</view>
		</view>
		
		<view :style="'height: '+ (systems.navigationHeight+systems.ktxStatusHeight+134) + 'rpx;'"></view>
		
		<!-- 机柜 -->
		<view class="machine-item" v-for="item,index in groupList" @click="jumpUrl('/machineAndCell/machineInfo/machineInfo?id='+item.devId)">
			<view class="ddflex" style="justify-content: space-between;">
				<view class="machine-title fflex tover">{{item.devName}}</view>
				<!-- <view class="state">在线</view> -->
			</view>
			<view class="machine-number">机柜编号：{{item.devId}}</view>
			<view class="machine-info ddflex">
				<view class="ddflex">
					<view class="margin-r40">温度：{{item.cabTemp?item.cabTemp:0}}℃</view>
					<view class="margin-r40">仓门数：{{item.doorCount?item.doorCount:0}}</view>
					<view class="margin-r40">电池数：{{item.batNum?item.batNum:0}}</view>
				</view>
				<view class="fflex tover" style="color: #2A82E4;text-align: right;">
					{{item.storeName?item.storeName:''}}
				</view>
			</view>
			<view class="address-box ddflex">
				<image class="address-icon" src="../../static/pages/images/address-icon.png"></image>
				<view class="fflex tover">{{item.address}}</view>
				<image @click.stop="toLocation(item)" class="daohang" src="../../static/pages/images/daohang.png"></image>
			</view>
		</view>
		
		<view v-if="groupList.length==0">
			<view class="nodata">
				<image :src="picUrlss+'empty.png'"></image>
				<text>暂无设备~</text>
			</view>
		</view>
		
		<left-pop v-if="showPop" :showPop="showPop" :btnColor="'#11BB8B'" :showBtn='false' title="解除提醒" width="460rpx"
			@closePop="closePop()" @popSubmit="popSubmit()">
			<view class="" style="height: 100%;flex-direction: column;display: flex;">
				<view @click="closePop()" class="pop-title ddflex" :style="'height: '+ systems.navigationHeight + 'rpx;padding-top:' + systems.ktxStatusHeight + 'rpx;'+'line-height: '+ systems.navigationHeight + 'rpx;'">
					<view>切换门店</view>
					<image class="change-ssicon" src="../../static/pages/images/change.png"></image>
				</view>
				<scroll-view :scroll-y="true" @scrolltolower="loadLeftData" class="pop-content fflex">
					<view :class="'pop-content-item '+(actMerchant.id==item.id?'pop-content-active':'')" v-for="(item,index) in merchantList" @click="changePop(item)">
						<view class="pop-content-name">{{item.name}}</view>
						<image class="pop-content-icon" src=""></image>
					</view>
				</scroll-view>
			</view>
		</left-pop>
		
		<foot channel="machine" :isUpdate="isUpdate"></foot>
	</view>
</template>

<script>
	const app = getApp();
	const req = require("../../utils/request.js");
	import foot from "../../components/nav-bar/index";
	import leftPop from "../../components/left-pop/index";

	export default {
		components: {
			foot,
			leftPop
		},
		props: {},
		data() {
			return {
				picUrlss: req.public.picUrls,
				systems: {},
				isTop:0,
				isUpdate: false,
				
				userInfo:{},
				
				showPop: false,
				tempData: null,
				
				groupList: [],
				page: 1,
				limit: 10,
				searchValue:'',
				isQuerying:false,//正在查询数据
				
				merchantList: [],
				merchantPage: 1,
				merchantLimit: 10,
				
				actMerchant:null,//当前门店
			}
		},
		onLoad(options) {
			// 判断当前登录用户身份
			if(req.isLogins(true)){
				this.userInfo = req.getStorage('userInfo')
				
				if(!this.userInfo.storeId){
					this.getMerchantList()
				}else{
					this.actMerchant={
						id:this.userInfo.storeId
					}
					this.getMachineList()
				}
			}else{
				this.getMachineList()
			}
			uni.$on('reSetLogin',(data)=>{
				console.log('reSetLogin')
				this.actMerchant = null
				// 判断当前登录用户身份
				if(req.isLogins(true)){
					this.userInfo = req.getStorage('userInfo')
					
					if(!this.userInfo.storeId){
						this.getMerchantList()
					}else{
						this.actMerchant={
							id:this.userInfo.storeId
						}
						this.getMachineList()
					}
				}else{
					this.getMachineList()
				}
			})
		},
		onShow() {
			this.setData({
				isUpdate: !this.isUpdate
			}); 
		},
		onReachBottom() {
			this.getMachineList(true)
		},
		methods: {
			jumpUrl(url){
				if(req.isLogins(true)){
					uni.navigateTo({
						url:url
					})
				}
			},
			// 弹窗弹出
			openPop(item) {
				this.tempData = JSON.parse(JSON.stringify(item))
				this.showPop = true
			},
			closePop(){
				this.showPop = false
			},
			changePop(item){
				this.actMerchant = item
				this.closePop()
				this.getMachineList()
			},
			// 获取机柜列表
			getMachineList(isPage){
				if(!this.actMerchant||!this.actMerchant.id) return
				if(this.isQuerying) return
				else this.isQuerying = true
				if (!isPage) {
					this.page = 1
				}
				let queryParams = {
					page: this.page,
					limit: this.limit,
				}
				if(this.searchValue){
					queryParams.devId=this.searchValue
				}
				queryParams.storeId = this.actMerchant.id
				req.getRequest(
					'/admin/v2/cab/page',
					queryParams,
					data => {
						if (data != null && data.list.length > 0) {
							if (!isPage) {
								this.groupList = data.list
							} else {
								this.groupList = this.groupList.concat(data.list)
							}
							this.page++
						} else {
							if (!isPage)
								this.groupList = data.list
							else {}
						}
						uni.hideLoading()
						this.isQuerying = false
					}
				)
			},
			loadLeftData(res){
				this.getMerchantList(true)
			},
			getMerchantList(isPage){
				if (!isPage) {
					this.merchantPage = 1
				}
				let queryParams = {
					page: this.merchantPage,
					limit: this.merchantLimit,
				}
				queryParams.merchantId = req.getStorage('userInfo').merchantId?req.getStorage('userInfo').merchantId:null
				req.getRequest(
					'/admin/v2/store/page',
					queryParams,
					data => {
						if (data != null && data.list.length > 0) {
							if (!isPage) {
								this.merchantList = data.list
							} else {
								this.merchantList = this.merchantList.concat(data.list)
							}
							this.merchantPage++
						} else {
							if (!isPage)
								this.merchantList = data.list
							else {}
						}
						if(!isPage){
							if(this.actMerchant){
								
							}else{
								this.actMerchant = this.merchantList[0]
							}
							this.getMachineList()
						}
					}
				)
			},
			toLocation(item) {
				let plugin = requirePlugin('routePlan');
				let key = req.public.mapLBSKEY; //使用在腾讯位置服务申请的key
				let referer = '皮小店商家端'; //调用插件的app的名称
				// console.log("名称====" + referer);
				let endPoint = JSON.stringify({
					//终点
					'name': item.devName,
					'latitude': item.latitude,
					'longitude':item.longitude
				});
				uni.navigateTo({
					url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint
				});
			},
			searchFn(){
				this.getMachineList()
			},
			saomaFn(){
				// 只允许通过相机扫码
				uni.scanCode({
					onlyFromCamera: false,
					success: (res) =>{
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						// did设备id
						this.searchValue = res.result
						this.searchFn()
					}
				});
			}
		},
		mounted() {
			const systemInfo = uni.getSystemInfoSync();
			// px转换到rpx的比例
			let pxToRpxScale = 750 / systemInfo.windowWidth;
			let systems = {
				ktxStatusHeight: systemInfo.statusBarHeight * pxToRpxScale, // 状态栏的高度
				navigationHeight: 44 * pxToRpxScale // 导航栏的高度
			};
			systems.barHeight = systems.ktxStatusHeight + systems.navigationHeight;
			this.systems = systems;
		},
		onPageScroll: function(e) {
			if (e.scrollTop > this.systems.barHeight) {
				this.isTop = 1;
			} else {
				this.isTop = 0;
			}
		}
	}
</script>

<style>
	@import "./machine.css";
</style>