import AuthButton from '@/components/AuthButton'
import BreadTit from '@/components/BreadTit'
import LookObjTable from '@/components/LookObjTable'
import history, { urlParameter } from '@/utils/history'
import { Button } from 'antd'
import React, { useCallback, useEffect, useRef } from 'react'
import { useLocation } from 'react-router-dom'
import styles from './index.module.scss'
import { getObj1InfoTableAPI2, object1infoOutAPI } from '@/store/action/object1'
import { useDispatch, useSelector } from 'react-redux'
import { RootState } from '@/store'
import { statusObj } from '@/utils/dataChange'
function LookObject1() {
  const dispatch = useDispatch()

  // 获取地址栏参数
  const location = useLocation()
  const urlParamRef = useRef<any>({})
  useEffect(() => {
    urlParamRef.current = urlParameter(location.search)
    // console.log("地址栏参数", urlParamRef.current);
  }, [location])

  // 点击返回
  const cancelFu = () => {
    history.push({
      pathname: `/object`,
      state: {
        k: urlParamRef.current.k ? urlParamRef.current.k : '1',
        d: urlParamRef.current.d
      }
    })
  }

  const getInfo = useCallback(async () => {
    const id = urlParamRef.current.id
    const res1 = await object1infoOutAPI(id)
    const res2 = await getObj1InfoTableAPI2(id)
    const info = res1.data
    info.statusTxt = statusObj[info.status]
    const list = res2.data
    dispatch({ type: 'object1/getLookInfo', payload: { info, list } })
  }, [dispatch])

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

  const { info, list: tableList } = useSelector((state: RootState) => state.object1Store.lookInfo)

  return (
    <div className={styles.LookObject1}>
      <div className='breadTit'>
        <BreadTit>
          <div className='breadTitRow'>藏品登记</div>
          <div className='splitStr'>/</div>
          <div className='breadTitRow active'>查看</div>
        </BreadTit>
      </div>

      <div className='objectSonMain'>
        <div className='topTit'>登记信息</div>
        <div className='topInfo'>
          <div className='topInfoRow'>
            <div>
              <div className='one'>登记编号：</div>
              <div>{info.num}</div>
            </div>
            <div>
              <div className='one'>登记人员：</div>
              <div>{info.creatorName}</div>
            </div>
          </div>
          <div className='topInfoRow'>
            <div>
              <div className='one'>藏品来源：</div>
              <div>{info.sourceName}</div>
            </div>
            <div>
              <div className='one'>审核结果：</div>
              <div>{info.statusTxt}</div>
            </div>
          </div>
          <div className='topInfoTex' title={info.description}>
            <span>登记说明：</span>
            {info.description ? info.description : '-'}
          </div>
          <div className='topInfoTex' title={info.reason}>
            <span>审核说明：</span>
            {info.reason ? info.reason : '-'}
          </div>
        </div>
        <br />
        <div className='topTit'>藏品信息</div>
        <div className='goodsInfo'>
          {/* 封装的表格信息 */}
          <LookObjTable data={tableList} y={340} />
        </div>
        <div className='backBtn'>
          {info.status === 2 ? (
            <AuthButton
              id={102}
              type='primary'
              onClick={() =>
                history.push(
                  `/object/1/add?k=${urlParamRef.current.k}&d=${urlParamRef.current.d}&id=${urlParamRef.current.id}`
                )
              }
            >
              编辑
            </AuthButton>
          ) : null}
          &emsp;
          <Button onClick={cancelFu}>返回</Button>
        </div>
      </div>
    </div>
  )
}

const MemoLookObject1 = React.memo(LookObject1)

export default MemoLookObject1
