import request from '@/helpers/request'
import { state } from '@/state'
import { Button, Empty, Grid, GridItem, Icon, showToast } from 'vant'
import { defineComponent, onMounted, reactive } from 'vue'
import styles from './index.module.less'
import { useRoute, useRouter } from 'vue-router'
import { postMessage } from '@/helpers/native-message'
import iconLook from './image/look.svg'
import iconCourse from './image/icon-course.png'
export default defineComponent({
  name: 'lessonCourseware',
  setup() {
    const route = useRoute()
    const router = useRouter()
    const data = reactive({
      loading: true,
      list: [] as any
    })
    const getList = async () => {
      data.loading = true
      if (route.query.courseScheduleId) {
        try {
          const res: any = await request.post(
            state.platformApi + '/courseSchedule/getCoursewareDetail',
            {
              params: {
                courseScheduleId: route.query.courseScheduleId,
                coursewareId: route.query.id
              }
            }
          )
          if (Array.isArray(res?.data)) {
            data.list = res.data
          }
        } catch (error) {}
      } else {
        try {
          const res: any = await request.post(
            state.platformApi + '/courseSchedule/myCoursewareDetail/' + route.query.id
          )
          if (Array.isArray(res?.data)) {
            data.list = res.data
          }
        } catch (error) {}
      }
      data.loading = false
    }
    onMounted(() => {
      getList()
    })

    const handleClick = (item: any) => {
      if (route.query.code === 'select') {
        console.log('选择课时')
        setCoursewareDetail(item)
        return
      }
      router.push({
        path: '/coursewarePlay',
        query: {
          id: item.lessonCoursewareDetailId
        }
      })
    }
    // 绑定课时
    const setCoursewareDetail = async (item: any) => {
      try {
        const res: any = await request.post(
          state.platformApi + '/courseSchedule/setCoursewareDetail',
          {
            params: {
              courseScheduleId: route.query.courseScheduleId,
              coursewareDetailId: item.lessonCoursewareDetailId
            }
          }
        )
        if (res.code === 200) {
          showToast('保存成功')
          postMessage({ api: 'back' })
        }
      } catch (error) {}
    }
    return () => (
      <div style={{ paddingTop: '14px' }}>
        <Grid gutter={14} columnNum={3} class={styles.grid}>
          {data.list.map((item: any) => {
            return (
              <GridItem>
                <div
                  class={styles.gridItem}
                  onClick={() => handleClick(item)}
                >
                  <img src={iconCourse} class={styles.cover} />
                  <div class={styles.title}>
                    <div>{item.coursewareDetailName}</div>
                    {route.query.code !== 'select' && <div>已使用 {item.useNum} 次</div>}
                  </div>
                  {route.query.code !== 'select' ? (
                    <div class={styles.num}>
                      查看
                      <Icon name="play-circle-o" />
                    </div>
                  ) : (
                    <div class={styles.num}>选择</div>
                  )}

                  {route.query.code == 'select' && !item.unlock && (
                    <div class={styles.look} onClick={(e: Event) => e.stopPropagation()}>
                      <Icon name={iconLook} /> 未解锁
                    </div>
                  )}
                </div>
              </GridItem>
            )
          })}
        </Grid>
        {!data.loading && !data.list.length && <Empty description="空空如也" />}
        <Button
          onClick={() => {
            location.href =
              'http://192.168.3.114:1000/teacher.html#/coursewarePlay?id=1610595720511209474&courseId=1612270880549044226'
          }}
        >
          胜强测试
        </Button>
      </div>
    )
  }
})
