hts/apps/staffai/app/[locale]/manage/staffs/[staffId]/item.tsx

358 lines
8.9 KiB
TypeScript

"use client";
import { DeleteOutlined, EditOutlined, PlusOutlined, ExclamationCircleOutlined, SyncOutlined } from '@ant-design/icons';
import { Button, Divider, message, Drawer, Modal } from 'antd';
import React, { useState, useRef } from 'react';
import { PageContainer, FooterToolbar } from '@ant-design/pro-layout';
import ProTable from '@ant-design/pro-table';
import type { ProColumns, ActionType } from '@ant-design/pro-table';
import ProDescriptions from '@ant-design/pro-descriptions';
import type { ProDescriptionsItemProps } from '@ant-design/pro-descriptions';
// import CreateUserForm from './components/CreateUserForm';
// import UpdateUserForm from './components/UpdateUserForm';
// import type { UserInfo, UserListItem } from './data.d';
// import {
// queryUserList,
// updateUser,
// addUser,
// removeUser,
// } from './service';
import md5 from 'md5';
import {
DocsListItem,
UserInfo, queryDocsList,
removeDocs,
syncDocs
} from '@/lib/http/staff';
import Link from 'next/link';
const { confirm } = Modal;
/**
* 添加节点
* @param fields
*/
const handleAdd = async (fields: UserInfo) => {
const hide = message.loading('正在添加');
try {
fields.password = md5(fields.password)
fields.user_alias = fields.user_name
console.log("----------:", fields)
// await addUser({ ...fields });
hide();
message.success('添加成功');
return true;
} catch (error) {
hide();
message.error('添加失败请重试!');
return false;
}
};
/**
* 更新节点
* @param fields
*/
const handleUpdate = async (fields: UserInfo) => {
const hide = message.loading('正在更新');
try {
// fields.deptId = Number(fields.deptId)
fields.password = md5(fields.password)
// await updateUser(fields);
hide();
message.success('更新成功');
return true;
} catch (error) {
hide();
message.error('更新失败请重试!');
return false;
}
};
/**
* 删除节点
* @param selectedRows
*/
const handleRemove = async (staffId: number, selectedRows: DocsListItem[]) => {
const hide = message.loading('正在删除');
if (!selectedRows) return true;
console.log("------>", selectedRows)
try {
await removeDocs({
staff_id: staffId,
ids: selectedRows.map((row) => row.id),
});
hide();
message.success('删除成功,即将刷新');
return true;
} catch (error) {
hide();
message.error('删除失败,请重试');
return false;
}
};
/**
* 同步文档节点
* @param selectedRows
*/
const handleSync = async (staffId: number) => {
const hide = message.loading('正在同步');
console.log("--handleSync---->", staffId)
try {
await syncDocs({
staff_id: staffId,
});
hide();
message.success('同步成功,即将刷新');
return true;
} catch (error) {
hide();
message.error('同步失败,请重试');
return false;
}
};
const DocsList: React.FC<{ staffId?: string, serial_no?: string }> = ({ staffId, serial_no }) => {
const [createModalVisible, handleModalVisible] = useState<boolean>(false);
const [updateModalVisible, handleUpdateModalVisible] = useState<boolean>(false);
const [showDetail, setShowDetail] = useState<boolean>(false);
const actionRef = useRef<ActionType>();
const [currentRow, setCurrentRow] = useState<DocsListItem>();
const [selectedRowsState, setSelectedRows] = useState<DocsListItem[]>([]);
const showDeleteConfirm = (item: DocsListItem) => {
confirm({
title: '是否删除记录?',
icon: <ExclamationCircleOutlined />,
content: '删除的记录不能恢复,请确认!',
onOk() {
handleRemove(Number(staffId), [item]).then(() => {
actionRef.current?.reloadAndRest?.();
});
},
onCancel() {
},
});
};
const columns: ProColumns<DocsListItem>[] = [
{
title: '编号',
dataIndex: 'id',
hideInSearch: true,
},
{
title: '原始文件名',
dataIndex: 'file_name',
hideInSearch: true,
// render: (dom, entity) => {
// return <a onClick={() => {
// setCurrentRow(entity);
// setShowDetail(true);
// }}>{dom}</a>;
// },
},
{
title: '文件存储相对路径',
dataIndex: 'file_path',
hideInSearch: true,
hideInTable: true,
},
// {
// title: '创建人',
// dataIndex: 'create_user',
// hideInSearch: true,
// // hideInTable: true,
// },
{
title: '文件大小(字节)',
dataIndex: 'file_size',
hideInSearch: true,
},
{
title: '创建时间',
dataIndex: 'created_time',
valueType: 'dateTime',
hideInSearch: true,
// hideInTable: true,
},
{
title: '操作',
dataIndex: 'option',
valueType: 'option',
render: (_, record) => (
<>
{/* <Button
type="primary"
icon={<EditOutlined />}
onClick={() => {
handleUpdateModalVisible(true);
setCurrentRow(record);
}}
>
编辑
</Button> */}
<Button
type="primary"
icon={<SyncOutlined />}
onClick={() => {
// handleUpdateModalVisible(true);
// setCurrentRow(record);
handleSync(Number(staffId))
}}
>
</Button>
<Divider type="vertical" />
<Button
type="primary"
danger
icon={<DeleteOutlined />}
onClick={() => {
showDeleteConfirm(record);
}}
>
</Button>
</>
),
},
];
return (
<PageContainer>
<ProTable<DocsListItem>
headerTitle="知识库列表"
actionRef={actionRef}
rowKey="id"
// search={{
// labelWidth: 120,
// }}
options={{
search: false // 隐藏高级搜索栏
}}
toolBarRender={() => [
<Link href={`/manage/repository/new?staffId=${staffId}`} >
<Button type="primary" key="primary"
// onClick={() => handleModalVisible(true)}
>
<PlusOutlined />
</Button>
</Link>,
]}
params={{
current: 0,
id: Number(staffId),
}}
request={queryDocsList}
columns={columns}
rowSelection={{
onChange: (_, selectedRows) => setSelectedRows(selectedRows),
}}
pagination={{ pageSize: 10 }}
/>
{selectedRowsState?.length > 0 && (
<FooterToolbar
extra={
<div>
<a style={{ fontWeight: 600 }}>{selectedRowsState.length}</a> &nbsp;&nbsp;
</div>
}
>
<Button
onClick={async () => {
await handleRemove(Number(staffId), selectedRowsState);
setSelectedRows([]);
actionRef.current?.reloadAndRest?.();
}}
>
</Button>
</FooterToolbar>
)}
{/* <CreateUserForm
key={'CreateUserForm'}
onSubmit={async (value) => {
const success = await handleAdd(value);
if (success) {
handleModalVisible(false);
setCurrentRow(undefined);
if (actionRef.current) {
actionRef.current.reload();
}
}
}}
onCancel={() => {
handleModalVisible(false);
if (!showDetail) {
setCurrentRow(undefined);
}
}}
createModalVisible={createModalVisible}
/> */}
{/* <UpdateUserForm
key={'UpdateUserForm'}
onSubmit={async (value) => {
const success = await handleUpdate(value);
if (success) {
handleUpdateModalVisible(false);
setCurrentRow(undefined);
if (actionRef.current) {
actionRef.current.reload();
}
}
}}
onCancel={() => {
handleUpdateModalVisible(false);
if (!showDetail) {
setCurrentRow(undefined);
}
}}
updateModalVisible={updateModalVisible}
values={currentRow || {}}
/> */}
<Drawer
width={600}
open={showDetail}
onClose={() => {
setCurrentRow(undefined);
setShowDetail(false)
}}
closable={false}
>
{staffId && (
<ProDescriptions<DocsListItem>
column={2}
title={staffId}
request={async () => ({
data: currentRow || {},
})}
params={{
id: staffId,
}}
columns={columns as ProDescriptionsItemProps<DocsListItem>[]}
/>
)}
</Drawer>
</PageContainer>
);
};
export default DocsList;