<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>{{ formData.gridName }}</span>
          </div>
          <div class="info-prop">
            <span class="info-title-2">服务事项：</span>
            <span>{{ formData.serviceMatterShow }}</span>
          </div>
          <div class="info-prop">
            <span class="info-title-2">活动标题：</span>
            <span>{{ formData.title }}</span>
          </div>
          <div class="info-prop">
            <span class="info-title-2">活动目标：</span>
            <span>{{ formData.target }}</span>
          </div>
          <div class="info-prop">
            <span class="info-title-2">活动内容：</span>
            <p class="text_p"
               v-html="formData.content"></p>
          </div>
          <div class="info-prop">
            <span class="info-title-2">服务人数：</span>
            <span>{{ formData.peopleCount }}</span>
          </div>
          <div class="info-prop">
            <span class="info-title-2">活动时间：</span>
            <span>{{ formData.activityTime }}</span>
          </div>
          <div class="info-prop">
            <span class="info-title-2">活动地址：</span>
            <span>{{ formData.address }}</span>
          </div>
          <div class="info-prop">
            <span class="info-title-2">活动地图：</span>
            <div class="div_map">

              <div id="app_act_detail"></div>

            </div>
          </div>
          <div class="info-prop">
            <span class="info-title-2">活动成果：</span>
            <p class="text_p"
               v-html="formData.result"></p>

          </div>
        </div>
      </div>

    </div>

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

    </div>

  </div>
</template>

<script>

import { Loading } from 'element-ui' // 引入Loading服务

var map
var search
var markers
var infoWindowList
var geocoder // 新建一个正逆地址解析类

let loading // 加载动画
export default {
  data () {
    return {
      formData: {},
      initLoading: false
    }
  },
  components: {},
  mounted () {
    this.initMap()
  },

  methods: {

    handleCancle () {
      this.diaDestroy()
      this.$emit('diaClose')

    },

    diaDestroy () {
      if (map) {
        map.destroy()
      }
    },

    async initForm (row) {

      this.startLoading()
      const { user } = this.$store.state
      this.agencyId = user.agencyId
      for (let item of this.serviceList) {

        if (row.serviceMatter === item.value) {
          row.serviceMatterShow = item.label
        }
      }
      this.formData = { ...row }

      this.initLoading = true

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

      this.endLoading()

    },

    // 地图初始化函数，本例取名为init，开发者可根据实际情况定义
    initMap () {
      // 定义地图中心点坐标
      var center = new window.TMap.LatLng(this.formData.latitude, this.formData.longitude)
      // 定义map变量，调用 TMap.Map() 构造函数创建地图
      map = new window.TMap.Map(document.getElementById('app_act_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()
    },

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

    handleSearchMap () {
      infoWindowList.forEach((infoWindow) => {
        infoWindow.close()
      })
      infoWindowList.length = 0
      markers.setGeometries([])
      // 在地图显示范围内以给定的关键字搜索地点
      search
        .searchRectangle({
          keyword: this.keyWords,
          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.formData.latitude = lat
            this.formData.longitude = lng
            this.convert()
          } else {
            this.$message.error('未检索到相关位置坐标')
          }
        })
    },

    handleMoveCenter () {
      //修改地图中心点
      const center = map.getCenter()
      const lat = center.getLat()
      const lng = center.getLng()
      this.formData.latitude = lat
      this.formData.longitude = lng
      this.setMarker(lat, lng)
      this.convert(lat, lng)
    },

    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.formData.latitude, this.formData.longitude);
      }
      // map.setCenter(location);
      markers.updateGeometries([
        {
          id: 'main', // 点标注数据数组
          position: location,
        },
      ]);
      geocoder
        .getAddress({ location: location }) // 将给定的坐标位置转换为地址
        .then((result) => {
          // this.formData.address = result.result.address
          // 显示搜索到的地址
        });
    },

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

<style lang="scss" scoped >
@import "@/assets/scss/modules/management/detail-main.scss";
</style>

<style  lang="scss" scoped>
.text_p {
  margin: 0 0;
  border: 3px;

  > p {
    margin: 0 0;
  }
}
</style>

