import {View} from '@tarojs/components'
import Banner from '@/components/banner'
import Search from '@/components/search'
import SideBar from '@/components/sideBar'
import Product from '@/components/product'
import MoveBtn from '@/components/moveBtn'
import ShopCart from '@/components/shopCart'
import { goLink } from '@/common/common'
import styles  from './index.module.scss'
import { useEffect, useRef, useState } from 'react'
import Taro, { Events, useDidShow, usePullDownRefresh, useRouter } from '@tarojs/taro'
import useManualPullDownRefresh from '@/use/useManualPullDownRefresh'

import {GetProductKindListApi, GetProductListApi} from '@/api/material'
import useLogin from '@/use/useLogin'

export default () => {

  const {checkLogin} = useLogin()
  useDidShow(async () => {
    await checkLogin()
  })

  useEffect(() => {
    categoryList()
  }, [])

  //获取面料种类
  const [kindData, setKindData] = useState<any>({list:[], defaultId:0})
  const {fetchData} = GetProductKindListApi()
  const categoryList = async () => {
    const res = await fetchData()
    if(res.data?.list) {
      setKindData({...kindData, list:res.data.list, defaultId: res.data.list[0].id})
      setFiltrate({...filtrate, product_kind_id:res.data.list[0].id})
    }
    
  }

  //获取面料列表
  const [productData, setProductData] = useState({list:[], total:0})
  const [hasMore, setHasMore] = useState(true)
  const [filtrate, setFiltrate] = useState({product_kind_id:0, size: 5,page: 1})
  const pageNum = useRef({size:filtrate.size, page:filtrate.page})
  const {fetchData: productFetchData, state: productState} = GetProductListApi()
  //获取数据方法
  const getProductList = async () => {
    const {data,total} = await productFetchData(filtrate)
    setProductData({...productData,list:data.list,total})
    setRefresherTriggeredStatus(() => false)
  }
  //监听查询条件
  useEffect(() => {
    if(filtrate.product_kind_id)
      getProductList()
  }, [filtrate])

  //点击面料类型
  const getProductKindId = async (e) => {
    pageNum.current.page = 1
    setFiltrate({...filtrate, size:5, product_kind_id:e.id})
    setHasMore(true)
  }

  //上拉加载数据
  const getScrolltolower = () => {
    if(productData.list.length >= productData.total) {
      setHasMore(false)
    } else {
      pageNum.current.page++
      const newSize = pageNum.current.size * pageNum.current.page
      setFiltrate({...filtrate, size:newSize})
    }
  }


  const [showShopCart, setShowShopCart] = useState(false)
  
  //列表下拉刷新
  const [refresherTriggeredStatus, setRefresherTriggeredStatus] = useState(false)
  const getRefresherRefresh = async () => {
    pageNum.current.size = 1
    setFiltrate({...filtrate, size:5})
    setHasMore(true)
    setRefresherTriggeredStatus(true)
  }


  //页面下拉刷新
  // const res = useManualPullDownRefresh()
  usePullDownRefresh(() => {
    console.log('123')
  })
  
  return (
    <MoveBtn onClick={() => setShowShopCart(!showShopCart)}>
      <View className={styles.main}>
        <Banner/>
        <View className={styles.search}>
          <View className={styles.search_collect}>我的收藏</View>
          <View className={styles.search_input}>
            <Search disabled={true} style={{width: '263rpx'}} clickOnSearch={() => goLink('/pages/searchList/search')}/>
          </View>
        </View>
        <View className={styles.products}>
          <SideBar list={kindData.list} height="100%" defaultValue={kindData.defaultId} hasMore={hasMore} selfOnScrolltolower={() =>  getScrolltolower()} sideBarOnClick={(e) => getProductKindId(e)} heightItem={150} refresherTriggered={refresherTriggeredStatus} selfOnRefresherRefresh={() => getRefresherRefresh()}>
            <Product productList={productData.list}/>
          </SideBar>
        </View>
        <View className='common_safe_area_y'></View>
        <ShopCart show={showShopCart} onClose={() => setShowShopCart(false)}/>
      </View>
    </MoveBtn>
  )
}
