import OEmpty from '@/components/m-empty/index';
import OHeader from '@/components/m-header/index';
import OSearch from '@/components/m-search/index';
import OSticky from '@/components/m-sticky/index';
import request from '@/helpers/request';
import { state } from '@/state';
import dayjs from 'dayjs';
import { Cell, CellGroup, List, Image } from 'vant';
import { defineComponent, onMounted, reactive } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import styles from './help-center/index.module.less';

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

    const getList = async () => {
      try {
        if (form.isClick) return;
        form.isClick = true;
        const res = await request.post('/edu-app/open/helpCenterContent/page', {
          data: {
            ...form.params,
            catalogType: 'STUDENT'
          }
        });
        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();
    };

    const onDetail = (item: any) => {
      if (item.linkType === 'OUT') {
        window.location.href = item.linkUrl;
      } else {
        router.push({
          path: '/information-detail',
          query: {
            id: item.id
          }
        });
      }
    };

    onMounted(() => {
      getList();
    });
    return () => (
      <div
        class={[
          styles.helpCenter,
          !form.listState.dataShow && 'emptyRootContainer'
        ]}>
        <OSticky position="top">
          <OHeader border={false} isBack={route.query.platformType != 'ANALYSIS'} />
          <OSearch onSearch={onSearch} />
        </OSticky>

        {form.listState.dataShow ? (
          <List
            // v-model:loading={form.listState.loading}
            finished={form.listState.finished}
            finishedText=" "
            class={[styles.container, styles.containerInformation]}
            onLoad={getList}
            immediateCheck={false}>
            {form.list.map((item: any) => (
              <Cell
                class={styles.cell}
                onClick={() => onDetail(item)}
                titleStyle={{
                  display: 'flex',
                  flexDirection: 'column',
                  justifyContent: 'space-between'
                }}>
                {{
                  icon: () => (
                    <div
                      class={[styles.img, styles.bgImg]}
                      style={
                        item.coverImage
                          ? {
                              backgroundImage: `url(${item.coverImage})`,
                              backgroundSize: 'cover'
                            }
                          : ''
                      }></div>
                  ),
                  // <Image src={item.coverImage} class={styles.img} fit="cover" />,
                  title: () => (
                    <>
                      <div class={[styles.title, 'van-ellipsis']}>
                        {item.title}
                      </div>
                      <div class={[styles.content, 'van-multi-ellipsis--l2']}>
                        {item.summary}
                      </div>

                      <div class={styles.time}>
                        {item.createTime
                          ? dayjs(item.createTime).format('YYYY年MM月DD日')
                          : ''}
                      </div>
                    </>
                  )
                }}
              </Cell>
            ))}
          </List>
        ) : (
          <OEmpty description="暂无数据" />
        )}
      </div>
    );
  }
});
