import { NTabPane, NTabs } from 'naive-ui';
import { PropType, defineComponent, onMounted, ref, toRefs } from 'vue';
import styles from './index.module.less';
import SelectItem from './select-item';
import { useResizeObserver } from '@vueuse/core';
import { useCatchStore } from '/src/store/modules/catchData';

export default defineComponent({
  name: 'select-music',
  props: {
    type: {
      type: String,
      default: 'myResources'
    },
    /** 类型 */
    cardType: {
      type: String as PropType<'' | 'homerowk-record' | 'prepare'>,
      default: ''
    },
    /** 从哪里使用 */
    from: {
      type: String,
      default: ''
    }
  },
  emits: ['select', 'add'],
  setup(props, { emit }) {
    const { type } = toRefs(props);
    const tabType = ref(type.value);
    const catchStore = useCatchStore();

    onMounted(async () => {
      useResizeObserver(
        document.querySelector(
          '.select-resource .n-tabs-nav--top'
        ) as HTMLElement,
        (entries: any) => {
          const entry = entries[0];
          const { height } = entry.contentRect;
          document.documentElement.style.setProperty(
            '--modal-lesson-tab-height',
            height + 'px'
          );
        }
      );

      // 获取教材分类列表
      await catchStore.getMusicSheetCategory(true);
    });
    return () => (
      <div class={[styles.selectMusic, 'select-resource']}>
        <NTabs
          animated
          value={tabType.value}
          paneClass={styles.paneTitle}
          justifyContent="center"
          paneWrapperClass={styles.paneWrapperContainer}
          onUpdate:value={(val: string) => {
            tabType.value = val;
          }}>
          <NTabPane name="myResources" tab={'我的曲目'}>
            <SelectItem
              cardType={props.cardType}
              from={props.from}
              type="myResources"
              onAdd={(item: any) => emit('add', item)}
            />
          </NTabPane>
          <NTabPane name="shareResources" tab={'共享曲目'}>
            <SelectItem
              from={props.from}
              cardType={props.cardType}
              type="shareResources"
              onAdd={(item: any) => emit('add', item)}
            />
          </NTabPane>
          <NTabPane name="myCollect" tab="收藏曲目">
            <SelectItem
              from={props.from}
              cardType={props.cardType}
              type="myCollect"
              onAdd={(item: any) => emit('add', item)}
            />
          </NTabPane>
        </NTabs>
      </div>
    );
  }
});
