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

      <el-form ref="ref_form"
               :inline="true"
               :model="formData"
               class="form">

        <el-form-item label="单位名称："
                      prop="unitName"
                      label-width="150px"
                      style="display: block">
          <span class="detail_span">{{formData.unitName}}</span>

        </el-form-item>
        <el-form-item label="分类："
                      prop="type"
                      label-width="150px"
                      style="display: block">

          <span class="detail_span">{{formData.type}}</span>

        </el-form-item>
        <el-form-item label="服务事项："
                      prop="serviceMatterList"
                      label-width="150px"
                      style="display: block">
          <p class="detail_span"
             v-for="(item,index) in formData.serviceMatterList"
             :key="index">{{item}}</p>

        </el-form-item>
        <el-form-item label="联系人："
                      prop="contact"
                      label-width="150px"
                      style="display: block">
          <span class="detail_span">{{formData.contact}}</span>

        </el-form-item>
        <el-form-item label="联系电话："
                      prop="contactMobile"
                      label-width="150px"
                      style="display: block">
          <span class="detail_span">{{formData.contactMobile}}</span>
        </el-form-item>
        <el-form-item label="在职党员："
                      style="display: block"
                      prop="memberCount"
                      label-width="150px">
          <span class="detail_span">{{formData.memberCount}}</span>
        </el-form-item>
        <el-form-item label="备注："
                      prop="remark"
                      label-width="150px"
                      style="display: block">
          <span class="detail_span">{{formData.remark?formData.remark:'无'}}</span>
        </el-form-item>
        <el-form-item label="地理位置："
                      prop="address"
                      label-width="150px"
                      style="display: block">
          <span class="detail_span">{{formData.address}}</span>

        </el-form-item>
        <el-form-item label="位置坐标："
                      prop="longitude"
                      label-width="150px"
                      style="display: block">
          <div style="width:500px">

            <div id="app">

            </div>

          </div>
        </el-form-item>

      </el-form>

    </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: {},

    }
  },
  components: {},
  created () {
    this.initMap()
  },

  methods: {
    handleCancle () {

      this.$emit('diaClose')

    },

    async initForm (row) {

      this.startLoading()
      this.formData = { ...row }
      map.setCenter(new TMap.LatLng(this.formData.latitude, this.formData.longitude))
      this.setMarker(this.formData.latitude, this.formData.longitude)

      this.endLoading()

    },

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

  }
}
</script>
<style lang="scss" scoped >
@import "@/assets/scss/modules/visual/communityManageForm.scss";
</style>
<style scoped>
.detail_span {
  width: 500px;
  font-weight: bold;
  text-align: left;
  margin: 0 0;
  font-size: 16px;
}

.form {
  margin-top: 30px;
}
</style>


