import { state } from '@/state'
import request from '@/helpers/request'
import { Cell, List, Sticky, Image, CellGroup } from 'vant'
import { defineComponent } from 'vue'
import styles from './special.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 {
      list: [],
      dataShow: true, // 判断是否有数据
      loading: false,
      finished: false,
      // 1热门资讯,2开屏广告，3闪页管理，4轮播图管理 5按钮管理 6 乐理章节
      params: {
        search: '',
        type: query.type || 1,
        platformType: state.platformType,
        page: 1,
        rows: 20
      }
    }
  },
  async mounted() {},
  methods: {
    async getList() {
      try {
        let params = this.params
        const res = await request.post('/api-cms/news/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
      } 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: 'specialDetail',
          query: {
            id: item.id
          }
        })
      }
    }
  },
  render() {
    return (
      <div class={styles['special']}>
        <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="没有更多了"
            onLoad={this.getList}
          >
            {this.list.map((item: any) => (
              <CellGroup
                inset
                class={'mb12'}
                onClick={() => {
                  this.onDetail(item)
                }}
              >
                <Cell
                  title={item.title}
                  titleClass={['van-ellipsis', styles.title]}
                  v-slots={{
                    label: () => (
                      <div class={styles.label}>
                        <div class={styles.time}>{item.createTime}</div>

                        <Image
                          class={styles.imgCover}
                          src={item.coverImage}
                          fit="cover"
                        />
                      </div>
                    )
                  }}
                />
              </CellGroup>
            ))}
          </List>
        ) : (
          <ColResult btnStatus={false} classImgSize="SMALL" tips="暂无内容" />
        )}
      </div>
    )
  }
})
