<template>
  <div>

    <div class="dialog-h-content scroll-h">

      <div v-if="initLoading"
           class="m-row">
        <div class="m-info">
          <div class="info-prop">
            <span class="info-title-2">所属组织：</span>
            <span>{{ dataForm.orgName||'--'  }}</span>
          </div>

          <div class="info-prop">
            <span class="info-title-2">检测点名称：</span>
            <span>{{ dataForm.name||'--' }}</span>
          </div>

          <div class="info-prop">
            <span class="info-title-2">服务时间：</span>
            <span>{{ dataForm.serveTime||'--' }}</span>
          </div>

          <div class="info-prop">
            <span class="info-title-2">咨询电话：</span>
            <span>{{ dataForm.mobile||'--'  }}</span>
          </div>
          <div class="info-prop">
            <span class="info-title-2">检测点地址：</span>
            <span>{{ dataForm.address||'--'  }}</span>
          </div>

          <div class="info-prop">
            <span class="info-title-2">地图位置：</span>
            <div class="div_map">

              <div id="app_detail"></div>

            </div>
          </div>

        </div>
      </div>

    </div>
    <div class="div-btn">
      <el-button size="small"
                 @click="closeSubmit">关 闭</el-button>

    </div>

  </div>
</template>

<script>
import { Loading } from 'element-ui' // 引入Loading服务
import { requestPost, requestGet } from '@/js/dai/request'

var map;
var search;
var markers;
var geocoder;
var infoWindowList;

let loading // 加载动画
export default {
  data () {
    return {
      initLoading: false,
      dataForm: {
        id: ''
      },

    };
  },
  computed: {

  },
  created () {

  },
  props: {

  },
  methods: {
    diaDestroy () {
      if (map) {
        map.destroy()
      }
    },
    closeSubmit () {
      this.diaDestroy()
      this.$emit("closeDialog");
    },

    async init (id) {

      this.startLoading()

      this.dataForm.id = id
      await this.getInfo();
      this.initLoading = true

      this.$nextTick(() => {
        this.initMap()
      })

      this.endLoading()

    },

    // 获取信息
    async getInfo () {
      let url = `/epmetuser/icPointNucleicMonitoring/${this.dataForm.id}`
      const { data, code, msg } = await requestGet(url)

      if (code === 0) {
        this.dataForm = data;
      } else {

        this.$message.error(msg)
      }
    },

    // 地图初始化函数，本例取名为init，开发者可根据实际情况定义
    initMap () {
      // 定义地图中心点坐标
      let { latitude, longitude } = this.$store.state.user;
      console.log('lat' + latitude + ',lon' + longitude)

      if (this.dataForm.latitude && this.dataForm.longitude) {
        latitude = this.dataForm.latitude
        longitude = this.dataForm.longitude
      }
      if (!latitude || latitude == "" || latitude == "0") {
        latitude = 39.9088810666821;
        longitude = 116.39743841556731;
      }
      var center = new window.TMap.LatLng(latitude, longitude)
      // 定义map变量，调用 TMap.Map() 构造函数创建地图
      map = new window.TMap.Map(document.getElementById("app_detail"), {
        center: center, // 设置地图中心点坐标
        zoom: 17.2, // 设置地图缩放级别
        pitch: 43.5, // 设置俯仰角
        rotation: 45, // 设置地图旋转角度
      });
      search = new window.TMap.service.Search({ pageSize: 10 });
      // 新建一个地点搜索类
      markers = new TMap.MultiMarker({
        map: map,
        geometries: [],
      });
      infoWindowList = Array(10);

      geocoder = new TMap.service.Geocoder(); // 新建一个正逆地址解析类

      // 监听地图平移结束
      map.on("panend", () => {
        this.handleMoveCenter();
      });
      this.handleMoveCenter()
      this.convert();
    },
    handleMoveCenter () {
      //修改地图中心点
      const center = map.getCenter();
      const lat = center.getLat();
      const lng = center.getLng();
      this.dataForm.latitude = lat;
      this.dataForm.longitude = lng;
      this.setMarker(lat, lng);
      this.convert(lat, lng);
    },
    // 地图查询
    handleSearchMap () {
      infoWindowList.forEach((infoWindow) => {
        infoWindow.close();
      });
      infoWindowList.length = 0;
      markers.setGeometries([]);
      // 在地图显示范围内以给定的关键字搜索地点
      search
        .searchRectangle({
          keyword: this.dataForm.address,
          bounds: map.getBounds(),
        })
        .then((result) => {
          let { data } = result;
          if (Array.isArray(data) && data.length > 0) {
            const {
              location: { lat, lng },
            } = data[0];

            map.setCenter(new TMap.LatLng(lat, lng));
            this.setMarker(lat, lng);
            this.dataForm.latitude = lat;
            this.dataForm.longitude = lng;
            this.convert();
          } else {
            this.$message.error("未检索到相关位置坐标");
          }
        });
    },
    convert (lat, lng) {
      markers.setGeometries([]);
      // var input = document.getElementById('location').value.split(',');
      let location;
      if (lat && lng) {
        location = new TMap.LatLng(lat, lng);
      } else {
        location = new TMap.LatLng(
          this.dataForm.latitude,
          this.dataForm.longitude
        );
      }

      // map.setCenter(location);
      markers.updateGeometries([
        {
          id: "main", // 点标注数据数组
          position: location,
        },
      ]);
      geocoder
        .getAddress({ location: location }) // 将给定的坐标位置转换为地址
        .then((result) => {
          this.dataForm.address =
            this.dataForm.address !== "" &&
              this.dataForm.address !== null &&
              (this.dataForm.longitude === "" || this.dataForm.longitude === null)
              ? this.dataForm.address
              : result.result.address;
          // 显示搜索到的地址
          console.log(this.dataForm.address);
        });
    },

    setMarker (lat, lng) {
      markers.setGeometries([]);
      markers.add([
        {
          id: "4",
          styleId: "marker",
          position: new TMap.LatLng(lat, lng),
          properties: {
            title: "marker4",
          },
        },
      ]);
    },

    // 开启加载动画
    startLoading () {
      loading = Loading.service({
        lock: true, // 是否锁定
        text: '正在加载……', // 加载中需要显示的文字
        background: 'rgba(0,0,0,.7)' // 背景颜色
      })
    },
    // 结束加载动画
    endLoading () {
      // clearTimeout(timer);
      if (loading) {
        loading.close()
      }
    }

  },
};
</script>
<style scoped>
.position_label >>> .el-form-item__label::before {
  color: #fff !important;
}
.submit {
  text-align: center;
  margin: auto;
}
.form-class .el-input {
  width: 75%;
}
.form-class .el-textarea {
  width: 75% !important;
}
</style>
<style lang="scss" scoped >
@import "@/assets/scss/modules/management/detail-main.scss";
</style>

