

/*
 * @Author: code4eat awesomedema@gmail.com
 * @Date: 2022-12-16 09:42:52
 * @LastEditors: code4eat awesomedema@gmail.com
 * @LastEditTime: 2024-07-26 16:34:21
 * @FilePath: /BudgetManaSystem/src/pages/budgetMana/monthlySet/index.tsx
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */



import BMSPagecontainer from '@/components/BMSPageContainer';

import { useEffect, useRef, useState } from 'react';
import './style.less';

import { TreeProps, Input, Modal, message, Popover, DatePicker, Tooltip } from 'antd';
import { DataNode } from 'antd/es/tree';

import { TransferItem, TransferProps } from 'antd/es/transfer';

import expandedIcon from '../../../../static/treenode_open.png';
import closeIcon from '../../../../static/treenode_collapse.png';
import { BMSTable } from '@/components/BMSTable';
import { ActionType, ModalForm, ProColumns, ProFormDigit, ProFormSelect } from '@ant-design/pro-components';
import { createFromIconfontCN } from '@ant-design/icons';
import { commitRequest, downloadTemplateReq, getCurrentCommitStatusReq, getDataByBtn, getPingjiListByCode, getSecondaryDistriComputeTableData, getTreeData, getTreeDataRespType, importExcelData, save, saveWorkLang } from './service';
import FormItem from 'antd/es/form/FormItem';

import { getComputeDate } from '@/pages/Home/service';

import React from 'react';
import DirectoryTree from 'antd/es/tree/DirectoryTree';
import { getDeepestTreeData } from '@/utils/tooljs';

import { getCurrentCheckStatus } from '@/services/auth';

import { useLocation } from '@umijs/max';
import { useAccess } from '@umijs/max';

import { getParamsData } from '@/pages/setting/baseSetting/paramsMana/service';

import 'moment/locale/zh-cn';
import locale from 'antd/es/date-picker/locale/zh_CN';
import moment from 'moment';
import { getJiezhuanStatus } from '@/pages/budgetMana/monthlySet/service';
import '../../../utils/zhongtaiA';
import BMSUpload from '@/components/BMSUpload';
import { getWorkData } from '@/pages/setting/checkUnitSet/medicalGroupSet/service';
import { getCurrentUnitCheckStatusReq } from '../secondaryDitriComputed/service';


const SearchIcon = createFromIconfontCN({
    scriptUrl: '',
});

const IconFont = createFromIconfontCN({
    scriptUrl: '',
});


export type TableListItem = {
    key: number;
    name: string;
};

let inputsRefKeys: string[] = [];

let currentComputeEmp: undefined | any = undefined; //当前工作量计算人员


