import React, { useEffect, useRef, useState } from 'react'; import { BMSTable } from '@/components/BMSTable'; import { createFromIconfontCN } from '@ant-design/icons'; import { ProColumns } from '@ant-design/pro-components'; import { Tabs, InputNumber, message } from 'antd'; import { debounce } from 'lodash'; import { Key } from 'react'; import { getUnitRetainListReq, getUnitTypes, saveUnitRetainReq } from '../../service'; import './style.less'; import TableSelecter from './tableSelector'; const IconFont = createFromIconfontCN({ scriptUrl: '', }); export const Distribute = ({ computeDate }: { computeDate: string }) => { const [tabs, set_tabs] = useState([]); const [editable, set_editable] = useState(false); const [dataSource, set_dataSource] = useState([]); const [currentTab, set_currentTab] = useState(undefined); const [tableColumns, set_tableColumns] = useState([]); const [titles, set_titles] = useState([]); const [tableSelecterVisible, set_tableSelecterVisible] = useState(false); const [currentBlock, set_currentBlock] = useState(undefined); const [inputNum, set_inputNum] = useState(0); const [currentEdit, set_currentEdit] = useState({ colCode: '', num: 0, rowCode: '' }); const [slectedRows, set_slectedRows] = useState([]); const [editMode, set_editMode] = useState(false); const wrapContainerRef = useRef(null); const tableBodyRef = useRef(null); const getTab = async () => { const resp = await getUnitTypes(); if (resp) { const arr = resp.list.map((a: any, index: number) => ({ label: a.name, key: a.code, })); set_tabs([...arr]); if (arr.length > 0) set_currentTab(arr[0].key); } }; const onTabChange = (tabKey: string) => { set_currentTab(tabKey); }; const getTableData = async (unitType: string) => { if (!unitType) return; const resp = await getUnitRetainListReq(computeDate, unitType); if (resp) { const { title = [], remainData = [], data = [] } = resp; set_titles( title.map((a: any) => { const value = remainData.filter((b: any) => a.code == b.code)[0].value; return { ...a, value, }; }) ); const arr = title.map((a: any) => ({ title: a.name, dataIndex: `${a.code}`, key: `${a.code}`, width: 230, renderText(num: number, record: any) { const InputComponent = () => { const [inputNum, set_inputNum] = useState(0); const handleChange = (num: number | null) => { set_inputNum(num ? num : 0); }; const onBlurhandle = () => { set_currentEdit({ colCode: a.code, num: inputNum, rowCode: record.unitCode }); }; return ( ); }; return ; }, })); set_tableColumns([ { title: '核算单元名称', dataIndex: 'unitName', key: 'unitName', width: 182, ellipsis: true, fixed: 'left', }, ...arr, ]); const tableData = data.map((a: any) => { let obj: any = {}; let totalCount = 0; title.forEach((b: any) => { const { data: rowData } = a; const needItem = rowData.filter((c: any) => c.code == b.code); obj[`${b.code}`] = needItem.length > 0 ? needItem[0].value : 0; if (b.code != 'totalBonus') { totalCount = totalCount + (needItem.length > 0 ? needItem[0].value : 0); } }); return { ...a, ...obj, totalBonus: totalCount, }; }); set_dataSource([...tableData]); } }; const tableSelecterCommit = (keys: Key[], rows: any[]) => { set_slectedRows([...slectedRows, ...rows]); if (currentBlock.code == 'totalBonus') { const newTitles = titles.map((a) => { if (a.code == currentBlock.code) { return { ...a, value: rows.reduce((prev: number, cur: any) => prev + cur.retainBonus, 0), }; } else { const needArr = rows.filter((b) => b.retainCode == a.code); return { ...a, value: needArr.length > 0 ? needArr.reduce((prev: number, cur: any) => prev + cur.retainBonus, 0) : 0, }; } }); set_titles([...newTitles]); } else { const amount = rows.reduce((prev: number, cur: any) => prev + cur.retainBonus, 0); const newTitles = titles.map((a) => { if (a.code == currentBlock.code) { return { ...a, value: a.value + amount, }; } else { return a; } }); const titlesvalueCount = newTitles.reduce((prev: number, cur: any) => { if (cur.code != 'totalBonus') { return prev + cur.value; } else { return prev; } }, 0); set_titles( newTitles.map((a: any) => { if (a.code == 'totalBonus') { return { ...a, value: titlesvalueCount }; } else { return a; } }) ); } set_tableSelecterVisible(false); }; const saveHandle = async () => { const reaultData = dataSource.map((a) => { return { unitCode: a.unitCode, unitName: a.unitName, data: titles.map((b) => ({ code: b.code, value: a[`${b.code}`] })), }; }); const arr = titles.map((a) => { const ids = slectedRows.filter((b) => b.retainCode == a.code).map((c) => c.id); return { retainCode: a.code, retainIds: [...new Set(ids)], }; }); const result = { data: reaultData, remainData: titles.map((a) => ({ code: a.code, value: a.value })), historyRetain: arr, }; const resp = await saveUnitRetainReq(result); if (resp) { message.success('保存成功!'); set_editMode(false); set_slectedRows([]); } }; useEffect(() => { getTableData(currentTab as string); }, [editMode]); useEffect(() => { // 更新 titles const newTitles = titles.map((b) => { if (b.code === currentEdit.colCode) { return { ...b, value: b.value - currentEdit.num }; } else { return b; } }); const titlesvalueCount = newTitles.reduce((prev: number, cur: any) => { if (cur.code != 'totalBonus') { return prev + cur.value; } else { return prev; } }, 0); set_titles( newTitles.map((a: any) => { if (a.code == 'totalBonus') { return { ...a, value: titlesvalueCount }; } else { return a; } }) ); // 基于更新后的 titles 同时更新 dataSource const updatedDataSource = dataSource.map((item) => { if (item.unitCode == currentEdit.rowCode) { let total = 0; titles.forEach((a) => { if (a.code != 'totalBonus') { if (a.code == currentEdit.colCode) { total = total + currentEdit.num; } else { total = total + item[`${a.code}`]; } } }); return { ...item, [`${currentEdit.colCode}`]: currentEdit.num, totalBonus: total }; } else { return item; } }); set_dataSource(updatedDataSource); }, [currentEdit]); useEffect(() => { if (currentTab) { getTableData(currentTab); } }, [currentTab]); useEffect(() => { getTab(); }, []); useEffect(() => { if (titles.length > 0) { const handleScroll = () => { if (wrapContainerRef.current && tableBody) { wrapContainerRef.current.scrollLeft = tableBody.scrollLeft; } }; const tableBody = document.querySelector('.bms-ant-table-body') as HTMLElement; // 获取 bms-ant-table-cell 元素 const td = document.querySelector('.bms-ant-table-cell') as HTMLElement; const fixedTd = document.querySelector('.bms-ant-table-cell-fix-left') as HTMLElement; if (td) { // 获取元素的样式 const style = getComputedStyle(td); // 获取元素的宽度,包含 padding 和 border const paddingLeft = parseFloat(style.paddingLeft); const paddingRight = parseFloat(style.paddingRight); const borderLeftWidth = parseFloat(style.borderLeftWidth); const borderRightWidth = parseFloat(style.borderRightWidth); const tdWidth = td.clientWidth + paddingLeft + paddingRight + borderLeftWidth + borderRightWidth; // console.log('bms-ant-table-cell 的实际宽度(包含 padding 和 border):', tdWidth); } else { // console.log('没有找到 .bms-ant-table-cell 元素'); } if (fixedTd) { // 获取元素的样式 const style = getComputedStyle(fixedTd); // 获取元素的宽度,包含 padding 和 border const paddingLeft = parseFloat(style.paddingLeft); const paddingRight = parseFloat(style.paddingRight); const borderLeftWidth = parseFloat(style.borderLeftWidth); const borderRightWidth = parseFloat(style.borderRightWidth); const fixedTdWidth = fixedTd.clientWidth + paddingLeft + paddingRight + borderLeftWidth + borderRightWidth; // console.log('bms-ant-table-cell-fix-left 的实际宽度(包含 padding 和 border):', fixedTdWidth); } else { // console.log('没有找到 .bms-ant-table-cell-fix-left 元素'); } if (tableBody) { tableBody.addEventListener('scroll', handleScroll); } // 清理函数,用于移除事件监听器 return () => { if (tableBody) { tableBody.removeEventListener('scroll', handleScroll); } }; } }, [titles]); return (
set_tableSelecterVisible(bool)} title="添加保留考核奖金" rowKey={'id'} defaultSelectedKeys={slectedRows.map((a: any) => a.id)} record={{ ...currentBlock, currentTab }} open={tableSelecterVisible} onFinish={tableSelecterCommit} />
保留奖金分配 {!editMode && ( set_editMode(true)}> 开始分配 )} {editMode && (
set_editMode(false)}> 取消 saveHandle()}> 保存
)}
{tabs.length > 0 && }
可分配额 对总额进行分配
{titles.map((a, index) => (
{a.name}(元) {a.value}
{ set_currentBlock(a); set_tableSelecterVisible(true); }} />
))}
{titles.length > 0 && (
)}
); };