
<style scoped>
.citySelector-container {
  position: relative;
  padding: 0;
}
.citySelector-container .controller-container {
  display: flex;
  position: fixed;
  top: 130px;
  max-width: 40px;
  right: 10px;
  padding: 8px;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: white;
  box-shadow: 0 4px 10px 0 rgba(0,0,0,0.1);
  border-radius: 8px;
  z-index: 999;
}
.citySelector-container .controller-container .controller:not(:first-child) {
  padding-top: 5px;
}
.citySelector-container .controller {
  width: 100%;
  height: 35px;
  text-align: center;
  line-height: 35px;
  font-size: 26px;
  color: #666;
  font-family: Georgia, 'Times New Roman', Times, serif;
}
.citySelector-container .controller-touched {
  position: absolute;
  width: 80px;
  height: 80px;
  top: 5%;
  right: 120%;
  text-align: center;
  line-height: 80px;
  font-size: 28px;
  color: #222;
  background: #8B8989;
  transition: all 0.5s ease-in-out;
  border-radius: 8px;
}
.citySelector-container .list-container {
  position: relative;
  width: 100vw;
  background:rgb(255, 255, 252);
  z-index: 1;
}
.citySelector-container .list-title {
  display: flex;
  min-height: 70px;
  padding: 5px 30px;
  align-items: center;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-size: 30px;
  color: #2F8FFF;
  background: rgba(47,143,255,0.05);
}
.citySelector-container .list-content {
  display: flex;
  min-height: 68px;
  padding: 5px 30px;
  align-items: center;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  color: #666;
  line-height: 68px;
  font-size: 28px;
  border-bottom: 1px solid rgba(153, 153, 153 , .2);
}

.search-container {
  display: flex;
  padding: 0 30px;
  justify-content: space-around;
  align-items: center;
  box-sizing: border-box;
}

.search-container > input {
  font-size: 28px;
  display: block;
  flex-grow: 1;
  height: 80px;
  margin: 20px auto;
  box-sizing: border-box;
  padding-left: 40px;
  color: #666;
  border: none;
  background: rgba(221,221,221,0.18);
  border-radius:40px;
}

.positionCity {
  display: flex;
  margin-left: 30px;
  align-items: center;
}

.positionCity > img {
  display: block;
  width: 30px;
  height: 30px;
}

.positionCity > div {
  font-size: 26px;
  color: #222;
}
</style>

<template>
  <div class="citySelector-container">
    <div class="search-container">
      <input
        v-model="searchValue"
        @input="searchChange($event)"
        :placeholder="searchPlaceHolder"
        />

      <div @click="positionCityClick" class="positionCity">
        <img src="/static/images/location-blue.svg" />
        <div>{{positionCity}}</div>
      </div>
    </div>
    <div class="list-container">
      <div
        v-for="(item, index) in (isSearching ? searchData : data)"
        :key="index" class="list"
      >
        <div v-if="item.title" class="list-title">{{item.title}}</div>
        <div @click.stop="listClick(_item, _index, index)" v-for="(_item, _index) in item.list" :key="_index" class="list-content">
          {{_item}}
        </div>
      </div>
    </div>
    <div
      @touchmove.stop="controllerTouchmove"
      @touchend.stop="controllerTouchend"
      @click="controllerClick"
      class="controller-container"
    >
      <div v-show="controllerTouched && barHeight && barOffset" class="controller-touched">
        {{controllerTouched && data[controllerTouched - 1].title}}
      </div>
      <div
        :data-touchIndex="index"
        v-for="(item ,index) in data" :key="index"
        class="controller"
      >
        {{item.title}}
      </div>
    </div>
  </div>
</template>
<script>
export default {
  /**
   * data结构：
   * [
   *  {
   *    title: String
   *    list: [
   *      item1: String,
   *      item2: String
   *    ]
   *  }
   * ]
   * searchPlaceHolder: String 搜索栏placeHolder
   * positionCity: String 定位城市
  */
  props: {
    data: {
      type: Array,
      default: () => []
    },
    searchPlaceHolder: {
      type: String,
      default: function () {
        return '请输入城市名'
      }
    },
    positionCity: {
      type: String,
      default: function () {
        return '成都'
      }
    }
  },
  data: () => {
    return {
      barOffset: undefined,
      barHeight: undefined,
      controllerTouched: undefined,
      listOffset: 0,
      listOffsets: [],
      searchValue: '',
      isSearching: false,
      searchData: []
    }
  },
  onShow () {
    this.searchData = []
    this.isSearching = false
    this.searchValue = ''
  },
  mounted () {
    const that = this
    // 获取整体容器长度、控制条长度、距离顶部高度
    wx.createSelectorQuery().select('.controller-container').boundingClientRect(function (res) {
      that.barOffset = res.top
    }).exec()
    wx.createSelectorQuery().select('.controller').boundingClientRect(function (res) {
      that.barHeight = res.height
    }).exec()
    wx.createSelectorQuery().selectAll('.list-title').boundingClientRect(function (res) {
      that.listOffsets = res.map(item => item.top)
    }).exec()
  },
  methods: {
    searchChange (e) {
      const value = e.target.value
      if (!value || !value.length) {
        this.searchData = []
        this.isSearching = false
        return
      }
      const data = this.data.map(item => item.list).flat().filter(item => item.indexOf(value) === 0)
      this.searchData = [
        {
          title: '搜索结果',
          list: data
        }
      ]
      this.isSearching = true
    },
    controllerTouchstart (e) {
      return false
    },
    controllerTouchmove (e) {
      if (this.barOffset && this.barHeight) {
        this.controllerTouched = Math.ceil((e.clientY - this.barOffset) / this.barHeight) >= this.data.length
          ? this.data.length
          : Math.ceil((e.clientY - this.barOffset) / this.barHeight)
        wx.pageScrollTo({
          scrollTop: this.listOffsets[this.controllerTouched - 1]
        })
      }
    },
    controllerTouchend (e) {
      setTimeout(() => {
        this.controllerTouched = 0
      }, 500)
    },
    controllerClick (e) {
      this.controllerTouched = Math.ceil((e.clientY - this.barOffset) / this.barHeight) >= this.data.length
        ? this.data.length
        : Math.ceil((e.clientY - this.barOffset) / this.barHeight)
      wx.pageScrollTo({
        scrollTop: this.listOffsets[this.controllerTouched - 1]
      })
    },
    listClick (item, index, totalIndex) {
      this.$emit('listClick', item, index, totalIndex)
    },
    positionCityClick (item, index, totalIndex) {
      this.$emit('positionCityClick', this.positionCity)
    }
  },
  computed: {
  }
}
</script>
