import { View } from '@tarojs/components'
import { memo, useEffect, useMemo, useState } from 'react'
import classnames from 'classnames'
import styles from './index.module.scss'

interface prosObj {
  obj?: {
    sale_mode?: Number
    collect_status?: Number | string
    status?: Number
    payment_method?: Number
    settle_mode?: Number | string
    is_shipper_picks_up?: boolean
  }
  handApplyGoods?: () => void
  handApplyMoney?: () => void
  nextBuy?: (any) => void
  handSureGoods?: (any) => void
  handShowCodeList?: (any) => void
  cancle?: (any) => void
  handEdit?: () => void
  goodsObj?: any
  // handSureGoods?: () => void,
}
const BottomApply = (props: prosObj) => {
  const {
    obj = {
      sale_mode: 0,
      collect_status: '',
      status: '',
      payment_method: 0,
      settle_mode: '',
      is_shipper_picks_up: false,
    },
    handApplyGoods,
    handApplyMoney,
    handSureGoods,
    cancle,
    handShowCodeList,
    handEdit,
    goodsObj = {},
    nextBuy,
    // handSureGoods
  } = props

  // 判断显示申请退货
  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.is_shipper_picks_up)
            || (obj.sale_mode === 0 && obj.status == 3 && obj.collect_status == 2 && goodsObj?.av_return_product?.length && !obj.is_shipper_picks_up)
            || (obj.sale_mode === 0 && obj.status == 3 && obj.collect_status == 1 && goodsObj?.av_return_product?.length && !obj.is_shipper_picks_up)
            || (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 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 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 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 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)
             || (obj.sale_mode === 0 && obj.status === 13)
    ) {
      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 === 1 && obj.status === 7)
            || (obj.sale_mode === 2 && obj.status === 10)
    ) {
      return true
    }
    else { return false }
  }, [obj])

  // 判断是否显示修改订单
  const showEdit = useMemo(() => {
    if (obj.status == 0 || obj.status == 1) {
      return true
    }
    else {
      return false
    }
  }, [obj])

  const [btnList, setbtnList] = useState<any[]>([])
  useMemo(() => {
    const arr: any[] = []
    let nums = 0
    if (showCancel) {
      nums = nums + 1
      arr.push({
        key: 'showCancel',
        label: '取消订单',
        bgKind: 1,
        sort: 1,
      })
    }
    if (canBuy) {
      nums = nums + 1
      arr.push({
        key: 'canBuy',
        label: '去付款',
        bgKind: 2,
        sort: 20,
      })
    }
    if (showBuy) {
      nums = nums + 1
      arr.push({
        key: 'showBuy',
        label: '再次购买',
        bgKind: 3,
        sort: 10,
      })
    }
    if (showTake) {
      nums = nums + 1
      arr.push({
        key: 'showTake',
        label: '确认收货',
        bgKind: 1,
        sort: 9,
      })
    }
    if (showCodeList) {
      nums = nums + 1
      arr.push({
        key: 'showCodeList',
        label: '销售码单',
        bgKind: 1,
        sort: 12,
      })
    }
    if (showTuikuan) {
      nums = nums + 1
      arr.push({
        key: 'showTuikuan',
        label: '申请退款',
        bgKind: 3,
        sort: 3,
      })
    }
    if (showTuihuo) {
      nums = nums + 1
      arr.push({
        key: 'showTuihuo',
        label: '申请退货',
        bgKind: 3,
        sort: 2,
      })
    }
    if (showEdit) {
      nums = nums + 1
      console.log('101010')
      arr.push({
        key: 'showEdit',
        label: '修改订单',
        bgKind: 3,
        sort: 9,
      })
    }
    const fiterArr = arr.sort((a, b) => a.sort - b.sort)
    setbtnList([...fiterArr])
    return nums
  }, [obj])

  const submitBtns = (e, key) => {
    if (key === 'showTuihuo') {
      handApplyGoods?.()
    }
    if (key === 'showTuikuan') {
      handApplyMoney?.()
    }
    if (key === 'showCodeList') {
      handShowCodeList?.(e)
    }
    if (key === 'showTake') {
      handSureGoods?.(e)
    }
    if (key === 'showCancel') {
      cancle?.(e)
    }
    if (key === 'showBuy') {
      nextBuy?.(e)
    }
    // if (key === 'canBuy') {
    //   toPay?.(e)
    // }
    if (key === 'showEdit') {
      handEdit?.()
    }
  }

  return (
    <>
      {/* {
        showBuy && <View className={styles.title} onClick={e => nextBuy?.(e)}>再次购买</View>
      }
      {
        showTuihuo && <View className={styles.title} onClick={() => handApplyGoods?.()}>申请退货</View>
      }
      {
        showTake && <View className={styles.title} onClick={e => handSureGoods?.(e)}>确认收货</View>
      }
      {
        showTuikuan && <View className={styles.title} onClick={() => handApplyMoney?.()}>申请退款</View>

      }
      {
        showCodeList && <View className={styles.title} onClick={e => handShowCodeList?.(e)}>销售码单</View>

      }

      {
        !showTuihuo && !showTuikuan && !showBuy && <View className={styles.title} >暂无更多</View>
      } */}
      {
        btnList.map((item, index) =>
          index < btnList.length - 3 && (
            <View key={index} className={styles.title} onClick={e => submitBtns(e, item.key)}>
              {item.label}
            </View>
          ),
        )
      }
    </>
  )
}
export default memo(BottomApply)
