<template>
	<view class="" style="position: relative;">
		<view class="top">
			<view class="" style="height:var(--status-bar-height) ;">
			</view>
			<view class="nav">
				<view class="">
					<u-icon name="scan" @click="scanFn" color="white" :size="30"></u-icon>
				</view>
				<view class="" style="margin-left: 20rpx;">
					<u-icon name="coupon-fill" color="white" @click="showModal=true" :size="28"></u-icon>
				</view>
				<view class="" style="margin-left: 20rpx;">
					<u-icon name="plus-circle" color="white" @click="navto('/pages/addLand/addLand')"
						:size="28"></u-icon>
				</view>

			</view>
		</view>
		<view class="">
			<view class="" style="height:var(--status-bar-height) ;">
			</view>
			<view class="nav">
			</view>
		</view>

		<view class="content" v-if="true">
			<!-- 土地信息 -->
			<view class="">
				<view class="card" v-if="Object.keys(dataobj).length>0">
					<view class="" @click="navgo">
						<view class="" style="margin: 10rpx 0;">
							土地名称: {{dataobj.title}}
						</view>
						<!-- <view class="" style="margin: 10rpx 0;">
							农作物: 洋芋
						</view> -->
						<view class="" style="margin: 10rpx 0;">
							土地面积: {{dataobj.total_area}}亩
						</view>
						<view class="" style="margin: 10rpx 0;">
							种植面积: {{dataobj.total_area-dataobj.residual_area}}亩
						</view>
					</view>
					<view class="changeLand" @click="show=true">
						切换土地
					</view>

				</view>
				<view class="card" v-else>
					<view class="">
						<view class="">
							暂无信息
						</view>

					</view>


				</view>
			</view>
			<view class="add-thing" @click="showPicker1=true" v-if="columns2.length>0">
				记农事
			</view>
			<!-- 苗情监测 -->
			<view class="">
				<view class="live-video soil-monitor">
					<view class="head">
						<view class="tit">
							苗情监测数据
						</view>
						<view class="flushed-time">
							<u-icon name="clock"></u-icon>
							<text>刷新时间: {{getNowTimeFn()}} </text>
						</view>
					</view>
				</view>
				<view class="video" @click="navto('/pages/live/live')">
					<!-- <u--image :src="url" width="100%" height="300rpx"></u--image> -->
					<u-swiper :list="dataobj.pic" width="100%" height="300rpx"></u-swiper>
				</view>


			</view>
			<!-- 土壤监测 -->

			<view class="">

				<view class="soil-monitor">
					<view class="head">
						<view class="tit">
							土壤监测数据
						</view>
						<view class="flushed-time">
							<u-icon name="clock"></u-icon>
							<text>刷新时间: {{getNowTimeFn()}} </text>
						</view>
					</view>
					<view class="monitor-content"
						v-if="dataobj&&dataobj.monitor&&dataobj.soil_monitor_data&&Object.keys(dataobj.soil_monitor_data).length>0">
						<view class="monitor-card" v-for="(item,index) in soilList" :key="index">
							<view class="name">
								<view class="">
									<u--image :src="item.icon" width="38.55rpx" height="38.55rpx"></u--image>
								</view>
								<view class="" style="margin-left: 10rpx;">
									{{item.name}}
								</view>

							</view>
							<view class="num">
								<text style="font-size: 70rpx;">{{item.num}}</text>
								<text>{{item.unit}}</text>
							</view>
						</view>
					</view>
					<view class="coneng-detail" v-else>

						<view class="">
							<image src="@/static/img/zw.png" mode="aspectFit"></image>
							<view class="">
								暂无数据
							</view>
						</view>
					</view>

				</view>
				<!-- 环境监测 -->
				<view class="soil-monitor">
					<view class="head">
						<view class="tit">
							环境监测数据
						</view>
						<view class="flushed-time">
							<u-icon name="clock"></u-icon>
							<text>刷新时间: {{getNowTimeFn()}} </text>
						</view>
					</view>
					<view class="monitor-content"
						v-if="dataobj&&dataobj.monitor&&dataobj.monitor.air_monitor_data&&Object.keys(dataobj.monitor.air_monitor_data).length>0">
						<view class="monitor-card" v-for="(item,index) in environmentList" :key="index">
							<view class="name">
								<view class="">
									<u--image :src="item.icon" width="38.55rpx" height="38.55rpx"></u--image>
								</view>
								<view class="" style="margin-left: 10rpx;">
									{{item.name}}
								</view>

							</view>
							<view class="num">
								<text style="font-size: 70rpx;">{{item.num}}</text>
								<text>{{item.unit}}</text>
							</view>
						</view>
					</view>


					<view class="coneng-detail" v-else>

						<view class="">
							<image src="@/static/img/zw.png" mode="aspectFit"></image>
							<view class="">
								暂无数据
							</view>
						</view>
					</view>
				</view>


			</view>

			<!-- 组件 -->

			<u-loading-page :loading="showLoading"></u-loading-page>


			<u-action-sheet :show="show" keyName="name" closeOnClickOverlay :actions="columns" title="请选择"
				@close="show = false" @select="confirm">
			</u-action-sheet>
		</view>
		<view class="empty" v-else>
			<view class="addLand">
				<view class="">
					你还没有土地信息
				</view>
				<view class="addBtn" @click="navto('/pages/addLand/addLand')">
					去添加
				</view>
			</view>

		</view>
		<!-- 组件 -->

		<view class="Modal">
			<u-popup :show="showModal" mode="center" @close="closeModal" :round="10">
				<view class="Modal">
					<text> 产品编号:</text>
					<uni-data-select v-model="equipmentId" style="width: 300rpx;margin-left: 30rpx;"
						:localdata="range"></uni-data-select>
				</view>

				<view class="Modal-btn">
					<view class="Modal-btna" @click="closeModal">
						取消
					</view>
					<view class="Modal-btnb" @click="bindFn">
						确定
					</view>
				</view>
			</u-popup>

		</view>
		<u-picker keyName="name" :show="showPicker1" @cancel="showPicker1=false" @confirm="confirm1"
			:columns="columns2"></u-picker>

		<u-picker keyName="label" :show="showPicker" @cancel="showPicker=false" @confirm="confirm2"
			:columns="columns1"></u-picker>
	</view>
