import request from '@/helpers/request'
import {
  Cell,
  List,
  Sticky,
  Image,
  CellGroup,
  Collapse,
  CollapseItem
} from 'vant'
import { defineComponent } from 'vue'
import styles from './theory.module.less'
import ColSearch from '@/components/col-search'
import ColResult from '@/components/col-result'

export default defineComponent({
  name: 'special',
  data() {
    const query = this.$route.query
    return {
      activeNames: [] as any,
      list: [],
      dataShow: true, // 判断是否有数据
      loading: false,
      finished: false,
      // 1热门资讯,2开屏广告，3闪页管理，4轮播图管理 5按钮管理 6 乐理章节
      params: {
        search: '',
        page: 1,
        rows: 20
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    async getList() {
      try {
        let params = this.params
        const res = await request.post('/api-cms/music/theory/app/page', {
          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

        const tempList: any = this.list
        this.list.length > 0 && this.activeNames.push(tempList[0].id)
      } catch {
        this.dataShow = false
        this.finished = true
      }
    },
    onSearch(val: string) {
      this.params.search = val
      this.params.page = 1
      this.list = []
      this.dataShow = true // 判断是否有数据
      this.loading = false
      this.finished = false
      this.getList()
    },
    onDetail(item: any) {
      if (item.linkUrl) {
        window.location.href = item.linkUrl
      } else {
        this.$router.push({
          path: 'theoryDetail',
          query: {
            id: item.id
          }
        })
      }
    }
  },
  render() {
    return (
      <div class={styles['theory']}>
        <Sticky offsetTop={0} position="top" class={'mb12'}>
          <ColSearch onSearch={this.onSearch} />
        </Sticky>
        {this.dataShow ? (
          <List
            class={styles.videoList}
            v-model:loading={this.loading}
            finished={this.finished}
            finishedText="没有更多了"
            immediateCheck={false}
            onLoad={this.getList}
          >
            {this.list.map((parent: any) => (
              <Collapse v-model={this.activeNames} border={false}>
                <CollapseItem
                  title={parent.name}
                  name={parent.id}
                  center
                  v-slots={{
                    title: () => (
                      <div class={[styles.groupTitle]}>
                        {parent.url && (
                          <Image
                            src={parent.url}
                            fit="cover"
                            class={styles.groupImg}
                          />
                        )}

                        <p class={['van-ellipsis', styles.musicTitle]}>
                          {parent.name}
                        </p>
                      </div>
                    )
                  }}
                >
                  {parent.newsInformationList.map((item: any) => (
                    <Cell
                      title={item.title}
                      class={styles.cell}
                      border={false}
                      onClick={() => {
                        this.onDetail(item)
                      }}
                      titleClass={['van-ellipsis', styles.title]}
                    />
                  ))}
                </CollapseItem>
              </Collapse>
            ))}
          </List>
        ) : (
          <ColResult btnStatus={false} classImgSize="SMALL" tips="暂无内容" />
        )}
      </div>
    )
  }
})
