1
0

index.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import { PageContainer } from '@ant-design/pro-components';
  2. import React, { useEffect, useState } from 'react';
  3. import { Button, Card, Form, Input, message, Modal, Space, Table } from 'antd';
  4. import { PlusCircleOutlined, ReloadOutlined, SearchOutlined } from '@ant-design/icons';
  5. import Edit from '@/pages/Infrared/brand/edit';
  6. import type { ColumnsType } from 'antd/es/table';
  7. import moment from 'moment/moment';
  8. import { delInfraredBrands, queryInfraredBrands } from '@/services/Infrared/brand';
  9. interface DataType {
  10. key: React.ReactNode;
  11. name: string;
  12. record_id: string;
  13. created_at: string;
  14. category_name: string;
  15. }
  16. /**
  17. * 红外管理- 品牌管理
  18. * @constructor
  19. */
  20. const Brand: React.FC = () => {
  21. const [form] = Form.useForm();
  22. const [loading, setLoading] = useState(false);
  23. const [pagination, setPagination] = useState({ total: 0, current: 1, pageSize: 10 });
  24. const [dataList, setDataList] = useState([]);
  25. const [searchData, setSearchData] = useState<object | null>({});
  26. const [visible, setVisible] = useState(false);
  27. const [editData, setEditData] = useState(null);
  28. const getList = () => {
  29. const params = {
  30. q: 'page',
  31. current: pagination.current,
  32. pageSize: pagination.pageSize,
  33. ...searchData,
  34. };
  35. queryInfraredBrands(params).then((res) => {
  36. if (res && res.code === 0) {
  37. setDataList(res.data.list || []);
  38. setPagination(res.data.pagination);
  39. setLoading(false);
  40. }
  41. });
  42. };
  43. useEffect(() => {
  44. setLoading(true);
  45. getList();
  46. }, []);
  47. useEffect(() => {
  48. getList();
  49. }, [searchData]);
  50. // 查询
  51. const onFinish = () => {
  52. form.validateFields().then((data) => {
  53. setLoading(true);
  54. setSearchData(data);
  55. });
  56. };
  57. // 查询重置
  58. const onReset = () => {
  59. form.resetFields();
  60. setLoading(true);
  61. setSearchData(null);
  62. };
  63. // 新增
  64. const onAdd = () => {
  65. setVisible(true);
  66. setEditData(null);
  67. };
  68. // 编辑
  69. const toEdit = (record: any) => {
  70. setVisible(true);
  71. setEditData(record);
  72. };
  73. // 编辑回调
  74. const editCallback = () => {
  75. setLoading(true);
  76. setVisible(false);
  77. getList();
  78. };
  79. // 删除
  80. const toDel = (record: any) => {
  81. Modal.confirm({
  82. title: '删除',
  83. content: `确认删除品牌:[${record.name}]`,
  84. onOk: () => {
  85. setLoading(true);
  86. delInfraredBrands(record.record_id)
  87. .then((res) => {
  88. if (res && res.code === 0) {
  89. message.success('删除成功');
  90. getList();
  91. } else {
  92. message.error('删除失败');
  93. }
  94. })
  95. .catch((e) => {
  96. message.error(e?.message);
  97. });
  98. },
  99. });
  100. };
  101. const tableChange = (page: any) => {
  102. setLoading(true);
  103. const params = {
  104. q: 'page',
  105. current: page.current,
  106. pageSize: page.pageSize,
  107. ...searchData,
  108. };
  109. queryInfraredBrands(params).then((res) => {
  110. if (res && res.code === 0) {
  111. setDataList(res.data.list || []);
  112. setPagination(res.data.pagination);
  113. setLoading(false);
  114. }
  115. });
  116. };
  117. const columns: ColumnsType<DataType> = [
  118. {
  119. title: '序号',
  120. align: 'center',
  121. key: 'index',
  122. render: (_: any, row: any, index: number) => index + 1,
  123. },
  124. {
  125. title: '品牌名称',
  126. dataIndex: 'name',
  127. key: 'name',
  128. },
  129. {
  130. title: '所属类型',
  131. dataIndex: 'category_name',
  132. key: 'category_name',
  133. },
  134. {
  135. title: '创建时间',
  136. dataIndex: 'created_at',
  137. key: 'created_at',
  138. render: (v) => v && <span>{moment(v).format('YYYY-MM-DD HH:mm')}</span>,
  139. },
  140. {
  141. title: '操作',
  142. key: 'action',
  143. render: (_, record) => (
  144. <Space size="middle">
  145. <a
  146. onClick={() => {
  147. toEdit(record);
  148. }}
  149. >
  150. 编辑
  151. </a>
  152. <a
  153. style={{ color: 'red' }}
  154. onClick={() => {
  155. toDel(record);
  156. }}
  157. >
  158. 删除
  159. </a>
  160. </Space>
  161. ),
  162. },
  163. ];
  164. const paginationProps = {
  165. showSizeChanger: true,
  166. showQuickJumper: true,
  167. showTotal: (total: number) => {
  168. return <span> 共 {total}条 </span>;
  169. },
  170. ...pagination,
  171. };
  172. return (
  173. <PageContainer>
  174. <div>
  175. <Card>
  176. <Form form={form} layout="inline" onFinish={onFinish}>
  177. <Form.Item name="like_name" label="品牌名称">
  178. <Input placeholder="请输入品牌名称" />
  179. </Form.Item>
  180. <Form.Item style={{ marginBottom: '10px' }}>
  181. <Space>
  182. <Button type="primary" htmlType="submit">
  183. <SearchOutlined />
  184. 查询
  185. </Button>
  186. <Button htmlType="button" onClick={onReset}>
  187. <ReloadOutlined />
  188. 重置
  189. </Button>
  190. </Space>
  191. </Form.Item>
  192. </Form>
  193. </Card>
  194. <Button htmlType="button" type="primary" style={{ margin: '20px 0' }} onClick={onAdd}>
  195. <PlusCircleOutlined />
  196. 新增品牌
  197. </Button>
  198. <Table
  199. columns={columns}
  200. dataSource={dataList}
  201. rowKey={(record) => record.record_id}
  202. loading={loading}
  203. pagination={paginationProps}
  204. onChange={tableChange}
  205. />
  206. {visible && <Edit visible={visible} editCallback={editCallback} detailData={editData} />}
  207. </div>
  208. </PageContainer>
  209. );
  210. };
  211. export default Brand;