</template>

<script setup>
	import {
		reactive,
		ref
	} from 'vue';
	import {
		onLoad,
		onShow
	} from "@dcloudio/uni-app"
	import {
		bindproduct,
		lindexist,
		landlist,
		plantlist,
		landproduct
	} from '@/api/api.js'

	const showPicker = ref(false)
	const showPicker1 = ref(false)
	const showModal = ref(false)
	const columns1 = reactive([
		[{
			label: '施肥',
			route: "/pages/husbandryForm/fertilize"
		}, {
			label: '除草',
			route: "/pages/husbandryForm/weeding"
		}, {
			label: '灌溉',
			route: "/pages/husbandryForm/irrigate"
		}, {
			label: '除虫',
			route: "/pages/husbandryForm/Pest"
		}, {
			label: '收获',
			route: "/pages/husbandryForm/harvest"
		}]
	]);

	const columns2 = reactive([])
	const range = reactive([])
	// 土壤监测数据
	const url = ref('https://ceshi-worker-task.lihaink.cn//uploads//images//20231121//202311211336582b7920656.jpg')
	const soilList = reactive([{
			icon: "/static/img/CTWD.png",
			name: "土壤温度",
			num: 20,
			unit: "℃",

		}, {
			icon: "/static/img/SD.png",
			name: "土壤湿度",
			num: 20,
			unit: "%RH",

		}, {
			icon: "/static/img/sj.png",
			name: "土壤导电率",
			num: 20,
			unit: "μS/cm",

		}, {
			icon: "/static/img/CTPH.png",
			name: "土壤PH值",
			num: 20,
			unit: "PH",

		}, {
			icon: "/static/img/AHL.png",
			name: "土壤含氮量",
			num: 20,
			unit: "mg/kg",

		}, {
			icon: "/static/img/AQHL.png",
			name: "土壤含磷量",
			num: 20,
			unit: "mg/kg",

		}, {
			icon: "/static/img/AQHL.png",
			name: "土壤含钾量",
			num: 20,
			unit: "mg/kg",

		},

	])
	const dataobj = reactive({})
	const tdId = ref('')
	const palntId = ref('')
	// 绑定设备
	const equipmentId = ref("")
	const bindFn = () => {
		biproduct()
		showModal.value = false
	}
	// 扫码
	const scanFn = () => {
		uni.scanCode({
			success: function(res) {
				// console.log('条码类型：' + res.scanType);
				console.log('条码内容：' + res.result.split('='));
				uni.navigateTo({
					url: '/pages/landDetail/results?id=' + res.result.split('=')[1]
				})
			}
		});
	}

	const show = ref(false)
	const showLoading = ref(false)
	const columns = reactive([]);
	// 环境监测数据
	const environmentList = reactive([{
			icon: "/static/img/FS.png",
			name: "风速",
			num: 20,
			unit: "%",

		}, {
			icon: "/static/img/FX.png",
			name: "风向",
			num: 20,
			unit: "%",

		}, {
			icon: "/static/img/CTWD.png",
			name: "温度",
			num: 20,
			unit: "℃",

		}, {
			icon: "/static/img/SD.png",
			name: "湿度",
			num: 20,
			unit: "%RH",

		}, {
			icon: '/static/img/CO2.png',
			name: "二氧化碳",
			num: 20,
			unit: "ppm",

		}, {
			icon: "/static/img/sj.png",
			name: "气压",
			num: 20,
			unit: "KPa",

		}, {
			icon: "/static/img/JY.png",
			name: "雨量",
			num: 20,
			unit: "mm",

		}, {
			icon: "/static/img/GZ.png",
			name: "光照量",
			num: 20,
			unit: "Lux",

		},

	])
	onShow(() => {
		getlist()
		getlist1()
		getlist2()
	})


	//列表获取
	const getlist = () => {
		lindexist({
			land_id: tdId.value
		}).then((res) => {

			if (res.code == 1) {
				getlist3(res.data.id)
				Object.assign(dataobj, res.data);

				if (res.data && res.data.monitor && res.data.monitor.soil_monitor_data) {
					soilList[0].num = res.data.monitor.soil_monitor_data.temperature
					soilList[1].num = res.data.monitor.soil_monitor_data.moisture
					soilList[2].num = res.data.monitor.soil_monitor_data.conductivity
					soilList[3].num = res.data.monitor.soil_monitor_data.ph
					soilList[4].num = res.data.monitor.soil_monitor_data.n_content
					soilList[5].num = res.data.monitor.soil_monitor_data.p_content
					soilList[6].num = res.data.monitor.soil_monitor_data.k_content
				}
				if (res.data && res.data.monitor && res.data.monitor.air_monitor_data) {
					environmentList[0].num = res.data.monitor.air_monitor_data.wind_speed
					environmentList[1].num = res.data.monitor.air_monitor_data.wind_direction
					environmentList[2].num = res.data.monitor.air_monitor_data.temperature
					environmentList[3].num = res.data.monitor.air_monitor_data.moisture
					environmentList[4].num = res.data.monitor.air_monitor_data.co2_content
					environmentList[5].num = res.data.monitor.air_monitor_data.pressure
					environmentList[6].num = res.data.monitor.air_monitor_data.rainfall
					environmentList[7].num = res.data.monitor.air_monitor_data.light_intensity

				}


			}
		})

	}

	const getlist1 = () => {
		landlist({
			page_no: 1,
			page_size: 99995,
			keyword: ''
		}).then((res) => {

			if (res.code == 1) {
				if (res.data.length > 0) {
					columns.splice(0, columns.length, ...res.data.map((step, index) => {
						return {
							name: step.title,
							id: step.id,
						};
					}));
				}


			}
		})

	}

	const getlist2 = () => {
		landproduct().then((res) => {
			if (res.code == 1) {
				range.splice(0, range.length, ...res.data.map((step, index) => {
					return {
						text: step.name,
						value: step.product_id,
					};
				}));

				// console.log(res)
			}
		})


	}
	const getlist3 = (id) => {
		plantlist({
			land_id: id
		}).then((res) => {
		
			if (res.code == 1) {
				 columns2.splice(0)
				let arr1 = []
				arr1.splice(0, arr1.length, ...res.data.map((step, index) => {
					return {
						name: step.kind + '-' + step.id,
						id: step.id,
					};
				}));
				columns2.push(arr1)

			}
		})


	}

	//关闭弹窗
	const closeModal = () => {
		showModal.value = false
		equipmentId.value = ''
	}



	//绑定产品
	const biproduct = () => {
		bindproduct({
			land_id: dataobj.id,
			product_id: equipmentId.value
		}).then((res) => {
			if (res.code == 1) {
				uni.$u.toast(res.msg);
				// this.dataobj=res.data
				equipmentId.value = ''
			}
			console.log(res)
		})
	}
	const confirm2 = (e) => {
		showPicker.value = false
	
		uni.navigateTo({
			url: e.value[0].route + '?id=' + palntId.value
		})
	}
	const confirm1 = (e) => {
		palntId.value = e.value[0].id
		showPicker1.value = false
		showPicker.value = true
	}
	const confirm = (e) => {
		// console.log(e)
		tdId.value = e.id

		getlist()

		show.value = false
		showLoading.value = true
		setTimeout(() => {
			showLoading.value = false
		}, 1000)
	}
	const getNowTimeFn = () => {
		const now = new Date();
		const hour = now.getHours();
		const minute = now.getMinutes();
		const formattedHour = hour < 10 ? '0' + hour : hour;
		const formattedMinute = minute < 10 ? '0' + minute : minute;
		const currentTime = formattedHour + ':' + formattedMinute;
		return currentTime
	}

	const navgo = () => {

		uni.navigateTo({
			url: '/pages/landDetail/index?id=' + dataobj.id
		})
	}
	const navto = (url) => {
		uni.navigateTo({
			url
		})
	}
	getNowTimeFn()
