<template>
  <div class="lyf-ui-search">
    <div class="lyf-ui-search-main">
      <lyf-icon icon="search1" :size=14 :color="placeholderColor"></lyf-icon>
      <form action="" class="lyf-ui-search-form">
        <input 
          ref="lyfUi_searchInputRef" 
          class="lyf-ui-search-input" 
          :type="$attrs.type || 'search'" 
          :inputmode="$attrs.inputmode || 'text'"
          :placeholder="$attrs.placeholder"
          :value="value" 
          :readonly="$attrs.readonly"
          @input="onInput" 
          @focus="onFocus" 
          @blur="onBlur" 
          @change="onChange" 
        />
      </form>
      <div class="lyf-ui-search-clear-icon" v-show="value.trim().length > 0" @click="clearClick">
        <lyf-icon icon="close2" :color="placeholderColor"></lyf-icon>
      </div>
    </div>
    <div class="lyf-ui-search-action" v-if="_showAction" @click="actionClick">{{ actionText }}</div>
  </div>
</template>

<script>
export default {
  name: 'LyfSearch',
  inheritAttrs: false,
  props: {
    value: {
      type: String,
      default: ''
    },
    actionText: {
      type: String,
      default: '取消'
    },
    showAction: {
      type: Boolean,
      default: false
    },
    showActionAfterFocus: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      isFocus: false,
      placeholderColor: 'var(--placeholder-color)'
    }
  },
  computed: {
    _showAction() {
      if (this.showAction) return true

      if (this.showActionAfterFocus) {
        if (this.isFocus) {
          return true
        } else {
          return this.value.trim().length > 0
        }
      }
      return false
      // return this.showActionAfterFocus
    }
  },
  emits: ['focus', 'input', 'clear', 'change', 'action-event'],
  methods: {
    onFocus() {
      setTimeout(() => {
        this.isFocus = true
        this.$emit('focus')
      }, 50)
    },
    onBlur() {
      setTimeout(() => {
        this.isFocus = false
      }, 50)
    },
    onInput(e) {
      this.$emit('input', e.target.value)
    },
    onChange(e) {
      this.$emit('change', e.target.value)
    },
    clearClick() {
      this.$emit('input', '')
      this.$emit('clear')
      this.$refs.lyfUi_searchInputRef.focus()
    },
    actionClick() {
      this.$emit('action-event')
    },
    openKeyboard() {
      this.$refs.lyfUi_searchInputRef.focus()
    }
  },
}
</script>

<style scoped>
.lyf-ui-search {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.lyf-ui-search .lyf-ui-search-main {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--search-input-height);
  padding-left: var(--space_12);
  border-radius: var(--search-input-radius);
  background-color: var(--search-input-background);
}

.lyf-ui-search .lyf-ui-search-main .lyf-ui-search-form {
  flex: 1;
  display: flex;
  align-items: center;
  height: var(--search-input-height);
}

.lyf-ui-search .lyf-ui-search-main .lyf-ui-search-input {
  width: 100%;
  padding: var(--search-input-margin);
  box-sizing: border-box;
  border: none;
  border-radius: 0;
  outline: none;
  background-color: transparent;
  color: var(--font-color_1);
  font-size: 14px;
  caret-color: var(--caret-color);
}

.lyf-ui-search .lyf-ui-search-main .lyf-ui-search-clear-icon {
  display: flex;
  align-items: center;
  height: var(--search-input-height);
  padding-right: var(--search-clear-padding-right);

  img {
    width: 18px;
    height: 18px;
  }
}

.lyf-ui-search .lyf-ui-search-action {
  padding-left: var(--space_16);
  color: var(--search-aciton-color);
  font-size: var(--search-aciton-font-size);
}
</style>