import React from 'react';
import {Popconfirm} from 'antd';
import {deleteFileAndDir} from "../../../utils/File";
import {useDispatch, useSelector} from "react-redux";
import {dirFileRemove} from "../../../redux/dirMessage_reducer";
import {removeTableBarItem, setActiveKey} from "../../../redux/tableBarItem_reducer";
const DirDeleteFile = (prop) => {
    console.log("prop",prop)
    const dispatch= useDispatch()
    const activeKey=useSelector(state => state.tableBarItem.activeKey);
    const items = useSelector(state => state.tableBarItem.data)

    const deleteFile = () => {
        // 删除文件
        deleteFileAndDir(prop.filePath)
        // 更新树
        dispatch(dirFileRemove({"filePath":prop.filePath}))
        // 更新bar
        let targetKey = prop.filePath
        let newActiveKey = activeKey;
        let lastIndex = -1;
        items.forEach((item, i) => {
            if (item.key === targetKey) {
                lastIndex = i - 1;
            }
        });
        dispatch(removeTableBarItem(targetKey));
        const newPanes = items.filter((item) => item.key !== targetKey);
        if (newPanes.length && newActiveKey === targetKey) {
            if (lastIndex >= 0) {
                newActiveKey = newPanes[lastIndex].key;
            } else {
                newActiveKey = newPanes[0].key;
            }
        }
        dispatch(setActiveKey({"activeKey":newActiveKey}));
        prop.closeMenu()
    };
    const cancelDeleteFile = () => {

        prop.closeMenu()
    };
    return <Popconfirm
        placement="right"
        title="确认删除文件"
        description="文件删除后将从磁盘删除"
        onConfirm={deleteFile}
        onCancel={cancelDeleteFile}
        okText="确认"
        cancelText="取消"
    >
        <span>删除文件</span>
    </Popconfirm>;
};
export default DirDeleteFile;