/** 格式化为 datetime-local 输入值 */
export function formatDateTimeLocal(date: Date): string {
  const pad = (n: number) => String(n).padStart(2, '0')
  return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}T${pad(date.getHours())}:${pad(date.getMinutes())}`
}

/** 默认范围：本月 1 日 00:00 ~ 今天 23:59 */
export function getDefaultMonthRange(): { start: string; end: string } {
  const now = new Date()
  const start = new Date(now.getFullYear(), now.getMonth(), 1, 0, 0, 0)
  const end = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 0)
  return {
    start: formatDateTimeLocal(start),
    end: formatDateTimeLocal(end)
  }
}

/** datetime-local -> API 查询参数 (YYYY-MM-DD HH:mm) */
export function toApiDateTime(value: string): string {
  if (!value) return ''
  return value.replace('T', ' ')
}
