<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.agencyName }}</span>
          </div>

          <div class="info-prop">
            <span class="info-title-2">所属网格：</span>
            <span>{{ dataForm.gridName?dataForm.gridName:'--' }}</span>
          </div>

          <div class="info-prop">
            <span class="info-title-2">小区名称：</span>
            <span>{{ dataForm.neighborHoodName}}</span>
          </div>

          <div class="info-prop">
            <span class="info-title-2">小区编码：</span>
            <span>{{ dataForm.coding?dataForm.coding:'--'}}</span>
          </div>

          <div v-if="dataForm.qrcodeUrl"
               style="display: flex;flex-direction: column;">
            <img style="margin-left: 70px;width: 200px;"
                 :src="dataForm.qrcodeUrl">
            <a style="margin-left: 80px"
               :href="dataForm.qrcodeUrl"
               target="_blank">下载</a>
          </div>

          <div class="info-prop">
            <span class="info-title-2">关联物业：</span>
            <span>{{ dataForm.propertyShow?dataForm.propertyShow:'--'}}</span>
          </div>
          <div class="info-prop">
            <span class="info-title-2">实有楼栋：</span>
            <span>{{dataForm.realBuilding?dataForm.realBuilding:0 }}</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>
</template>

<script>
import { mapGetters } from 'vuex'
import { Loading } from 'element-ui' // 引入Loading服务
import { requestPost } from '@/js/dai/request'
var map
var search
var markers
var infoWindowList
let loading // 加载动画
export default {
  data () {
    return {

      btnDisable: false,
      initLoading: false,

      dataForm: {
        neighborHoodName: '', // 小区名称【不超过50字】
        agencyId: '', // 所属组织ID
        agencyName: '',
        gridId: '', //所属网格ID
        propertyId: '', //关联物业
        address: '', //详细地址
        remark: '', //备注【最大500字】
        location: '', //坐标位置
        longitude: '', //经度
        latitude: '', //纬度
        realBuilding: 0,
        coding: '',
        sysCoding: ''
      },


    }
  },
  components: {},
  mounted () {

  },

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

    async initForm (row) {
      this.dataForm = JSON.parse(JSON.stringify(row))

      let propertyShowList = []
      this.dataForm.propertyList.forEach(element => {
        propertyShowList.push(element.name)
      });

      this.dataForm.propertyShow = propertyShowList.join(',')

      this.initLoading = true

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

    },

    // 地图初始化函数，本例取名为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)

      // 监听地图平移结束
      map.on('panend', () => {
        this.handleMoveCenter()
      })
      this.handleMoveCenter()
    },

    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.dataForm.latitude = lat
            this.dataForm.longitude = lng
          } else {
            this.$message.error('未检索到相关位置坐标')
          }
        })
    },

    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)
    },



    resetData () {

      this.dataForm = {
        neighborHoodName: '', // 小区名称【不超过50字】
        agencyId: '', // 所属组织ID
        agencyName: '',
        gridId: '', //所属网格ID
        propertyId: '', //关联物业
        address: '', //详细地址
        remark: '', //备注【最大500字】
        location: '', //坐标位置
        longitude: '', //经度
        latitude: '', //纬度
        realBuilding: 0,
        coding: '',
        sysCoding: ''
      }

    },
    // 开启加载动画
    startLoading () {
      loading = Loading.service({
        lock: true, // 是否锁定
        text: '正在加载……', // 加载中需要显示的文字
        background: 'rgba(0,0,0,.7)' // 背景颜色
      })
    },
    // 结束加载动画
    endLoading () {
      // clearTimeout(timer);
      if (loading) {
        loading.close()
      }
    }
  },
  computed: {
    dataRule () {
      return {
        neighborHoodName: [
          { required: true, message: '小区名称不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '小区名称长度在 1 到 50个字符',
            trigger: 'blur'
          }
        ],
        agencyId: [
          { required: true, message: '所属组织不能为空', trigger: 'blur' }
        ],
        gridId: [
          { required: true, message: '所属网格不能为空', trigger: 'blur' }
        ],
        coding: [
          { required: true, message: '小区编码不能为空', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '详细地址不能为空', trigger: 'blur' }
        ],
        longitude: [
          { required: true, message: '坐标不能为空', trigger: 'blur' }
        ]
      }
    },
    propertyRule () {
      name: [
        { required: true, message: '物业名称不能为空', trigger: 'blur' }
        // { min: 1, max: 50, message: '小区名称长度在 1 到 50个字符', trigger: 'blur' }
      ]
    }
  },
  props: {}
}
</script>
<style lang="scss" scoped >
@import "@/assets/scss/modules/management/detail-main.scss";
</style>



