import {Cell, Space} from '@nutui/nutui-react-taro'
import {useEffect, useState, CSSProperties} from "react";
import {InfiniteLoading} from '@nutui/nutui-react-taro'
import {useRouter} from '@tarojs/taro'
import {pageHjmGpsLog} from "@/api/hjm/hjmGpsLog";
import {HjmGpsLog} from "@/api/hjm/hjmGpsLog/model";

const InfiniteUlStyle: CSSProperties = {
  height: '80vh',
  width: '100%',
  padding: '0',
  overflowY: 'auto',
  overflowX: 'hidden',
}

function PayRecord() {
  const {params} = useRouter();
  const [list, setList] = useState<HjmGpsLog[]>([])
  const [page, setPage] = useState(1)
  const [hasMore, setHasMore] = useState(true)
  const reload = async () => {
    pageHjmGpsLog({page, gpsNo: params.id}).then(res => {
      if (res?.list && res?.list.length > 0) {
        const newList = list?.concat(res.list)
        setList(newList);
        setHasMore(true)
      } else {
        setHasMore(false)
      }
    })
  }

  const reloadMore = async () => {
    setPage(page + 1)
    reload();
  }

  useEffect(() => {
    setPage(2)
    reload()
  }, [])

  return (
      <Cell>
        <ul style={InfiniteUlStyle} id="scroll">
          <InfiniteLoading
            target="scroll"
            hasMore={hasMore}
            onLoadMore={reloadMore}
            onScroll={() => {
              console.log('onScroll')
            }}
            onScrollToUpper={() => {
              console.log('onScrollToUpper')
            }}
            loadingText={
              <>
                加载中
              </>
            }
            loadMoreText={
              <>
                没有更多了
              </>
            }
          >
            {list?.map(item => {
              return (
                <Cell style={{padding: '0'}}>
                  <div className={'flex w-full justify-between items-center'}>
                    <div className={'flex'}>
                      <Space>
                        <div className={'flex flex-col'}>
                          <div className={'real-name py-1'}>
                            时间：{item.createTime}
                          </div>
                          <div className={'text-gray-400 py-1'}>速度：{item.speed} km/h，经纬度：{item.latitude},{item.longitude}</div>
                        </div>
                      </Space>
                    </div>
                  </div>
                </Cell>
              )
            })}
          </InfiniteLoading>
        </ul>
      </Cell>
  )
}

export default PayRecord
