<template>
	<view :style="{height:'100vh'}">
		<u-status-bar></u-status-bar>
		<u-navbar :title="taskTitle" :autoBack="true" :titleStyle="{
				fontSize:'18px'
			}" :height='44' :safeAreaInsetTop=true leftIconSize='20' leftIconColor='rgb(153, 153, 153)'>
		</u-navbar>
		<view class="" 
			style="margin-top: 44px;;background-color: #f0f0f0;border-top: 1px solid #f0f0f0; height:calc(100vh - 44px)">
			<view style="margin: 10px;background-color: #fff;padding: 10px;" v-show="btnStatus == 0">
				<view class="item">
					<view class="blueTiao"></view>
					<view class="title">
						基本信息
					</view>
				</view>
				<view class="contentItem">
					<view class="itemC">
						<text>任务标题</text>
						<text>{{taskTitle}}</text>
					</view>
					<view class="itemC">
						<text>任务类型</text>
						<text>{{taskType == 1? "日常巡查" : taskType == 2 ? "特别检查" : taskType == 3 ? "汛前巡检" : '' }}</text>
					</view>
					<view class="itemC">
						<text>任务内容</text>
						<text>{{taskContent ? taskContent : ''}}</text>
					</view>
					<view class="itemC">
						<text>开始日期</text>
						<text>{{startDate}}</text>
					</view>
					<view class="itemC">
						<text>结束日期</text>
						<text>{{endDate}}</text>
					</view>
				</view>

			</view>
			<view v-show="status !== 2 && btnStatus == 0" style="display:flex;justify-content: center;margin-top: 20%;">
				<view v-show="status !== 2 && btnStatus == 0" class="circle-btn" @click="startXc">{{statusType[status]}}
				</view>
			</view>
			<view class="" style="height: calc(100vh - 50px);overflow: auto;">


				<view v-show="btnStatus != 0" style="margin: 10px;background-color: #fff;padding: 10px;height: calc(100% - 40px);">
					<view class="item">
						<view class="blueTiao"></view>
						<view class="title">
							巡检项
						</view>
					</view>
					<view class="contentItem1" style="height: calc(100% - 32px);">
						<db-form :xjItem="xjItem"></db-form>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import moment from 'moment'
	import DbForm from './dbForm.vue'
	export default {
		components: {
			DbForm
		},
		data() {
			return {
				taskTitle: '',
				taskType: '',
				taskContent: '',
				startDate: '',
				endDate: '',
				status: '',
				id: '',
				btnStatus: 0,
				xjItem: [],
				list: ["正常", "异常"],
				current: 3,
				statusType: {
					0: '开始巡查',
					1: '继续巡查',
					2: '已完成'
				},
			};
		},
		methods: {
			// 开始巡查
			async startXc() {
				try {
					if (this.status == 0) {
						const {
							data
						} = await uni.$http.get(`/gunshiApp/ykz/inspect/task/startInspect/${this.id}`)
						if (data.code == 200) {
							this.btnStatus = 1;
							this.getXjItem(this.id)
						}
					} else if (this.status == 1) {
						this.btnStatus = 1;
						this.getXjItem(this.id)
					}
					this.getLocationWithInterval();

				} catch (error) {
					uni.$showMsg();
				}
			},
			// 获取巡查项
			async getXjItem(id) {
				try {
					const {
						data
					} = await uni.$http.get(`/gunshiApp/ykz/inspect/detail/info?taskId=${id}`)
					if (data.code == 200) {
						this.xjItem = data.data
					}
				} catch (error) {
					uni.$showMsg();
				}
			},

			getLocationWithInterval() {
				this.getLocation(); // 首次获取经纬度信息
				// 设置定时器，每隔5秒获取一次经纬度信息
				let timer = setInterval(() => {
					this.getLocation();
				}, 300000); // 五分钟
				uni.setStorageSync("timer", timer);
			},
			// 获取经纬度
			getLocation() {
				uni.getLocation({
					type: 'gcj02',
					success: (res) => {
						this.latitude = res.latitude;
						this.longitude = res.longitude;
						this.sendLocation(res.latitude, res.longitude)
					},
					fail: (err) => {
						console.log('Failed to get location:', err);
					}
				});
			},
			// 将经纬度传给服务器
			async sendLocation(latitude, longitude) {
				try {
					const {
						data
					} = await uni.$http.post(`/gunshiApp/ykz/inspectTaskTrack/insert`, {
						taskId: this.id,
						lttd: latitude,
						lgtd: longitude
					})
					if (data.code == 200) {
						console.log('Location sent successfully')
					}
				} catch (error) {
					uni.$showMsg();
				}
			}

		},
		onLoad(option) {
			this.taskTitle = option.taskTitle
			this.taskType = option.taskType
			this.taskContent = option.taskContent == "null" ? null : option.taskContent
			this.startDate =  option.startDate == "null" ? "" : option.startDate
			this.endDate = option.endDate == "null" ? "" : option.endDate
			this.status = option.status
			this.id = option.id
		}
	}
</script>

<style lang="scss" scoped>
	.myTitleStyle {
		font-size: 30px;
		background-color: red;
		height: 200px;
	}

	.tabsClass {
		margin-top: 44px;
		height: 44px;
		padding: 0 40px;
		border-top: 1px solid #eee;
		border-bottom: 1px solid #eee;
	}

	.circle-btn {
		width: 120px;
		height: 120px;
		background-color: #007aff;
		border-radius: 50%;
		color: #fff;
		text-align: center;
		line-height: 120px;
	}

	.item {

		padding-bottom: 10px;
		display: flex;
		align-items: center;

		// justify-content: space-between;
		.blueTiao {
			background-color: #007aff;
			width: 5px;
			height: 16px;
			margin-right: 10px;
		}

		.title {
			border-width: 0px;
			font-family: '微软雅黑 Bold', '微软雅黑 Regular', '微软雅黑', sans-serif;
			font-weight: 700;
			font-style: normal;
			font-size: 15px;
			color: #666666
		}

		.titleRight {
			display: flex;

			.border {
				border-width: 0px;
				background-color: rgba(236, 245, 255, 1);
				box-sizing: border-box;
				border-width: 1px;
				border-style: solid;
				border-color: rgba(187, 220, 255, 1);
				border-radius: 5px;
				box-shadow: none;
				width: 58px;
				height: 25px;
				color: #689FFF;
				text-align: center;
			}
		}

	}

	.contentItem {
		.itemC {
			display: flex;
			justify-content: space-between;
			display: flex;
			color: #666666;
			padding: 10px;
			border-bottom: 1px solid #f0f0f0;
		}

		.itemNo {
			color: #666666;
			padding: 10px;
		}
	}
</style>