import { Cell, Image, List } from 'vant'
import { defineComponent } from 'vue'
import styles from './index.module.less'
import iconStudent from '@common/images/icon_student.png'
import request from '@/helpers/request'
import ColResult from '@/components/col-result'
import ColHeader from '@/components/col-header'

export default defineComponent({
  name: 'my-fans',
  data() {
    return {
      list: [],
      dataShow: true, // 判断是否有数据
      loading: false,
      finished: false,
      params: {
        page: 1,
        rows: 20
      }
    }
  },
  mounted() {},
  methods: {
    async getList() {
      try {
        const params = this.params
        const res = await request.post('/api-teacher/teacher/queryMyFans', {
          data: {
            ...params
          }
        })
        this.loading = false
        const result = res.data || {}
        // 处理重复请求数据
        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
      }
    }
  },
  render() {
    return (
      <div>
        <ColHeader />
        {this.dataShow ? (
          <List
            v-model:loading={this.loading}
            finished={this.finished}
            finishedText=" "
            class={[styles.liveList, 'mb12']}
            onLoad={this.getList}
          >
            {this.list.map((item: any) => (
              <Cell
                class={styles.myFans}
                titleStyle={{ paddingLeft: '8px' }}
                v-slots={{
                  icon: () => (
                    <Image
                      class={styles.userImg}
                      src={item.avatar || iconStudent}
                      fit="cover"
                    />
                  ),
                  title: () => (
                    <div class={styles.userInfo}>
                      <div class={styles.userName}> {item.userName} </div>
                      <div class={styles.piNameSubject}>
                        <span class={styles.subject}>{item.subjectName}</span>
                      </div>
                    </div>
                  )
                }}
              />
            ))}
          </List>
        ) : (
          <ColResult btnStatus={false} classImgSize="SMALL" tips="暂无粉丝" />
        )}
      </div>
    )
  }
})
