<template>
	<view>
		<view class="page-body">
			<view class="utabs">
				<view style="width: 95%;margin: 0 auto;">
					<u-tabs :list="list" :is-scroll="true" :current="current" @change="change" item-width="140"
						font-size="24" gutter="20" bar-width="60" active-color="#42b983"></u-tabs>
				</view>
			</view>
			<view class="LocationAndDetails">
				<view class="search_bot_input">
					<input type="text" value="" placeholder="请输入设备ID" @input="searchinput" />
					<u-icon name="search" size="40" class="icon" @click="search"></u-icon>
				</view>
			</view>
			<view class="page-section page-section-gap">
				<map class="map" :markers="covers" scale="3" @markertap="markertap">
					<cover-view slot="callout">
						<block v-for="(item,index) in covers" :key="index">
							<cover-view class="customCallout" :marker-id="item.id">
								<cover-view class="content">
									{{item.title}}
								</cover-view>
							</cover-view>
						</block>
					</cover-view>
				</map>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				id: 0, // 使用 marker点击事件 需要填写id
				title: 'map',
				covers: [],
				list: [],
				current: 0,
				icon: [
					// {
					// 	id: '', //全部
					// 	url: "../../static/images/distribution/7610e3983eb33ed5b9ad72ebdfc8ed2.png",
					// 	name: "全部"
					// },
					{
						id: 2, //杀虫灯
						name: "杀虫灯"
					},
					{
						id: 3, //测报灯
						name: "测报灯"
					},
					{
						id: 4, //性诱测报
						name: "性诱测报"
					},
					{
						id: 5, //环境检测
						name: "环境监测"
					},
					{
						id: 6, //监控设备
						name: "监控设备"
					},
					{
						id: 7, //孢子仪
						name: "孢子仪",
					},
					{
						id: 10, //性诱2.0
						name: "性诱2.0"
					},
				],
				type: '5', //设备类型
				typeindex: null, //设备选择
				device_id: '', //设备号
				serTF: false, //设备搜索显示隐藏
				punctuationTF: false, //判断是否以点击标点
				punctuation_id: "", //点击标点的id
				punctuation_did: '',
				punctuation_type: '',
				listindex: 1, //以选择设备名称
				disstyle: {
					position: "absolute",
					bottom: "0rpx"
				},
				facilitydatas: [],
			}
		},
		onLoad() {
			this.device_id = ""
			this.usertype()
		},
		methods: {
			async history() { //获取分布位置
				const res = await this.$myRequest({
					url: '/api/api_gateway?method=home.homes.equip_map_location_list',
					data: {
						equip_type: this.type,
						device_id: this.device_id
					}
				})
				console.log(res)
				this.covers = []
				var arr = []
				for (var i = 0; i < res.length; i++) {
					if (res[i].lat !== "" && res[i].lng !== "") {
						// if (i < 2000) {
							var obj = {}
							obj.latitude = Number(res[i].lat)
							obj.longitude = Number(res[i].lng)
							obj.id = i
							obj.title = res[i].device_id
							obj.width = 30
							obj.height = 30
							obj.joinCluster = true
							arr.push(obj)
						// }
					}
				}
				this.covers = arr
				this.facilitydatas = res
				if (res.length == 0) {
					uni.showToast({
						title: '未查询到设备',
						duration: 2000,
						icon: "none"
					});
				}
			},
			async usertype() { //设备列表
				const res = await this.$myRequest({
					url: '/api/api_gateway?method=home.homes.user_device_type',
				})

				for (var i = 0; i < res.length; i++) {
					if (res[i].id == 8 ||
					   res[i].id == 12 ||
					   res[i].id == 14 ||
					   res[i].id == 16 
					) {
						continue;
					}
					var obj = {
						name: res[i].type_name,
						id: res[i].id
					}
					this.list.push(obj)
				}
				this.type = this.list[0].id
				this.history(this.device_id)
				// console.log(this.list)
			},
			change(index) {
				console.log(index)
				this.device_id = ""
				this.current = index
				for (var i = 0; i < this.icon.length; i++) {
					if (this.list[index].name == this.icon[i].name) {
						console.log(this.icon[i].id)
						this.type = this.icon[i].id
					}
				}
				this.history()
			},
			markertap(e) {
				console.log(e)
				console.log(this.facilitydatas[e.detail.markerId])
				this.device_id = this.facilitydatas[e.detail.markerId].device_id
				this.punctuation_did = this.facilitydatas[e.detail.markerId].d_id
				this.punctuation_type = this.facilitydatas[e.detail.markerId].device_type_id
				uni.showModal({
					title: '设备详情查询',
					content: "设备id:" + this.device_id,
					cancelText: "取消",
					confirmText: "设备详情",
					success: (res) => {
						if (res.confirm) {
							this.eqinfo(this.facilitydatas[e.detail.markerId])
						} else if (res.cancel) {
							// this.serTFs()
						}
					}
				});
			},
			serTFs() {
				this.history()
			},
			eqinfo(item) { //设备信息
				if (this.punctuation_type == '') {
					uni.showToast({
						title: '请点击需查看的设备',
						duration: 2000,
						icon: "none"
					});
				} else {
					switch (this.punctuation_type) {
						case 2:
							uni.navigateTo({
								url: "../prevention/equipmentdetails?shebei=" + JSON.stringify(item)
							})
							break;
						case 5:
							uni.navigateTo({
								url: "../environment/equipment?shebei=" + JSON.stringify(item)
							})
							break;
						case 10:
							uni.navigateTo({
								url: "../cb/xy2.0/particulars?info=" + JSON.stringify(item)
							})
							break;
						default:
							uni.navigateTo({
								url: "../cb/equip-detail/equip-detail?info=" + JSON.stringify(item)
							})
							break;
					}

				}
			}
		}
	}
