<template>
  <div class="searchOption">
    <div>
      <input id="keyword"
             v-model="searchStyle"
             type="radio"
             value="keyword">
      <label for="keyword">按关键字搜索</label>
    </div>
    <div>
      <input id="lnglat"
             v-model="searchStyle"
             type="radio"
             value="lnglat">
      <label for="lnglat">按经纬度搜索</label>
    </div>

  </div>

  <div class="content">
    <img src="@/assets/img/location.png">
    <input v-model="searchContent"
           type="text"
           placeholder="请输入搜索位置/坐标"
           @input="searchPlace()">
    <img src="@/assets/img/search.png"
         @click="showList()">
  </div>
  <div ref="locationList"
       class="my-click-transition">
    <li v-for="(item,index) in localList"
        :key="index"
        class="itemLocation"
        @click="flyTo(item.geometry,item.id)"><img src="@/assets/img/location.png">
      <span class="locationName">{{ item.name }}</span>
      <span class="locationAddress">{{ item.address }}</span>
    </li>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  data () {
    return {
      searchType: '',
      searchStyle: 'keyword',
      searchContent: '',
      localList: []
    }
  },
  methods: {
    showList () {
      // if (this.$refs.locationList.className == 'my-click-transition') {
      //   this.$refs.locationList.className = 'locationList'
      // } else {
      //   this.$refs.locationList.className = 'my-click-transition'
      // }
    },
    // 实时查询
    searchPlace () {
      let url
      if (this.searchStyle == 'keyword') {
        console.log('111')
        url = 'http://192.168.11.35:8500/thmap/poi/searchFeat/keywords?keywords=' + this.searchContent
      } else {
        console.log('222')
        const arr = this.searchContent.split(',')
        url = 'http://192.168.11.35:8500/thmap/poi/searchFeat/keywords?lat=' + arr[0] + '&lng=' + arr[1] + '&distance=' + 50
      }

      axios.get(url).then((res) => {
        // console.log(res.data.data.datas, '数据集')
        if (res.data.data.datas) {
          // console.log('有数据了')
          this.$refs.locationList.className = 'locationList'
          this.localList = res.data.data.datas
        } else {
          this.$refs.locationList.className = 'my-click-transition'
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    flyTo (lonLatInfo, id) {
      const Arr = lonLatInfo.split(' ')

      // const Num1 = parseFloat(Arr[1])
      const Num1 = Number(Arr[1].match(/\d+(\.\d+)?/g)[0])
      const Num2 = Number(Arr[2].match(/\d+(\.\d+)?/g)[0])
      const Arr1 = [Num1, Num2]
      // console.log(this.$parent, '父组件')
      this.$parent.flyTo(Arr1, id)
      // // console.log(Arr1, '点位信息')
    }
  }
}
</script>

<style lang="scss" scoped>
.searchOption {
  width: 300px;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 15px;
  line-height: 50px;
  input {
    margin-right: 5px;
  }
}
.content {
  width: 300px;
  height: 45px;
  padding: 10px 10px;
  background-color: rgba(245, 245, 245, 0.8);
  box-shadow: 0px 15px 10px -15px #565656;
  display: flex;
  justify-content: space-between;

  input {
    padding-left: 10px;
  }
}
// 位置列表
.locationList {
  margin-top: 5px;
  width: 300px;
  // height: 100px;
  background-color: rgba(245, 245, 245, 0.8);
  transition: height 0.5s ease-in;
  -webkit-transform: height 0.5s ease-in;
}
.my-click-transition {
  overflow: hidden;
  margin-top: 5px;
  transition: height 0.5s ease-out;
  -webkit-transform: height 0.5s ease-out;
  height: 0;
  width: 300px;
  background-color: rgba(245, 245, 245, 0.8);
}
.itemLocation {
  width: 300px;
  height: 32px;
  position: relative;
  padding-left: 32px;
  font-size: 13px;
  overflow: hidden;
  line-height: 32px;
  list-style: none;
  cursor: pointer;
  img {
    width: 26px;
    height: 26px;
    position: absolute;
    top: 3px;
    left: 0px;
  }
  .locationName {
    color: #0082e5;
  }
  .locationAddress {
    color: #565656;
  }
}
</style>
