import { View } from '@tarojs/components'
import { memo, useCallback, useMemo, useRef } from 'react'
import Big from 'big.js'
import type { NumberParam } from '../settingNumber'
import SettingNumber from '../settingNumber'
import useCommon from '../../useHook/useCommon'
import styles from './index.module.scss'
import ProductBlock from './productBlock'
import { useCurrenCode } from '@/context/ContextCodeSetting'

interface Parma {
  orderData: any
  onUpdateData?: (val: any) => void
}
export default memo((props: Parma) => {
  const { dispatch, productData } = useCurrenCode()
  const { orderData = {}, onUpdateData } = props
  const productList = useMemo(() => {
    console.log('productData123:::', orderData)
    if (!orderData?.product_details) { return [] }
    return orderData?.product_details
  }, [orderData])

  const getNumber: NumberParam = useCallback((num, type) => {
    // 更新整体数据
    const updateData = {
      price: orderData?.price_admin,
      weight: orderData?.weight_admin,
      weight_error: orderData?.weight_error_admin,
    }
    if (type === 'weight') {
      updateData.weight = num * 1000
    }
    else if (type === 'sale_price') {
      updateData.price = num * 100
    }
    else {
      updateData.weight_error = num * 1000
    }

    console.log(`aa${type}`, num)

    // 需要传给后端
    orderData.weight_admin = updateData.weight
    orderData.price_admin = updateData.price
    orderData.weight_error_admin = updateData.weight_error

    console.log('orderData123::', updateData.price)

    // 更新原始数组
    productList?.map((item) => {
      item?.product_color_details?.map((citem) => {
        if (citem.sale_mode === 0) {
          citem.weight_setting = Big(updateData.weight).times(citem.roll).add(citem.weight).toNumber()
          citem.weight_error_setting = Big(updateData.weight_error).times(citem.roll).add(citem.weight_error).toNumber()
        }
        else {
          citem.weight_setting = Big(updateData.weight).add(citem.weight).toNumber()
          citem.weight_error_setting = Big(updateData.weight_error).add(citem.weight_error).toNumber()
        }
        citem.price_setting = Big(updateData.price).add(citem.price).toNumber()
      })
    })
    orderData.product_details = productList
    console.log('orderData::', orderData)
    dispatch({ type: 'updateData', data: JSON.parse(JSON.stringify(orderData)), update_status: 1 })
  }, [productList])

  const defaultNum = useMemo(() => {
    console.log('orderData::默认', productData)
    return {
      weight: Big(orderData?.weight_admin || 0).div(Big(1000)).toNumber(),
      sale_price: Big(orderData?.price_admin || 0).div(Big(100)).toNumber(),
      weight_error: Big(orderData?.weight_error_admin || 0).div(Big(1000)).toNumber(),
    }
  }, [orderData])

  return <>
    <SettingNumber sale_mode={orderData?.sale_mode} onNumber={getNumber} defaultValue={defaultNum} />
    <View className={styles.product_list}>
      {productList?.map((item) => {
        return <ProductBlock key={item.id} productInfo={item} sale_mode_name={orderData?.sale_mode_name} />
      })}
    </View>
  </>
})
