import { ScrollView, View } from '@tarojs/components'
import Taro, { useDidShow } from '@tarojs/taro'
import { forwardRef, memo, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react'
import classnames from 'classnames'
import VirtualList from '@tarojs/components/virtual-list'
import Goods from '../goodsItem'
import styles from './index.module.scss'
import Divider from '@/components/divider'
import Search from '@/components/search'
import Popup from '@/components/popup'
import BottomCustomer from '@/components/BottomCustomer'
import DotLoading from '@/components/dotLoading'
import { formatDateTime, formatHashTag, formatPriceDiv, formatRemoveHashTag, formatWeightDiv } from '@/common/format'

interface prosObj {
  sale_mode?: number
  showPopup?: false | true
  closePopup?: () => void
  goodList?: any[]
  // typeList?: any[]
  handCheck?: (any) => void
  addNums?: (any) => void
  reduceNums?: (any) => void
  getSearchData: (any) => void
  handPlus: (any) => void
  handSure: () => void
  obj?: {
    code?: string
    name?: string

  }
  modeFont: Number | string
  hasBottom?: true | false // 默认不占位底部
  onBlur?: (e: string, res: object) => void
}
const ShoppingCart = (props: prosObj, ref) => {
  const {
    sale_mode = -1,
    showPopup = false,
    closePopup,
    goodList = [],
    handCheck,
    addNums,
    reduceNums,
    getSearchData,
    handPlus,
    obj = {},
    modeFont = 0,
    handSure,
    hasBottom = true,
    onBlur,
  } = props

  const SearchRef = useRef<any>()

  useImperativeHandle(ref, () => ({
    Object: props,
    SearchRef,
  }))

  // 判断选择的类型
  const showModefont = useMemo(() => {
    if (sale_mode == 0) {
      return '条'
    }
    else {
      return '米'
    }
  }, [sale_mode])

  // 已选的颜色
  const selectTotal = useMemo(() => {
    const totalArr = goodList.filter((item) => {
      return item.showInput
    })
    return totalArr.length
  }, [goodList])

  // 选择的条数或者米数
  const selectNums = useMemo(() => {
    const arr: any = []
    goodList.forEach((item) => {
      if (item.showInput) {
        arr.push(Number(item.nums))
      }
    })
    const hate = sum(arr)
    return hate
  }, [goodList])
  // onInputEven={(e, item) => oninputEvent?.(e, item)}

  function sum(arr) {
    let s = 0
    for (let i = arr.length - 1; i >= 0; i--) {
      s += arr[i]
    }
    return s
  }

  // 确认添加
  const handTake = () => {
    if (selectTotal == 0) {
      return Taro.showToast({
        title: '请选择一条添加！',
        icon: 'error',
      })
    }
    else {
      handSure()
    }
  }

  const rows = ({ id, index, style, data }: any) => {
    const item = data[index]
    return (
      <>
        <Goods
          onBlur={(e, id) => onBlur?.(e, id)}
          modeFont={(sale_mode as number)}
          handPlus={item => handPlus?.(item)}
          value={item || {}}
          clickAdd={item => addNums?.(item)}
          clickReduce={(item) => { reduceNums?.(item) }}
          goodList={goodList}
        ></Goods>
      </>
    )
  }
  const Rows = memo(rows)

  return (
    <Popup showTitle={false} show={showPopup} onClose={() => closePopup?.()}>
      <ScrollView className={styles.scrllStyle} scrollY>
        <View className={styles.shopcartBox}>
          <View className={styles.topTitle}>
            {formatHashTag(obj?.code, obj?.name)}
          </View>
          {/* <Divider direction="horizontal" customStyles={{ margin: '12px 0' }}></Divider> */}
          {/* <View className={styles.typeFont}>布料类型</View>
          <View className={styles.flexType}>
            {typeList.map((item, index) => {
              return (
                <View
                  onClick={() => {
                    handCheck?.(item)
                  }}
                  key={index}
                  className={classnames(item.checked ? styles.activemodeFont : styles.modeFont)}
                >
                  {item.name}
                </View>
              )
            })}
          </View> */}
          <View style={{ position: 'sticky', top: 0, background: 'white', zIndex: '9999' }}>
            <Divider direction="horizontal" customStyles={{ margin: '12px 0' }}></Divider>
            <View className={styles.flexFonts}>
              <View className={styles.kingFont}>颜色分类({goodList?.length})</View>
              <View className={styles.danwei}>
                {sale_mode == 0 ? '大货' : sale_mode == 1 ? '剪版' : '散剪'}单位：{showModefont}
              </View>
            </View>
            <View className={styles.searchBox}>
              <Search
                ref={SearchRef}
                cursorSpacing={100}
                placeholder="请输入搜索布料"
                showBtn={false}
                changeOnSearch={getSearchData}
                adjustPosition
              />
            </View>
          </View>
          {goodList.length > 0 && (
            <>
              <View className={styles.scrollviewBig}>
                <VirtualList
                  className={styles.scrollview}
                  height={350} /* 列表的高度 */
                  width="100%" /* 列表的宽度 */
                  itemData={goodList} /* 渲染列表的数据 */
                  itemCount={goodList.length + 0} /*  渲染列表的长度 */
                  itemSize={100} /* 列表单项的高度  */
                  overscanCount={1}
                >
                  {Rows}
                </VirtualList>
              </View>

              {/* <View className={styles.kongOne}></View> */}
            </>
          )}
          {goodList.length === 0 && (
            <>
              <View className={styles.loading_more}>
                加载中
                <DotLoading />
              </View>
            </>
          )}
          {/* {!hasBottom && <View style={{ height: '100rpx' }}></View>} */}
          {/* <View className={classnames(hasBottom === true ? styles.posBox : styles.noBottom)}> */}
          <View className={styles.noBottom}>
            <View className={styles.flexBox}>
              <View className={styles.selectFont}>
            当前已选{selectTotal} 种颜色，共 {selectNums} {showModefont}
              </View>
              <View className={styles.rightBtn} onClick={() => handTake()}>确认添加</View>
            </View>
          </View>
        </View>
      </ScrollView>
    </Popup>
  )
}
export default memo(forwardRef(ShoppingCart))
