import { Checkbox, Input, Popconfirm, Space, Typography } from 'antd';
import { ConfigProvider, Table } from 'antd5';
import { cloneDeep } from 'lodash';
import { memo, useMemo } from 'react';
import { FormattedMessage } from 'umi';
import ExpandIcon from '../../ExpandIcon';
import PermissionParentShow from '../../PermissionParentShow';
import { leafBuildTree, tree2FlatArray } from '../../utils/array';
import { requestBatchUnbind } from '../api';
import style from './index.less';
const { Text } = Typography;
export interface OptionList {
isOptShow?: boolean;
DataSensitiveMap?: any;
DataSensitiveLevelMap?: any;
setOpenObj?: any;
params?: any;
setParams?: any;
setExpandedAll?: any;
expandedAll?: any;
formatMessage?: any;
moment?: any;
appId?: any;
setRenewalParams?: any;
currentUser?: any;
batchParams?: any;
setBatchParams?: any;
data?: any;
permission?: any;
run?: any;
showSelected?: boolean;
}
const OptionListTable = (props: OptionList) => {
const {
isOptShow,
DataSensitiveMap,
DataSensitiveLevelMap,
setOpenObj,
params,
setParams,
setExpandedAll,
expandedAll,
formatMessage,
moment,
appId,
setRenewalParams,
currentUser,
batchParams,
setBatchParams,
data,
permission,
run,
showSelected,
} = props;
const optionListColumn = [
{
title: ,
dataIndex: 'name',
ellipsis: true,
render: (__: any, record: any) => {
return (
<>
{record?.Id ? (
{record?.isAllowApply == 0 && record?.isBind == false
? formatMessage({ id: 'component.PermissionParentShow.unapprovalapply' })
: record.name}
) : (
{record?.isAllowApply == 0 && record?.isBind == false
? formatMessage({ id: 'component.PermissionParentShow.unapprovalapply' })
: record.name}
)}
>
);
},
},
{
title: ,
dataIndex: 'dataSensitive',
ellipsis: true,
render: (__: any, record: any) => {
return (
{DataSensitiveLevelMap[record.dataSensitive]?.text}
);
},
},
{
title: ,
dataIndex: 'sensitiveLevel',
ellipsis: true,
render: (__: any, record: any) => {
return (
{DataSensitiveMap[record.sensitiveLevel]?.text}
);
},
},
{
title: ,
dataIndex: 'expiredAt',
ellipsis: true,
render: (__: any, record: any) => {
return record.hasOwnProperty('expiredAt') ? (
{record.expiredAt == 0
? formatMessage({ id: 'pages.setting.base.defaultExpirationTime.forever' })
: record.expiredAt < 0
? '-'
: moment().valueOf() < record.expiredAt * 1000
? moment(record.expiredAt * 1000).format('YYYY-MM-DD HH:mm:ss')
: `${moment(record.expiredAt * 1000).format('YYYY-MM-DD HH:mm:ss')}(${formatMessage({
id: 'base.expiredAted',
})})`}
) : null;
},
},
{
title: (
),
dataIndex: 'permissionOption',
fixed: 'right',
hidden: !isOptShow,
render: (__: any, record: any) => [
<>
{record?.isBind ? (
{
try {
const allSettledPromise = requestBatchUnbind(
{ optionList: [record.id] },
currentUser,
appId,
formatMessage,
);
if (allSettledPromise) {
allSettledPromise.then(() => {
run();
});
}
} catch (error) {}
}}
>
) : null}
{record?.expiredAt < 0 || record?.isAllowApply === 0 || !record?.isBind ? null : (
{
setRenewalParams({ optionList: [record.id] });
setOpenObj({ open: true, reload: false });
}}
>
)}
>,
],
},
];
const dataSource = useMemo(() => {
const list = permission?.optionList ?? data?.optionList ?? [];
if (!showSelected) return list;
const leaf = tree2FlatArray(list, '_parentId')
.filter((v: any) => v.isBind)
.map((v: any) => v.id);
return leafBuildTree(leaf, list);
}, [data?.optionList, permission?.optionList, showSelected]);
if (!data?.optionList?.length) return null;
return (
{Boolean(isOptShow) && (
{
setParams({ ...params, optionList: val.target.value });
}}
style={{ width: 200 }}
/>
{
setExpandedAll({ ...expandedAll, optionListKey: true });
}}
>
{formatMessage({ id: 'base.expandedall.true' })}
{
setExpandedAll({ ...expandedAll, optionListKey: false });
}}
>
{formatMessage({ id: 'base.expandedall.false' })}
)}
{
setBatchParams((key) => {
return { ...key, optionList: rowKey };
});
},
getCheckboxProps: (record: any) => ({
disabled:
record?.expiredAt < 0 || record?.isAllowApply === 0 || !record?.isBind,
}),
}
: {
columnWidth: 50,
hideSelectAll: true,
renderCell: (checked, record: any) => {
return ;
},
}
}
columns={optionListColumn as any}
pagination={false}
expandable={{
expandIcon: (p) => ,
expandedRowKeys: expandedAll?.optionList ?? [],
onExpand: (expanded, record) => {
const temp = cloneDeep(expandedAll?.optionList) ?? [];
if (!expanded) {
const arr = temp?.filter((key) => record.id !== key);
setExpandedAll({ ...expandedAll, optionList: arr });
} else {
temp?.push(record.id);
setExpandedAll({ ...expandedAll, optionList: temp });
}
},
}}
dataSource={dataSource}
/>
);
};
export default memo(OptionListTable);