import dayjs from 'dayjs'
import { P_GROUP_GROUP, P_GROUP_PERSONAL, P_TYPE_DAILY, P_TYPE_TOTAL } from '../column'
import { useTable } from './table'
import { useAutoUpdate } from './auto-update'

export function useStudyData(init: any) {
  const date = ref(dayjs().format('YYYY-MM-DD'))

  const groupList = [
    {
      value: P_GROUP_PERSONAL,
      label: '个人',
    },
    {
      value: P_GROUP_GROUP,
      label: '小组',
    },
  ]

  const activeGroup = ref(P_GROUP_PERSONAL)
  const typeList = [
    {
      value: P_TYPE_DAILY,
      label: '每日数据',
    },
    {
      value: P_TYPE_TOTAL,
      label: '总计',
    },
  ]
  const activeType = ref(P_TYPE_TOTAL)
  const loading = ref(false)

  watch([() => activeType.value, () => activeGroup.value, () => date.value], () => init())

  onMounted(async () => {
    init()
  })

  return {
    ...useTable(),
    ...useAutoUpdate(activeType, date, init),
    loading,
    date,
    groupList,
    activeGroup,
    typeList,
    activeType,
  }
}
