pages_zichan_area.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import React, { useState } from 'react';
  2. import {
  3. Button, Table, Space,
  4. Form, Input, message, Modal, Card,
  5. Popconfirm, Tag, Radio
  6. } from 'antd';
  7. import {
  8. useQuery,
  9. } from '@tanstack/react-query';
  10. import dayjs from 'dayjs';
  11. import 'dayjs/locale/zh-cn';
  12. import { EnableStatus } from '../share/types.ts'
  13. import type {
  14. ZichanArea,
  15. } from '../share/monitorTypes.ts';
  16. import { ZichanAreaAPI } from './api/index.ts';
  17. // 资产归属区域管理页面
  18. export const ZichanAreaPage = () => {
  19. const [form] = Form.useForm();
  20. const [formMode, setFormMode] = useState<'create' | 'edit'>('create');
  21. const [editingId, setEditingId] = useState<number | null>(null);
  22. const [modalVisible, setModalVisible] = useState(false);
  23. const [isLoading, setIsLoading] = useState(false);
  24. const [searchParams, setSearchParams] = useState({
  25. page: 1,
  26. limit: 10,
  27. name: '',
  28. code: ''
  29. });
  30. // 获取资产归属区域列表
  31. const { data, isLoading: isTableLoading, refetch } = useQuery({
  32. queryKey: ['zichanAreas', searchParams],
  33. queryFn: () => ZichanAreaAPI.getZichanAreaList(searchParams)
  34. });
  35. // 处理表单提交
  36. const handleSubmit = async (values: Partial<ZichanArea>) => {
  37. setIsLoading(true);
  38. try {
  39. if (formMode === 'create') {
  40. await ZichanAreaAPI.createZichanArea(values);
  41. message.success('创建成功');
  42. } else {
  43. await ZichanAreaAPI.updateZichanArea(editingId!, values);
  44. message.success('更新成功');
  45. }
  46. setModalVisible(false);
  47. refetch();
  48. } catch (error) {
  49. message.error('操作失败');
  50. } finally {
  51. setIsLoading(false);
  52. }
  53. };
  54. // 处理编辑
  55. const handleEdit = async (record: ZichanArea) => {
  56. setFormMode('edit');
  57. setEditingId(record.id);
  58. form.setFieldsValue(record);
  59. setModalVisible(true);
  60. };
  61. // 处理删除
  62. const handleDelete = async (id: number) => {
  63. try {
  64. await ZichanAreaAPI.deleteZichanArea(id);
  65. message.success('删除成功');
  66. refetch();
  67. } catch (error) {
  68. message.error('删除失败');
  69. }
  70. };
  71. // 处理搜索
  72. const handleSearch = (values: any) => {
  73. setSearchParams(prev => ({
  74. ...prev,
  75. ...values,
  76. page: 1
  77. }));
  78. };
  79. // 处理分页变化
  80. const handlePageChange = (page: number, pageSize?: number) => {
  81. setSearchParams(prev => ({
  82. ...prev,
  83. page,
  84. limit: pageSize || prev.limit
  85. }));
  86. };
  87. // 处理新增
  88. const handleAdd = () => {
  89. setFormMode('create');
  90. setEditingId(null);
  91. form.resetFields();
  92. setModalVisible(true);
  93. };
  94. // 表格列定义
  95. const columns = [
  96. {
  97. title: 'ID',
  98. dataIndex: 'id',
  99. key: 'id',
  100. width: 80
  101. },
  102. {
  103. title: '区域名称',
  104. dataIndex: 'name',
  105. key: 'name'
  106. },
  107. {
  108. title: '区域编码',
  109. dataIndex: 'code',
  110. key: 'code'
  111. },
  112. {
  113. title: '区域图片',
  114. dataIndex: 'image_url',
  115. key: 'image_url',
  116. render: (url?: string) => url ? <img src={url} alt="区域图片" style={{ width: 50, height: 50 }} /> : '-'
  117. },
  118. {
  119. title: '是否启用',
  120. dataIndex: 'is_enabled',
  121. key: 'is_enabled',
  122. render: (enabled?: EnableStatus) => (
  123. <Tag color={enabled === EnableStatus.ENABLED ? 'success' : 'default'}>
  124. {enabled === EnableStatus.ENABLED ? '启用' : '禁用'}
  125. </Tag>
  126. )
  127. },
  128. {
  129. title: '创建时间',
  130. dataIndex: 'created_at',
  131. key: 'created_at',
  132. render: (date: Date) => dayjs(date).format('YYYY-MM-DD HH:mm:ss')
  133. },
  134. {
  135. title: '操作',
  136. key: 'action',
  137. width: 200,
  138. render: (_: any, record: ZichanArea) => (
  139. <Space>
  140. <Button type="link" onClick={() => handleEdit(record)}>编辑</Button>
  141. <Popconfirm
  142. title="确定要删除吗?"
  143. onConfirm={() => handleDelete(record.id)}
  144. >
  145. <Button type="link" danger>删除</Button>
  146. </Popconfirm>
  147. </Space>
  148. )
  149. }
  150. ];
  151. return (
  152. <div>
  153. <Card>
  154. <Form layout="inline" onFinish={handleSearch}>
  155. <Form.Item name="name" label="区域名称">
  156. <Input placeholder="请输入区域名称" />
  157. </Form.Item>
  158. <Form.Item name="code" label="区域编码">
  159. <Input placeholder="请输入区域编码" />
  160. </Form.Item>
  161. <Form.Item>
  162. <Button type="primary" htmlType="submit">搜索</Button>
  163. </Form.Item>
  164. <Form.Item>
  165. <Button onClick={handleAdd}>新增</Button>
  166. </Form.Item>
  167. </Form>
  168. </Card>
  169. <Card style={{ marginTop: 16 }}>
  170. <Table
  171. columns={columns}
  172. dataSource={data?.data}
  173. loading={isTableLoading}
  174. rowKey="id"
  175. pagination={{
  176. current: searchParams.page,
  177. pageSize: searchParams.limit,
  178. total: data?.pagination.total,
  179. onChange: handlePageChange
  180. }}
  181. />
  182. </Card>
  183. <Modal
  184. title={formMode === 'create' ? '新增资产归属区域' : '编辑资产归属区域'}
  185. open={modalVisible}
  186. onCancel={() => setModalVisible(false)}
  187. footer={null}
  188. >
  189. <Form
  190. form={form}
  191. layout="vertical"
  192. onFinish={handleSubmit}
  193. >
  194. <Form.Item
  195. name="name"
  196. label="区域名称"
  197. rules={[{ required: true, message: '请输入区域名称' }]}
  198. >
  199. <Input placeholder="请输入区域名称" />
  200. </Form.Item>
  201. <Form.Item
  202. name="code"
  203. label="区域编码"
  204. rules={[{ required: true, message: '请输入区域编码' }]}
  205. >
  206. <Input placeholder="请输入区域编码" />
  207. </Form.Item>
  208. <Form.Item
  209. name="image_url"
  210. label="区域图片"
  211. >
  212. <Input placeholder="请输入图片URL" />
  213. </Form.Item>
  214. <Form.Item
  215. name="description"
  216. label="区域描述"
  217. >
  218. <Input.TextArea placeholder="请输入区域描述" />
  219. </Form.Item>
  220. <Form.Item
  221. name="is_enabled"
  222. label="是否启用"
  223. initialValue={EnableStatus.ENABLED}
  224. >
  225. <Radio.Group>
  226. <Radio value={EnableStatus.ENABLED}>启用</Radio>
  227. <Radio value={EnableStatus.DISABLED}>禁用</Radio>
  228. </Radio.Group>
  229. </Form.Item>
  230. <Form.Item>
  231. <Space>
  232. <Button type="primary" htmlType="submit" loading={isLoading}>
  233. 确定
  234. </Button>
  235. <Button onClick={() => setModalVisible(false)}>
  236. 取消
  237. </Button>
  238. </Space>
  239. </Form.Item>
  240. </Form>
  241. </Modal>
  242. </div>
  243. );
  244. };