</script>

<style lang="scss">
	.utabs {
		width: 100%;
		position: fixed;
		top: 0px;
		z-index: 100;
		background-color: #FFFFFF;
	}

	.LocationAndDetails {
		width: 100%;
		height: 60rpx;
		position: fixed;
		top: 54px;

		.search_bot_input {
			width: 90%;
			height: 54rpx;
			background-color: #E4E4E4;
			border-radius: 27rpx;
			position: relative;
			box-sizing: border-box;
			padding-top: 8rpx;
			margin: 0 auto;

			input {
				width: 85%;
				// text-indent: 1rem;
				font-size: 26rpx;
				padding-left: 40rpx;
				box-sizing: border-box;
			}

			.icon {
				position: absolute;
				top: 8rpx;
				right: 26rpx;
			}
		}

		// .Location{
		// 	display: flex;
		// 	margin-top: 20rpx;
		// 	justify-content: space-around;
		// 	.particulars_par {
		// 		background-color: #F1F1F1;
		// 		text-align: center;
		// 		width: 40%;
		// 		height: 80rpx;
		// 		line-height: 80rpx;
		// 		font-size: 28rpx;
		// 		border-radius: 40rpx;
		// 	}
		// 	.particulars_ser {
		// 		background-color: #57C87B;
		// 		text-align: center;
		// 		width: 40%;
		// 		height: 80rpx;
		// 		line-height: 80rpx;
		// 		font-size: 28rpx;
		// 		color: #FFFFFF;
		// 		border-radius: 40rpx;
		// 	}
		// }
	}

	.page-section {
		margin-top: 100px;
		height: 80vh;

		// position: fixed;
		// top: 144px;
		.map {
			width: 100%;
			height: 100%;
		}
	}

	.particulars {
		width: 100%;
		height: 160rpx;
		position: absolute;
		bottom: 0;

		.search_btn_top {
			width: 100%;
			height: 80rpx;
			line-height: 80rpx;
			font-size: 32rpx;
			padding-left: 30rpx;
			box-sizing: border-box;
		}

		.search_btn_bot {
			width: 100%;
			display: flex;
		}

	}

	.distri_ser {
		position: absolute;
		bottom: 0rpx;
		right: 0px;
		width: 100%;
		height: 440rpx;
		background-color: #FFFFFF;

		.distri_ser_input {
			width: 90%;
			margin: 20rpx auto;
			display: flex;
			background-color: #F1F1F1;
			height: 60rpx;
			border-radius: 30rpx;
			padding: 10rpx 20rpx;
			box-sizing: border-box;

			input {
				width: 90%;
				font-size: 28rpx;
				margin-right: 20rpx;
			}
		}

		.distri_ser_title {
			width: 90%;
			margin: 0 auto;
			padding-left: 20rpx;
			border-left: 4rpx solid #57C87B;
			font-size: 28rpx;
		}

		.distri_ser_type {
			width: 90%;
			margin: 20rpx auto;
			display: flex;
			justify-content: space-around;

			.type_items {
				height: 120rpx;
				width: 120rpx;
				padding: 20rpx 0;
			}

			.type_items_bor {
				height: 120rpx;
				width: 120rpx;
				border: 2rpx solid #57C87B;
				padding: 20rpx 0;
			}

			.type_items_img {
				width: 70rpx;
				height: 70rpx;
				margin-left: 20rpx;
			}

			.type_items_p {
				font-size: 24rpx;
				text-align: center;
			}
		}
	}

	.search_btn {
		width: 100%;
		display: flex;

		.btn_f,
		.btn_t {
			width: 50%;
			text-align: center;
			height: 80rpx;
			line-height: 80rpx;
			font-size: 28rpx;
		}

		.btn_f {
			background-color: #F1F1F1;
		}

		.btn_t {
			background-color: #57C87B;
			color: #FFFFFF;
		}
	}
</style>
