import ColResult from '@/components/col-result'
import request from '@/student/home-layout-orchestra/request-home'
import { verifyUrl } from '@/helpers/toolsValidate'
import { dateFormat } from '@/helpers/utils'
import { openDefaultWebView } from '@/student/home-layout-orchestra/state-orchestra'
import { Cell, CellGroup, Image, List } from 'vant'
import { defineComponent, reactive } from 'vue'
import { useRouter } from 'vue-router'
import styles from './index.module.less'
import event from '../../event'

export default defineComponent({
  name: 'info-list',
  setup() {
    const router = useRouter()
    const params = reactive({
      platformType: 'STUDENT',
      type: '1',
      clientType: 'STUDENT',
      page: 1,
      rows: 20
    })
    const state = reactive({
      list: [],
      dataShow: true, // 判断是否有数据
      loading: false,
      finished: false
    })
    const getList = async () => {
      try {
        const res = await request.post('/api-cms/news/page', {
          data: {
            ...params
          }
        })
        state.loading = false
        const result = res.data || {}
        // 处理重复请求数据
        if (state.list.length > 0 && result.pageNo === 1) {
          return
        }
        state.list = state.list.concat(result.rows || [])
        state.finished = result.pageNo >= result.totalPage
        params.page = result.pageNo + 1
        state.dataShow = state.list.length > 0
      } catch {
        state.dataShow = false
        state.finished = true
      }
    }

    getList()
    return () => (
      <>
        {state.dataShow ? (
          <List
            v-model:loading={state.loading}
            finished={state.finished}
            finishedText=" "
            immediateCheck={false}
            class={[styles.liveList]}
            onLoad={getList}
          >
            <CellGroup inset class={state.list.length > 0 && styles.cellGroup}>
              {state.list.map((item: any) => (
                <Cell
                  valueClass={styles.valueContent}
                  onClick={() => {
                    // let url =
                    //   location.origin +
                    //   location.pathname +
                    //   '#/specialDetail?id=' +
                    //   item.id

                    // if (verifyUrl(item.linkUrl)) {
                    //   openDefaultWebView(item.linkUrl, () => {
                    //     window.location.href = item.linkUrl
                    //   })
                    // } else {
                    //   openDefaultWebView(url, () => {
                    //     router.push({
                    //       path: '/specialDetail',
                    //       query: {
                    //         id: item.id
                    //       }
                    //     })
                    //   })
                    // }
                    event.emit('downloadApp')
                  }}
                >
                  {{
                    icon: () => (
                      <Image src={item.coverImage} class={styles.infoImg} />
                    ),
                    value: () => (
                      <>
                        <h2 class={'van-multi-ellipsis--l2'}>{item.title}</h2>
                        <p>{dateFormat(item.createTime, 'YYYY年MM月DD日')}</p>
                      </>
                    )
                  }}
                </Cell>
              ))}
            </CellGroup>
          </List>
        ) : (
          <ColResult
            btnStatus={false}
            classImgSize="SMALL"
            tips="暂无热门资讯"
          />
        )}
      </>
    )
  }
})
