import React, { useCallback, useEffect, useState } from "react";
import styles from "./index.module.scss";
import { Button, Modal, Popconfirm } from "antd";
import { leftTabArr } from "..";
import classNames from "classnames";
import { MessageFu } from "@/utils/message";

const towTableArr = [
  {
    id: 2,
    name: "asdasd",
    type: "文件夹",
  },
  {
    id: 22,
    name: "sadsssscxxxx",
    type: "文件",
  },
];

type Props = {
  mId: { id: number; name: string };
  id1: { id: number; name: string };
  id2: { id: number; name: string };
  closeFu: () => void;
};

function A1OMove({ mId, id1, id2, closeFu }: Props) {
  // 一级id
  const [tab1, setTab1] = useState({ id: 0, name: "" });

  // 二级id
  const [tab2, setTab2] = useState({ id: 0, name: "" });

  // 表格数据
  const [tableInfo, setTableInfo] = useState(leftTabArr);

  // 封装发送请求的函数
  const getListFu = useCallback((id: number) => {
    console.log("文件移动发送请求", id);

    // setTableInfo();
  }, []);

  // 选中文件夹
  const cutFileFu = useCallback(
    (id: number, name: string, type?: string) => {
      if (type === "文件夹") {
        if (tab1.id) {
          // 第二级

          setTab2({ id, name });
        } else {
          // 第一级
          setTab1({ id, name });
          setTab2({ id: 0, name: "" });

          // 后面调试接口的时候 删掉
          setTimeout(() => {
            setTableInfo(towTableArr);
          }, 200);
        }
      }
    },
    [tab1.id]
  );

  useEffect(() => {
    if (tab1.id) getListFu(tab1.id);
  }, [getListFu, tab1]);

  useEffect(() => {
    if (tab2.id) getListFu(tab2.id);
  }, [getListFu, tab2.id]);

  // 面包屑的根目录
  const mianB1Fu = useCallback(() => {
    setTableInfo(leftTabArr);
    setTab1({ id: 0, name: "" });
    setTab2({ id: 0, name: "" });
  }, []);

  const mianB2Fu = useCallback(() => {
    if (tab2.id) {
      setTab1({ id: tab1.id, name: tab1.name });

      setTab2({ id: 0, name: "" });
    }
  }, [tab1.id, tab1.name, tab2.id]);

  // 点击移动
  const btnOkFu = useCallback(() => {
    if (tab2.id) {
      // 进入到了二级
      if (tab2.id === id2.id) return MessageFu.warning("不能和之前的位置一样!");
    } else if (tab1.id) {
      // 一级
      if (tab1.id === id1.id) return MessageFu.warning("不能和之前的位置一样!");
    }
  }, [id1.id, id2.id, tab1.id, tab2.id]);

  return (
    <Modal
      wrapClassName={styles.A1OMove}
      destroyOnClose
      open={true}
      title="移动文件"
      footer={
        [] // 设置footer为空，去掉 取消 确定默认按钮
      }
    >
      <div className="A1OMMain">
        <div className="A1OMTit">
          当前文件位置：{id1.name} {id2.id ? <>/ {id2.name}</> : null} /{" "}
          {mId.name}
        </div>

        <div className="A1OMxie">
          <span onClick={mianB1Fu}>根目录</span>

          {tab1.id ? (
            <>
              {">"} <span onClick={mianB2Fu}>{tab1.name}</span>
            </>
          ) : null}
          {tab2.id ? (
            <>
              <span>
                {" "}
                {">"} {tab2.name}
              </span>
            </>
          ) : null}
        </div>

        <div className="A1OMArr">
          {tableInfo.map((v) => (
            <div
              className={classNames(
                "A1OMArrRow",
                v.type === "文件夹" ? "A1OMArrRowAc" : ""
              )}
              key={v.id}
              onClick={() => cutFileFu(v.id, v.name, v.type)}
            >
              {v.name}
            </div>
          ))}
        </div>

        <div className="A1OMBtn">
          <Popconfirm
            title="放弃编辑后，信息将不会保存!"
            okText="放弃"
            cancelText="取消"
            onConfirm={closeFu}
            okButtonProps={{ loading: false }}
          >
            <Button>取消</Button>
          </Popconfirm>
          &emsp;
          <Button type="primary" onClick={btnOkFu} disabled={!tab1.id}>
            移动至此
          </Button>
        </div>
      </div>
    </Modal>
  );
}

const MemoA1OMove = React.memo(A1OMove);

export default MemoA1OMove;
