import AuthButton from "@/components/AuthButton";
import BreadTit from "@/components/BreadTit";
import ImageLazy from "@/components/ImageLazy";
import LookModal from "@/components/LookObjTable/LookModal";
import { RootState } from "@/store";
import { object3infoOutAPI } from "@/store/action/object3";
import { obj3InStorage, statusObj } from "@/utils/dataChange";
import history, { urlParameter } from "@/utils/history";
import { Button, Table } from "antd";
import React, {
  useCallback,
  useEffect,
  useMemo,
  useRef,
  useState,
} from "react";
import { useDispatch, useSelector } from "react-redux";
import { useLocation } from "react-router-dom";
import styles from "./index.module.scss";
function LookObject3() {
  const dispatch = useDispatch();

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

  const getInfo = useCallback(async () => {
    const id = urlParamRef.current.id;
    const res1 = await object3infoOutAPI(id);
    const info = res1.data.entity;
    const list = res1.data.child;
    info.statusTxt = statusObj[info.status];
    dispatch({ type: "object3/getLookInfo", payload: { info, list } });
  }, [dispatch]);

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

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

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

  // 控制弹窗的显示隐藏
  const [show, setShow] = useState(false);
  // 点击表格里面的查看
  const lookIdRef = useRef(-1);

  const lookGoods = useCallback((id: number) => {
    lookIdRef.current = id;
    setShow(true);
  }, []);

  // 表格格式
  const columns = useMemo(() => {
    const tempArr = [
      {
        title: "缩略图",
        render: (item: any) => (
          <ImageLazy width={120} height={70} src={item.thumb} />
        ),
      },
      {
        title: "藏品编号名称",
        dataIndex: "dictNum",
      },
      {
        title: "藏品编号",
        render: (item: any) => (item.num ? item.num : "-"),
      },
      {
        title: "藏品名称",
        dataIndex: "name",
      },
      {
        title: "类别",
        dataIndex: "dictGoodType",
      },
      {
        title: "完残程度",
        dataIndex: "complete",
      },
      {
        title: "藏品位置",
        render: (item: any) => obj3InStorage(item.storageAncestor),
      },
      {
        title: "入库状态",
        render: (item: any) =>
          info.statusTxt === "已完成" ? "已入库" : "未入库",
      },
      {
        title: "操作",
        render: (item: any) => (
          <>
            <Button type="text" danger onClick={() => lookGoods(item.id)}>
              查看
            </Button>
          </>
        ),
      },
    ];

    return tempArr;
  }, [info.statusTxt, lookGoods]);

  return (
    <div className={styles.LookObject3}>
      <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="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">
          {/* 表格信息 */}
          <Table
            size="small"
            scroll={{ y: 360 }}
            dataSource={tableList}
            columns={columns}
            rowKey="id"
            pagination={false}
          />
        </div>
        <div className="backBtn">
          {info.status === 2 ? (
            <AuthButton
              id={302}
              type="primary"
              onClick={() =>
                history.push(
                  `/object/3/add?k=${urlParamRef.current.k}&d=${urlParamRef.current.d}&id=${urlParamRef.current.id}`
                )
              }
            >
              编辑
            </AuthButton>
          ) : null}
          &emsp;
          <Button onClick={cancelFu}>返回</Button>
        </div>
      </div>
      {/* 点击查看出来的对话框 */}
      {show ? (
        <LookModal
          id={lookIdRef.current}
          show={show}
          closeShow={() => setShow(false)}
        />
      ) : null}
    </div>
  );
}

const MemoLookObject3 = React.memo(LookObject3);

export default MemoLookObject3;
