import request from '@/helpers/request'
import { state } from '@/state'
import { Button, Empty, Grid, GridItem, Icon, showToast, Toast } from 'vant'
import { defineComponent, onMounted, reactive } from 'vue'
import styles from './index.module.less'
import iconLook from './image/look.svg'
import { useRoute, useRouter } from 'vue-router'
import OEmpty from '@/components/o-empty'
import OSticky from '@/components/o-sticky'
import OHeader from '@/components/o-header'
import CourseItem from './component/CourseItem'
export default defineComponent({
  name: 'lessonCourseware',
  setup() {
    const route = useRoute()
    const router = useRouter()
    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
      if (route.query.code === 'select') {
        try {
          const res: any = await request.post(
            state.platformApi + `/courseSchedule/getCourseware/${route.query.courseScheduleId}`
          )
          if (Array.isArray(res?.data)) {
            const data = res.data.map((n: any) => {
              return {
                coverImg: n.coverImg,
                name: n.coursewareName,
                id: n.lessonCoursewareId,
                courseNum: n.coursewareNum
              }
            })
            data.list = filterData(data)
          }
        } catch (error) {}
      } else {
        try {
          const res: any = await request.post(state.platformApi + '/courseSchedule/myCourseware')
          if (Array.isArray(res?.data)) {
            data.list = filterData(res.data)
          }
        } catch (error) {}
      }
      data.loading = false
    }
    onMounted(() => {
      getList()
    })
    const handleClick = (item: any) => {
      if (route.query.code === 'select') {
        router.push({
          path: '/courseList',
          query: {
            ...route.query,
            id: item.id
          }
        })
        return
      }
      router.push({
        path: '/courseList',
        query: {
          id: item.id
        }
      })
    }

    return () => (
      <div class={[styles.lessonCourseware, !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) => {
          console.error(key)
          return <CourseItem term={key} list={data.list[key]} onItemClick={(row) => handleClick(row)} />
        })}
        {!data.loading && !Object.values(data.list).length && <OEmpty tips="没有课件" />}
      </div>
    )
  }
})
