import MHeader from '@/components/m-header';
import MSticky from '@/components/m-sticky';
import { defineComponent, onMounted, reactive, ref } from 'vue';
import styles from './index.module.less';
import { useRoute, useRouter } from 'vue-router';
import { Button, Collapse, CollapseItem, Image, showToast } from 'vant';
import request from '@/helpers/request';
import library1 from './images/library-1.png';
import library2 from './images/library-2.png';
import library3 from './images/library-3.png';
import { useEventListener, useWindowScroll } from '@vueuse/core';
import KnowledgeGuide from '@/custom-plugins/guide-page/knowledge-guide';
import MEmpty from '@/components/m-empty';
export default defineComponent({
  name: 'knowledge-ligrary',
  setup() {
    const router = useRouter();
    const route = useRoute();
    const showGuide = ref()
    const forms = reactive({
      cid: route.query.cid,
      list: [] as any,
      activeNames: sessionStorage.getItem('knowledge-ligrary-key')
        ? Number(sessionStorage.getItem('knowledge-ligrary-key'))
        : 0,
      background: 'transparent',
      color: '#fff'
    });

    const getList = async () => {
      try {
        const { data } = await request.post(
          '/edu-app/lessonCourseware/queryStudentLessonDetail',
          {
            requestType: 'form',
            data: {
              lessonCoursewareId: forms.cid
            }
          }
        );
        forms.list = data;
        showGuide.value = true
      } catch {
        //
      }
    };

    const getBg = (index: number) => {
      if (index % 3 === 1) {
        return library1;
      } else if (index % 3 === 2) {
        return library2;
      } else if (index % 3 === 0) {
        return library3;
      }
    };

    // 练习模式
    const onGotoModel = async (type: string) => {
      try {
        const { data } = await request.get(
          '/edu-app/studentUnitExamination/checkKnowledgePointIds',
          {
            params: {
              lessonCoursewareId: forms.cid
            }
          }
        );
        if (!data) {
          setTimeout(() => {
            showToast('暂无题目');
          }, 100);
          return;
        }

        if (type === 'TEST') {
          // 模拟测试
          router.push({
            path: '/examination-mode',
            query: { lessonCoursewareId: forms.cid }
          });
        } else {
          router.push({
            path: '/practice-mode',
            query: { lessonCoursewareId: forms.cid }
          });
        }
      } catch {
        //
      }
    };

    onMounted(() => {
      useEventListener(document, 'scroll', () => {
        const { y } = useWindowScroll();
        if (y.value > 52) {
          forms.background = '#fff';
          forms.color = '#323333';
        } else {
          forms.background = 'transparent';
          forms.color = '#fff';
        }
      });
      getList();
    });
    return () => (
      <div class={styles.knowledgeLibrary}>
        <MSticky position="top">
          <MHeader
            border={false}
            background={forms.background}
            color={forms.color}>
            {{
              right: () => (
                <div
                  class={[styles.wroingBtn]}
                  onClick={() => router.push('/wroing-book')}>
                  <i class={styles.iconWroing}></i>错题本
                </div>
              )
            }}
          </MHeader>
        </MSticky>

        <div class={styles.btnGroup}>
          <div
            id="knowledge-0"
            class={styles.btnPractice}
            onClick={() => onGotoModel('PRACTICE')}></div>

          <div
            id="knowledge-1"
            class={styles.btnTest}
            onClick={() => onGotoModel('TEST')}></div>
        </div>

        <div class={[styles.containerSection, styles.librarySection]}>
          <Collapse
            v-model={forms.activeNames}
            accordion
            border={false}
            onChange={(val: any) => {
              sessionStorage.setItem('knowledge-ligrary-key', val);
            }}>
            {forms.list.map((item: any, index: number) => (
              <CollapseItem
                title={item.lessonCoursewareDetailName}
                name={index}
                value={forms.activeNames === index ? '收起' : '展开'}
                border={false}>
                {item.knowledgeDetails &&
                  item.knowledgeDetails.map((detail: any, index: number) => (
                    <div
                      class={styles.unitItem}
                      onClick={() =>
                        router.push({
                          path: '/unit-detail',
                          query: {
                            detailId: detail.lessonCoursewareKnowledgeDetailId
                          }
                        })
                      }>
                      <Image
                        {...{ id: index == 0 ? 'knowledge-2' : '' }}
                        class={styles.unitImg}
                        lazyLoad
                        src={getBg(index)}
                      />
                      <p class={styles.name}>
                        <span>{detail.name}</span>
                      </p>
                    </div>
                  ))}
                {item.knowledgeDetails && item.knowledgeDetails.length <= 0 && (
                  <MEmpty description="暂无章节" />
                )}
              </CollapseItem>
            ))}
          </Collapse>
        </div>
        {showGuide.value && <KnowledgeGuide></KnowledgeGuide>}
      </div>
    );
  }
});