</script>
<style lang="scss">
	page {
		background-color: $theme-bg-color;
	}

	.top {
		background-color: #34D190;
		position: fixed;
		z-index: 999999;
		width: 750rpx;


	}

	.coneng-detail {
		width: 478rpx;
		height: 341rpx;
		background: #FFFFFF;
		border-radius: 6px 6px 6px 6px;
		opacity: 1;
		font-size: 25rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		color: #737373;
		display: flex;
		justify-content: center;
		align-items: center;
		text-align: center;
		margin: 70rpx auto;

		image {
			width: 280rpx;
			height: 142rpx;
			margin-bottom: 20rpx;


		}
	}

	.add-thing {
		width: 266.36rpx;
		height: 66.59rpx;
		border-radius: 42.06rpx 42.06rpx 42.06rpx 42.06rpx;
		border: 1px solid $theme-main-color;
		margin: 20rpx auto;
		line-height: 66rpx;
		text-align: center;
		color: $theme-main-color;
	}

	.Modal {
		padding: 60rpx 40rpx;
		display: flex;

		text {
			margin-top: 5rpx;
		}

		.Modal-btn {
			display: flex;
			justify-content: space-around;
			padding-bottom: 20rpx;

			.Modal-btna {
				width: 120rpx;
				height: 60rpx;
				line-height: 60rpx;
				font-size: 25rpx;
				background-color: #e5e0d8;
				text-align: center;
				border-radius: 30rpx;
				color: #fff;
			}

			.Modal-btnb {
				width: 120rpx;
				height: 60rpx;
				line-height: 60rpx;
				font-size: 25rpx;
				background-color: #34D190;
				text-align: center;
				border-radius: 30rpx;
				color: #fff;

			}
		}
	}

	.uni-select__selector {
		z-index: 9999 !important;
	}

	.nav {
		height: 70rpx;
		display: flex;
		justify-content: flex-end;
		box-sizing: border-box;
		align-items: center;
		padding-right: 40rpx;
	}

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

		.changeLand {
			width: 200rpx;
			height: 70rpx;
			line-height: 70rpx;
			text-align: center;
			border-radius: 30rpx;
			background-color: $theme-main-color;
			color: white;
		}
	}

	.content {
		// min-height: 92vh;	
		padding: 0 30rpx;
		padding-top: 20rpx;
		box-sizing: border-box;
	}

	.soil-monitor {
		margin-top: 40rpx;

		.head {

			.tit {
				font-size: 33.29rpx;
				color: black;
			}

			.flushed-time {
				display: flex;
				font-size: 26.29rpx;
				color: #737373;
				margin-top: 10rpx;
			}
		}

		.monitor-content {
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;

			.monitor-card {
				margin-top: 20rpx;
				background-color: white;
				padding: 30rpx 20rpx;
				border-radius: 30rpx;
				width: 332.94rpx;
				box-sizing: border-box;
				height: 212.03rpx;

				.name {
					font-size: 29.79rpx;
					display: flex;
					align-items: center;
				}

				.num {
					margin-top: 30rpx;
					color: $theme-main-color;

				}
			}

		}
	}

	.video {
		margin-top: 20rpx;
		width: 100%;
		height: 300rpx;
	}

	.empty {
		height: 100vh;
		background-color: $theme-bg-color;
		position: relative;

		.addLand {
			width: auto;
			height: 100rpx;
			color: grey;
			position: absolute;
			top: 50%;
			left: 50%;
			transform: translate(-50%, -50%);

			.addBtn {
				margin-top: 20rpx;
				width: 250rpx;
				height: 70rpx;
				border: 1px solid $theme-main-color;
				line-height: 70rpx;
				border-radius: 50rpx;
				text-align: center;
			}

		}
	}
</style>