import OEmpty from '@/components/o-empty'
import OPopup from '@/components/o-popup'
import request from '@/helpers/request'
import { router } from '@/router/routes-common'
import dayjs from 'dayjs'
import { Button, Cell, Image, List } from 'vant'
import { defineComponent, onMounted, reactive } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import AddInformation from './modal/add-information'
import styles from './orchestra-information.module.less'

export default defineComponent({
  name: 'orchestra-information',
  setup() {
    const route = useRoute()
    const router = useRouter()
    const state = reactive({
      addStatus: false,
      isLoading: false,
      list: [] as any,
      listState: {
        dataShow: true, // 判断是否有数据
        loading: false,
        finished: false
      },
      params: {
        type: 'HOT_CONSULTATION',
        clientType: 'SCHOOL',
        page: 1,
        rows: 20
      }
    })

    const getList = async () => {
      try {
        if (state.isLoading) return
        state.isLoading = true
        const res = await request.post('/api-school/sysNewsInformation/page', {
          data: {
            ...state.params,
            orchestraPhotoAlbumId: route.query.photoId
          }
        })
        state.listState.loading = false
        const result = res.data || {}
        // 处理重复请求数据
        if (state.list.length > 0 && result.current === 1) {
          return
        }
        const rows = result.rows || []
        state.list = state.list.concat(rows)
        state.listState.finished = result.current >= result.pages
        state.params.page = result.current + 1
        state.listState.dataShow = state.list.length > 0
        state.isLoading = false
      } catch {
        state.listState.dataShow = false
        state.listState.finished = true
        state.isLoading = false
      }
    }

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

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

    onMounted(() => {
      getList()
    })
    return () => (
      <div class={styles.information}>
        <Button icon="plus" block class={styles.addPhone} onClick={() => (state.addStatus = true)}>
          添加资讯
        </Button>

        {state.listState.dataShow ? (
          <List
            v-model:loading={state.listState.loading}
            finished={state.listState.finished}
            finishedText=" "
            onLoad={getList}
            immediateCheck={false}
          >
            {state.list.map((item: any, index: number) => (
              <Cell center class={styles.cell} onClick={() => onDetail(item)}>
                {{
                  icon: () => <Image src={item.coverImage} class={styles.img} />,
                  title: () => (
                    <div>
                      <div class={[styles.title, 'van-ellipsis']}>{item.title}</div>
                      <div class={[styles.content, 'van-multi-ellipsis--l2']}>{item.memo}</div>
                      <div class={styles.time}>
                        {dayjs(item.createTime).format('YYYY年MM月DD日')}
                      </div>
                    </div>
                  )
                }}
              </Cell>
            ))}
          </List>
        ) : (
          <OEmpty btnStatus={false} classImgSize="SMALL" tips="暂无资讯" />
        )}

        <OPopup v-model:modelValue={state.addStatus} style={{ background: '#f8f8f8' }}>
          <AddInformation onClose={() => (state.addStatus = false)} onGetList={onSearch} />
        </OPopup>
      </div>
    )
  }
})
