import { Input, View } from '@tarojs/components'
import { useEffect, useMemo, useRef, useState } from 'react'
import Big from 'big.js'
import styles from './index.module.scss'

interface params {
  minNum?: number // 最小值
  maxNum?: number // 最大值
  step?: number // 步长
  defaultNum?: number // 默认值
  digits?: number // 多少位小数
  onChange?: (val: number, obj?: any) => void
  onBlue?: (val: number, obj?: any) => void // 失去焦点触发
  onClickBtn?: (val: number, obj?: any) => void
  unit?: string
  otherData?: any
}
const Counter = ({ minNum = 0, maxNum = 100, step = 1, digits = 0, defaultNum = 0, onChange, onBlue, onClickBtn, unit = '', otherData }: params) => {
  const [value, setValue] = useState<any>({ count: defaultNum })
  // 保留小数
  const formatDigits = (num) => {
    num = `${num}`
    if (num.includes('.') && digits > 0) {
      console.log('num::', num.includes('.'))
      const res = num.split('.')
      const last_num = res[1].substr(0, digits)
      return `${res[0]}.${last_num}`
    }
    return parseFloat(num)
  }
  const onPlus = () => {
    const { count } = value
    let num_res = Big(count).add(step).toNumber()
    num_res = num_res >= maxNum ? maxNum : num_res
    num_res = formatDigits(num_res)
    setValue({ ...value, count: num_res })
    onChange?.(parseFloat(num_res), otherData)
    onClickBtn?.(parseFloat(num_res), otherData)
  }
  const minus = () => {
    const { count } = value
    let num_res = Big(count).minus(step).toNumber()
    num_res = num_res < minNum ? 0 : num_res
    setValue({ ...value, count: num_res })
    onChange?.(parseFloat(num_res), otherData)
    onClickBtn?.(parseFloat(num_res), otherData)
  }

  // 检查数据
  const checkData = (val) => {
    const num = parseFloat(val)
    if (num > maxNum) { return maxNum }
    if (num < minNum) { return minNum }
    return val
  }

  const onInputEven = (e) => {
    const res = e.detail.value
    if (res === '') {
      setValue({ ...value, count: minNum })
      onChange?.(minNum, otherData)
    }
    else if (!Number.isNaN(Number(res))) {
      let count = formatDigits(res)
      count = checkData(count)
      setValue({ ...value, count })
      onChange?.(parseFloat(count as string), otherData)
    }
    else {
      const num = parseFloat(res)
      if (!Number.isNaN(num)) {
        let count = formatDigits(num)
        count = checkData(count)
        setValue({ ...value, count })
        onChange?.(count as number, otherData)
      }
      else {
        setValue({ ...value, count: defaultNum })
        onChange?.(defaultNum, otherData)
      }
    }
  }

  const onBluerEven = () => {
    const num = parseFloat(value.count)
    if (!Number.isNaN(num)) {
      let count = formatDigits(num)
      count = checkData(count)
      setValue({ ...value, count })
      onBlue?.(count as number, otherData)
    }
    else {
      setValue({ ...value, count: defaultNum })
      onBlue?.(defaultNum, otherData)
    }
  }
  return (
    <View className={styles.main}>
      <View className={styles.reduce} onClick={() => minus()}>
        -
      </View>
      <View className={styles.input}>
        <Input value={String(value.count)} onInput={onInputEven} onBlur={onBluerEven} type="digit" alwaysEmbed cursorSpacing={150} />
        <View className={styles.unit}>{unit}</View>
      </View>
      <View className={styles.plus} onClick={() => onPlus()}>
        +
      </View>
    </View>
  )
}

export default Counter
