<template>
	<view class="page">
		<u-navbar title="设备详情" :border-bottom="false" :background="bgc" title-color='#2E4975' title-size='36'
			height='45'></u-navbar>
		<map class="map" id="map" ref="map" :scale="zoomSize" :latitude="latitude" :longitude="longitude"
			:show-location="true" :markers="markers" :polygons="polyline" ></map>
		<view class="info_card">
			<view class="info_tit">
				设备信息
			</view>
			<view class="lines"></view>
			<view class="cont">
				
				
				<view class="info_li">
					<view class="half_infoli">
						车牌号：<span v-if="deviceInfos.vehicleNum">{{deviceInfos.vehicleNum}}</span>
						<span v-else>--</span>
					</view>
					<view class="half_infoli">
						MAC：<span>{{deviceInfos.mac}}</span>
					</view>
				</view>
				<view class="info_li">
					<view class="half_infoli">
						SN：<span>{{deviceInfos.sn}}</span>
					</view>
					<view class="half_infoli">
						车辆型号：
						<span v-if="deviceInfos.model">{{deviceInfos.model}}</span>
						<span v-else>--</span>
					</view>
				</view>
				<view class="info_li">
					运营区域：
					<span v-if="deviceInfos.areaName">{{deviceInfos.areaName}}</span>
					<span v-else>--</span>
				</view>
				
			</view>
			<view class="lines"></view>
			<view class="cont">
			
				<view class="info_li">
					<view class="half_infoli">
						车辆状态：<span>{{status()}}</span>
					</view>
					<view class="half_infoli">
						续航里程：
						<span v-if="deviceInfos.remainingMileage">{{deviceInfos.remainingMileage}} KM</span>
						<span v-else>--</span>
						
					</view>
				</view>
				<view class="info_li">
					<view class="half_infoli">
						电池电量：
						<span v-if="deviceInfos.remainingPower">{{deviceInfos.remainingPower}}%</span>
						<span v-else>--</span>
					</view>
				</view>
				
			<!-- 	<view class="info_li">
					异常原因：<span>已完成</span>
				</view> -->
			</view>
		<!-- 	<view class="lines"></view>
			<view class="cont">
			
				<view class="info_li">
					
					<view class="half_infoli">
						GPRS信号：<span>1Km</span>
					</view>
				</view>
			
			</view> -->
			<view class="lines"></view>
			<view class="cont">
			
			
				<view class="info_li">
					最后定位坐标
					<span v-if="deviceInfos.latitude">{{deviceInfos.latitude}},{{deviceInfos.longitude}}</span>
					<span v-else>--</span>
				</view>
				<view class="info_li" style="flex-wrap: wrap;display: inline-block;">
					最后定位地址：
					<span v-if="deviceInfos.location">{{deviceInfos.location}}</span>
					<span v-else>--</span>
				</view>
				<view class="info_li">
					最后定位时间：
					<span v-if="deviceInfos.lastTime">{{deviceInfos.lastTime}}</span>
					<span v-else>--</span>
				</view>
			</view>
		</view>
		<view class="info_card" style="margin-top: 20rpx;" v-if="deviceInfos.etOrders!=''"> 
			<view class="info_tit">
				订单信息
			</view>
			<view class="lines"></view>
			<view class="cont">
			
			
				<view class="info_li">
					当前用户：
					<span v-if="deviceInfos.etOrders[0].phonenumber">{{deviceInfos.etOrders[0].phonenumber}}</span>
					<span v-else>--</span>
				</view>
				<view class="info_li" style="flex-wrap: wrap;display: inline-block;">
					当前订单编号：
					<span v-if="deviceInfos.etOrders[0].orderNo">{{deviceInfos.etOrders[0].orderNo}}</span>
					<span v-else>--</span>
				</view>
				<view class="info_li">
					订单开始时间：
					<span v-if="deviceInfos.etOrders[0].createTime">{{deviceInfos.etOrders[0].createTime}}</span>
					<span v-else>--</span>
				</view>
			</view>
		</view>
		<view class="bot_btn">
			<view class="btn" @click="btn(0)">
				开锁
			</view>
			<view class="btn"  @click="btn(1)">
				关锁
			</view>
			<view class="btn"  @click="btn(2)">
				车辆禁用
			</view>
			<view class="btn"  @click="btn(3)">
				车辆解禁
			</view>
			<view class="btn"  @click="btn(4)">
				响铃寻车
			</view>
			<view class="btn" @click="tipshow=true" v-if="info.type==1">
				维修处理
			</view>
			<view class="btn" @click="tipshow=true" v-if="info.type==2">
				换电处理
			</view>
		</view>
		<u-mask :show="false" @click="show = false" :z-index='100' />
		<view class="tip_box" v-if="false">
			<view class="top">
				<view class="tip">
					修改车牌号
				</view>
				
				<view class="ipt_box">
					<view class="text">
					车牌号
					</view>
					<view class="ipt">
						<input type="text"
							 v-model="searchKeyword"
							 placeholder="0.00"
							 class="input"
							 placeholder-style="color:#C7CDD3"
							>
							元
					</view>
				</view>
				
			</view>
			<view class="bot">
				<view class="bot_left" @click="show=false">
					取消
				</view>
				<view class="bot_right">
					确定
				</view>
			</view>
		</view>
		<!-- <view class="bot">
			<view class="btn">
				退款
			</view>
		</view> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bgc: {
					backgroundColor: "#F7FAFE",
				},
				show:true,
				showgj:true,
				searchKeyword: '11',
				latitude: 39.916527,
				longitude: 116.397128,
				isMap: false,
				zoomSize: 15,
				markers: [],
				polyline: [],
				polygons: [],
				cardId: '001区域',
				sn:'',
				deviceInfos:{}

			}
		},
		onLoad(e) {
			this.sn=e.id
			this.deviceInfo()
			this.getArea()
		
		},
		methods: {
			btn(num){
				if(num==0){
					this.$u.post('/appVerify/admin/unlocking?sn='+this.sn).then((res) => {
						if (res.code === 200) {
							// 处理接口返回的数据，将边界数据转换为地图组件需要的折线结构
							this.deviceInfo()
						}
					}).catch(error => {
						console.error("Error fetching area data:", error);
					});
				}else if(num==1){
					this.$u.post('/appVerify/admin/lock?sn='+this.sn).then((res) => {
						if (res.code === 200) {
							// 处理接口返回的数据，将边界数据转换为地图组件需要的折线结构
							this.deviceInfo()
						}
					}).catch(error => {
						console.error("Error fetching area data:", error);
					});
				}else if(num==2){
					this.$u.post('/appVerify/device/offline?sn='+this.sn).then((res) => {
						if (res.code === 200) {
							// 处理接口返回的数据，将边界数据转换为地图组件需要的折线结构
							this.deviceInfo()
						}
					}).catch(error => {
						console.error("Error fetching area data:", error);
					});
				}else if(num==3){
					this.$u.post('/appVerify/device/online?sn='+this.sn).then((res) => {
						if (res.code === 200) {
							// 处理接口返回的数据，将边界数据转换为地图组件需要的折线结构
							this.deviceInfo()
						}
					}).catch(error => {
						console.error("Error fetching area data:", error);
					});
				}else if(num==4){
					this.$u.post('/app/device/ring?sn=' + this.sn).then((res) => {
						if (res.code === 200) {
							
						}
					})
				}
			},
			status(){
				if(this.deviceInfos.onlineStatus==0){
					return '离线'
				}else{
					if(this.deviceInfos.status==0){
						return '未上架'
					}else if(this.deviceInfos.status==1){
						return '待租'
					}else if(this.deviceInfos.status==2){
						return '预约中'
					}else if(this.deviceInfos.status==3){
						return '骑行中'
					}else if(this.deviceInfos.status==4){
						return '临时锁车中'
					}else if(this.deviceInfos.status==8){
						return '下线'
					}
				}
				
			},
			deviceInfo() {
				this.$u.get('/app/device/info?sn=' + this.sn).then((res) => {
					if (res.code === 200) {
						this.deviceInfos = res.data
						this.latitude= parseFloat(this.deviceInfos.latitude)
						this.longitude=  parseFloat(this.deviceInfos.longitude )
						if (this.deviceInfos.status == 0) {
							this.markers.push({
								id: parseFloat(this.deviceInfos.sn),
								latitude: parseFloat(this.deviceInfos.latitude),
								longitude: parseFloat(this.deviceInfos.longitude),
								// title: item.deviceName,
								width: 40,
								height: 40,
								iconPath: 'https://lxnapi.ccttiot.com/bike/img/static/u76wT0QiWdAGJdF4v6AR',
								callout: {
									content: '未上架', // 修改为你想要显示的文字内容
									color: '#ffffff', // 修改为文字颜色
									fontSize: 14, // 修改为文字大小
									borderRadius: 10, // 修改为气泡圆角大小
									bgColor: '#000000', // 修改为气泡背景颜色
									padding: 6, // 修改为气泡内边距
									display: 'ALWAYS', // 修改为气泡的显示策略
								}
							})		
						} else if (this.deviceInfos.status == 1) {
							this.markers.push({
							id: parseFloat(this.deviceInfos.sn),
							latitude: parseFloat(this.deviceInfos.latitude),
							longitude: parseFloat(this.deviceInfos.longitude),
								// title: item.deviceName,
								width: 40,
								height: 40,
								iconPath: 'https://lxnapi.ccttiot.com/bike/img/static/u1UD93BU1vfshWFoDwgX',
								callout: {
									content: '待租' + this.deviceInfos.remainingPower + '%', // 修改为你想要显示的文字内容
									color: '#2679D1', // 修改为文字颜色
									fontSize: 14, // 修改为文字大小
									borderRadius: 10, // 修改为气泡圆角大小
									bgColor: '#D4ECFF', // 修改为气泡背景颜色
									padding: 6, // 修改为气泡内边距
									display: 'ALWAYS', // 修改为气泡的显示策略
								}
							})
						} else if (this.deviceInfos.status == 2) {
							this.markers.push({
								id: parseFloat(this.deviceInfos.sn),
								latitude: parseFloat(this.deviceInfos.latitude),
								longitude: parseFloat(this.deviceInfos.longitude),
								// title: item.deviceName,
								width: 40,
								height: 40,
								iconPath: 'https://lxnapi.ccttiot.com/bike/img/static/u1UD93BU1vfshWFoDwgX',
								callout: {
									content: '预约' + this.deviceInfos.remainingPower + '%', // 修改为你想要显示的文字内容
									color: '#2679D1', // 修改为文字颜色
									fontSize: 14, // 修改为文字大小
									borderRadius: 10, // 修改为气泡圆角大小
									bgColor: '#D4ECFF', // 修改为气泡背景颜色
									padding: 6, // 修改为气泡内边距
									display: 'ALWAYS', // 修改为气泡的显示策略
								}
							})
						} else if (this.deviceInfos.status == 3) {
							this.markers.push({
							id: parseFloat(this.deviceInfos.sn),
							latitude: parseFloat(this.deviceInfos.latitude),
							longitude: parseFloat(this.deviceInfos.longitude),
								// title: item.deviceName,
								width: 40,
								height: 40,
								iconPath: 'https://lxnapi.ccttiot.com/bike/img/static/u1UD93BU1vfshWFoDwgX',
								callout: {
									content: '骑行' + this.deviceInfos.remainingPower + '%', // 修改为你想要显示的文字内容
									color: '#2679D1', // 修改为文字颜色
									fontSize: 14, // 修改为文字大小
									borderRadius: 10, // 修改为气泡圆角大小
									bgColor: '#D4ECFF', // 修改为气泡背景颜色
									padding: 6, // 修改为气泡内边距
									display: 'ALWAYS', // 修改为气泡的显示策略
								}
							})
						} else if (this.deviceInfos.status == 4) {
							this.markers.push({
							id: parseFloat(this.deviceInfos.sn),
							latitude: parseFloat(this.deviceInfos.latitude),
							longitude: parseFloat(this.deviceInfos.longitude),
								// title: item.deviceName,
								width: 40,
								height: 40,
								iconPath: 'https://lxnapi.ccttiot.com/bike/img/static/u1UD93BU1vfshWFoDwgX',
								callout: {
									content: '锁车' + this.deviceInfos.remainingPower + '%', // 修改为你想要显示的文字内容
									color: '#2679D1', // 修改为文字颜色
									fontSize: 14, // 修改为文字大小
									borderRadius: 10, // 修改为气泡圆角大小
									bgColor: '#D4ECFF', // 修改为气泡背景颜色
									padding: 6, // 修改为气泡内边距
									display: 'ALWAYS', // 修改为气泡的显示策略
								}
							})
						} else if (this.deviceInfos.status == 8) {
							this.markers.push({
							id: parseFloat(this.deviceInfos.sn),
							latitude: parseFloat(this.deviceInfos.latitude),
							longitude: parseFloat(this.deviceInfos.longitude),
								// title: item.deviceName,
								width: 40,
								height: 40,
								iconPath: 'https://lxnapi.ccttiot.com/bike/img/static/u76wT0QiWdAGJdF4v6AR',
								callout: {
									content: '下线', // 修改为你想要显示的文字内容
									color: '#ffffff', // 修改为文字颜色
									fontSize: 14, // 修改为文字大小
									borderRadius: 10, // 修改为气泡圆角大小
									bgColor: '#000000', // 修改为气泡背景颜色
									padding: 6, // 修改为气泡内边距
									display: 'ALWAYS', // 修改为气泡的显示策略
								}
							})
						}
						if (this.deviceInfos.onlineStatus == 0) {
							this.markers.push({
								id: parseFloat(this.deviceInfos.sn),
								latitude: parseFloat(this.deviceInfos.latitude),
								longitude: parseFloat(this.deviceInfos.longitude),
								// title: item.deviceName,
								width: 40,
								height: 40,
								iconPath: 'https://lxnapi.ccttiot.com/bike/img/static/u4UKmB47AxOj3YKIaajM',
								callout: {
									content: '离线', // 修改为你想要显示的文字内容
									color: '#ffffff', // 修改为文字颜色
									fontSize: 14, // 修改为文字大小
									borderRadius: 10, // 修改为气泡圆角大小
									bgColor: '#000000', // 修改为气泡背景颜色
									padding: 6, // 修改为气泡内边距
									display: 'ALWAYS', // 修改为气泡的显示策略
								}
							})
						}
						
					}
				})
			},
			convertBoundaryToPolyline(boundary) {
				if (!boundary) return null;
			
				const points = JSON.parse(boundary).map(coord => ({
					latitude: coord[1],
					longitude: coord[0]
				}));
			
				const polyline = {
					points: points,
					fillColor: "#55888840", //填充颜色
					strokeColor: "#22FF00", //描边颜色
					strokeWidth: 2, //描边宽度
					zIndex: 1, //层级
				};
			
				return polyline;
			},
			convertBoundaryToPolylines(boundaries,num) {
				if(num==1){
					console.log('判断');
					return boundaries.map(boundary => {
						if (!boundary) return null;
						let coords;
						try {
							coords = JSON.parse(boundary);
						} catch (error) {
							console.error("Error parsing boundary JSON:", error);
							return null;
						}
						if (!Array.isArray(coords)) {
							console.error("Parsed boundary is not an array:", coords);
							return null;
						}
						const points = coords.map(coord => ({
							latitude: coord[1],
							longitude: coord[0]
						}));
						return {
							points: points,
							fillColor: "#3A7EDB40", //填充颜色
							strokeColor: "#3A7EDB", //描边颜色
							strokeWidth: 2, //描边宽度
							zIndex: 1, //层级
							
						};
					}).filter(polyline => polyline !== null); // 过滤掉无效的折线数据
				}else if(num==2){
					return boundaries.map(boundary => {
						if (!boundary) return null;
						let coords;
						try {
							coords = JSON.parse(boundary);
						} catch (error) {
							console.error("Error parsing boundary JSON:", error);
							return null;
						}
						if (!Array.isArray(coords)) {
							console.error("Parsed boundary is not an array:", coords);
							return null;
						}
						const points = coords.map(coord => ({
							latitude: coord[1],
							longitude: coord[0]
						}));
						return {
							points: points,
							fillColor: "#FFF5D640", //填充颜色
							strokeColor: "#FFC107", //描边颜色
							strokeWidth: 2, //描边宽度
							zIndex: 1, //层级
					
						};
					}).filter(polyline => polyline !== null); // 过滤掉无效的折线数据
				}else if(num==3){
					return boundaries.map(boundary => {
						if (!boundary) return null;
						let coords;
						try {
							coords = JSON.parse(boundary);
						} catch (error) {
							console.error("Error parsing boundary JSON:", error);
							return null;
						}
						if (!Array.isArray(coords)) {
							console.error("Parsed boundary is not an array:", coords);
							return null;
						}
						const points = coords.map(coord => ({
							latitude: coord[1],
							longitude: coord[0]
						}));
						return {
							points: points,
							fillColor: "#FFD1CF40", //填充颜色
							strokeColor: "#FF473E", //描边颜色
							strokeWidth: 2, //描边宽度
							zIndex: 1, //层级
					
						};
					}).filter(polyline => polyline !== null); // 过滤掉无效的折线数据
				}
				
			},
			getParking() {
				// 发送请求获取数据
				this.$u.get('/app/parking/list').then((res) => {
					if (res.code === 200) {
						// 处理接口返回的数据
						const type1Data = [];
						const type2Data = [];
						const type3Data = [];
						
						res.rows.forEach(row => {
							if (row.type == 1) {
								type1Data.push(row);
							} else if (row.type == 2) {
								type2Data.push(row);
							} else if (row.type == 3) {
								type3Data.push(row);
							}
						});
						const validBoundaries = type1Data.map(row => row.boundaryStr).filter(boundary =>
							typeof boundary === 'string' && boundary.trim() !== '');
						const polylines = this.convertBoundaryToPolylines(validBoundaries,1);
						
						
						const validBoundaries1 = type2Data.map(row => row.boundaryStr).filter(boundary =>
							typeof boundary === 'string' && boundary.trim() !== '');
						const polylines1 = this.convertBoundaryToPolylines(validBoundaries1,2);
						
						const validBoundaries2 = type3Data.map(row => row.boundaryStr).filter(boundary =>
							typeof boundary === 'string' && boundary.trim() !== '');
						const polylines2 = this.convertBoundaryToPolylines(validBoundaries2,3);
						// 将处理后的数据添加到 this.polyline 中
							this.polyline = this.polyline.concat(polylines2);
						this.polyline = this.polyline.concat(polylines1);
						this.polyline = this.polyline.concat(polylines);
			
						// console.log(this.polyline);
						res.rows.forEach(item => {
							this.markers.push({
								id: parseFloat(item.parkingId),
								latitude: parseFloat(item.latitude),
								longitude: parseFloat(item.longitude),
								// title: item.deviceName,
								width: 20,
								height: 40,
								iconPath: 'https://lxnapi.ccttiot.com/bike/img/static/u9yz0bKCWKyev0JYpTne',
								callout: {
									content: item.parkingName, // 修改为你想要显示的文字内容
									color: '#ffffff', // 修改为文字颜色
									fontSize: 14, // 修改为文字大小
									borderRadius: 10, // 修改为气泡圆角大小
									bgColor: '#000000', // 修改为气泡背景颜色
									padding: 6, // 修改为气泡内边距
									display: 'ALWAYS', // 修改为气泡的显示策略
								}
							})
						})
					}
				}).catch(error => {
					console.error("Error fetching parking data:", error);
				});
			},
			getArea() {
				// 发送请求获取数据
				this.$u.get('/app/area/list').then((res) => {
					if (res.code === 200) {
						// 处理接口返回的数据，将边界数据转换为地图组件需要的折线结构
						const polylines = res.rows
							.filter(area => area.boundaryStr) // 过滤掉boundary为空的数据
							.map(area => this.convertBoundaryToPolyline(area.boundaryStr));
						// 更新折线数据
						this.polyline = polylines;
						// console.log(this.polyline);
							this.getParking()
					}
				}).catch(error => {
					console.error("Error fetching area data:", error);
				});
			},
		}
	}
