import ColResult from '@/components/col-result'
import { Cell, CellGroup, List, Image, Icon } from 'vant'
import { defineComponent } from 'vue'
import styles from './live.module.less'
import iconLive from '../images/icon-live.png'
import request from '@/helpers/request'
import dayjs from 'dayjs'
import { state } from '@/state'
import Tips from './tips'
import { moneyFormat } from '@/helpers/utils'

export default defineComponent({
  name: 'group',
  data() {
    const query = this.$route.query
    return {
      list: [],
      dataShow: true, // 判断是否有数据
      loading: false,
      finished: false,
      params: {
        teacherId: query.teacherId,
        courseType: "GROUP",
        groupStatus: 'APPLY',
        page: 1,
        rows: 20
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    formatTime(time: string) {
      const timeStr = dayjs(time || new Date())
      // const weekStr = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
      // // console.log(timeStr.day())

      return timeStr.format('YYYY-MM-DD HH:mm')
    },
    async getList() {
      try {
        const params: any = {
          ...this.params
        }
        if (state.version) {
          params.version = state.version || '' // 处理ios审核版本
          params.platform = 'ios-student'
        }
        const res = await request.post(
          '/api-student/courseGroup/queryPageCourseGroup',
          {
            data: {
              ...params
            }
          }
        )
        this.loading = false
        const result = res.data || {}
        // 处理重复请求数据
        if (this.list.length > 0 && result.pageNo === 1) {
          return
        }
        const rows = result.rows || []
        rows.forEach((item: any) => {
          const num = item.maxStudentNum - item.studentCount
          item.studentLastCount = num <= 0 ? 0 : num
        })
        this.list = this.list.concat(result.rows || [])
        this.finished = result.pageNo >= result.totalPage
        this.params.page = result.pageNo + 1
        this.dataShow = this.list.length > 0
      } catch {
        this.dataShow = false
        this.finished = true
      }
    },
    onDetail(item: any) {
      this.$router.push({
        path: '/groupDetail',
        query: {
          groupId: item.courseGroupId
        }
      })
    }
  },
  render() {
    return (
      <div>
        <Tips
          type="GROUP"
          class={styles.tips}
          title="什么是小组课？"
          content="小组课是老师根据教学目的编排的课程，固定时间进行1V5线上授课。您可根据老师开放的课程内容和您的时间安排，选择您感兴趣的课程组进行学习。"
        />
        {this.dataShow ? (
          <List
            class={styles.liveList}
            v-model:loading={this.loading}
            immediateCheck={false}
            finished={this.finished}
            finishedText=" "
          >
            {this.list.map((item: any) => (
              <CellGroup
                class={styles.liveGroup}
                border={false}
                onClick={() => this.onDetail(item)}
              >
                <div class={styles.liveTop}>
                  <img src={iconLive} class={styles.iconLive} />
                  <span>开课时间： {this.formatTime(item.courseStartTime)} </span>
                </div>
                <div class={styles.liveCenter}>
                  <Image
                    class={styles.liCover}
                    src={item.backgroundPic}
                    fit="cover"
                  />
                  <span class={styles.subjectName}>{item?.subjectName}</span>
                </div>

                <div class={styles.liContent}>
                  <div class={[styles.liTitle, 'van-ellipsis']}>
                    {item.courseGroupName}
                  </div>
                  <div class={styles.users}>
                    <div class={styles.lean}>
                      {item.studentLastCount > 0 ? (<span class={styles.num}>
                        剩余{item.studentLastCount}个名额
                      </span>) : ""}
                    </div>
                    <div class={styles.price}>
                      {item.coursePrice > 0 && (
                        <span class={styles.priceNum}>
                          <i>¥</i>
                          {moneyFormat(
                            item.coursePrice, '0.00'
                          )}
                        </span>
                      )}
                      <span class={styles.label}>
                        {item.coursePrice > 0 && '/'}
                        {item.courseNum}课时
                      </span>
                    </div>
                  </div>
                </div>
              </CellGroup>
            ))}
          </List>
        ) : (
          <ColResult btnStatus={false} classImgSize="SMALL" tips="暂无小组课" />
        )}
      </div>
    )
  }
})
