<template>
	<view class="page">
		<u-navbar title="设备详情" :border-bottom="false" :background="bgc" title-color='#000' title-size='36'
			height='58'></u-navbar>
		<view class="box">
			<view class="boxtop">
				<view class="bh">
					{{xqobj.sn}} <u-icon name="file-text" size="34" @click="copybtn(xqobj.sn)"></u-icon>
				</view>
				<view class="cdbbuttom">
					<view class="one">
						<text>网络状态</text><text>设备状态</text><text>可租</text><text>电池</text><text>可还</text>
					</view>
					<view class="two">
						<text style="color: #1DBE7B;" v-if="xqobj.onlineStatus == 1">在线</text>
						<text style="color: red;" v-if="xqobj.onlineStatus == 0">离线</text>
						<text style="color: #1DBE7B;" v-if="xqobj.status == 1">正常</text>
						<text style="color: red;" v-if="xqobj.status == 2">异常</text>
						<text>{{xqobj.currentPowerBankNum}}</text><text>{{xqobj.currentPowerBankNum}}</text><text>{{xqobj.slotNum - xqobj.currentPowerBankNum}}</text>
					</view>
				</view>
				<view class="yuny">
					<text>已运营{{differenceInDays}}天</text> <text>{{xqobj.createTime}}</text>
				</view>
			</view>

			<view class="boxbd">
				<view class="butt">
					<text>刷新</text> <text>重启设备</text><text
						@click="btntcsy">弹出所有</text><text>卡位({{xqobj.modelName}})</text>
				</view>
				<view class="cbdxq">
					<view style="color: #3D3D3D;" v-for="(item,index) in list" :key="index" @click="btntc(item.deviceId)">
						{{item.sn == null ? '空闲' : item.sn + '/'}}{{item.currentPower == null ? '' : item.currentPower + '%'}}
					</view>
				</view>
				<view class="tishi">
					<view class="">
						提示：单点可强制弹出；
					</view>
					<view class="" style="margin-top: 5rpx;">
						长按卡位可以锁定卡位！（锁定的卡位将不会被租借）
					</view>
				</view>
				<view class="sjxx">
					<view class="tit">
						商家信息
					</view>
					<view class="dpxx">
						<text>店铺名称：</text> <text style="color: #1DBE7B;">{{xqobj.storeName}}</text>
					</view>
					<view class="dpdz">
						<text style="width: 240rpx;">店铺地址：</text> <text>{{xqobj.storeAddress}}</text>
					</view>
					<view class="dxxx">
						断线信息
					</view>
					<view class="zjdx">
						<text>最近断线时间：</text> <text>{{xqobj.lastOfflineTime}}</text>
					</view>
					<view class="zjdx">
						<text>最近登录时间：</text> <text>{{xqobj.lastLoginTime}}</text>
					</view>
					<view class="zjdx">
						<text>最近断线时长：</text> <text>{{duration.days + '天' + duration.hours + '时' +  duration.minutes + '分' + duration.seconds + '秒'}}</text>
					</view>
				</view>
			</view>
		</view>


	</view>
</template>

<script>
	export default {
		data() {
			return {
				cabinetId: '',
				xqobj: {},
				differenceInDays: '',
				list: [],
				bgc: {
					background: '#25CE88'
				},
				duration:''
			}
		},
		onLoad(option) {
			this.cabinetId = option.cabinetId
			this.getxq()
		},
		methods: {
			btntcsy() {
				if(Array.isArray(this.list)){
					let deviceIds = ''
					this.list.forEach((item, index) => {
						if (item.deviceId) {
							if (deviceIds !== '') {
								deviceIds += ','
							}
							deviceIds += item.deviceId
						}
					})
					this.$u.put(`/agent/device/${deviceIds}/pop`).then(res => {
						if (res.code == 200) {
							uni.showToast({
								title: '已弹出所有设备',
								icon: 'success',
								duration: 2000
							})
							this.getxq()
						}
					})
				}else{
					uni.showToast({
						title: '所有设备均已弹出',
						icon: 'none',
						duration: 2000
					})
				}
			},
			btntc(deviceId) {
				if (deviceId) {
					this.$u.put(`/agent/device/${deviceId}/pop`).then(res => {
						if (res.code == 200) {
							uni.showToast({
								title: '已弹出该设备',
								icon: 'success',
								duration: 2000
							})
							this.getxq()
						}
					})
				} else {
					uni.showToast({
						title: '该空位无设备',
						icon: 'none',
						duration: 2000
					});
				}
			},
			getxq() {
				this.$u.get(`/agent/cabinet/${this.cabinetId}`).then(res => {
					if (res.code == 200) {
						this.xqobj = res.data
						this.list = res.data.currentDeviceList
						if (this.list) {
							let allSlots = Array.from({
								length: this.xqobj.slotNum
							}, (_, i) => i + 1);
							let missingSlots = [];
							this.list.forEach(item => {
								let index = allSlots.indexOf(item.currentSlot);
								if (index !== -1) {
									allSlots.splice(index, 1);
								}
							})
							missingSlots = allSlots;
							missingSlots.forEach(slot => {
								this.list.push({
									currentSlot: slot
								})
								this.list.sort((a, b) => b.currentSlot - a.currentSlot)
							})
						} else {
							this.list = this.xqobj.slotNum
						}

						let dateTime = new Date(this.xqobj.createTime);
						if (isNaN(dateTime.getTime())) {
							console.error(`无法解析日期时间字符串: ${this.xqobj.createTime}`);
							return;
						}
						let now = new Date().getTime();
						let differenceInMilliseconds = now - dateTime.getTime();
						this.differenceInDays = Math.floor(differenceInMilliseconds / (1000 * 60 * 60 * 24))
						
						// 计算断线时长
						let lastLoginTimeStr = this.xqobj.lastLoginTime  
						let lastOfflineTimeStr = this.xqobj.lastOfflineTime  
						let nows = new Date()
						let lastLoginTime = this.parseDateString(lastLoginTimeStr)  
						let lastOfflineTime = this.parseDateString(lastOfflineTimeStr)  
						if (lastLoginTime && lastLoginTime > lastOfflineTime) {  
						    let diff = lastLoginTime - lastOfflineTime
						    this.duration = this.formatDuration(diff)  
						    // console.log(` ${duration.days} '天', ${duration.hours} '小时', ${duration.minutes} '分钟'`)  
						} else {  
						    let diff = nows - lastOfflineTime 
						     this.duration = this.formatDuration(diff) 
						    // console.log(` ${duration.days} 天, ${duration.hours} 小时, ${duration.minutes} 分钟`)  
						}
						
					}
				})
			},
			parseDateString(dateStr) {
			    return dateStr ? new Date(dateStr) : null
			},
			formatDuration(durationMs) {
			    let duration = new Date(durationMs) 
			    let days = Math.floor(durationMs / (1000 * 60 * 60 * 24))  
			    durationMs -= days * (1000 * 60 * 60 * 24)
			    let hours = Math.floor((durationMs % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))  
			    durationMs -= hours * (1000 * 60 * 60) 
			    let minutes = Math.floor((durationMs % (1000 * 60 * 60)) / (1000 * 60)) 
				let seconds = Math.floor((durationMs % (1000 * 60)) / 1000)
			    return { days, hours, minutes,seconds} 
			},
		
			copybtn(text) {
				uni.setClipboardData({
					data: text,
					success: function(res) {
						uni.showToast({
							title: '复制成功',
						});
					}
				});
			},

		}
	}
