import { useState, useEffect, useRef } from 'react'
import { getChannelLineChat } from '@/services/agent'
import { fillTime, cutDays, cutMonths, getList } from '../utils'

const now = new Date()

export default function useReport() {
  const [statType, setStatType] = useState('day')
  const [custType, setCustType] = useState('new')
  const [chartData, setChartData] = useState([])
  const xAxisDataRef = useRef()

  useEffect(() => {
    // 按月默认统计半年的, 按天默认统计7天的
    const num = statType === 'month' ? 6 : 7
    const startDate = statType === 'month' ? cutMonths(now, num) : cutDays(now, num)
    xAxisDataRef.current = getList(startDate, num, statType)

    getChannelLineChat({
      type: statType,
      customerType: custType,
      startDate: fillTime(startDate.toJSON().substring(0, 10), true),
      endDate: fillTime(now.toJSON().substring(0, 10), false),
    }).then((res) => {
      setChartData(res || [])
    })
  }, [statType, custType])

  return [statType, setStatType, custType, setCustType, chartData, xAxisDataRef]
}
