<template>
  <div class="search-bar">
    <form action @submit.prevent="onSearch">
      <input
        type="search"
        class="search-input"
        :placeholder="placeholder"
        v-model="currentValue"
        v-bind="$attrs"
        @input="onChange"
        ref="formInput"
      />
    </form>
  </div>
</template>

<script>
export default {
  props: {
    value: String,
    focus: { type: Boolean, default: false },
    placeholder: { type: String, default: '搜索校友、活动、组织' }
  },
  data() {
    return {
      currentValue: this.value,
      debounced: null
    }
  },
  watch: {
    value(value) {
      this.currentValue = value
    }
  },
  computed: {
    formInput() {
      return this.$refs.formInput
    },
    classes() {
      return {
        'is-focus': this.currentValue
      }
    }
  },
  methods: {
    bindFocus() {
      this.formInput.focus()
    },
    onChange() {
      this.$emit('input', this.currentValue)
      this.$emit('change', this.currentValue)
    },
    onSearch() {
      this.$emit('search', this.currentValue)
    }
  },
  mounted() {
    this.bindFocus()
  }
}
</script>

<style lang="scss">
.search-bar {
  padding: 10px 0 15px;
}
.search-input {
  display: block;
  width: 100%;
  height: 30px;
  line-height: 30px;
  background: #f3f4f5 url('~@/assets/img/icon_search.png') no-repeat 14px center;
  background-size: 15px;
  padding: 0 20px 0 40px;
  border-radius: 15px;
  &.is-focus {
    background: #f3f4f5;
  }
}
</style>
