import { state } from '@/state'
import request from '@/helpers/request'
import { Cell, List, Sticky } from 'vant'
import { defineComponent } from 'vue'
import ColSearch from '@/components/col-search'
import ColResult from '@/components/col-result'

export default defineComponent({
  name: 'help-center',
  data() {
    const query = this.$route.query
    const title = query.catalogType == '2' ? '公告列表' : '帮助中心'
    document.title = title

    // 处理兼容性问题
    if (query.mode === 'accompany') {
      query.platformType = 'ANALYSIS'
    }
    return {
      list: [],
      dataShow: true, // 判断是否有数据
      dataLoading: false,
      loading: false,
      finished: false,
      // 1:帮助中心，2：公告管理
      params: {
        catalogIds: query.catalogType || 1,
        title: '',
        status: 1,
        // STUDENT:学生 TEACHER：老师 ANALYSIS:智能陪练
        catalogType: query.platformType || state.platformType,
        page: 1,
        rows: 20
      }
    }
  },
  methods: {
    async getList() {
      try {
        if (this.dataLoading) {
          return
        }
        this.dataLoading = true
        let params = this.params
        const res = await request.post('/api-cms/helpCenterContent/list', {
          data: {
            ...params
          }
        })
        this.dataLoading = false
        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.title = val
      this.params.page = 1
      this.list = []
      this.dataShow = true // 判断是否有数据
      this.loading = false
      this.finished = false
      this.getList()
    },
    onDetail(item: any) {
      this.$router.push({
        path: 'helpCenterDetail',
        query: {
          id: item.id,
          catalogType: this.params.catalogType
        }
      })
    }
  },
  render() {
    return (
      <div>
        <Sticky offsetTop={0} position="top" class={'mb12'}>
          <ColSearch onSearch={this.onSearch} />
        </Sticky>
        {this.dataShow ? (
          <List
            v-model:loading={this.loading}
            finished={this.finished}
            finishedText="没有更多了"
            onLoad={this.getList}
          >
            {this.list.map((item: any) => (
              <Cell
                title={item.title}
                titleClass={'van-ellipsis'}
                isLink
                onClick={() => {
                  this.onDetail(item)
                }}
              />
            ))}
          </List>
        ) : (
          <ColResult btnStatus={false} classImgSize="SMALL" tips="暂无内容" />
        )}
      </div>
    )
  }
})
