import { PropType, defineComponent, onMounted, reactive } from 'vue';
import styles from './index.module.less';
import { NButton, NSpace } from 'naive-ui';
import TheSearch from '/src/components/TheSearch';
export default defineComponent({
  name: 'search-group',
  props: {
    categoryChildList: {
      type: Array as PropType<any>,
      default: () => []
    },
    defaultWikiCategoryId: {
      type: String,
      default: ''
    },
    wikiCategoryId: {
      type: String,
      default: ''
    },
    searchValue: {
      type: String,
      default: ''
    }
  },
  emits: ['search', 'add', 'update:searchValue'],
  expose: ['init'],
  setup(props, { emit }) {
    // const catchStore = useCatchStore();
    const forms = reactive({
      keyword: props.searchValue || '',
      wikiCategoryId: props.wikiCategoryId || props.defaultWikiCategoryId || ''
    });
    

    const onSearch = () => {
      emit('search', forms);
    };
    onMounted(async () => {
      // 获取教材分类列表
      // await catchStore.getMusicSheetCategory()

      console.log(forms, 'formsformsformsforms')
    });
    return () => (
      <div class={styles.searchGroup}>
        <div class={[styles.searchCatatory]}>
          <NSpace size="small" class={styles.btnType}>
            {props.categoryChildList.length > 0 ? (
              <NButton
                type={
                  forms.wikiCategoryId === props.defaultWikiCategoryId
                    ? 'primary'
                    : 'default'
                }
                secondary={
                  forms.wikiCategoryId === props.defaultWikiCategoryId ? false : true
                }
                round
                size="small"
                focusable={false}
                onClick={() => {
                  forms.wikiCategoryId = props.defaultWikiCategoryId;
                  onSearch();
                }}>
                全部
              </NButton>
            ) : (
              <span></span>
            )}
            {props.categoryChildList.map((item: any) => (
              <NButton
                type={forms.wikiCategoryId === item.id ? 'primary' : 'default'}
                secondary={forms.wikiCategoryId === item.id ? false : true}
                round
                size="small"
                focusable={false}
                onClick={() => {
                  forms.wikiCategoryId = item.id;
                  onSearch();
                }}>
                {item.name}
              </NButton>
            ))}
          </NSpace>
          <TheSearch
            class={styles.inputSearch}
            value={props.searchValue}
            onUpdate:value={(val: string) => {
              forms.keyword = val
              emit("update:searchValue", val)
            }}
            placeholder="请输入音乐家名称"
            round
            onSearch={(val: string) => {
              forms.keyword = val;
              onSearch();
            }}
          />
        </div>
      </div>
    );
  }
});
