<template>
  <div class="map-container">
    <div class="container-top">
      <div class="next-stand">
        <img src="../assets/images/next.png" alt="" />
        <span>0</span>
      </div>
      <div class="real-time">
        <img src="../assets/images/realTime.png" alt="" />
        <span>{{ mileData.mile ? ileData.mile : 0 }}</span>
      </div>
    </div>
    <div class="albuginea backBtn" @click="initTilesetLayer()">
      {{ show3D ? "关闭" : "开启" }}三维
    </div>
    <div class="albuginea" @click="$router.back()">
      <i class="el-icon-back"></i> 返回
    </div>
    <mars-map @mapLoad="mapLoad" :options="options"></mars-map>
    <div class="video-list">
      <grids></grids>
    </div>
  </div>
</template>

<script>
import MarsMap from "@/components/mars-map";
import grids from "@/components/grids.vue";
import { getCarIdInfo } from "@/api/yunkun/index.js";
import { historyLine, carMidle } from "@/api/yunkun/yunkun.js";
export default {
  data() {
    const basePathUrl = window.basePathUrl || "";
    return {
      mileData: {
        mile: "", //已经行驶多少里程
      },
      queryParams: {
        startTime: undefined,
        endTime: undefined,
      },
      map: null,
      mapLayer: {},
      show3D: false,
      options: {
        scene: {
          center: {
            lat: 31.184348,
            lng: 120.625126,
            alt: 6764.6,
            heading: 357.9,
            pitch: -31.5,
          },
        },
        control: {
          contextmenu: { preventDefault: false, hasDefault: false },
        },
      },
      Line: [],
      carUrl: basePathUrl + "static/qiche.gltf",
    };
  },
  components: { MarsMap, grids },
  beforeDestroy() {},
  created() {},
  methods: {
    /**地图渲染完毕 */
    mapLoad(map) {
      this.map = map;
      map.fixedLight = true; // 固定光照，避免gltf模型随时间存在亮度不一致。
      // 创建矢量图层
      this.mapLayer.Line = new mars3d.layer.GraphicLayer();
      this.map.addLayer(this.mapLayer.Line);

      this.mapLayer.car = new mars3d.layer.GraphicLayer();
      this.map.addLayer(this.mapLayer.car);

      this.createLine();
      this.createCar();
    },
    /**
     * 加载姑苏区三维图层
     */
    initTilesetLayer() {
      if (this.map) {
        if (!this.mapLayer.tiles3dLayer) {
          this.mapLayer.tiles3dLayer = new mars3d.layer.TilesetLayer({
            name: "姑苏区建筑物",
            url: "https://www.jichuanglanhai.com:88/3dtiles/yunkun/tileset.json",
            maximumScreenSpaceError: 16,
            maximumMemoryUsage: 1024 / 2,
            dynamicScreenSpaceError: false,
            skipLevelOfDetail: true,
            preferLeaves: true,
            flyTo: false,
            style: {
              color: {
                conditions: [["true", `color("rgba(42, 160, 224, 1)")`]],
              },
            },
          });
          this.map.addLayer(this.mapLayer.tiles3dLayer);
          this.show3D = true;
        } else {
          this.mapLayer.tiles3dLayer.show = !this.mapLayer.tiles3dLayer.show;
          this.show3D = !this.show3D;
        }
      } else {
      }
    },
    // 绘制历史轨迹线 -- 起点坐标
    createLine() {
      this.Line = [
        [120.60011, 31.26247],
        [120.599811, 31.267667],
        [120.599341, 31.27582],
        [120.59705, 31.278812],
        [120.599466, 31.279271],
        [120.61856, 31.28155],
        [120.642973, 31.284112],
      ];
      const marker = new mars3d.graphic.BillboardEntity({
        position: this.Line[0],
        style: {
          image: require("../assets/images/origin.png"),
          scale: 1,
          horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
          verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
          clampToGround: true,
          pixelOffsetY: 5,
        },
      });
      this.mapLayer.Line.addGraphic(marker);

      const line = new mars3d.graphic.PolylineEntity({
        positions: this.Line,
        style: {
          width: 10,
          color: "#00F7AC",
          outline: true,
          outlineColor: "#008DF8",
          outlineWidth: 5,
          clampToGround: true,
        },
      });
      this.mapLayer.Line.addGraphic(line);

      // 创建起始点坐标
    },

    //创建实时车辆漫游
    async createCar() {
      const lastPoint = this.Line[this.Line.length - 1];
      const car = new mars3d.graphic.FixedRoute({
        name: "漫游对象",
        speed: 45,
        positions: [
          lastPoint,
          [120.6554, 31.285252, 13.6],
          [120.654003, 31.298916, 10.8],
          [120.649845, 31.299078, 14.2],
          [120.647362, 31.308061, 17.4],
          [120.645631, 31.314448, 16.1],
          [120.638285, 31.312808, 10],
        ],
        // camera: {
        //   type: "gs",
        //   heading: 0,
        //   radius: 3000,
        // },
        model: {
          show: true,
          url: this.carUrl,
          scale: 0.5,
          minimumPixelSize: 30,
          silhouette: true,
          silhouetteColor: "#FFB200",
        },
        circle: {
          radius: 200,
          materialType: mars3d.MaterialType.CircleWave,
          materialOptions: {
            color: "#FFB200",
            count: 2,
            speed: 6,
          },
        },
        polyline: {
          width: 10,
          color: "#00F7AC",
          outline: true,
          outlineColor: "#008DF8",
          outlineWidth: 5,
          clampToGround: true,
        },
      });
      this.mapLayer.car.addGraphic(car);
      let res = await getCarIdInfo("1328CC51-C858-4FFA-8714-4F354BB49CF8");

      this.bindPopup(car, res.data);
      car.openPopup();
      car.start();
    },

    // 车辆弹窗
    bindPopup(fixedRoute, data) {
      const userList = data.userList.map((item) => {
        return `<div class="popup-item">
              <div class="popup-lable">${item.name}：</div>
              <div class="popup-value">${item.ename}</div>
            </div>`;
      });

      const zbList = data.zbList.map((item) => {
        return `<div class="popup-item">
              <div class="popup-lable">${item.thingType}：</div>
              <div class="popup-value">${item.type}</div>
            </div>`;
      });

      let html = `<div class="diy-popup">
        <div class="pointer"></div>
            <div class="popup-title">001号线_01_早送_守押<br/>
              001_苏E12345
              </div>
            <div class="popup-item">
                <div class="popup-lable">线路类型：</div>
                <div class="popup-value">${data.lineType}</div>
              </div>
              <div class="sub-title">装备信息</div>
              <div class="popup-grid">
                ${zbList.join("")}
                </div>
                <div class="sub-title">人员信息</div>
                     <div class="popup-grid">
                      ${userList.join("")}
                </div>
                <div class="sub-title">标的物信息</div>
                 <div class="popup-grid">
                      <div class="popup-item">
                <div class="popup-lable">标的物总数：</div>
                <div class="popup-value">${data.bdwInfo.count}只</div>
              </div>
                </div>
          </div>`;

      fixedRoute.bindPopup(html, {
        className: "carPopup",
        offsetX: 280,
        offsetY: 100,
        // closeButton: false,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.albuginea {
  cursor: pointer;
  position: absolute;
  top: 103px;

  left: calc(554px + 24px);
  z-index: 100;
  width: 80px;
  height: 32px;
  text-align: center;
  line-height: 32px;
  background: #032b57;
  border-radius: 2px;
  border: 1px solid #0084ff;
  font-weight: 400;
  font-size: 14px;
  color: #ffffff;
}
.backBtn {
  top: 145px;
}
.albuginea:hover {
  background: #0084ff;
}
.container-top {
  position: fixed;
  top: 100px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  .next-stand {
    margin-right: 40px;
  }
  & > div {
    width: 200px;
    img {
      height: 38px;
      width: 100%;
      display: block;
      margin-bottom: 10px;
    }
    span {
      display: block;
      font-size: 40px;
      color: #ffffff;
      line-height: 51px;
      letter-spacing: 1px;
      text-align: center;
      font-style: normal;

      background: linear-gradient(270deg, #00e5ff 0%, #d8d8d8 100%);
      /* 将背景限制在文字区域内 */
      -webkit-background-clip: text;
      background-clip: text;
      /* 设置文字颜色，会被背景图像遮住 */
      color: transparent;
      font-family: "DIN-Regular-2";
    }
  }
}
.video-list {
  position: absolute;
  bottom: 30px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 100;
  padding: 10px;
  display: flex;
  align-items: center;
  border: 1px solid #415367;
  border-radius: 10px;
  background: rgba(28, 31, 34, 0.6);
}
</style>