</script>

<style lang="scss">
	page {
		overflow-x: hidden;
		background-color: #F3F3F3;
	}

	.page {
		padding-bottom: 250rpx;
		width: 750rpx;

		.map {
			width: 750rpx;
			height: 752rpx;
		}
		.tip_box {
			position: fixed;
			left: 72rpx;
			top: 628rpx;
			width: 610rpx;
			// height: 282rpx;
			background: #FFFFFF;
			border-radius: 30rpx 30rpx 30rpx 30rpx;
			z-index: 110;
			padding-bottom: 100rpx;
			.top {
				padding: 52rpx 38rpx 42rpx 36rpx;
				.ipt_box{
					margin-top: 22rpx;
					display: flex;
					flex-wrap: nowrap;
					align-items: center;
					.text{
						width: 96rpx;
						font-weight: 400;
						font-size: 32rpx;
						color: #3D3D3D;
					}
					.ipt{
						padding: 10rpx 18rpx;
						display: flex;
						align-items: center;
						justify-content: space-between;
						margin-left: 26rpx;
						width: 420rpx;
						height: 64rpx;
						border-radius: 0rpx 0rpx 0rpx 0rpx;
						border: 2rpx solid #979797;
						.input{
							width: 80%;
						}
					}
				}
				.tip{
					width: 100%;
					text-align: center;
					font-weight: 700;
					font-size: 32rpx;
					color: #3D3D3D;
				}
				.txt {
					margin-top: 32rpx;
					width: 100%;
					text-align: center;
					font-weight: 500;
					font-size: 32rpx;
					color: #3D3D3D;
				}
			}
		
			.bot {
				position: absolute;
				
				width: 610rpx;
				// border-top: 2rpx solid #D8D8D8;
				display: flex;
				flex-wrap: nowrap;
				// height: 100%;
				bottom: -20rpx;
				.bot_left {
					border-radius: 0rpx 0rpx 0rpx 30rpx;
					width: 50%;
					height: 86rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					font-weight: 500;
					font-size: 32rpx;
					color: #3D3D3D;
					background: #EEEEEE;
				}
		
				.bot_right {
						border-radius: 0rpx 0rpx 30rpx 0rpx;
					width: 50%;
					height: 86rpx;
					background: #4C97E7;
					display: flex;
					align-items: center;
					justify-content: center;
					color: #FFFFFF;
					// border-left: 2rpx solid #D8D8D8;
					font-weight: 500;
					font-size: 32rpx;
					// color: #4C97E7;
		
				}
		
			}
		}
		.bot_btn{
			position: fixed;
			bottom: 0;
			display: flex;
			flex-wrap: wrap;
			padding: 40rpx 18rpx;
			width: 750rpx;
			height: 230rpx;
			background: #fff;
			// background: linear-gradient( 180deg, #FFFFFF 0%, rgba(255,255,255,0) 100%);
			box-shadow: 0rpx 10rpx 64rpx 0rpx rgba(0,0,0,0.08);
			border-radius: 0rpx 0rpx 0rpx 0rpx;
			.btn:nth-child(4n) {
			    margin-right: 0;
			}
			.btn{
				margin-right: 18rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				width: 164rpx;
				height: 66rpx;
				background: #E2F2FF;
				border-radius: 0rpx 0rpx 0rpx 0rpx;
				border: 2rpx solid #4C97E7;
				font-weight: 400;
				font-size: 28rpx;
				color: #3D3D3D;
			}
		}
		.info_card {
			background: #FFFFFF;

			.info_tit {
				display: flex;
				flex-wrap: nowrap;
				padding: 22rpx 28rpx;
				font-weight: 600;
				font-size: 32rpx;
				color: #3D3D3D;

				.money {
					margin-left: auto;
					font-weight: 500;
					font-size: 32rpx;
					color: #4C97E7;
				}
			}

			.lines {
				width: 750rpx;
				height: 2rpx;
				border: 2rpx solid #ccc;
			}

			.cont {
				padding: 26rpx 28rpx;

				.info_li {
					width: 98%;
					display: flex;
					flex-wrap: nowrap;
					font-weight: 400;
					font-size: 28rpx;
					color: #808080;

					span {
						color: #3D3D3D;
					}
					white-space: nowrap;
					/* 禁止换行 */
					overflow: hidden;
					/* 超出部分隐藏 */
					text-overflow: ellipsis;
					/* 显示省略号 */
					line-height: 48rpx;

					.half_infoli {
							display: flex;
							flex-wrap: nowrap;
						width: 50%;
						font-weight: 400;
						font-size: 28rpx;
						color: #808080;

						span {
							color: #3D3D3D;
						}
						.input{
							width: 30%;
						}
					}
				}
			}
		}

		
	}
</style>