<template>
  <div class="map">
    <!-- <div class="build-btn locate" @click="getLocale">
      <img src="https://cdn.cunwuyun.cn/dvcp/h5/Location2.png" alt=""/>
      当前<br>位置
    </div> -->
    <div class="map-content">
      <AiTMap :map.sync="map" :lib.sync="lib" :ops="ops" :libraries="['service', 'tools']"/>
    </div>
    <div class="footer">
      <div class="btn" @click="confirm">确认定位</div>
    </div>
  </div>
</template>

<script>
import {mapState, mapActions} from 'vuex'

export default {
  data() {
    return {
      ops: {},
      lib: null,
      map: null,
      markerLayer: '',
      isFlag: false,
      latLng: {lat: '', lng: ''},
      address: ''
    }
  },
  computed: {...mapState(['user'])},
  mounted() {
    this.initMap()
  },
  onShow() {
    document.title = "选择上报位置"
  },
  methods: {
    ...mapActions(['injectJWeixin']),
    initMap(retryTimes = 0) {  //初始化地图
      this.$nextTick(() => {
        let {map} = this
        if (map) {
          map.setZoom(15)
          map.on("click", (evt) => {
            this.handleMapClick(evt)
          });
        } else {
          if (retryTimes < 10)
            setTimeout(() => {
              this.initMap(++retryTimes)
            }, 500)
          else console.error("地图渲染失败")
        }
      })
    },
    getLocale() {
      uni.getLocation({
        type: 'wgs84',
        geocode: true,
        success: (res) => {
          const lat = res.latitude;
          const lng = res.longitude;
          this.handleMapClick({latlng: {lat, lng}})
        },
      });
    },
    handleMapClick(evt) {
      console.log(evt)
      let {lib: TMap, map} = this
      if (this.markerLayer) {
        this.markerLayer.setMap(null);
      }
      this.markerLayer = new TMap.MultiMarker({id: 'marker-layer', map});
      this.markerLayer.add({
        position: evt.latLng
      });
      this.latLng = evt.latLng
      this.address = evt.poi.name
    },
    confirm() {
      if (!this.latLng.lat) {
        return this.$u.toast(`未获取到定位信息，无法定位`)
      }

      uni.$emit('chooseLat', {
        lat: this.latLng.lat,
        lng: this.latLng.lng, 
        address: this.address
      })
      uni.navigateBack({
        delta: 1
      })
    },
  }
}
</script>

<style lang="scss" scoped>
uni-page-body {
  height: 100%;
}

.map {
  height: 100vh;

  .build-btn {
    width: 80px;
    height: 160px;
    background: #FFF;
    box-shadow: 0 4px 8px 0 rgba(138, 138, 138, 0.5);
    border-radius: 8px;
    position: fixed;
    bottom: 136px;
    right: 24px;
    z-index: 99999;
    padding: 16px 16px 0;
    box-sizing: border-box;
    font-size: 24px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #666;
    line-height: 30px;

    &.locate {
      transform: translateY(calc(-100% - 20px));
    }

    img {
      width: 48px;
      height: 48px;
      margin-bottom: 8px;
    }
  }

  .map-content {
    width: 100%;
    height: 100%;
  }

  .footer {
    width: 100%;
    position: fixed;
    bottom: 0;
    left: 0;
    z-index: 99999;
    display: flex;
    font-size: 32px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    height: 112px;
    line-height: 112px;
    text-align: center;

    .click {
      flex: 1;
      color: #333;
      background-color: #fff;
    }

    .btn {
      flex: 2;
      background: #1365DD;
      color: #FFF;
    }
  }
}
</style>
