<template>
  <div class="pub-search">
    <div class="flex">
      <div
        v-if="title"
        class="title"
      >
        {{ title }}
      </div>
      <div class="content flex-wrap">
        <div
          class="search"
          @keyup.enter="checkSearch"
        >
          <el-popover
            :value="popoverVisible"
            placement="bottom"
            trigger="manual"
          >
            <span>{{ searchTooltip }}</span>
            <el-input
              slot="reference"
              v-model="input"
              :placeholder="placeholder"
              prefix-icon="el-icon-search"
              clearable
              @focus="inputFocusOn = true"
              @blur="inputFocusOn = false"
            />
          </el-popover>
        </div>
        <div class="btn">
          <button
            class="btnAn"
            @click="checkSearch"
          >
            搜索
          </button>
        </div>
        <slot />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'SearchWithTooltip',
  props: {
    // title
    title: {
      type: String,
      default: ''
    },
    // placeholder
    placeholder: {
      type: String,
      default: '请输入您要搜索的内容'
    },
    searchTooltip: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      input: '',
      inputFocusOn: false
    }
  },
  computed: {
    popoverVisible() {
      return this.searchTooltip && this.inputFocusOn
    }
  },
  watch: {
    input(val) {
      if (val === '') {
        this.$emit('clearSearchData', val)
      }
    }
  },
  methods: {
    checkSearch() {
      this.$emit('getSearchData', this.input)
    },
    setSearch(val) {
      this.input = val
    }
  }
}
</script>

<style lang="scss" scoped>
.pub-search {
  .content {
    display: flex;
    align-items: center;
  }
  .content > div {
    margin-right: 12px;
    &:last-child {
      margin-right: 0;
    }
  }
  ::v-deep .el-select {
    width: 136px;
    .el-input__suffix {
      right: 5px;
      top: 2px;
    }
    .el-input__inner {
      font-size: 14px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      color: #101116;
    }
    // .el-icon-arrow-up:before {
    //   content: "\f057";
    // }
  }
  .title {
    line-height: 36px;
    font-size: 20px;
    font-family: Microsoft YaHei;
    font-weight: bold;
    color: #303133;
    position: relative;
    padding-left: 16px;
    &::after {
      position: absolute;
      content: '';
      width: 4px;
      height: 20px;
      top: 50%;
      margin-top: -9px;
      left: 0;
      background: #2d67e3;
    }
  }
  ::v-deep .el-input__icon {
    line-height: 36px;
  }
  ::v-deep .search {
    height: 32px;
    box-shadow: 0px 3px 3px 0px rgba(0, 0, 0, 0.06);
    border-radius: 4px;
    .el-input {
      width: 320px;
      height: 32px;
      line-height: 32px;
      > .el-input__inner {
        width: 100%;
        height: 100%;
      }
    }
  }
  .btnAn {
    padding: 0 18px;
    height: 32px;
    line-height: 32px;
    background: #2d67e3;
    box-shadow: 0px 3px 4px 0px rgba(0, 0, 0, 0.18);
    border-radius: 4px;
    outline: none;
    border: none;
    cursor: pointer;
    color: #fff;
    font-size: 14px;
    font-family: Microsoft YaHei;
    font-weight: bold;
  }
}
</style>
