<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">
					<view class="" @click="navgo">
						<view class="">
							土地名称: {{dataobj.title}}
						</view>
						<!-- <view class="" style="margin: 10rpx 0;">
							农作物: 洋芋
						</view> -->
						<view class="">
							面积: {{dataobj.total_area}}亩
						</view>
						<view class="">
							面积: {{dataobj.total_area}}亩
						</view>
					</view>
					<view class="changeLand" @click="show=true">
						切换土地
					</view>

				</view>
			</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>
				</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">
					<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>
			<!-- 环境监测 -->
			<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">
					<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>

			<!-- 组件 -->

			<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="">
			<u-modal title="绑定设备" :show="showModal" @confirm='bindFn' @cancel="showModal=false" :showCancelButton='true'
				:closeOnClickOverlay="true" @close="showModal=false" :title="title">
				<view class="slot-content">
					<view class="" style="display: flex;align-items: center;">
						设备编号: <up-input style="margin-left: 20rpx;" placeholder="请输入设备编号" border="surround"
							v-model="equipmentId"></up-input>
					</view>
				</view>
			</u-modal>
		</view>
	</view>
</template>

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

	const showModal = ref(false)


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

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

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

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

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

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

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

		},

	])
	const dataobj = reactive({})
	const tdId = ref()
	// 绑定设备
	const equipmentId = ref("")
	const bindFn = () => {
		bindevice()
		showModal.value = false
	}
	// 扫码
	const scanFn = () => {
		uni.scanCode({
			success: function(res) {
				console.log('条码类型：' + res.scanType);
				console.log('条码内容：' + res.result);
			}
		});
	}

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

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

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

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

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

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

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

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

		},

	])
	onLoad(() => {


		getlist()
		
		getlist1()
	})
	const getlist = () => {
		lindexist({
			land_id: ''
		}).then((res) => {
			console.log(res)
			if (res.code == 1) {
				Object.assign(dataobj, res.data);

			}
		})

	}

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

			if (res.code == 1) {

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

			}
		})

	}


	const bindevice = () => {
		binddevice({
			land_id: '2222',
			device_code: equipmentId.value
		}).then((res) => {
			if (res.code == 1) {
				uni.$u.toast(res.msg);
				// this.dataobj=res.data
			}
			console.log(res)
		})
	}
	const confirm = (e) => {
		console.log(e)
		
		
		
		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" scoped>
	.top {
		background-color: #34D190;
		position: fixed;
		z-index: 999999;
		width: 750rpx;


	}

	.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;
		background-color: $theme-bg-color;
		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>