import Taro, { useRouter } from '@tarojs/taro'
import { React, useState, useEffect, useRef } from 'react'
import iconsearch from '@/assets/icons/housemantj/search.png'
import location from '@/assets/icons/housemantj/location.png'
import Tip from '@/components/tip'
import { getIndexType,  getResourceList } from '@/services/home'
import Card from '../components/Card'
import { getLocation } from '@/utils/location'

import './less/Recommend.less'


export default (props) => {
  const { router, person } = props
  const [activeTab, setActiveTab] = useState(0)
  const { params } = useRouter()
  const { tabJump } = params || {}
  const [typeList, setTypeList] = useState([])
  
  const locRef = useRef()

  const [queryParams, setQueryParams] = useState({ location: '', pageNum: 1, pageSize: 10, typeId: '' })

  // 获取资源表信息
  const [alllist, setAllList] = useState([])
  //分类标签
  const tabs = [{ title: '附近' }].concat(typeList.map(x => ({ ...x, title: x.typeName })))
  //切换上面的标签
  const handleTabChange = (e) => {
    const { index } = e.detail
    setActiveTab(index)
    const tab = tabs[index].typeId
    setQueryParams({
      ...queryParams,
      typeId: tab
    })
    if (index == 0) {
      setQueryParams({ location:  locRef.current, pageNum: 1, pageSize: 10, typeId: '' })
    }
  }

  useEffect(() => {
    getLocation().then((res) => {
      locRef.current = `${res.longitude},${res.latitude}`
      setQueryParams({
        ...queryParams,
        location: locRef.current
      })
    })

    //查询分类标签表
    getIndexType({ pageSize: 20 }).then((res) => {
      setTypeList(res.records || [])
    })
    
  }, [])

  useEffect(() => {
    if (tabJump) {
      // 更新 typeId
      // 更新 分页
    }
    setActiveTab(tabJump - 0)
  }, [tabJump])


  useEffect(() => {
    if (locRef.current) {
      getResourceList(queryParams).then((res) => {
        setAllList(res.records || [])
      })
    }
  }, [queryParams])

  const onSearch = () => {
    // 用绝对路径
    Taro.navigateTo({ url: '/pages/search/search' });
  }


  const RefreshCard=()=>{
    console.log(1111)
  }

  
  return (
    <view style={{ height: '100%', overflow: 'auto' }}>
      <view className='search' onClick={onSearch}>
        <input className='searchInput' disabled />
        <image className='searchicon' src={iconsearch} />
        <view className='searchword'>搜索</view>
      </view>
      <Tip />
      <view className='index-tabs'>
        <view className='position'>
          <image className='location' src={location} />
          <view className='city'>南京</view>
          <view className='line' />
        </view>
        <scroll-view scroll-x='true' scroll-view='true' bindscrolltoupper='upper' bindscrolltolower='lower' bindscroll='scroll'>
          <mp-tabs
            tabClass='tabs-Unselected'
            swiperClass='tabs-swiper'
            activeClass='tabs-Selected'
            tabs={tabs}
            current={activeTab}
            onChange={handleTabChange}
            activeTab={activeTab}
          >
          </mp-tabs>
        </scroll-view>
      </view>
      <scroll-view scroll-y='true' scroll-view='true'  	bindscrolltolower='lower' bindscrolltoupper='upper'	 bindscroll='scroll'
      
      >
        <view className='waterfall'>
          {
            alllist.map((item) => <Card key={item.resourceNo} item={item} />)
          }
        </view>
        <view className='botton'>已经到底了~</view>
      </scroll-view>
    </view>
  )
}
