import OEmpty from '@/components/o-empty'
import OSearch from '@/components/o-search'
import OSticky from '@/components/o-sticky'
import request from '@/helpers/request'
import { forms } from '@/school/train-planning/create'
import { state } from '@/state'
import { Cell, CellGroup, List } from 'vant'
import { defineComponent, onMounted, reactive } from 'vue'
import styles from './index.module.less'

export default defineComponent({
  name: 'help-center',
  setup() {
    const form = reactive({
      isClick: false,
      list: [] as any,
      listState: {
        dataShow: true, // 判断是否有数据
        loading: false,
        finished: false
      },
      params: {
        keyword: null,
        status: true,
        page: 1,
        rows: 20
      }
    })

    const getList = async () => {
      try {
        if (form.isClick) return
        form.isClick = true
        const res = await request.post(state.platformApi + '/helpCenterContent/page', {
          data: {
            ...form.params,
            catalogType: state.platformType
          }
        })
        form.listState.loading = false
        const result = res.data || {}
        // 处理重复请求数据
        if (form.list.length > 0 && result.current === 1) {
          return
        }
        form.list = form.list.concat(result.rows || [])
        form.listState.finished = result.current >= result.pages
        form.params.page = result.current + 1
        form.listState.dataShow = form.list.length > 0
        form.isClick = false
      } catch {
        form.listState.dataShow = false
        form.listState.finished = true
        form.isClick = false
      }
    }

    const onSearch = (val: any) => {
      form.params.keyword = val
      form.params.page = 1
      form.list = []
      form.listState.dataShow = true // 判断是否有数据
      form.listState.loading = false
      form.listState.finished = false
      getList()
    }

    onMounted(() => {
      getList()
    })
    return () => (
      <>
        <OSticky position="top">
          <OSearch onSearch={onSearch} />
        </OSticky>

        {form.listState.dataShow ? (
          <List
            v-model:loading={form.listState.loading}
            finished={form.listState.finished}
            finishedText=" "
            class={[styles.liveList]}
            onLoad={getList}
            immediateCheck={false}
          >
            {form.list.map((item: any) => (
              <Cell titleClass={[styles.title, 'van-ellipsis']} title={'ijijii'} isLink></Cell>
            ))}
          </List>
        ) : (
          <OEmpty btnStatus={false} tips="暂无数据" />
        )}
      </>
    )
  }
})
