<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" scale="3" :markers="covers" :enable-zoom="true" @markertap="markertap"
					@regionchange="regionchange">
				</map>
			</view>
		</view>
	</view>
</template>
<script>
	import {
		Debounce,
		Throttle
	} from "../../util/anitthro.js"
	var imports = ""
	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, //杀虫灯
						url: "../../static/images/distribution/1bd535eb7dbb0809940030d40c64b4c.png",
						name: "杀虫灯"
					},
					{
						id: 3, //测报灯
						url: "../../static/images/distribution/0b551e50be351dbc14f0dd6470e3443.png",
						name: "测报灯"
					},
					{
						id: 4, //性诱测报
						url: "../../static/images/distribution/be5c1cfed22713a9544f020cf41c25f.png",
						name: "性诱测报"
					},
					{
						id: 5, //环境检测
						url: "../../static/images/distribution/8325b1b6079456ce43f952ce13d2919.png",
						name: "环境监测"
					},
					{
						id: 6, //监控设备
						url: "../../static/images/distribution/2eb9e550709430a1bd8178568c14785.png",
						name: "监控设备"
					},
					{
						id: 7, //孢子仪
						url: "../../static/images/distribution/54a96e2b0ad4efeecbd4a7b5e6deda3.png",
						name: "孢子仪",
					},
					{
						id: 10, //性诱2.0
						url: "../../static/images/distribution/515ea6143e0aaff4a823270c7aa00a6.png",
						name: "性诱2.0"
					},
				],
				type: '', //设备类型
				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()
		},
		onShow() {},
		methods: {
			async history(device_id) { //获取分布位置
				const res = await this.$myRequest({
					url: '/api/api_gateway?method=home.homes.equip_map_location_list',
					data: {
						equip_type: this.type,
						device_id: device_id
					}
				})
				// console.log(res)
				this.covers = []
				var arr = []
				for (var i = 0; i < res.length; i++) {
					if (i < 2000) {
						var obj = {}
						obj.latitude = Number(res[i].lat)
						obj.longitude = Number(res[i].lng)
						obj.id = parseInt(i)
						obj.title = res[i].device_name || res[i].device_id
						// obj.width = 30
						// obj.height = 30
						obj.iconPath = "https://webstaticimg.oss-cn-hangzhou.aliyuncs.com/bigdata_app/image/distribution/"+res[i].device_type_id+".png"
						arr.push(obj)
					}

					// console.log(arr)
				}
				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].type_name == "性诱3.0" ||
					   res[i].type_name == "吸虫塔" ||
					   res[i].type_name == "病虫害可视监测" ||
					   res[i].type_name == "小麦赤霉病" ||
					   res[i].type_name == "温室大棚" 
					   ){
						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
					}
				}
				console.log(this.device_id)
				this.history(this.device_id)
			},
			markertap(e) {
				console.log(this.facilitydatas[e.detail.markerId])
				this.punctuation_did = this.facilitydatas[e.detail.markerId].d_id
				this.punctuation_type = this.facilitydatas[e.detail.markerId].device_type_id
				this.device_id = this.facilitydatas[e.detail.markerId].device_id
				console.log(this.device_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()
						}
					}
				});
			},
			search() { //搜索
				this.history(imports)
			},
			searchinput(e) {
				console.log(e.detail.value)
				imports = e.detail.value
				console.log(imports)
				Debounce(() => {
					this.history(imports)
				}, 1000)()
			},
			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 3:

							// case 4:
							// 	uni.navigateTo({
							// 		url: "../cb/xy/equip-set/xyhistoryile?d_id=" + this.punctuation_did + "&device_id=" + this.device_id
							// 	})
							// 	break;
						case 5:
							uni.navigateTo({
								url: "../environment/equipment?shebei=" + JSON.stringify(item)
							})
							break;
							// case 6:
							// 	// uni.navigateTo({
							// 	// 	url: "../prevention/ucharts?d_id=" + this.punctuation_did + "&device_id=" + this.punctuation_id
							// 	// })
							// 	break;
							// case 7:
							// 	uni.navigateTo({
							// 		url: "../cb/bzy/equip-set/bzyhistoryile?d_id=" + this.punctuation_did + "&device_id=" + this.device_id
							// 	})
							// 	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;
					}

				}
			},
			regionchange(e) {
				console.log(e)
			}
		}
	}
</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;
			}

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

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

		.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>