const UnitCheckProjectScore: React.FC = () => {
    const location = useLocation();
    const [ifCheckPage, set_ifCheckPage] = useState(false); //是否为二次分配审核页面
    const [treeData, set_treeData] = useState<getTreeDataRespType[]>([]);
    const [treeDataDefault, set_treeDataDefault] = useState<getTreeDataRespType[]>([]);
    const [tableColumn, set_tableColumn] = useState<ProColumns[]>([]);
    const [currentSelectedTreeNode, set_currentSelectedTreeNode] = useState<any | undefined>();

    const [currentComputeDate, set_currentComputeDate] = useState<string | undefined>();

    const [ifShowTip, set_ifShowTip] = useState(false);
    const [auditType,set_auditType] = useState('0'); 
    const [commitStatus, set_commitStatus] = useState('0'); //提交状态
    const [unitCheckStat,set_unitCheckStat] = useState('0'); //单元审核状态
    const [expandedKeys, setExpandedKeys] = useState<React.Key[]>([]);
    const [searchValue, setSearchValue] = useState('');
    const [autoExpandParent, setAutoExpandParent] = useState(true);

    const [ifEditMode, set_ifEditMode] = useState(false);

    const tableRef = useRef<ActionType>();

    const [needSaveData, set_needSaveData] = useState<any | undefined>(undefined);

    // const [inputsRefKeys, set_inputsRefKeys] = useState<string[]>([]);



    const inputsRef = useRef<{ [key: string]: any }>({});

    const [pageData, set_pageData] = useState({ total: 0, completedTotal: 0, leftTotal: 0 });

    const [dataSource, set_dataSource] = useState([]);

    const [tableData, set_tableData] = useState([]);

    const [currentInputRefKeys, set_currentInputRefKeys] = useState<string | undefined>(undefined);
    const [tableH, set_tableH] = useState(0);
    const [currentTreeDataFilter, set_currentTreeDataFilter] = useState({ name: '全部单元', code: 0 });
    const [treeFilterVisible, set_treeFilterVisible] = useState(false);

    const [ifBanAllAction, set_ifBanAllAction] = useState(true);  //是否掩藏所有操作
    const [pingjiOptions, set_pingjiOptions] = useState<undefined | any>(undefined);

    const tableSelecterRef = React.createRef<{ saveHandle: any; getSelectedKeys: any }>();

    const [ifShowModal, set_ifShowModal] = useState(false);
    const [saveLoading, set_saveLoading] = useState(false);


    const column: ProColumns[] = [
        {
            title: '人员',
            width: 140,
            fixed: 'left',
            dataIndex: 'Employee',
        },
    ];

    const tableSelecterColumn: any[] = [
        {
            title: '核算日期',
            dataIndex: 'calculateDate',
        },
        {
            title: '项目数值',
            dataIndex: 'indicatorValue',
        },
        {
            title: '项目名称',
            dataIndex: 'indicatorName',
        },
        {
            title: '参考核算单元',
            dataIndex: 'unitName',
        },
        {
            title: '人员',
            dataIndex: 'userName',
        },
    ];

    interface TableSelecterProps extends TransferProps<TransferItem> {
        tableSelecterColumn: any[];
        record: any
    }

    const TableSelecter = React.forwardRef(({ tableSelecterColumn, record }: TableSelecterProps, ref) => {

        const [datasource, set_datasource] = useState<any[]>([]);
        const [selectedKeys, setSelectedKeys] = useState<React.Key[]>([]);
        const [showList, set_showList] = useState<any[]>([]);

        //获取表格数据
        const getFuncList = async () => {
            const resp = await getWorkData({
                computeDate: currentComputeDate as string,
                unitCode: currentSelectedTreeNode.code,
                empNo: currentComputeEmp.empNo,
                code: currentComputeEmp.colCode
            });
            if (resp) {
                set_datasource(resp);
                set_showList(resp);
            }
        }


        const onSelectChange = (newSelectedRowKeys: React.Key[]) => {
            setSelectedKeys([...newSelectedRowKeys]);

        };

        const saveHandle = async () => {

            const needData = datasource.filter(item => selectedKeys.includes(item.id));
            const total = needData.reduce((prev: number, cur: any) => prev + cur.indicatorValue, 0);

            const value = total.toFixed(4);

            const resp = await saveWorkLang({
                computeDate: currentComputeDate as string,
                unitCode: currentSelectedTreeNode.code,
                empNo: currentComputeEmp.empNo,
                code: currentComputeEmp.colCode,
                value: value
            });

            if (resp) {
                message.success('操作成功！');
                set_ifShowModal(false);
                getTableData({});
            }
        }

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

        return (
            <div className='TableSelecter'>
                <Input placeholder={'请输入项目名称'} allowClear
                    suffix={
                        <IconFont style={{ color: '#99A6BF' }} type="iconsousuo" />
                    }
                    style={{ marginBottom: 8 }}
                    onChange={(e) => {
                        if (e.target.value.length != 0) {
                            const result = datasource.filter(item => item.indicatorName.indexOf(e.target.value) != -1);
                            set_showList(result);
                        } else {
                            set_showList(datasource);
                        }

                    }}

                />
                <BMSTable columns={tableSelecterColumn}
                    options={{
                        density: true,
                        setting: {
                            listsHeight: 100,
                        },
                    }}
                    rowKey='id'
                    scroll={{ y: 400 }}
                    tableAlertRender={false}
                    rowSelection={{
                        // 自定义选择项参考: https://ant.design/components/table-cn/#components-table-demo-row-selection-custom
                        // 注释该行则默认不显示下拉选项
                        // selections: [Table.SELECTION_ALL, Table.SELECTION_INVERT],
                        onChange: onSelectChange,
                    }}
                    pagination={{ showTitle: false, showSizeChanger: true }}
                    dataSource={showList}
                />
                <div className='footer'>
                    <span className='cancel' onClick={() => set_ifShowModal(false)}>取消</span>
                    <span className='ok' onClick={() => saveHandle()}>{`确认(${selectedKeys.length > 0 && selectedKeys.length})`}</span>
                </div>
            </div>

        )
    });


    const onSelect: TreeProps['onSelect'] = (selectedKeys, info: any) => {
        // console.log('selected', selectedKeys, info);
        if (!ifEditMode) {
            //编辑时，需要先保存后切换
            const { node } = info;
            if (!node.child) {
                set_currentSelectedTreeNode(node);
            }
        } else {
            Modal.confirm({
                title: '提示', content: '当前存在编辑未保存，请先保存！', okText: '确定',
                cancelText: '取消',
            })
        }


    };

    const downloadTemplate = () => {
        downloadTemplateReq(currentComputeDate as string, currentSelectedTreeNode.code);
    }

    const importData = () => {

        return (
            <ModalForm
                width={360}
                title={`导入${name}数据`}
                trigger={
                    <span key="3" className='compute cancel'>导入</span>
                }
                submitter={{
                    render: (props, defaultDoms) => {
                        const needBtn = defaultDoms.filter((b) => {
                            return b.key != 'rest'
                        })
                        return [
                            ...needBtn,
                        ];
                    },
                }}
                onFinish={async (values) => {
                    // console.log({values});
                    const { importFile: { fileList } } = values;

                    let formData = new FormData();
                    formData.append('file', fileList[0].originFileObj);
                    formData.append('computeDate', currentComputeDate as string);
                    formData.append('unitCode', currentSelectedTreeNode.code);


                    const resp = await importExcelData(formData);

                    if (resp) {
                        getTableData({});
                        return true;
                    }

                }}
            >
                <FormItem name={'importFile'}>
                    <BMSUpload downloadTemplateFile={() => true ? downloadTemplate() : () => { }} />
                </FormItem>

            </ModalForm>
        )
    }


    const getCurrentComputeDate = async () => {
        const resp = await getComputeDate();
        set_currentComputeDate(resp);
    }


    const getCurrentCommitStatus = async () => {
        if (currentSelectedTreeNode) {
            const resp = await getCurrentCommitStatusReq({
                computeDate: currentComputeDate as string,
                unitCode: currentSelectedTreeNode.code
            });
            set_commitStatus(`${resp}`);
        }
    }

    const getCurrentUnitCheckStatus = async () => {
        if (currentSelectedTreeNode) {
            const resp = await getCurrentUnitCheckStatusReq(currentComputeDate as string, currentSelectedTreeNode.code);
            set_unitCheckStat(`${resp}`);//0 未审核 1 已审核
        }
    }


    //工作量计算
    const calcuHanle = (rowData: any) => {
        currentComputeEmp = rowData;
        set_ifShowModal(true);
    }

    const gennerateColumns = (data: { title: any; userList: any; }, inputsRefKeys?: string[]) => {
        const { title } = data;
        const _title = title.filter((a: any) => a.code != 'Employee');
        _title.sort((prev: any, next: any) => prev.sort - next.sort);

        const _columns = _title.map((item: any, index: number) => {
            return {
                title: <div>{item.name}{item.source == 2 && <span style={{ display: 'inline-flex', marginLeft: 5, justifyContent: 'center', alignItems: 'center', width: 14, height: 14, borderRadius: 4, background: '#80BFFF', color: '#fff' }}>A</span>}</div>,
                dataIndex: `${item.code}`,
                width: 160,
                renderText: (_: any, record: any) => {
                    if (ifEditMode) {

                        let _val = _;
                        if (item.evaluation == 2) {

                            return <ProFormSelect
                                noStyle
                                fieldProps={{
                                    defaultValue: _,
                                    onFocus: () => { currentComputeEmp = { ...record, colCode: item.code } },
                                    onChange(value, option) {

                                        const updatedList = needSaveData.secondValueVos.map((a: any) => {
                                            if (a.empNo == record.empNo) {
                                                //value里是否存在对应的code
                                                const ifhasColCode = (a.value.filter((c: any) => c.code == currentComputeEmp.colCode)).length == 0 ? false : true;

                                                if (!ifhasColCode) {
                                                    //当不存在对应的code时
                                                    let list = [...a.value];
                                                    list.push({ code: currentComputeEmp.colCode, value: value });
                                                    return { ...a, value: list }
                                                }


                                                const arr = a.value.map((b: any) => {
                                                    if (b.code == item.code) {
                                                        return { ...b, value: value ? value : 0 } //value有为undefined的可能
                                                    } else {
                                                        return b
                                                    }
                                                });
                                                return { ...a, value: arr }
                                            } else {
                                                return a
                                            }
                                        });

                                        set_needSaveData({ ...needSaveData, secondValueVos: updatedList });
                                    },
                                }}
                                options={pingjiOptions[`${item.code}`]}
                            />
                        }

                        return <ProFormDigit noStyle min={-1000000000} width={120} fieldProps={{
                            ref: ref => {
                                inputsRef.current[`${record.empNo}-${item.code}`] = ref;
                                inputsRefKeys?.push(`${record.empNo}-${item.code}`);
                                //console.log({ref});
                            },

                            onBlur: () => {
                                const updatedList = needSaveData.secondValueVos.map((a: any) => {
                                    if (a.empNo == record.empNo) {

                                        const ifhasColCode = (a.value.filter((c: any) => c.code == currentComputeEmp.colCode)).length == 0 ? false : true;

                                        if (!ifhasColCode) {
                                            //当不存在对应的code时
                                            let list = [...a.value];
                                            list.push({ code: currentComputeEmp.colCode, value: _val ? _val : 0 });
                                            return { ...a, value: list }
                                        }

                                        const arr = a.value.map((b: any) => {
                                            if (b.code == item.code) {
                                                return { ...b, value: _val ? _val : 0 } //value有为undefined的可能
                                            } else {
                                                return b
                                            }
                                        });
                                        return { ...a, value: arr }
                                    } else {
                                        return a
                                    }
                                });

                                set_needSaveData({ ...needSaveData, secondValueVos: updatedList })
                            },
                            onChange(value) {
                                _val = value ? value : 0;
                            },
                            onPressEnter: (event: any) => {
                                if (event.code === 'Enter') {
                                    event.preventDefault();

                                    const value = event.target.value ? Number(event.target.value) : 0;
                                    const updatedList = needSaveData.secondValueVos.map((a: any) => {
                                        if (a.empNo == record.empNo) {

                                            const ifhasColCode = (a.value.filter((c: any) => c.code == currentComputeEmp.colCode)).length == 0 ? false : true;

                                            if (!ifhasColCode) {
                                                //当不存在对应的code时
                                                let list = [...a.value];
                                                list.push({ code: currentComputeEmp.colCode, value: value ? value : 0 });
                                                return { ...a, value: list }
                                            }


                                            const arr = a.value.map((b: any) => {
                                                if (b.code == item.code) {
                                                    return { ...b, value: value ? value : 0 } //value有为undefined的可能
                                                } else {
                                                    return b
                                                }
                                            });
                                            return { ...a, value: arr }
                                        } else {
                                            return a
                                        }
                                    });

                                    set_needSaveData({ ...needSaveData, secondValueVos: updatedList });

                                    const index = inputsRefKeys ? inputsRefKeys.findIndex(a => a == `${record.empNo}-${item.code}`) : -1;
                                    //console.log({index,input:inputsRef.current,inputsRefKeys});
                                    const nextInput = inputsRefKeys ? inputsRef.current[inputsRefKeys[index + 1]] : null;
                                    if (nextInput) {
                                        inputsRefKeys && set_currentInputRefKeys(inputsRefKeys[index + 1]);
                                        nextInput.focus();
                                    }
                                }
                            },
                            onFocus: () => { set_currentInputRefKeys(`${record.empNo}-${item.code}`); currentComputeEmp = { ...record, colCode: item.code }; },
                            defaultValue: _,

                        }} />
                    } else {
                        return <span style={{ display: 'block' }}>{_}{item.source == 3 && <img onClick={() => calcuHanle({ ...record, colCode: item.code })} style={{ width: 16, height: 16, cursor: 'pointer', marginLeft: 8 }} src={require('../../../../static/calcu.png')} />}</span>
                    }
                },
            }
        });

        set_tableColumn([...column, ..._columns]);
    }

    const getTableData = async (params: any) => {

        if (currentSelectedTreeNode && currentComputeDate) {
            const resp = await getSecondaryDistriComputeTableData({
                computeDate: currentComputeDate,
                unitCode: currentSelectedTreeNode.code,
                ...params,
            });
            if (resp) {
                const { title } = resp;
                title.forEach((a: any) => {
                    if (a.evaluation == 2) {
                        getPingjiData(a.code);
                    }
                })

                set_needSaveData(resp);
                //buildTableData(resp);
            }
        }

        return []
    }


    const getPingjiData = async (code: string) => {
        const resp = await getPingjiListByCode(code);
        if (resp) {
            const needData = resp.map((a: any) => ({ label: a.levelName, value: a.levelName }));
            set_pingjiOptions({ ...pingjiOptions, [`${code}`]: needData })
        }
    }



    const buildTableData = (resp: any, inputsRefKeys?: string[]) => {
        const { title, secondValueVos } = resp;
        const _columns = gennerateColumns(resp, inputsRefKeys);
        const data = secondValueVos.map((item: any) => {
            let total = 0;
            let rowData: { [key: string]: any } = {};

            item.value.forEach((b: any) => {
                const needTitle = title.filter((a: any) => a.code == b.code);
                if (needTitle.length > 0) {
                    rowData[`${needTitle[0].code}`] = b.value
                }
                total = total + b.value;
            });

            return { ...item, ...rowData, id: Math.random(), _columns, Employee: item.empName }
        });

        const compeltedTotal = data.reduce((prev: any, cur: any) => prev + cur.totalScore, 0);
        const leftTotal = resp.totalBonus - compeltedTotal;
        set_pageData({ ...pageData, total: resp.totalBonus ? resp.totalBonus.toFixed(2) : 0, completedTotal: compeltedTotal.toFixed(2), leftTotal: Number(leftTotal.toFixed(2)) });

        set_dataSource(data);
        set_tableData(data);
    }


    const commitBtnhandle = async () => {

        Modal.confirm({
            title: '注意',
            okText: '确定',
            cancelText: '取消',
            content: `${commitStatus == '1' ? '取消提交' : '提交'}当前选择的核算单元的数据?`,
            onOk: async () => {
                const resp = await commitRequest({
                    computeDate: currentComputeDate as string,
                    unitCode: currentSelectedTreeNode.code,
                    submit: commitStatus == '1' ? '0' : '1',  //1 提交 0 取消
                });
                if (resp) {
                    message.success('提交成功！');
                    getTreeReqFunc(currentComputeDate as string, ifCheckPage ? '1' : '0');
                    getCurrentCommitStatus();
                }
            }

        })
    }


    const dataList: any[] = [];

    const getParentKey = (key: React.Key, tree: any[]): React.Key => {
        let parentKey: React.Key;
        for (let i = 0; i < tree.length; i++) {
            const node = tree[i];
            if (node.child) {
                if (node.child.some((item: { code: React.Key; }) => item.code === key)) {
                    parentKey = node.code;
                } else if (getParentKey(key, node.child)) {
                    parentKey = getParentKey(key, node.child);
                }
            }
        }
        return parentKey!;
    };


    const onTreeSearchKeyChange = (e: React.ChangeEvent<HTMLInputElement>) => {

        const { value } = e.target;
        const newExpandedKeys = dataList
            .map((item) => {
                if (item.name.indexOf(value) > -1) {
                    return getParentKey(item.code, treeData);
                }
                return null;
            });

        const b = newExpandedKeys.filter((item, i, self) => item && self.indexOf(item) === i);

        setExpandedKeys(newExpandedKeys as React.Key[]);
        setSearchValue(value);
        setAutoExpandParent(true);
    }

    //左侧树结构筛选
    const changeTreeDataFilter = (obj: any) => {

        set_currentTreeDataFilter(obj);
        set_treeFilterVisible(false);

        interface Node {
            map: boolean;
            child?: Node[];
        }

        function filterNodes(node: Node): Node | null {
            if (node.map === (obj.code == 1 ? false : true) && (!node.child || node.child.length === 0)) {
                return node; // 如果节点map为false并且是叶子节点，返回该节点
            }

            if (Array.isArray(node.child)) {
                // 对每个子节点递归执行此过程
                let child = node.child.map(filterNodes).filter((n): n is Node => n !== null);
                if (child.length > 0) {
                    // 如果有任何子节点的map属性为false，返回包含这些子节点的新节点
                    return { ...node, child: child };
                }
            }

            // 如果节点map为true，或者所有子节点的map都为true，返回null
            return null;
        }

        if (obj.code == 0) {
            set_treeData(treeDataDefault);
        } else {
            const result = treeDataDefault.map(a => {
                return filterNodes(a);
            });

            set_treeData(result.filter(b => b != null) as any);
        }


    }


    const onExpand = (newExpandedKeys: React.Key[]) => {
        setExpandedKeys(newExpandedKeys);
        setAutoExpandParent(false);
    };

    const generateList = (data: getTreeDataRespType[]) => {
        for (let i = 0; i < data.length; i++) {
            const node = data[i];
            const { code, name } = node;
            dataList.push({ code: code, name: name });
            if (node.child) {
                generateList(node.child);
            }
        }
    };

    generateList(treeData as any);

    const getTreeReqFunc = async (computeDate: string, type: string) => {
        const resp = await getTreeData(computeDate, type);
        set_treeData(resp);
        set_treeDataDefault(resp);
    }




    const handleResize = (e: any) => {
        const wH = e.target.innerHeight;
        const tableHeight = wH - 290;
        set_tableH(tableHeight);
    }

    function doResize() {
        setTimeout(() => {
            const ev: any = new Event('resize');
            window.dispatchEvent(ev);
        }, 0)
    }

    const getData = async () => {  //获取按钮获取数据
        const resp = await getDataByBtn(currentComputeDate as string, currentSelectedTreeNode.code);
        message.success('数据获取成功！');
        getTableData({});

    }

    const saveHandle = async () => {
        set_saveLoading(true);
        try {
            const resp = await save({
                computeDate: currentComputeDate,
                unitCode: currentSelectedTreeNode.code,
                unitName: currentSelectedTreeNode.name,
                itemData: needSaveData.secondValueVos
            });
            if (resp) {
                message.success('操作成功！');
                set_ifEditMode(false);
            }
        } finally {
            set_saveLoading(false);
        }
    }

    const getJiezhuanStatusHandle = async () => {
        const resp = await getJiezhuanStatus(currentComputeDate as string);
        if (resp == 2) {
            set_ifBanAllAction(true);
        } else {
            set_ifBanAllAction(false);
        }
    }

    useEffect(() => {

        if (currentComputeDate) {
            getTreeReqFunc(currentComputeDate, ifCheckPage ? '1' : '0');
            getJiezhuanStatusHandle();

        }
    }, [currentComputeDate]);

    useEffect(() => {
        //tableRef.current?.reload();

        if (currentComputeDate && currentSelectedTreeNode) {
            getCurrentCommitStatus();
            getTableData({});
            getCurrentUnitCheckStatus();
        }

    }, [currentSelectedTreeNode, currentComputeDate]);

    //console.log({inputsRef,inputsRefKeys});

    useEffect(() => {
        //初始化左侧树结构数据后

        if (treeData?.length > 0) {
            if (!currentSelectedTreeNode) {
                if (treeData[0].child && treeData[0].child.length > 0) {
                    const [node, nodeParent] = getDeepestTreeData(treeData[0], 'child');

                    set_currentSelectedTreeNode(node);
                    setExpandedKeys([nodeParent.code]);
                    getCurrentCommitStatus();
                }
            }
        }
    }, [treeData]);

    useEffect(() => {
        if (location.pathname == "/secondaryDistribute/secondaryDitriCheck") {
            set_ifCheckPage(true);
        } else { set_ifCheckPage(false); }
    }, [location])



    useEffect(() => {
        if (needSaveData) {
            buildTableData(needSaveData, inputsRefKeys);
        }
    }, [needSaveData]);

    useEffect(() => {
        if (dataSource && Object.keys(inputsRef.current).length > 0) {
            currentInputRefKeys && inputsRef.current[currentInputRefKeys].focus();
        }
    }, [dataSource])


    useEffect(() => {
        if (auditType == '1') {
            //当审核中时，禁掉所有操作
            set_commitStatus('1');
        }
    }, [auditType]);

    useEffect(() => {

        let keys: string[] = [];

        if (ifEditMode && needSaveData) {
            // //获取所有输入框的ref key
            // const { secondValueVos } = needSaveData;
            // secondValueVos.forEach((a: any) => {
            //     a.value.forEach((b: any) => {
            //         keys.push(`${a.empNo}-${b.code}`)
            //     })
            // });
            // set_inputsRefKeys(keys);

        }

        if (needSaveData) {
            if (ifEditMode) {
                gennerateColumns(needSaveData, keys)
            } else {
                gennerateColumns(needSaveData)
            }

        }

        if (!ifEditMode) {
            set_currentInputRefKeys(undefined);
            getTableData({}); //重新拉数据
        }

    }, [ifEditMode]);

    useEffect(() => {
        console.log({ inputsRefKeys, inputsRef })
        // if (ifEditMode && Object.keys(inputsRef.current).length > 0 && inputsRefKeys.length > 0) {

        //     inputsRef.current[`${inputsRefKeys[0]}`].focus();
        //     set_currentInputRefKeys(inputsRefKeys[0]);
        // }
    }, [inputsRef, inputsRefKeys])

    useEffect(() => {
        set_tableColumn(column as ProColumns[]);
        getCurrentComputeDate();
        window.addEventListener('resize', (e) => handleResize(e)) //监听窗口大小改变
        doResize();

        return () => {
            window.removeEventListener('resize', (e) => handleResize(e))
        }
    }, []);


    return (

        <div className='UnitCheckProjectScore'>

            <ModalForm title={`选择属于本单元的记录(${currentSelectedTreeNode?.name})`} width={600} submitter={{
                render: (props, defaultDoms) => []
            }} open={ifShowModal} modalProps={{
                closable: false,
            }}>
                <TableSelecter
                    ref={tableSelecterRef}
                    record={undefined}
                    tableSelecterColumn={tableSelecterColumn}
                ></TableSelecter>
            </ModalForm>


            {
                treeFilterVisible && (
                    <div className='selecterList'>
                        <div onClick={() => changeTreeDataFilter({ name: '全部单元', code: 0 })} className={currentTreeDataFilter.code == 0 ? 'list on' : 'list'}>全部单元</div>
                        <div onClick={() => changeTreeDataFilter({ name: ifCheckPage ? '未审核' : '未提交', code: 1 })} className={currentTreeDataFilter.code == 1 ? 'list on' : 'list'}>{ifCheckPage ? '未审核' : '未提交'}</div>
                        <div onClick={() => changeTreeDataFilter({ name: ifCheckPage ? '已审核' : '已提交', code: 2 })} className={currentTreeDataFilter.code == 2 ? 'list on' : 'list'}>{ifCheckPage ? '已审核' : '已提交'}</div>
                    </div>
                )
            }

            <div className='leftTree'>
                <div className='search'>
                    <Input
                        className='searchInput'
                        placeholder="请输入类目名称"
                        size='small'
                        allowClear
                        style={{ width: 158 }}
                        onChange={onTreeSearchKeyChange}
                        suffix={
                            <SearchIcon type='iconsousuo' />
                        }
                    />
                    <Tooltip placement="top" title={currentTreeDataFilter.name}>
                        <div className={currentTreeDataFilter.code == 0 ? 'filter' : currentTreeDataFilter.code == 1 ? 'filter orange' : 'filter blue'} onClick={() => set_treeFilterVisible(true)}></div>
                    </Tooltip>
                </div>
                {
                    treeData && treeData.length > 0 && currentSelectedTreeNode && (
                        <DirectoryTree
                            fieldNames={{ title: 'name', key: 'code', children: 'child' }}
                            rootStyle={{ height: '100%', paddingBottom: 50, overflowY: 'scroll', overflowX: 'hidden' }}
                            onSelect={onSelect}
                            onExpand={onExpand}
                            expandedKeys={expandedKeys}
                            autoExpandParent={autoExpandParent}
                            selectedKeys={[currentSelectedTreeNode.code]}
                            blockNode={true}

                            icon={() => null}

                            titleRender={
                                (nodeData: any) => {
                                    const strTitle = nodeData.name as string;
                                    const index = strTitle.indexOf(searchValue);
                                    const beforeStr = strTitle.substring(0, index);
                                    const afterStr = strTitle.slice(index + searchValue.length);
                                    const title =
                                        index > -1 ? (
                                            <span>
                                                {beforeStr}
                                                <span className="site-tree-search-value" style={{ color: '#3377ff', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>{searchValue}</span>
                                                {afterStr}
                                            </span>
                                        ) : (
                                            <span className='strTitle'>{strTitle}</span>
                                        );
                                    return <div style={{
                                        display: 'flex', flexDirection: 'row',
                                        width: '100%',
                                        justifyContent: 'space-between', alignItems: 'center', height: 32,
                                        borderRadius: '4px',
                                        overflow: 'hidden',
                                        color: '#17181A',
                                        textOverflow: 'ellipsis',
                                        whiteSpace: 'nowrap'

                                    }}>
                                        {title}
                                        {!nodeData.map && <span className={nodeData.unitType ? 'point lastChild' : 'point'}></span>}
                                    </div>
                                }
                            }
                            defaultSelectedKeys={[treeData[0].child[0].code]}
                            treeData={treeData as unknown as DataNode[]}
                            // treeData={treeDataNew}
                            switcherIcon={(props: any) => {
                                const { expanded } = props;
                                return !expanded ? <img style={{ width: 20, height: 20, position: 'relative', top: 5 }} src={expandedIcon} /> : <img style={{ width: 20, height: 20, position: 'relative', top: 5 }} src={closeIcon} />
                            }}
                        />
                    )
                }
            </div>
            {/* <div style={{width:16,height:'92vh',background:'#F5F7FA'}}></div> */}
            <div className='rightContent'>
                <BMSPagecontainer title={false} ghost>
                    <div className='tabContent'>
                        <div className='tableToolbar'>
                            <div className='search'>
                                <div style={{ marginRight: 16 }}>
                                    <span>核算年月：</span>
                                    {currentComputeDate&&<DatePicker
                                        onChange={(data, dateString) => {
                                            if (dateString) {
                                                set_currentComputeDate(dateString)
                                            } else {
                                                getCurrentComputeDate();
                                            }

                                        }}
                                        allowClear={true}
                                        picker='month'
                                        defaultValue={moment(currentComputeDate,'YYYY-MM')} 
                                        locale={locale}
                                        format='YYYY-MM' placeholder="请选择核算年月" />}
                                    
                                </div>
                                <>
                                    <span className='label'>检索：</span>
                                    <Input placeholder={'请输入姓名'} allowClear
                                        style={{ width: 160 }}
                                        suffix={
                                            <IconFont style={{ color: '#99A6BF' }} type="iconsousuo" />
                                        }
                                        onChange={(e) => {
                                            if (e.target.value.length > 0) {
                                                const result = tableData.filter((a: any) => a.Employee.indexOf(e.target.value) != -1);
                                                set_dataSource(result);
                                            } else {
                                                set_dataSource(tableData);
                                            }

                                        }}

                                    />
                                </>
                            </div>

                            <div className='btnGroupWrap'>
                                {
                                    (!ifCheckPage && !ifBanAllAction) && (
                                        //计算界面
                                        <>
                                            <Popover open={ifShowTip} content={auditType == '1' ? '当前处于审核中，无法操作！' : '当前处于提交中，无法操作！'} >
                                                <div className={(commitStatus != '0' || auditType == '1'||unitCheckStat == '1') ? 'btnGroup disabled' : 'btnGroup'}
                                                    /**
                                                     * 当审核中，三个操作按钮都不可点击
                                                     * 当非审核中，生成和添加不可操作
                                                     */
                                                    onMouseEnter={() => (auditType == '0'||unitCheckStat != '1') ? commitStatus == '1' ? set_ifShowTip(true) : set_ifShowTip(false) : set_ifShowTip(true)}
                                                    onMouseLeave={() => set_ifShowTip(false)}
                                                >
                                                    {!ifEditMode && <span className='compute cancel' onClick={(unitCheckStat != '1') ? commitStatus == '0'? () => getData():()=>{} : () => { }}>获取</span>}
                                                    {!ifEditMode && unitCheckStat != '1'?importData(): <span key="3" className='compute cancel'>导入</span>}
                                                    {!ifEditMode && <span className='cancel' onClick={(unitCheckStat != '1') ?commitStatus == '0'?() => set_ifEditMode(true):()=>null : () => { }}>编辑</span>}
                                                    {ifEditMode && <span className='cancel' onClick={(unitCheckStat != '1') ? commitStatus == '0' ? () => set_ifEditMode(false) : () => null : () => { }}>取消</span>}
                                                    {ifEditMode && (
                                                        <span
                                                            className={saveLoading ? 'editBtn loading-status' : 'editBtn'}
                                                            style={saveLoading ? { cursor: 'not-allowed', opacity: 0.6 } : {}}
                                                            onClick={(!saveLoading && unitCheckStat != '1' && commitStatus == '0') ? () => saveHandle() : undefined}
                                                        >
                                                            {saveLoading ? '保存中...' : '保存'}
                                                        </span>
                                                    )}

                                                </div>
                                            </Popover>
                                            {!ifEditMode && <div key="" className={(auditType == '1'||unitCheckStat == '1') ? 'commit disabled' : 'commit'} onClick={() => (auditType == '0'&&unitCheckStat != '1') ? commitBtnhandle() : () => { }}>{commitStatus == '1' ? '取消提交' : '提交'}</div>}
                                        </>
                                    )
                                }
                            </div>

                        </div>
                        {currentSelectedTreeNode && <BMSTable pagination={false} actionRef={tableRef} rowKey='id'
                            scroll={{ x: 120 * tableColumn.length, y: tableH }}
                            columns={[...tableColumn]} dataSource={dataSource}
                        />}
                    </div>
                </BMSPagecontainer>
            </div >
        </div >

    );

};

export default UnitCheckProjectScore;