import { Cell, Dialog, Icon, Image, List, Rate, Toast } from 'vant'
import { defineComponent } from 'vue'
import styles from './model/teacher-header.module.less'
import iconTeacher from '@common/images/icon_teacher.png'
import musicCert from '@common/images/music_cert.png'
import teacherCert from '@common/images/teacher_cert.png'
import request from '@/helpers/request'
import ColResult from '@/components/col-result'
import { postMessage } from '@/helpers/native-message'
export const getAssetsHomeFile = (fileName: string) => {
  const path = `./images/${fileName}`
  const modules = import.meta.globEager('./images/*')
  return modules[path].default
}

export default defineComponent({
  name: 'teacher-follow',
  data() {
    return {
      userInfo: {} as any,
      starGrade: 0,
      subjectNameList: [],
      list: [],
      dataShow: true, // 判断是否有数据
      loading: false,
      finished: false,
      params: {
        page: 1,
        rows: 20
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    getSubjectNameList(subject: string) {
      const subjectList = subject.split(',')
      return subjectList || []
    },
    onSort() {
      this.params.page = 1
      this.list = []
      this.dataShow = true // 判断是否有数据
      this.loading = false
      this.finished = false
      this.getList()
    },
    async getList() {
      try {
        let params = this.params
        const res = await request.post('/api-student/student/queryMyFollow', {
          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
      }
    },
    async onUnLike(item: any) {
      Dialog.confirm({
        message: '确定取消关注吗？',
        confirmButtonColor: 'var(--van-primary)'
      }).then(async () => {
        try {
          await request.get('/api-student/teacher/starOrUnStar', {
            params: {
              userId: item.userId,
              starStatus: 0
            }
          })
          Toast('取消关注成功')
          setTimeout(() => {
            this.onSort()
          }, 1000)
        } catch {}
      })
    }
  },
  render() {
    return (
      <div class={styles.teacherFollow}>
        {this.dataShow ? (
          <List
            class={styles.liveList}
            v-model:loading={this.loading}
            immediateCheck={false}
            finished={this.finished}
            finishedText="没有更多了"
          >
            {this.list.map((item: any) => (
              <div class={[styles.headerCount, styles.headerFollow]}>
                <Cell
                  class={styles['open-teacher-info']}
                  border={false}
                  center
                  v-slots={{
                    icon: () => (
                      <div style={{ position: 'relative', lineHeight: 0 }}>
                        <Image
                          class={styles.userLogo}
                          src={item.avatar || iconTeacher}
                          fit="cover"
                        />
                        {item.liveFlag === 1 && (
                          <p
                            class={styles.liveTag}
                            onClick={() => {
                              postMessage({
                                api: 'joinLiveRoom',
                                content: {
                                  roomId: item.roomUid,
                                  teacherId: item.userId
                                }
                              })
                            }}
                          >
                            直播中
                          </p>
                        )}
                      </div>
                    ),
                    'right-icon': () => (
                      <Icon
                        name="like"
                        color="#FF6363"
                        size={18}
                        onClick={() => {
                          this.onUnLike(item)
                        }}
                      />
                    )
                  }}
                >
                  <div class={styles['teacher-info']}>
                    <div class={styles['teacher-name']}>
                      <div class={styles.teacherCert}>
                        <span style={{ display: 'inline-block' }}>
                          {item.userName || `游客${item.userId || ''}`}
                        </span>

                        {item.entryFlag === 1 && (
                          <Image
                            class={styles.cert}
                            src={teacherCert}
                            // fit="contain"
                          />
                        )}
                        {item.musicianFlag === 1 && (
                          <Image
                            class={styles.cert}
                            src={musicCert}
                            // fit="contain"
                          />
                        )}
                      </div>
                    </div>
                    <div class={styles.level}>
                      {item.starGrade ? (
                        <Rate
                          readonly
                          modelValue={item.starGrade}
                          iconPrefix="iconfont"
                          color="#FFC459"
                          void-icon="star_default"
                          icon="star_active"
                          size={15}
                        />
                      ) : (
                        <span style={{ fontSize: '12px', color: '#999999' }}>
                          暂无评分
                        </span>
                      )}
                    </div>
                  </div>
                </Cell>
                <p class={styles.piNameSubject}>
                  <Image
                    class={styles.subjectSection}
                    src={getAssetsHomeFile('icon_subject.png')}
                    fit="contain"
                  />
                  {this.getSubjectNameList(item.subjectName).map(
                    (item: any) => (
                      <span class={styles.subject}>{item}</span>
                    )
                  )}
                </p>
                <div class={styles['teacher-bottom']}>
                  <div class={styles['teacher-data']}>
                    <div class={styles['teacher-data_item']}>
                      粉丝 <span>{item.fansNum || 0}</span>
                    </div>
                    <div class={styles['teacher-data_item']}>
                      已上课时 <span>{item.expTime || 0}</span>
                    </div>
                  </div>
                </div>
              </div>
            ))}
          </List>
        ) : (
          <ColResult btnStatus={false} classImgSize="SMALL" tips="暂无关注" />
        )}
      </div>
    )
  }
})
