import BreadTit from "@/components/BreadTit";
import ImageLazy from "@/components/ImageLazy";
import LookModal from "@/components/LookObjTable/LookModal";
import { RootState } from "@/store";
import { auditObject6API, object6infoOutAPI } from "@/store/action/object6";
import { statusObj } from "@/utils/dataChange";
import history, { urlParameter } from "@/utils/history";
import { Button, message, Select, Table } from "antd";
import TextArea from "antd/es/input/TextArea";
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";
const { Option } = Select;

function AuditObject6() {
  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.object6Store.lookInfo
  );

  // 审核结果筛选
  const [value, setValue] = useState(3);
  const valueChangeFu = (val: number) => {
    setValue(val);
  };
  // 审核说明
  const [value2, setValue2] = useState("");

  const getInfo = useCallback(async () => {
    const id = urlParamRef.current.id;
    const res1 = await object6infoOutAPI(id);
    const info = res1.data.entity;
    const list = res1.data.child;
    info.statusTxt = statusObj[info.status];
    dispatch({ type: "object6/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: `/object/6`,
      state: {
        k: urlParamRef.current.k ? urlParamRef.current.k : "1",
        d: urlParamRef.current.d,
      },
    });
  }, []);

  // 点击确定
  const btnOkFu = useCallback(async () => {
    const txt = value2.replaceAll(" ", "").replaceAll("\n", "");
    if (txt === "") return message.warning("审核说明不能为空!");
    const res: any = await auditObject6API({
      id: Number(urlParamRef.current.id),
      reason: value2,
      status: value,
    });
    if (res.code === 0) {
      message.success("操作成功!");
      cancelFu();
    }
  }, [cancelFu, value, value2]);

  // 表格格式
  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: "年代",
        dataIndex: "dictAge",
      },
      {
        title: "操作",
        render: (item: any) => (
          <>
            <Button type="text" danger onClick={() => lookGoods(item.id)}>
              查看
            </Button>
          </>
        ),
      },
    ];

    return tempArr;
  }, [lookGoods]);

  return (
    <div className={styles.AuditObject6}>
      <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="topInfoTex" title={info.description}>
            <span>注销说明：</span>
            {info.description ? info.description : "-"}
          </div>
        </div>
        <br />
        <div className="topTit">藏品信息</div>
        <div className="goodsInfo">
          {/* 表格信息 */}
          <Table
            size="small"
            scroll={{ y: 245 }}
            dataSource={tableList}
            columns={columns}
            rowKey="id"
            pagination={false}
          />
          <br />
          <div className="inputBox1">
            <div className="inputBoxTit">
              <span>* </span>审核结果：
            </div>
            <Select
              style={{ width: 150 }}
              value={value}
              onChange={(val) => valueChangeFu(val)}
            >
              <Option value={3}>通过</Option>
              <Option value={2}>不通过</Option>
            </Select>
          </div>
          <div className="inputBox1 inputBox2">
            <div className="inputBoxTit">
              <span>* </span>审核说明：
            </div>
            <div className="inputBoxText">
              <TextArea
                value={value2}
                onChange={(e) => setValue2(e.target.value)}
                rows={3}
                placeholder="请输入"
                showCount
                maxLength={255}
              />
            </div>
          </div>
        </div>
        <div className="backBtn">
          <Button onClick={btnOkFu} type="primary">
            提交
          </Button>
          &emsp;
          <Button onClick={cancelFu}>返回</Button>
        </div>
      </div>
      {/* 点击查看出来的对话框 */}
      {show ? (
        <LookModal
          id={lookIdRef.current}
          show={show}
          closeShow={() => setShow(false)}
        />
      ) : null}
    </div>
  );
}

const MemoAuditObject6 = React.memo(AuditObject6);

export default MemoAuditObject6;
