<template>
  <div class="guide-wrap">
    <nav-bar title="选择号码地区"/>
    <div class="flex wrap pr">
      <div class="city">
        <div v-for="(item, index) in data" :key="index">
          <span :id="item.name">{{item.name}}</span>
          <div
            class="line"
            v-for="child in item.list"
            :key="child.id"
            >
            <div @click="selectCountry(child)">
              <p>{{child.name}}</p>
              <p>{{child.code}}</p>
            </div>
          </div>
        </div>
      </div>
      <div id="cityLine">
        <a
          :href="'#' + item"
          v-for="item in alphabetData"
          :key="item"
        >{{item}}</a>
      </div>
    </div>
  </div>
</template>

<script>
import NavBar from "@/components/common/NavBar";

export default {
  name: "AreaCode",
  components: {
    NavBar
  },
  data() {
    return {
      data: [],
      alphabetData: []
    };
  },
  created() {
    // 获取国家城市
    let countryItems = this.$store.state.areaCountryArr;
    countryItems.length ? (this.data = countryItems) : this.getCountry();
  },
  mounted() {
    this.setTop();
  },
  methods: {
    // 设置国家索引top高度
    setTop() {
      let el = document.getElementById("cityLine");
      el.style.top =
        (document.documentElement.offsetHeight - el.offsetHeight) / 2 + "px";
    },
    // 选择国家
    selectCountry(item) {
      this.$store.commit("setAreaCodeObj", item);
      this.$util.storage.save("areaCodeItem", item);
      this.$router.go(-1);
    },
    // 获取国家城市
    getCountry() {
      let params = {};
      params.url = this.$apis.api_national_country_prefix;
      this.$net.req(params, this).then(
        res => {
          this.data = res.data;
          this.$store.commit("setAreaCountryArr", res.data);
          let alphabetData = [];
          this.data.forEach(element => {
            alphabetData.push(element.name);
            this.alphabetData = [...new Set(alphabetData)];
          });
        },
        e => {
          console.log(e);
        }
      );
    }
  }
};
</script>

<style scoped lang='less'>
.guide-wrap {
  height: 100vh;
  overflow: hidden;
  .city {
    height: calc(100vh - 44px);
    overflow-y: auto;
    background: #fff;
    width: 100%;
    flex-shrink: 0;
    span {
      font-weight: 400;
      display: flex;
      align-items: center;
      font-size: 18px;
      padding: 0 0 0 20px;
      height: 38px;
      color: #222;
      background: rgba(238, 238, 238, 1);
    }
    .line {
      height: 45px;
      font-size: 14px;
      color: #999;
      font-weight: 400;
      padding: 0 20px;
      div {
        display: flex;
        align-items: center;
        height: 100%;
        position: relative;
        p:first-child {
          width: 240px;
        }
      }
      div:after {
        content: "";
        position: absolute;
        bottom: 0;
        left: 20px;
        right: 20px;
        height: 1px;
        background: #ddd;
      }
      div:last-child:after {
        height: 0;
      }
    }
  }
  #cityLine {
    position: fixed;
    right: 0;
    top: 20%;
    display: flex;
    width: 22px;
    flex-direction: column;
    z-index: 2;
    a {
      width: 100%;
      text-align: center;
      height: 13px;
      font-size: 11px;
      font-weight: 500;
      color: #222;
      line-height: 13px;
      margin: 0 0 3px;
    }
  }
}
</style>
