import { useRef,useState,useMemo, useEffect } from 'react';

/**
 * 管理表格数据的查询搜索及分页
 */
const useSearch=(props)=>{
  const [search,setSearch] = useState(props.initSearch||{});
  const [service] = useState(()=>props.service);
  const [beforeSearch] = useState(()=>props.beforeSearch);
  const [beforeService] = useState(()=>props.beforeService);
  
  const [pagination,setPagination] = useState(()=>{
    let temp = {current:1,pageSize:10,total:0,startIdx:0,...props.pagination};
    temp.startIdx = (temp.current-1)*temp.pageSize;
    return temp;
  });
  const [updateData] = useState(()=>props.updateData);
  const [orderInfo,setOrderInfo]=useState({});
  
  const onPaginationChange=async (pageInfo,sorterInfo)=>{
    let {current,pageSize} = pagination;
    let orderValue=orderInfo;
    if(pageInfo){
      current = pageInfo.current;
      pageSize = pageInfo.pageSize;
    }

    if(sorterInfo){
      const {field,order}= sorterInfo;
      if(field){
        if(order){
          orderInfo[field] = order;
        }else{
          delete orderInfo[field];
        }
      }
      orderValue = orderInfo;
      setOrderInfo({...orderInfo});
    }
    

    let searchValue = await getSearchValue();
    refresh({
      searchValue,
      pageValue:{pageSize,current},
      orderValue});
  }

  const onSorter=async (sorter)=>{
    const {field,order}= sorter;
    if(field){
      if(order){
        orderInfo[field] = order;
      }else{
        delete orderInfo[field];
      }
    }
    setOrderInfo({...orderInfo});
    console.log(`${JSON.stringify(orderInfo)}`);
    refresh();
    
    
    
  }

  const getSearchValue=async ()=>{
    let value={};
    value = search;

    if(beforeSearch){
      value = beforeSearch(value);
    }
    return value;

  }
  const btnSearch=async ()=>{
    let value = await getSearchValue();
    if(value){
      refresh({searchValue:value});
    }
  }
  const btnReset=()=>{
    formRef.resetFields();
  }

  const refresh= async ({searchValue,pageValue,orderValue}={})=>{
    searchValue = searchValue || search;
    pageValue = pageValue || pagination;

    // 排序暂无
    // orderValue = orderValue || orderInfo;
    // let order=[];
    // if(orderValue){
    //   Object.keys(orderValue).map(name=>{
    //     if(orderValue[name]==='ascend'){
    //       order.push(`${name}`);
    //     }else if(orderValue[name]==='descend'){
    //       order.push(`-${name}`);
    //     }
    //   })
    // }

    let {current,pageSize} = pageValue;
    let params = {...searchValue,page:current,pageSize:pageSize};
    if(beforeService)params=beforeService(params);
    if(!params)return;
    let ret = await service(params);
    if(ret?.code==0){
      const {page} = ret.data;
      setPagination({
        current:page.pageNum||1,
        pageSize:page.pageSize||10,
        total:page.total,
        startIdx:(page.page-1)*page.pageSize
      })
    }

    if(updateData)updateData(ret);
    return ret;
  }


  return {
    onPaginationChange,
    pagination,
    btnSearch,
    search,
    setSearch,
    refresh,
    btnReset,
    onSorter
    
  };

}

export default useSearch;
