import { useState, useEffect } from 'react'
// import Taro from '@tarojs/taro'
import { ScrollView } from '@tarojs/components'
import { fetch } from '@/utils/request'
import { API_VISIT_RECORD } from '@/constants/api'
import './index.scss'

export default function UserDetailViewRecord (props) {
  const { CustomerId = null, AddFollow = () => {} } = props
  const [PageList, setPageList] = useState([])
  const [IsPull, setPull] = useState(false)
  const [pageNumber, setPageNumber] = useState(1)
  const [HasNextPage, setHasNextPage] = useState(true)

  useEffect(() => {
    GetPageList()
  }, [pageNumber])

  const GetPageList = () => {
    setHasNextPage(false)
    fetch({ url: API_VISIT_RECORD, method: 'get', payload: { pageNumber, pageSize: 10, customerId: CustomerId } }).then((res) => {
      setPageList(pageNumber === 1 ? res.records || [] : PageList.concat(res.records || []))
      setHasNextPage(res.current < res.pages)
      setPull(false)
    })
  }

  const PageRefresh = () => { // 页面下拉刷新回调
    setPull(true)
  }

  const PageLoadMore = () => { // 页面上拉加载更多
    if(HasNextPage) {
      setPageNumber(pageNumber + 1)
    }
  }

  useEffect(() => { // 下拉刷新触发
    if (IsPull) {
      if (pageNumber === 1) {
        GetPageList()
      } else {
        setPageNumber(1)
      }
    }
  }, [IsPull])

  return (
    <view className='components UserDetailViewRecord'>
      <ScrollView scroll-y refresher-enabled refresher-triggered={IsPull} onrefresherrefresh={PageRefresh} onscrolltolower={PageLoadMore} refresher-background='#fff'>
        <view className='PageContent'>

          <view className='Content'>
            {
              !PageList.length &&
              <text>暂无数据</text>
            }
            {
              PageList.map((item, index) => (
                <view className='flex-h' key={`ListItem-${index}`}>
                  <text>{item.activity}</text>
                  {
                    (item.activityName || item.buildingName) &&
                    <text>：</text>
                  }
                  <view className='flex-item'>
                    <text>{item.activityName || item.buildingName}</text>
                  </view>
                  <text>{item.visitTime}</text>
                </view>
              ))
            }
          </view>

          <view className='Btn'>
            <text className='active' onClick={AddFollow}>添加跟进</text>
          </view>

        </view>
      </ScrollView>
    </view>
  )
}
