import { Image, ScrollView, View } from '@tarojs/components'
import Taro from '@tarojs/taro'
import { memo, useCallback, useEffect, useMemo, useState } from 'react'
import classnames from 'classnames'
import styles from './index.module.scss'
// import BottomBtns from '@/components/BottomBtns'
import { formatPriceDiv } from '@/common/format'
import LabAndImg from '@/components/LabAndImg'

enum ReturnEnum {
  Refunded = 3, // 已退款
  RefundedBefore = 5, // 待退款-确认单
  RefundedAfter = 6, // 待退款
}

enum ReturnType {
  TypeOne = 2, // 预收退款
}
interface propsObj {
  obj: any
  cancle?: (arg: any, obj: any) => void
  nextBuy?: (arg: any, obj: any) => void
  toPay?: (arg: any, obj: any) => void
  handSee?: (any) => void
}
const ItemList = (props: propsObj) => {
  const navTo = (e) => {
    if (e.type == 1) {
      Taro.navigateTo({
        url: `/pages/refundDetail/index?id=${props?.obj?.id}`,
      })
    }
    else {
      Taro.navigateTo({
        url: `/pages/refundMoneyDetail/index?id=${props?.obj?.id}`,
      })
    }
  }

  // 根据退款状态判断使用数组
  const refundArr = useMemo(() => {
    // let booll = false
    // let arr: any[] = []
    if ((props?.obj.stage == ReturnEnum.Refunded || props?.obj.stage == ReturnEnum.RefundedBefore || props?.obj.stage == ReturnEnum.RefundedAfter) && props?.obj.sale_mode == 0 && props?.obj.type != ReturnType.TypeOne) {
      return props?.obj.quality_check_pass_product
    }
    else {
      return props?.obj.product_list
    }
    // if (booll) {
    //   arr = props?.obj.quality_check_pass_product
    // }
    // else {
    //   arr = props?.obj.product_list
    // }
    // return arr
  }, [props?.obj])

  const labAndImgObj = useCallback((item) => {
    return { lab: item.lab, rgb: item.rgb, texture_url: item.texture_url }
  }, [])

  // 待退款和已退款需要变更字段
  const totalGoods = useMemo(() => {
    if ((props?.obj.stage == ReturnEnum.Refunded || props?.obj.stage == ReturnEnum.RefundedBefore || props?.obj.stage == ReturnEnum.RefundedAfter) && props?.obj.type != ReturnType.TypeOne) {
      return props?.obj.quality_check_pass_fabrics
    }
    if ((props?.obj.stage != ReturnEnum.Refunded || props?.obj.stage != ReturnEnum.RefundedBefore || props?.obj.stage != ReturnEnum.RefundedAfter) && props?.obj.type != ReturnType.TypeOne) {
      return props?.obj.total_fabrics
    }
    if (props?.obj.type == ReturnType.TypeOne) {
      return props?.obj.total_fabrics
    }
  }, [props?.obj])
  // 待退款和已退款需要变更字段
  const totalColors = useMemo(() => {
    if ((props?.obj.stage == ReturnEnum.Refunded || props?.obj.stage == ReturnEnum.RefundedBefore || props?.obj.stage == ReturnEnum.RefundedAfter) && props?.obj.type != ReturnType.TypeOne) {
      return props?.obj.quality_check_pass_colors
    }
    if ((props?.obj.stage != ReturnEnum.Refunded || props?.obj.stage != ReturnEnum.RefundedBefore || props?.obj.stage != ReturnEnum.RefundedAfter) && props?.obj.type != ReturnType.TypeOne) {
      return props?.obj.total_colors
    }
    if (props?.obj.type == ReturnType.TypeOne) {
      return props?.obj.total_colors
    }
  }, [props?.obj])
  // 待退款和已退款需要变更字段
  const totalNums = useMemo(() => {
    if ((props?.obj.stage == ReturnEnum.Refunded || props?.obj.stage == ReturnEnum.RefundedBefore || props?.obj.stage == ReturnEnum.RefundedAfter) && props?.obj.sale_mode == 0 && props?.obj.type != ReturnType.TypeOne) {
      return props?.obj.quality_check_pass_number
    }
    if ((props?.obj.stage == ReturnEnum.Refunded || props?.obj.stage == ReturnEnum.RefundedBefore || props?.obj.stage == ReturnEnum.RefundedAfter) && props?.obj.sale_mode != 0 && props?.obj.type != ReturnType.TypeOne) {
      return props?.obj.quality_check_pass_number / 100
    }
    if ((props?.obj.stage != ReturnEnum.Refunded || props?.obj.stage != ReturnEnum.RefundedBefore || props?.obj.stage != ReturnEnum.RefundedAfter) && props?.obj.sale_mode == 0 && props?.obj.type != ReturnType.TypeOne) {
      return props?.obj.total_number
    }
    if ((props?.obj.stage != ReturnEnum.Refunded || props?.obj.stage != ReturnEnum.RefundedBefore || props?.obj.stage != ReturnEnum.RefundedAfter) && props?.obj.sale_mode != 0 && props?.obj.type != ReturnType.TypeOne) {
      return props?.obj.total_number / 100
    }
    if (props?.obj.type == ReturnType.TypeOne) {
      return props?.obj.total_number / 100
    }
  }, [props?.obj])

  // 注意这些判断都是要配合后端一起判断的恶心代码
  // 证明不是前端的问题
  // 列表条数
  const itemOneNums = useMemo(() => {
    if ((props?.obj.stage == ReturnEnum.Refunded || props?.obj.stage == ReturnEnum.RefundedBefore || props?.obj.stage == ReturnEnum.RefundedAfter) && props?.obj.sale_mode == 0 && props?.obj.type != ReturnType.TypeOne) {
      return props?.obj?.quality_check_pass_product[0]?.product_colors[0]?.return_roll
    }
    if ((props?.obj.stage != ReturnEnum.Refunded || props?.obj.stage != ReturnEnum.RefundedBefore || props?.obj.stage != ReturnEnum.RefundedAfter) && props?.obj.sale_mode == 0 && props?.obj.type != ReturnType.TypeOne) {
      return props?.obj?.product_list[0]?.product_colors[0]?.roll
    }
    if ((props?.obj.stage == ReturnEnum.Refunded || props?.obj.stage == ReturnEnum.RefundedBefore || props?.obj.stage == ReturnEnum.RefundedAfter) && props?.obj.sale_mode != 0 && props?.obj.type != ReturnType.TypeOne) {
      return props?.obj?.quality_check_pass_product[0]?.product_colors[0]?.length / 100
    }
    if ((props?.obj.stage != ReturnEnum.Refunded || props?.obj.stage != ReturnEnum.RefundedBefore || props?.obj.stage != ReturnEnum.RefundedAfter) && props?.obj.sale_mode != 0 && props?.obj.type != ReturnType.TypeOne) {
      return props?.obj?.product_list[0]?.product_colors[0]?.length / 100
    }
    if (props?.obj.type == ReturnType.TypeOne) {
      return props?.obj?.product_list[0]?.product_colors[0]?.length / 100
    }
  }, [props?.obj])

  const itemTwoNums = useMemo(() => {
    if ((props?.obj.stage == ReturnEnum.Refunded || props?.obj.stage == ReturnEnum.RefundedBefore || props?.obj.stage == ReturnEnum.RefundedAfter) && props?.obj.sale_mode == 0 && props?.obj.type != ReturnType.TypeOne) {
      return props?.obj?.quality_check_pass_product[0]?.product_colors[1]?.return_roll
    }
    if ((props?.obj.stage != ReturnEnum.Refunded || props?.obj.stage != ReturnEnum.RefundedBefore || props?.obj.stage != ReturnEnum.RefundedAfter) && props?.obj.sale_mode == 0 && props?.obj.type != ReturnType.TypeOne) {
      return props?.obj?.product_list[0]?.product_colors[1]?.roll
    }
    if ((props?.obj.stage == ReturnEnum.Refunded || props?.obj.stage == ReturnEnum.RefundedBefore || props?.obj.stage == ReturnEnum.RefundedAfter) && props?.obj.sale_mode != 0 && props?.obj.type != ReturnType.TypeOne) {
      return props?.obj?.quality_check_pass_product[0]?.product_colors[1]?.length / 100
    }
    if ((props?.obj.stage != ReturnEnum.Refunded || props?.obj.stage != ReturnEnum.RefundedBefore || props?.obj.stage != ReturnEnum.RefundedAfter) && props?.obj.sale_mode != 0 && props?.obj.type != ReturnType.TypeOne) {
      return props?.obj?.product_list[0]?.product_colors[1]?.length / 100
    }
    if (props?.obj.type == ReturnType.TypeOne) {
      return props?.obj?.product_list[0]?.product_colors[1]?.length / 100
    }
  }, [props?.obj])
  console.log(refundArr, 'wwww')
  return (

    <View className={styles.itemBox} onClick={() => navTo(props.obj)}>
      <View className={styles.topItem}>
        <View className={styles.orderNo}>单号：{props?.obj?.order_no}</View>
        <View className={styles.status}>{props?.obj?.stage_name}</View>
      </View>
      <View className={styles.pussName}>{props?.obj?.purchaser_name}</View>
      <View className={styles.line}></View>
      <View className={styles.contBox}>
        <View style={{ minWidth: '24%' }}>
          <View className={styles.leftCont} >
            {
              props?.obj?.product_list[0]?.texture_url === ''
                          && <Image className={styles.leftCont} mode="aspectFill" src="https://test.cdn.zzfzyc.com/mall/no_img.png"></Image>
            }
            {
              props?.obj?.product_list[0]?.texture_url !== ''
                          && <>
                            <LabAndImg value={labAndImgObj(props?.obj?.product_list?.[0])} /></>

            }
            <View className={styles.leftContNums}>颜色 ({props?.obj?.total_colors})</View>
          </View>
        </View>
        <View className={styles.rightCont}>
          <View className={styles.rightTop}>
            <View className={styles.productBox}>
              <View className={styles.productName}>{props?.obj?.product_list[0]?.code} {props?.obj?.product_list[0]?.name}</View>
              <View className={styles.productMode}>{props?.obj?.sale_mode === 0 ? '大货' : props?.obj?.sale_mode === 1 ? '剪版' : '散剪'}</View>
            </View>
            <View className={styles.shipMode}>{props?.obj?.shipment_mode_name}</View>
          </View>
          <View className={styles.colorsBox}>
            <View className={styles.colorNameOne}>{refundArr[0]?.product_colors[0]?.code} {refundArr[0]?.product_colors[0]?.name}</View>
            <View className={styles.colorName}>x{ itemOneNums } {props?.obj?.sale_mode === 0 ? '条' : 'm'}</View>
            <View className={styles.colorNameTwo}>¥ {refundArr[0]?.product_colors[0]?.sale_price / 100}/kg</View>
          </View>
          {
            refundArr[0]?.product_colors?.length > 1 && <>
              <View className={styles.colorsBox}>
                <View className={styles.colorNameOne}>{refundArr[0]?.product_colors[1]?.code} {refundArr[0]?.product_colors[1]?.name}</View>
                <View className={styles.colorName}>x{ itemTwoNums } {props?.obj?.sale_mode === 0 ? '条' : 'm'}</View>
                <View className={styles.colorNameTwo}>¥ {refundArr[0]?.product_colors[1]?.sale_price / 100}/kg</View>
              </View>
            </>
          }
        </View>
      </View>
      <View className={styles.lineOne}></View>
      <View className={styles.bottomMsg}>
        <View className={styles.msgLeft}>布料信息：</View>
        <View className={styles.msgRight}>{totalGoods} 种面料，{totalColors} 种颜色，共 {totalNums} {props?.obj?.sale_mode === 0 ? '条' : 'm'}</View>
      </View>
      {
        (props?.obj.stage == 3) && <View className={styles.bottomMsg}>
          <View className={styles.msgLeft}>退款金额：</View>

          <View className={styles.msgRightOne}>¥{formatPriceDiv(props?.obj?.refund_amount)}</View>
        </View>
      }
      {
        (props.obj.sale_mode == 0 && props?.obj.stage == 3 && (props?.obj.type == 1 || props?.obj.type == 3)) && <View className={styles.bottomBox}>
          <View className={styles.seeButon} onClick={(e) => { props?.handSee?.(e) }}>查看码单</View>
        </View>
      }
    </View>
  )
}
export default memo(ItemList)
