<template>
	<div class="flex align-center">
		<el-select v-model="curAnalogSn" filterable placeholder="请选择船只">
			<template v-for="item in $store.state.data.analogList">
				<el-option :key="item.id" v-if="isOnline(item)&&item.isSail == '1'" :label="`${item.iboatName} - 在航`"
					:value="item.sn">
					<div class="flex align-center text-el-primary">
						{{ item.iboatName }}
						<span class="padding-lr-xs">-</span>
						<span class="">在航</span>
					</div>
				</el-option>
			</template>
			<template v-for="item in $store.state.data.analogList">
				<el-option :key="item.id" v-if="isOnline(item)&&item.isSail != '1'" :label="`${item.iboatName} - 停航`"
					:value="item.sn">
					<div class="flex align-center text-el-regular">
						{{ item.iboatName }}
						<span class="padding-lr-xs">-</span>
						<span>停航</span>
					</div>
				</el-option>
			</template>
			<template v-for="item in $store.state.data.analogList">
				<el-option :key="item.id" v-if="item.isSail == '4'&&item.status == 10" :label="`${item.incineratorName} - 运行中`"
					:value="item.sn">
					<div class="flex align-center text-el-primary">
						{{ item.incineratorName }}
						<span class="padding-lr-xs">-</span>
						<span>运行中</span>
					</div>
				</el-option>
			</template>
			<template v-for="item in $store.state.data.analogList">
				<el-option :key="item.id" v-if="item.isSail == '4'&&item.status == 20" :label="`${item.incineratorName} - 停机中`"
					:value="item.sn">
					<div class="flex align-center text-el-secondary">
						{{ item.incineratorName }}
						<span class="padding-lr-xs">-</span>
						<span>停机中</span>
					</div>
				</el-option>
			</template>
			<template v-for="item in $store.state.data.analogList">
				<el-option :key="item.id" v-if="isOffline(item)&&item.isSail != '4'" :label="`${item.iboatName} - 离线`" :value="item.sn">
					<div class="flex align-center text-el-secondary">
						{{ item.iboatName }}
						<span class="padding-lr-xs">-</span>
						<span class="">离线</span>
					</div>
				</el-option>
			</template>
		</el-select>
	</div>
</template>

<script setup>
	import { getAnalog } from "@/api/ship/ship";
	import { isOffline, isOnline } from '@/utils/analog';
	import { getSensorLatestData, listSensor } from "@/api/Incinerator/sensorApi";
	const {
		proxy
	} = getCurrentInstance();
	let curAnalogSn = ref(null);
	let _startTime = 0;

	onUnmounted(() => {
		clearInterval(proxy.$store.state.data.analogIntervalTimer);
		proxy.$store.state.data.analogIntervalTimer = null;
	});

	function getData() {
		getAnalog().then((response) => {
			if(1 == 0){
				proxy.$store.state.data.analogList = response.data;
			}else{
				incinerData(response.data);
			}
			if (response.data.length == 0) {
				proxy.$modal.error("当前账号未分配船只");
				return;
			}

			if (curAnalogSn.value == null) {
				curAnalogSn.value = proxy.$store.state.data.analogList[0].sn;
				proxy.$store.state.data.curAnalogSn = proxy.$store.state.data.analogList[0].sn;
			}
		});
	}
	function incinerData(_obj){
		let _arrs = [];
		_obj.forEach(item => {
			item["isSail"] = 4;
			getSensorData(item).then(dist => {
				_arrs.push({...item, ...dist});
				proxy.$store.state.data.analogList = _arrs;
			});
		});
	}
	function getSensorData(row){
		let _data = {
						"status": 20,
						"runTime": 0,
						"0a4": 113.70087,
						"0a5": 22.680448,
						"rows": []
					};
		return new Promise((resolve, reject) => {
			listSensor({
				pageSize: 999,
				pageNum: 1,
				incineratorSn: row.incineratorSn 
			}).then(res => {
				if(res.rows.length){
					let _list = res.rows;
					getSensorLatestData({
						sensorPath: "root.yzkj."+row.incineratorSn+".**"
					}).then(ponse => {
						if(ponse.data.length){
							let _arr = ponse.data;
							_list.forEach(litem => {
								let _ntem = _arr.filter(ndata => ndata.fields[0].stringValue.indexOf(litem.sensorFullPath) != -1);
								if(_ntem.length){
									if(litem.sensorPath.indexOf("bds") != -1){
										let _lon = _ntem.filter(ndata => ndata.fields[0].stringValue.indexOf("long") != -1);
										let _lat = _ntem.filter(ndata => ndata.fields[0].stringValue.indexOf("lati") != -1);
										_data["0a4"] = _lon.length?_lon[0].fields[1].stringValue:113.70087;
										_data["0a5"] = _lat.length?_lat[0].fields[1].stringValue:22.680448;
									}else if(litem.sensorPath.indexOf("state") != -1){
										let _status = _ntem.filter(ndata => ndata.fields[0].stringValue.indexOf(litem.sensorFullPath) != -1);
										_data["status"] = _status.length?_status[0].fields[1].stringValue:20;
									}else{
										let _measurements = JSON.parse(litem.measurements);
										let _measurementsDescription = JSON.parse(litem.measurementsDescription);
										let _unit = (litem.unit != null&&litem.unit.indexOf("[") != -1?JSON.parse(litem.unit):[]);
										for(let d=0;d<_measurements.length;d++){
											let _dlon = _ntem.filter(ndata => ndata.fields[0].stringValue.indexOf(litem.sensorFullPath+"."+_measurements[d]) != -1);
											_data.rows.push({
												name: _measurementsDescription[d],
												unit: _unit[d],
												value: _dlon.length?_dlon[0].fields[1].stringValue:null
											})
										}
										
									}
								}
							});
							resolve(_data);
						}else{
							resolve(_data);
						}
					})
				}else{
					resolve(_data);
				}
			})
		})
	}
	watch(curAnalogSn, (newValue) => {
		proxy.$store.state.data.curAnalogSn = newValue;
		// console.log(`船只切换${newValue}`);
	});

	watch(
		() => proxy.$store.state.data.curAnalogSn,
		(newValue, oldValue) => {
			curAnalogSn.value = newValue;
			localStorage.setItem("curAnalogSn", curAnalogSn.value);
			// console.log(`船只切换${newValue}`);
			const newAnalog = computed(
				() =>
				proxy.$store.state.data.analogList.find(
					(item) => item.sn == newValue
				) || {}
			);
			const oldAnalog = computed(
				() =>
				proxy.$store.state.data.analogList.find(
					(item) => item.sn == oldValue
				) || {}
			);
			console.log(newAnalog.value);
			console.log(oldAnalog);
			if (
				(newAnalog.value &&
					oldAnalog.value &&
					newAnalog.value.sn != oldAnalog.value.sn) ||
				oldAnalog.value == undefined
			) {
				if (isOffline(newAnalog.value)) {
					proxy.$modal.notify(
						`最后数据时间：${newAnalog.value.time}`,
						`${newAnalog.value.iboatName || newAnalog.value.incineratorName}已离线`,
						"bottom-right"
					);
				}
			}
		}
	);

	curAnalogSn.value = localStorage.getItem("curAnalogSn");
	clearInterval(proxy.$store.state.data.analogIntervalTimer);
	getData();
	proxy.$store.state.data.analogTimer = setInterval(() => {
		getData();
	}, proxy.$store.state.data.duration);
</script>