import { defineComponent, onMounted, reactive } from 'vue';
import styles from './index.module.less';
import CardType from '/src/components/card-type';
import Pagination from '/src/components/pagination';
import SearchGroupResources from './search-group-resources';
import { materialQueryPage } from '../../api';
import { NModal, NSpin } from 'naive-ui';
import TheEmpty from '/src/components/TheEmpty';
import CardPreview from '/src/components/card-preview';

export default defineComponent({
  name: 'share-resources',
  setup() {
    const state = reactive({
      searchWord: '',
      loading: false,
      pageTotal: 0,
      pagination: {
        page: 1,
        rows: 20
      },
      searchGroup: {
        type: 'MUSIC', //
        keyword: '',
        bookVersionId: null,
        subjectId: null,
        sourceType: 2
      },
      tableList: [] as any,
      teachingStatus: false,
      show: false,
      item: {} as any
    });
    const getList = async () => {
      try {
        state.loading = true;
        const { data } = await materialQueryPage({
          ...state.searchGroup,
          ...state.pagination
        });
        state.loading = false;
        state.pageTotal = Number(data.total);
        state.tableList = data.rows || [];
      } catch {
        state.loading = false;
      }
    };

    const onSearch = async (item: any) => {
      state.pagination.page = 1;
      state.searchGroup = Object.assign(state.searchGroup, item);
      console.log(item, state.searchGroup);
      getList();
    };

    onMounted(() => {
      getList();
    });
    return () => (
      <>
        <SearchGroupResources
          onSearch={(item: any) => onSearch(item)}
          onAdd={() => (state.teachingStatus = true)}
        />

        <NSpin v-model:show={state.loading}>
          <div class={styles.list}>
            {state.tableList.map((item: any) => {
              const tmpItem = {
                id: item.id,
                coverImg: item.coverImg,
                type: item.type,
                title: item.name,
                isCollect: !!item.favoriteFlag,
                isSelected: item.sourceFrom === 'PLATFORM' ? true : false,
                content: item.content
              };
              return (
                <CardType
                  item={tmpItem}
                  onClick={(val: any) => {
                    state.show = true;
                    state.item = val;
                  }}
                />
              );
            })}

            {!state.loading && state.tableList.length <= 0 && (
              <TheEmpty description="暂无共享资源" />
            )}
          </div>
        </NSpin>

        <Pagination
          v-model:page={state.pagination.page}
          v-model:pageSize={state.pagination.rows}
          v-model:pageTotal={state.pageTotal}
          onList={getList}
        />

        {/* 弹窗查看 */}
        <CardPreview v-model:show={state.show} item={state.item} />

        {/* 添加自定义教材 */}
        <NModal
          v-model:show={state.teachingStatus}
          preset="card"
          showIcon={false}
          class={['modalTitle background', styles.teachingModal]}
          title={'自定义教材'}
          blockScroll={false}>
          1212
        </NModal>
      </>
    );
  }
});
