<template>
<div>
  <mt-popup class="area-code-panel-box" v-model="show" position="bottom" :style="{height: `${innerHeight}px`}">
    <div class="area-code-panel">
      <div class="search-bar" >
        <mt-field class="search-input left" placeholder-align="placeholderAlign" type="text" show-clear :placeholder="placeholder" @input="onSearch"></mt-field>
      </div>
      <span class="display-section"></span>
      <dl class="area-list" v-if="!searchPanel">
        <div v-for="(item, index) in areaCode" :id="'anchor-'+index">
          <dt>{{ item.letter }}</dt>
          <dd v-for="(it, i) in item.area_code_list" class="cells" @click="setArea(it)">
            <div class="cell-bd">{{ it.area_name }}</div>
            <div class="cell-ft">{{ it.area_code }}</div>
          </dd>
        </div>
      </dl>
      <dl class="area-list" v-else>
        <div v-for="(item, index) in areaCodeSearch">
          <dt :id="'anchor-'+index">{{ item.letter }}</dt>
          <dd v-for="(it, i) in item.area_code_list" class="cells" @click="setArea(it)">
            <div class="cell-bd">{{ it.area_name }}</div>
            <div class="cell-ft">{{ it.area_code }}</div>
          </dd>
        </div>
      </dl>
      <ul class="area-hash" :style="{marginTop: areaCodePanelMarginTop}">
        <li v-for="(item, index) in areaCode" @click="goAnchor('#anchor-'+index, item.letter)" :style="{color}">{{ item.letter }}</li>
      </ul>
    </div>
  </mt-popup>
</div>
</template>
<script lang="ts">
import ivyStore from '../ivyStore'
import * as utils from '../utils'
import { Component, Prop, Vue, Watch } from 'vue-property-decorator'

@Component({})
export default class AreaCodePanel extends Vue {
  @Prop() show: boolean
  @Prop() color: string
  innerHeight = 0
  areaCode: any[] = []
  areaCodeSearch: any[] = []
  searchPanel = false
  areaCodePanelMarginTop = ''
  isWeixin = utils.isWeixin()
  placeholder = '选择国家或地区'

  @Watch('show')
  onShowChanged(val) {
    val && this.getAreaCode()
  }
  async mounted() {
    await this.$nextTick()
    this.innerHeight = window.innerHeight
  }
  async getAreaCode() {
    let r = await ivyStore.objects.axios.get('/api/mobile/member/getAreaCodeList')
    this.areaCode = r.data
    await this.$nextTick()
    this.areaCodePanelMarginTop = `-${document.querySelector(".area-hash").clientHeight / 2}px`
  }
  goAnchor(selector, letter) {
    let anchor = document.querySelector(selector)
    document.querySelector(".area-code-panel").scrollTop = anchor.offsetTop - document.querySelector(".search-bar").clientHeight
    this.$Toast({
      message: letter,
      className: 'letter-zindex',
      duration: 800
    })
  }
  onSearch(val) {
    this.searchPanel = !!val
    this.areaCodeSearch = val ? getSearchResult(this.areaCode, val) : []
  }
  setArea(item) {
    this.$emit('on-close', item, false)
  }
}

function getSearchResult(catList, needle) {
  // 区号分了类别，在每个类别里面找到匹配的区号
  return catList.map(cat => ({...cat, area_code_list: cat.area_code_list.filter(area => area.area_name.includes(needle) || area.area_code.includes(needle))}))
    .filter(cat => cat.area_code_list.length)
}
</script>
<style lang="less">
@import (reference) url("../styles/common.less");
.letter-zindex{
  z-index: 99999 !important;
}
.area-code-panel-box{
  width: 100%;
  .area-code-panel{
    overflow-y: scroll;
  }
  .picker-slot-wrapper, .picker-item {
    backface-visibility: hidden;
  }
}
.mint-field.search-input{
  padding-bottom: 0 !important;
  padding-top: 0 !important;
  min-height: 1.76rem !important;
  .mint-cell-wrapper{
    background-image: none !important;
  }
}
.search-bar {
  padding: 0.4rem 0.8rem;
  background-color: #cecece;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 1.76rem;
  z-index: 2;
  .search-input {
    background-color: #ffffff;
    font-size: 0.56rem;
    padding-left: 1.28rem;
    padding-right: 0.4rem;
    padding-top: 0.18rem;
    padding-bottom: 0.18rem;
    border-radius: 0.16rem;
    box-sizing: border-box;
    position: relative;
    &:after {
      .iconfont-fe;
      content: "\e70b";
      position: absolute;
      left: 4.2rem;
      color: #999999;
      font-size: 0.8rem;
      top: 0.3rem;
    }
    &:before {
      border-top: none;
    }
  }
  .search-input.left {
    &:after {
      left: 0.4rem;
    }
  }
}
.display-section {
  display: block;
  height: 2.56rem;
}

.area-code-panel {
  width: 100%;
  height: 100%;
}
.area-code-panel-box{
  z-index: 99999 !important;
}
.area-code-panel {
  z-index: 99999;
  .area-list {
    background-color: #ffffff;
    .cells {
      -webkit-box-align: center;
      -webkit-align-items: center;
      align-items: center;
    }
    dt {
      padding-left: 0.6rem;
      padding-right: 1.6rem;
      height: 1.12rem;
      line-height: 1.12rem;
      background-color: #f5f5f5;
      color: #000000;
      font-size: 0.64rem;
      font-weight: bold;
    }
    dd {
      .vux-1px-b;
      &:after {
        border-color: #cccccc;
      }
      &:last-child {
        &:after {
          border: none;
        }
      }
      height: 2.0rem;
      line-height: 2.0rem;
      margin-left: 0.6rem;
      padding-right: 1.6rem;
      .cell-bd {
        font-size: 0.56rem;
        color: #333333;
      }
      .cell-ft {
        font-size: 0.56rem;
        color: #a0a0a0;
        font: 0.56rem/0.6rem Arial, Helvetica, sans-serif;
      }
    }
  }
  .area-hash {
    box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.3);
    z-index: 1;
    position: fixed;
    width: 0.88rem;
    right: 0.12rem;
    padding: 0.4rem 0;
    background: #ffffff;
    border-radius: 0.44rem;
    top: 50%;
    li {
      text-align: center;
      font-size: 0.44rem;
      color: @primary;
      font-weight: bold;
      padding-top: 0.14rem;
      padding-bottom: 0.14rem;
      font: bold 0.44rem/1.2em Arial, Helvetica, sans-serif;
    }
  }
}

</style>
