<template>
  <div class="down-layout">
    <el-form label-width="auto" style="max-width: 600px">
      <el-form-item label="场景码">
        <el-input v-model="sceneCode" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">下载数据</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script setup lang="ts">
import { ref } from "vue";
import { openLoading, closeLoading } from "@/helper/loading";
import { ElMessage } from "element-plus";
import { downloadPointsXLSL } from "@/util/pc4xlsl";

const sceneCode = ref("");
const onSubmit = async () => {
  if (!sceneCode.value) {
    ElMessage.error("请输入场景码");
  }

  openLoading();

  try {
    const data = await fetch(
      `https://4dkk.4dage.com/scene_view_data/${sceneCode.value}/images/vision.txt`
    ).then((res) => res.json());
    const points: number[][] = [];
    const desc: { title: string; desc: string }[] = [];
    const name = sceneCode.value;

    console.log(data);
    data.sweepLocations.forEach((item: any) => {
      const gga = item.ggaLocation;
      console.log(gga);
      if (gga.StatusIndicator != 4 || !(gga.lat && gga.lon)) {
        return;
      }
      points.push([Number(gga.lat), Number(gga.lon), Number(gga.alt)]);
      desc.push({ title: item.uuid, desc: item.uuid });
    });

    if (points.length === 0) {
      ElMessage.error("当前场景没有有效点位数据");
    } else {
      await downloadPointsXLSL(points, desc, name);
      ElMessage.success("文件下载成功");
    }
  } catch (e) {
    ElMessage.error("该场景不存在");
  }

  closeLoading();
};
</script>

<style scoped>
.down-layout {
  display: flex;
  width: 100%;
  height: 100%;
  align-items: center;
  justify-content: center;
}
</style>
