import request from '@/helpers/request'
import { state } from '@/state'
import { Button, Empty, Grid, GridItem, Icon, showToast, Toast } from 'vant'
import { defineComponent, onMounted, reactive, ref } from 'vue'
import styles from './unit-lesson.module.less'
import iconLook from './image/look.svg'
import { useRoute, useRouter } from 'vue-router'
import OEmpty from '@/components/o-empty'
import CourseItem from '@/views/lessonCourseware/component/CourseItem'
import OSticky from '@/components/o-sticky'
import OHeader from '@/components/o-header'
export default defineComponent({
  name: 'unit-Lesson',
  setup() {
    const route = useRoute()
    const router = useRouter()
    const forms = ref({} as any)
    const data = reactive({
      loading: true,
      list: [] as any
    })
    
    const filterData = (list: any[]) => {
      const schoolTerm = {}
      for(let i = 0; i < list.length; i++){
        if (schoolTerm[list[i].sortNo]){
          schoolTerm[list[i].sortNo].push(list[i])
        } else {
          schoolTerm[list[i].sortNo] = [list[i]]
        }
      }
      return schoolTerm
    }

    const getList = async () => {
      data.loading = true
      try {
        const res: any = await request.post(
          state.platformApi + '/courseSchedule/getCoursewareByClassId',
          {
            requestType: 'form',
            data: {
              classGroupId: route.query.classGroupId
            }
          }
        )
        if (Array.isArray(res?.data)) {
          const list = res.data.filter((item: any) => item.unitTestNum).map((n: any) => {
            return {
              coverImg: n.coverImg,
              name: n.coursewareName,
              id: n.lessonCoursewareId,
              courseNum: n.coursewareNum
            }
          })
          data.list = filterData(list)
        }
      } catch (error) {
        console.log(error)
      }

      data.loading = false
    }
    onMounted(() => {
      forms.value = { ...JSON.parse(sessionStorage.getItem('unit-create') || '{}') } as any
      getList()
    })
    const handleClick = (item: any) => {
      forms.value.coursewareName = item.coursewareName
      forms.value.lessonCoursewareId = item.lessonCoursewareId
      sessionStorage.setItem('unit-create', JSON.stringify(forms.value))
      router.push({
        path: '/uni-test',
        query: {
          lessonCoursewareId: item.lessonCoursewareId
        }
      })
    }

    return () => (
      <div
        class={[styles.unitLesson, Object.values(data.list).length ? '' : 'emptyRootContainer']}
      >
        <OSticky
          onGetHeight={(height: number) => {
            document.documentElement.style.setProperty('--header-height', height + 'px')
          }}
        >
          <OHeader
            border={false}
            background="rgba(255, 232, 206, 1)"
            color="rgba(124, 61, 18, 1)"
            title="选择教材"
          />
        </OSticky>
        {Object.keys(data.list).map((key: any) => {
          return <CourseItem term={key} list={data.list[key]} onItemClick={(row) => handleClick(row)} />
        })}
        {!data.loading && !Object.values(data.list).length && <OEmpty btnStatus={false} tips="暂无教材"></OEmpty>}

        {/* <Button onClick={() => {
          location.href = 'http://192.168.3.114:1000/teacher.html#/courseList?id=1610595624868495362'
        }}>胜强测试</Button>
        {!data.loading && !data.list.length && <OEmpty tips="没有课件" />} */}
      </div>
    )
  }
})
