import { View } from '@tarojs/components'
import { forwardRef, memo, useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react'
import style from './index.module.scss'

interface prosObj {
  obj?: {
    sale_mode?: Number
    collect_status?: Number | string
    status?: Number
    payment_method?: Number
    settle_mode?: Number | string
  }
  cancle?: (any) => void
  nextBuy?: (any) => void
  toPay?: (any) => void
  handSureGoods?: () => void
  showTuiGoods?: boolean
  handApplyGoods?: () => void
  handApplyMoney?: () => void
  handShowCodeList?: () => void
  showDetail?: boolean
  goodsObj?: any
}

const BottomBtns = (props: prosObj, ref) => {
  const {
    obj = {
      sale_mode: 0,
      collect_status: '',
      status: '',
      payment_method: 0,
      settle_mode: '',
    },
    showTuiGoods = false,
    cancle,
    nextBuy,
    toPay,
    handSureGoods,
    handApplyGoods,
    handApplyMoney,
    handShowCodeList,
    showDetail = false,
    goodsObj = {},
  } = props
  // 判断显示取消订单

  const showCancel = useMemo(() => {
    if (
      (obj.sale_mode === 0 && obj.status === 0)
            || (obj.sale_mode === 0 && obj.status === 1)
            || (obj.sale_mode === 0 && obj.status === 2 && obj.collect_status == 0)
            || (obj.sale_mode === 0 && obj.status === 7 && obj.collect_status == 0)
            || (obj.sale_mode === 0 && obj.status === 11 && obj.collect_status == 0)
            || (obj.sale_mode === 0 && obj.status === 11 && obj.settle_mode == 3)
            || (obj.sale_mode === 0 && obj.status === 3 && obj.settle_mode == 3)
            || (obj.sale_mode === 1 && obj.status === 10)
            || (obj.sale_mode === 2 && obj.status === 10)
            || (obj.sale_mode === 2 && obj.status === 0 && (obj.collect_status == 1 || obj.collect_status == 2))
            || (obj.sale_mode === 2 && obj.status === 0 && obj.collect_status == 0)
            || (obj.sale_mode === 1 && obj.status === 10)
            || (obj.sale_mode === 2 && obj.status === 10)
    ) {
      return true
    }
    else { return false }
  }, [obj])
  // 判断显示再次购买
  const showBuy = useMemo(() => {
    if (
      obj.sale_mode === 0
            || (obj.sale_mode === 1 && obj.status !== 10)
            || (obj.sale_mode === 2 && obj.status !== 10)
    ) {
      return true
    }
    else { return false }
  }, [obj])
  // 判断显示去付款
  const canBuy = useMemo(() => {
    if (
      (obj.sale_mode === 0 && obj.status === 7 && obj.collect_status == 1)
            || (obj.sale_mode === 0 && obj.status === 7 && obj.collect_status == 0)
            || (obj.sale_mode === 0 && obj.status === 7 && obj.settle_mode == 3)
            || (obj.sale_mode === 0 && obj.status === 3 && obj.collect_status == 1)
            || (obj.sale_mode === 0 && obj.status === 3 && obj.collect_status == 0)
            || (obj.sale_mode === 0 && obj.status === 8 && obj.collect_status == 1)
            || (obj.sale_mode === 0 && obj.status === 8 && obj.collect_status == 0)
            || (obj.sale_mode === 0 && obj.status === 9 && obj.collect_status == 1)
            || (obj.sale_mode === 0 && obj.status === 9 && obj.collect_status == 0)
            || (obj.sale_mode === 2 && obj.status === 2)
            || (obj.sale_mode === 2 && obj.status === 7)
            || (obj.sale_mode === 1 && obj.status === 10)
            || (obj.sale_mode === 2 && obj.status === 10)
    ) {
      return true
    }
    else { return false }
  }, [obj])

  // 判断显示确认收货
  const showTake = useMemo(() => {
    if (
      (obj.sale_mode === 0 && obj.status == 8 && obj.collect_status == 0)
            || (obj.sale_mode === 0 && obj.status == 8 && obj.collect_status == 1)
            || (obj.sale_mode === 0 && obj.status == 8 && obj.collect_status == 2)
            || (obj.sale_mode === 0 && obj.status == 8 && obj.settle_mode == 3)
            || (obj.sale_mode === 1 && obj.status == 8)
            || (obj.sale_mode === 2 && obj.status == 8)
    ) {
      return true
    }
    else { return false }
  }, [obj])

  // 判断显示申请退货
  const showTuihuo = useMemo(() => {
    if (
      (obj.sale_mode === 0 && obj.status == 9 && obj.collect_status == 0 && goodsObj?.av_return_product?.length)
            || (obj.sale_mode === 0 && obj.status == 9 && obj.collect_status == 1 && goodsObj?.av_return_product?.length)
            || (obj.sale_mode === 0 && obj.status == 9 && obj.collect_status == 2 && goodsObj?.av_return_product?.length)
            || (obj.sale_mode === 0 && obj.status == 9 && obj.settle_mode == 3 && goodsObj?.av_return_product?.length)
            || (obj.sale_mode === 2 && obj.status == 9 && goodsObj?.av_return_product?.length)
    ) {
      return true
    }
    else { return false }
  }, [obj])
  // 判断显示申请退款
  const showTuikuan = useMemo(() => {
    if (
      (obj.sale_mode === 0 && obj.status == 2 && obj.collect_status == 1 && goodsObj?.av_return_product?.length)
            || (obj.sale_mode === 0 && obj.status == 7 && obj.collect_status == 2 && goodsObj?.av_return_product?.length)
            || (obj.sale_mode === 0 && obj.status == 7 && obj.collect_status == 3 && goodsObj?.av_return_product?.length)
            || (obj.sale_mode === 0 && obj.status == 11 && (obj.collect_status == 1 || obj.collect_status == 2) && goodsObj?.av_return_product?.length)
            || (obj.sale_mode === 0 && obj.status == 3 && obj.collect_status == 0 && goodsObj?.av_return_product?.length)
            || (obj.sale_mode === 0 && obj.status == 3 && obj.collect_status == 2 && goodsObj?.av_return_product?.length)
            || (obj.sale_mode === 0 && obj.status == 3 && obj.collect_status == 1 && goodsObj?.av_return_product?.length)
            || (obj.sale_mode === 1 && obj.status == 0 && goodsObj?.av_return_product?.length)
            || (obj.sale_mode === 1 && obj.status == 9 && goodsObj?.av_return_product?.length)
            || (obj.sale_mode === 2 && obj.status == 0 && obj.collect_status == 1 && goodsObj?.av_return_product?.length)
            || (obj.sale_mode === 2 && obj.status == 0 && obj.collect_status == 2 && goodsObj?.av_return_product?.length)
    ) {
      return true
    }
    else { return false }
  }, [obj])

  // 判断是否显示码单
  // 订单状态：待付款、待发货、待提货、待收货、已收货、已完成
  const showCodeList = useMemo(() => {
    if (obj.status == 3 || obj.status == 8 || obj.status == 11 || obj.status == 9 || obj.status == 4) {
      return true
    }
    else { return false }
  }, [obj])

  // 多的不显示别的按钮
  // const currentNums = useRef<any>({ nums: 0 })
  const itemNums = useMemo(() => {
    let nums = 0
    if (showTake) {
      nums = nums + 1
    }
    if (showCancel) {
      nums = nums + 1
    }
    if (canBuy) {
      nums = nums + 1
    }
    if (showBuy) {
      nums = nums + 1
    }

    return nums
  }, [obj])

  useImperativeHandle(ref, () => ({
    itemNums,
  }))

  return (
    <View className={style.flexBox}>

      {
        (showTuihuo && showDetail && itemNums < 4) && <View className={style.nextBuy} onClick={() => handApplyGoods?.()}>申请退货</View>
      }

      {
        (showTuikuan && showDetail && itemNums < 4) && <View className={style.nextBuy} onClick={() => handApplyMoney?.()}>申请退款</View>

      }
      {
        (showCodeList && showDetail) && <View className={style.cancle} onClick={() => handShowCodeList?.()}>销售码单</View>
      }
      {
        showTake && showTuiGoods && <View className={style.cancle} onClick={() => handSureGoods?.()}>确认收货</View>
      }
      {
        showCancel && <View className={style.cancle} onClick={e => cancle?.(e)}>取消订单</View>
      }
      {
        showBuy && <View className={style.nextBuy} onClick={e => nextBuy?.(e)}>再次购买</View>
      }
      {
        canBuy && <View className={style.toBuy} onClick={e => toPay?.(e)}>去付款</View>
      }
    </View>
  )
}
export default memo(forwardRef(BottomBtns))
