import Popup from '@/components/popup'
import { useState, memo } from 'react'
import { View } from '@tarojs/components'
import './index.scss'
import AtCalendar from '@/components/calendar/index'
import dayjs from 'dayjs'

type DateArg = string | number | Date
interface Props {
  end?: DateArg
  start?: DateArg
  onSelectDate?: (any) => void
}

export default memo((props: Props) => {
  let { start = '', end = '', onSelectDate } = props
  const [time, setTime] = useState<any>({})

  const handTime = (e) => {
    const { start, end } = e.value
    // 如果选的是同一天的日期, end 自动加一天
    if (!end) {
      //判断如果没选下一天的时候
      e.value.end = `${dayjs(new Date(start)).add(1, 'day').format('YYYY-MM-DD')} 00:00:00`
    }
    if (start === end) {
      e.value.end = `${dayjs(new Date(start)).add(1, 'day').format('YYYY-MM-DD')} 00:00:00`
    }
    setTime(e)
  }

  
  return (
    <>
      <View className='time-box'>
        <AtCalendar
          isMultiSelect
          format='YYYY-MM-DD 00:00:00'
          currentDate={{
            start: start,
            end: end,
          }}
          onSelectDate={(e) => handTime?.(e)}
        />
      </View>
      <View className='sure-box' onClick={() => onSelectDate?.(time)}>
        确认
      </View>
    </>
  )
})
