import ColHeader from '@/components/col-header'
import ColSearch from '@/components/col-search'
import { Sticky, Image, List, Popup, Icon } from 'vant'
import { defineComponent } from 'vue'
import styles from './index.module.less'
import VideoItem from './video-item'

import banner from './images/banner.png'
import request from '@/helpers/request'
import ColResult from '@/components/col-result'
import { state } from '@/state'
import OrganSearch from '../practice-class/model/organ-search'

export default defineComponent({
  name: 'VideoClass',
  data() {
    const sessionSubjectId = sessionStorage.getItem('videoClassSubjectId')
    return {
      search: '',
      list: [],
      dataShow: true, // 判断是否有数据
      loading: false,
      finished: false,
      sessionSubjectId,
      params: {
        search: '',
        lessonSubject: (sessionSubjectId ||
          state.user.data?.subjectId ||
          null) as any,
        subjectName: '',
        page: 1,
        rows: 20
      },
      searchStatus: false,
      openStatus: false,
      subjectList: []
    }
  },
  async mounted() {
    try {
      const res = await request.get('/api-student/subject/subjectSelect')
      this.subjectList = res.data || []
    } catch {}
    let subjectName = ''
    this.subjectList.forEach((item: any) => {
      item.subjects?.forEach((child: any) => {
        if (child.id === Number(this.sessionSubjectId)) {
          subjectName = child.name
        }
      })
    })
    this.params.subjectName = subjectName || state.user.data?.subjectName || ''
    sessionStorage.removeItem('videoClassSubjectId')
  },
  methods: {
    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/videoLesson/selectGroup', {
          data: {
            ...params
          }
        })
        this.loading = false
        const result = res.data || {}
        console.log(result)
        // 处理重复请求数据
        if (this.list.length > 0 && result.pageNo === 1) {
          return
        }
        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
      }
    },
    onSort() {
      this.params.page = 1
      this.list = []
      this.dataShow = true // 判断是否有数据
      this.loading = false
      this.finished = false
      this.searchStatus = false
      this.getList()
    },
    onSearch(val: string) {
      this.params.search = val
      this.onSort()
    },
    onDetail(item: any) {
      this.$router.push({
        path: '/videoDetail',
        query: {
          groupId: item.id
        }
      })
    }
  },
  render() {
    return (
      <div class={styles.videoClass}>
        <Sticky offsetTop={0} position="top">
          <ColHeader
            class={styles.classHeader}
            border={false}
            isFixed={false}
            background="transparent"
          />
          <ColSearch
            placeholder="请输入老师名称/课程名称"
            onSearch={this.onSearch}
            v-slots={{
              left: () => (
                <div
                  class={styles.label}
                  onClick={() => {
                    this.searchStatus = !this.searchStatus
                    this.openStatus = !this.openStatus
                  }}
                >
                  {this.params.subjectName}
                  <Icon
                    classPrefix="iconfont"
                    name="down"
                    size={12}
                    color="#333"
                  />
                </div>
              )
            }}
          />
        </Sticky>

        <div class={styles.banner}>
          <Image src={banner} />
        </div>

        <div>
          {this.dataShow ? (
            <List
              class={styles.videoList}
              v-model:loading={this.loading}
              finished={this.finished}
              finishedText="没有更多了"
              onLoad={this.getList}
            >
              {this.list.map(item => (
                <VideoItem item={item} onClick={this.onDetail} />
              ))}
            </List>
          ) : (
            <ColResult btnStatus={false} tips="暂无视频课" />
          )}
        </div>

        <Popup
          show={this.searchStatus}
          position="bottom"
          round
          closeable
          safe-area-inset-bottom
          onClose={() => (this.searchStatus = false)}
          onClosed={() => (this.openStatus = false)}
        >
          {this.openStatus && (
            <OrganSearch
              subjectList={this.subjectList}
              onSort={this.onSort}
              v-model={this.params.lessonSubject}
              v-model:subjectName={this.params.subjectName}
            />
          )}
        </Popup>
      </div>
    )
  }
})
