import BreadTit from "@/components/BreadTit";
import LookModal from "@/components/LookObjTable/LookModal";
import { RootState } from "@/store";
import { stores3infoOutAPI } from "@/store/action/stores3";
import { 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 LookStores3() {
  const dispatch = useDispatch();

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

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

  const getInfo = useCallback(async () => {
    const id = urlParamRef.current.id;
    const res = await stores3infoOutAPI(id);
    const info = res.data.child[0];
    info.creatorName = res.data.entity.creatorName;
    info.create_time = res.data.entity.createTime;
    info.statusTxt = statusObj[res.data.entity.status];
    info.reason=res.data.entity.reason
    // 表格信息的整理
    const list = [] as any;
    const oldData = JSON.parse(res.data.entity.beforeJson).name;
    const newData = JSON.parse(res.data.entity.afterJson).name;
    console.log(123, oldData, newData);
    list.push({
      oldData,
      newData,
    });
    dispatch({ type: "stores3/getLookInfo", payload: { info, list } });
  }, [dispatch]);

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

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

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

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

  // 表格格式
  const columns = useMemo(() => {
    const tempArr = [
      {
        title: "原来位置",
        dataIndex: "oldData",
      },
      {
        title: "移库位置",
        dataIndex: "newData",
      },
    ];

    return tempArr;
  }, []);

  return (
    <div className={styles.LookStores3}>
      <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.name}</div>
            </div>
            <div>
              <div className="one">藏品编号：</div>
              <div>{info.dictNum}</div>
            </div>
          </div>
          <div className="topInfoRow">
            <div>
              <div className="one">登记人员：</div>
              <div>{info.creatorName}</div>
            </div>
            <div>
              <div className="one">创建日期：</div>
              <div>{info.create_time}</div>
            </div>
          </div>
          <div className="topInfoTex">
            <span>审核结果：</span>
            {info.statusTxt}
          </div>
          <div className="topInfoTex" title={info.reason}>
            <span>审核说明：</span>
            {info.reason ? info.reason : "-"}
          </div>
        </div>
        <br />
        <div className="topTit topTit2">
          移库记录
          <div className="titBtn">
            <Button onClick={() => lookGoods(info.id)}>查看</Button>
          </div>
        </div>
        <div className="goodsInfo">
          {/* 表格信息 */}
          <Table
            dataSource={tableList}
            columns={columns}
            rowKey="oldData"
            pagination={false}
          />
        </div>
        <div className="backBtn">
          <Button onClick={cancelFu}>返回</Button>
        </div>
      </div>
      {/* 点击查看出来的对话框 */}
      {show ? (
        <LookModal
          id={lookIdRef.current}
          show={show}
          closeShow={() => setShow(false)}
        />
      ) : null}
    </div>
  );
}

const MemoLookStores3 = React.memo(LookStores3);

export default MemoLookStores3;
