import React, { useCallback, useEffect, useMemo, useState } from 'react'
import styles from './index.module.scss'
import { B1listType } from '@/pages/B1ledger/data'
import { API_getStorageLog } from '@/store/action/B1ledger'
import { storageSelect } from '@/utils/select'
import MyTable from '@/components/MyTable'
import tabLeftArr from '@/pages/Layout/data'
import { Button } from 'antd'
import { authorityFu } from '@/utils/authority'
import { openLink } from '@/utils/history'
import { MessageFu } from '@/utils/message'
import { siteLocChange, timeChange } from '@/utils/deriveFu'
import { goodsStorageTableC } from '@/utils/tableData'

type Props = {
  info: B1listType
}

function Tab3stock({ info }: Props) {
  const [loding, setLoding] = useState(false)

  const [list, setList] = useState<B1listType[]>([])

  const getListFu = useCallback(async () => {
    const res = await API_getStorageLog(info.id)
    if (res.code === 0) {
      setLoding(true)
      setList(res.data)

      // 第一条入库信息
    }
  }, [info.id])

  useEffect(() => {
    getListFu()
  }, [getListFu])

  // 库存状态
  const staTxt = useMemo(() => {
    let txt = '（空）'
    const obj = storageSelect.find(v => v.value === info.siteStatus)
    if (obj) txt = obj.label
    return txt
  }, [info.siteStatus])

  const dataChange = useCallback((item: B1listType) => {
    const arr = tabLeftArr[2].son
    const obj = arr.find(v => v.pageType === item.type)
    return obj
  }, [])

  const staBtn = useMemo(() => {
    return [
      {
        title: '日期',
        render: (item: B1listType) => timeChange(item.date)
      },
      {
        title: '订单类型',
        render: (item: B1listType) => {
          const obj = dataChange(item)

          return obj ? obj.name : '（空）'
        }
      }
    ]
  }, [dataChange])

  const tableLastBtn = useMemo(() => {
    return [
      {
        title: '操作',
        render: (item: B1listType) => {
          return (
            <Button
              size='small'
              type='text'
              onClick={() => {
                const obj = dataChange(item)
                if (obj) {
                  authorityFu(obj.id, `您没有${obj.name}页面权限`, () => {
                    openLink(`${obj.path}_look/3/${item.id}`)
                  })
                } else MessageFu.warning(`type字段：${item.type} 错误`)
              }}
            >
              查看
            </Button>
          )
        }
      }
    ]
  }, [dataChange])

  // // 最新一条出库信息
  // const chuKuObj = useMemo(() => {
  //   let obj = list.find(v => v.type === 'CK' && v.status === 3)
  //   return obj || ({} as B1listType)
  // }, [list])

  // // 最新一条入库信息
  // const ruKuObj = useMemo(() => {
  //   let obj = list.find(v => v.type === 'RK' && v.status === 3)
  //   return obj || ({} as B1listType)
  // }, [list])

  return (
    <div className={styles.Tab3stock}>
      <div className='T3top'>
        <div className='T3topRow'>
          <div className='T3topRowll'>库存状态：</div>
          <div className='T3topRowrr'>{staTxt}</div>
        </div>
        <div className='T3topRow'>
          <div className='T3topRowll'>当前位置：</div>
          <div className='T3topRowrr'>{siteLocChange(info.siteLoc)}</div>
        </div>
        <div className='T3topRow'>
          <div className='T3topRowll'>入库时间：</div>
          <div className='T3topRowrr'>{timeChange(info.siteDateIn)}</div>
        </div>
        <div className='T3topRow'>
          <div className='T3topRowll'>出库时间：</div>
          <div className='T3topRowrr'>{timeChange(info.siteDateOut)}</div>
        </div>
      </div>

      {loding ? (
        <MyTable
          yHeight={690}
          list={list}
          columnsTemp={goodsStorageTableC}
          staBtn={staBtn}
          lastBtn={tableLastBtn}
          pagingInfo={false}
        />
      ) : null}
    </div>
  )
}

const MemoTab3stock = React.memo(Tab3stock)

export default MemoTab3stock
