<template>
  <div class="input-group">
    <input v-model="keyword" type="text" class="search-input form-control"
           @focus.stop.prevent="onFocus()"
           @blur.stop.prevent="onBlur()"
           @keyup.40="onSelectChange(1)"
           @keyup.38="onSelectChange(-1)"
           @keyup.13="onSearch()"/>
    <span @click="onSearch()">
          <button class="btn search-btn" type="button"><span class="glyphicon glyphicon-search" aria-hidden="true"></span> </button>
       </span>
    <div class="angle"></div>
    <a class="close" @click="close"><img src="/images/search/search-close.png"></a>
    <ul class="association" v-show="showAssociate">
      <li v-for="(k, index) in similarKeywords.data" class="item"
          :class="{'active': index==associate.activeIndex}"
          @click.stop.prevent="onAssociateClick(k)">{{ k }}
      </li>
    </ul>
  </div>
</template>
<script>
  export default {
    name: 'search',
    data () {
      return {
        keyword: '',
        inputValue: '',
        select: 'product',
        searchType: 'product',
        associate: {
          show: false,
          activeIndex: null
        }
      }
    },
    computed: {
      similarKeywords () {
        return this.$store.state.search.keywords
      },
      showAssociate () {
        return this.associate.show && this.similarKeywords.data && this.similarKeywords.data.length
      },
      floors () {
        return this.$store.state.floor.list.data
      },
      bgUrl () {
        return this.background && this.background !== '' ? this.background : this.floors[1].items[0].pictureUrl
      }
    },
    watch: {
      'keyword': {
        handler (val, oldVal) {
          let keywords = this.similarKeywords.data
          if (!keywords || !keywords.length || this.associate.activeIndex === null || val !== keywords[this.associate.activeIndex]) {
            this.onChange()
          }
        }
      }
    },
    methods: {
      close: function () {
        this.keyword = ''
      },
      onFocus () {
        this.associate.show = true
      },
      onBlur () {
        this.associate.show = false
      },
      onSelectChange (count) {
        let keywords = this.similarKeywords.data
        if (keywords && keywords.length) {
          let index = this.associate.activeIndex
          if (index === null) {
            index = -1
          }
          index += count
          if (index >= keywords.length) {
            index = 0
          } else if (index < 0) {
            index = keywords.length - 1
          }
          this.associate.activeIndex = index
          this.keyword = keywords[index]
        }
      },
      onChange () {
        this.associate.activeIndex = null
        if (!this.keyword) {
          this.associate.show = false
          this.$store.dispatch('resetSearchKeywords')
        } else {
          this.searchKeywords()
        }
      },
      searchKeywords () {
        this.associate.show = true
        this.$store.dispatch('searchKeywords', { keyword: this.keyword })
      },
      onSearch () {
        document.getElementsByClassName('search-input')[0].blur()
        if (this.keyword) {
          this.associate.show = false
          this.$store.dispatch('resetSearchKeywords')
          this.$router.push({path: '/pcb/search?w=' + encodeURIComponent(this.keyword)})
        }
      },
      onAssociateClick (word) {
        this.keyword = word
        this.onSearch()
      }
    }
  }
</script>
<style lang="scss" scoped>
  @import '~assets/scss/variables';
  .input-group {
    width: 540px;
    position: relative;
  }
  .select {
    font-size: 16px;
    color: #333;
    width: 70px;
    height: 30px;
    border-radius: 25px 0 0 25px;
    border: 1px solid #fff;
    float: left;
    padding-left: 2%;
    line-height: 30px;
  }
  .close {
    position: absolute;
    top: 3px;
    right: 55px;
    z-index: 1000;
    color: #000;
  }
  .angle {
    position: absolute;
    top: 13px;
    left: 52px;
    width: 0;
    height: 0;
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;
    border-top: 5px solid #3c7cf5;
    &:after{
      content:'';
      position: relative;
      display: block;
      width: 3px;
      height: 24px;
      top: -15px;
      left: 9px;
      border-right: 2px solid #3c7cf5;
    }
  }
  .search-input {
    font-size: 16px;
    height: 30px;
    border-radius: 25px 0 0 25px;
    width: 488px !important;
    border: 1px solid #fff;
    padding-right: 25px;
  }
  .btn:focus,.btn:active:focus,.btn.active:focus,.btn.focus,.btn:active.focus,.btn.active.focus {
    outline: none;
    border-color: transparent;
    box-shadow:none;
  }
  .search-btn {
    height: 30px;
    font-size: 22px;
    width: 50px;
    margin-left: 0;
    background: #fff;
    border: 1px solid #fff;
    border-radius: 0 25px 25px 0;
  }
  .glyphicon-search{
    color: #3c7cf5;
  }
  .association {
    position: absolute;
    background: $white;
    border: $border;
    border-top-width: 0;
    z-index: 21;
    width: 606px;
    top: 214px;
    left: 2px;
    .item {
      padding: 0 15px;
      line-height: 30px;
      cursor: pointer;
      text-align: left;

      &.active {
        background-color: $dark-bg;
      }
      &:hover {
        background-color: $grey-bg;
      }
    }
  }
</style>