</script>

<style lang="scss">
	/deep/ .u-title,
	/deep/ .uicon-nav-back {
		padding-bottom: 40rpx;
	}

	page {
		// background: linear-gradient(180deg, #25CE88 0%, rgba(255, 255, 255, 0) 100%);
	}

	.page {
		width: 750rpx;
		// position: fixed;
		// top: 0;
		// left: 0;

		.box {
			width: 750rpx;
			height: 1440rpx;
			background: #F4F5F7;
			border-radius: 0rpx 0rpx 0rpx 0rpx;
			padding-top: 22rpx;

			.boxbd {
				width: 750rpx;
				height: 100%;
				background: #FFFFFF;
				padding: 30rpx 42rpx;
				box-sizing: border-box;
				margin-top: 32rpx;

				.tishi {
					text-align: center;
					margin-top: 24rpx;
					font-weight: 500;
					font-size: 24rpx;
					color: #FF4444;
				}

				.sjxx {
					margin-top: 54rpx;

					.tit {
						font-weight: 600;
						font-size: 28rpx;
						color: #3D3D3D;
					}

					.dpxx {
						display: flex;
						justify-content: space-between;
						font-size: 28rpx;
						color: #808080;
						margin-top: 24rpx;
					}

					.dpdz {
						display: flex;
						justify-content: space-between;
						font-size: 28rpx;
						color: #808080;
						margin-top: 24rpx;
					}

					.dxxx {
						font-weight: 600;
						font-size: 28rpx;
						color: #3D3D3D;
						margin-top: 24rpx;
					}

					.zjdx {
						display: flex;
						justify-content: space-between;
						font-size: 28rpx;
						color: #808080;
						margin-top: 24rpx;
					}
				}

				.cbdxq {
					width: 680rpx;
					// height: 344rpx;
					background: #F2F3F5;
					border-radius: 24rpx 24rpx 24rpx 24rpx;
					margin: auto;
					margin-top: 52rpx;
					padding: 20rpx 12rpx;
					box-sizing: border-box;

					view {
						display: inline-block;
						width: 314rpx;
						background: #FFFFFF;
						border-radius: 27rpx 27rpx 27rpx 27rpx;
						font-weight: 500;
						font-size: 20rpx;
						color: #3D3D3D;
						text-align: center;
						line-height: 53rpx;
						margin-top: 16rpx;
						margin-right: 10rpx;
					}
				}

				.butt {
					display: flex;
					justify-content: space-between;

					text {
						text-align: center;
						width: 158rpx;
						height: 68rpx;
						line-height: 68rpx;
						background: #1DBE7B;
						border-radius: 10rpx 10rpx 10rpx 10rpx;
						font-weight: 500;
						font-size: 28rpx;
						color: #FFFFFF;
					}
				}
			}

			.boxtop {
				padding: 30rpx;
				box-sizing: border-box;
				width: 680rpx;
				height: 268rpx;
				background: #FFFFFF;
				border-radius: 24rpx 24rpx 24rpx 24rpx;
				margin: auto;

				.bh {
					font-size: 24rpx;
					color: #3D3D3D;
					border-bottom: 1px solid #ccc;
					padding-bottom: 14rpx;
				}

				.cdbbuttom {
					margin-top: 20rpx;
					font-size: 24rpx;
					color: #3D3D3D;
					border-bottom: 1px solid #ccc;
					padding-bottom: 24rpx;

					text {
						width: 100rpx;
						text-align: center;
					}

					.one {
						display: flex;
						justify-content: space-between;
					}

					.two {
						display: flex;
						justify-content: space-between;
						margin-top: 16rpx;
					}
				}

				.yuny {
					display: flex;
					justify-content: space-between;
					font-size: 24rpx;
					color: #3D3D3D;
					margin-top: 16rpx;
				}
			}
		}
	}
</style>