import { PropType, defineComponent, onMounted, reactive } from 'vue';
import styles from './index.module.less';
import { NButton, NInput } from 'naive-ui';
import { useCatchStore } from '/src/store/modules/catchData';
import { useThrottleFn } from '@vueuse/core';

export default defineComponent({
  name: 'resource-search-group',
  emits: ['search'],
  setup(props, { emit }) {
    const catchStore = useCatchStore();
    const forms = reactive({
      keyword: ''
    });

    const onSearch = () => {
      emit('search', forms);
    };

    const throttledFn = useThrottleFn(() => onSearch(), 500);

    // onMounted(async () => {});
    return () => (
      <>
        <div class={styles.searchGroup}>
          <NInput
            type="text"
            placeholder="请输入课件标题关键词"
            clearable
            v-model:value={forms.keyword}
            class={styles.inputSearch}
            onKeyup={(e: KeyboardEvent) => {
              if (e.code === 'Enter') {
                throttledFn();
              }
            }}
            onClear={() => {
              forms.keyword = '';
              throttledFn();
            }}>
            {{
              prefix: () => (
                <span
                  class={'icon-search-input'}
                  onClick={() => throttledFn()}></span>
              )
            }}
          </NInput>
        </div>
      </>
    );
  }
});
