import { DatePicker, Button, Space, Table, Select, Modal, Form, Input } from "antd";
import React, { useEffect, useRef, useState } from "react";
import { EditOutlined, DeleteOutlined } from '@ant-design/icons';
import { useParams } from "react-router";
import dayjs from 'dayjs';
import { useReactToPrint } from "react-to-print";
// Импортирование сервисов
import ProxyBodyService from "../../../api/services/proxy-body-service";
import ProxyHeadersService from "../../../api/services/proxy-header-service";
import OrganizationService from "../../../api/services/organization-service";
import IndividualService from "../../../api/services/individuals-service";
import ProductService from "../../../api/services/product-service";
const { Option } = Select;
export const ProxyView = ({
onOk,
onClick,
...props
}) => {
const columns = [
{
title: 'Код',
dataIndex: 'id',
key: 'id',
},
{
title: 'Наименование',
key: 'title',
render: (text, record) => products.find(it => it.id === record.productId)?.title,
},
{
title: 'ед. изм.',
dataIndex: 'unit',
},
{
title: 'Количество',
dataIndex: 'count',
},
{
title: 'Действия',
key: 'actions',
render: (text, record) => {
return (
<Space size="middle">
<div onClick={() => updateRecordHandler(record)}>
<EditOutlined />
</div>
<div onClick={() => deleteRecordHandler(record.id)}>
<DeleteOutlined />
</div>
</Space>
);
}
}
];
const componentRef = useRef();
const handlePrint = useReactToPrint({
content: () => componentRef.current,
});
const { id } = useParams();
const [proxy, setProxy] = useState(null);
const [list, setList] = useState([]);
const [individuals, setIndividuals] = useState([]);
const [organizations, setOrganizations] = useState([]);
const [products, setProducts] = useState([]);
const [currentRecord, setCurrentRecord] = useState(null);
const [visible, setVisible] = useState(false);
const [form] = Form.useForm();
useEffect(() => {
async function fetchData() {
const proxy = await ProxyHeadersService.getOneRecord(id);
const list = await ProxyBodyService.getAllHeadersRecords(id);
const individuals = await IndividualService.getAllRecords();
const organizations = await OrganizationService.getAllRecords();
const products = await ProductService.getAllRecords();
setList(list);
setProxy(proxy);
setIndividuals(individuals);
setOrganizations(organizations);
setProducts(products);
return () => {
setList([]);
setProxy(null);
setIndividuals([]);
setOrganizations([]);
setProducts([]);
};
}
fetchData();
}, [id]);
const createRecordHandler = () => {
setCurrentRecord(null);
form.resetFields();
setVisible(true);
};
const updateRecordHandler = (record) => {
setCurrentRecord(record);
form.setFieldsValue(record);
setVisible(true);
};
const deleteRecordHandler = async (recordId) => {
await ProxyBodyService.removeRecord(recordId);
setList(list.filter(it => it.id !== recordId));
};
const handleOk = async () => {
try {
const values = await form.validateFields();
if (currentRecord) {
setList(list.map(it => it.id === currentRecord.id ? { ...values, id: currentRecord.id } : it));
} else {
setList([...list, { ...values, id: new Date().getTime() }]); // Using a dummy id for new records
}
setCurrentRecord(null);
setVisible(false);
} catch (error) {
console.error("Validation failed:", error);
}
};
return (
<div style={{ padding: 16 }}>
<div ref={componentRef}>
<Space
direction={'vertical'}
align={'center'}
style={{ width: '100%', marginBottom: 24 }}
>
<h2>Доверенность № <strong>{proxy?.number}</strong></h2>
<Space>Дата выписки
<DatePicker
format="DD.MM.YYYY"
value={dayjs(proxy?.dischargeDate, 'YYYY-MM-DD') || null}
onChange={date => setProxy({ ...proxy, dischargeDate: date }, ProxyHeadersService.updateRecord({
...proxy, dischargeDate: date
}))}
style={{ width: 232 }}
allowClear={false}
/>
</Space>
<Space>Дата действия
<DatePicker
format="DD.MM.YYYY"
value={dayjs(proxy?.endDate, 'YYYY-MM-DD') || null}
onChange={date => setProxy({ ...proxy, endDate: date }, ProxyHeadersService.updateRecord({
...proxy, endDate: date
}))}
style={{ width: 232 }}
allowClear={false}
/>
</Space>
<Space>Доверенность выдана: <strong>
<Select
value={proxy?.organizationId || null}
onChange={value => setProxy({ ...proxy, organizationId: value }, ProxyHeadersService.updateRecord({
...proxy, organizationId: value
}))}
placeholder={"Выберите организацию"}
style={{ width: 425 }}
>
{organizations.map(it => <Option key={it.id} value={it.id}>
{it.title}
</Option>)}
</Select>
</strong></Space>
<Space>Получатель: <strong>
<Select
value={proxy?.individualId || null}
onChange={value => setProxy({ ...proxy, individualId: value }, ProxyHeadersService.updateRecord({
...proxy, individualId: value
}))}
placeholder={"Выберите получателя"}
style={{ width: 425 }}
>
{individuals.map(it => <Option key={it.id} value={it.id}>
{it.lastName} {it.firstName} {it.patronymic}
</Option>)}
</Select>
</strong></Space>
</Space>
<Table dataSource={list} columns={columns} />
</div>
<Space>
<Button onClick={createRecordHandler}>
Создать
</Button>
<Button type="dashed" onClick={handlePrint}>
Печать
</Button>
</Space>
<Modal
visible={visible}
title={currentRecord ? "Редактировать запись" : "Создать запись"}
onOk={handleOk}
onCancel={() => setVisible(false)}
>
<Form form={form} layout="vertical">
<Form.Item
name="productId"
label="Продукт"
rules={[{ required: true, message: 'Пожалуйста, выберите продукт' }]}
>
<Select placeholder="Выберите продукт">
{products.map(product => (
<Option key={product.id} value={product.id}>
{product.title}
</Option>
))}
</Select>
</Form.Item>
<Form.Item
name="unit"
label="Ед. изм."
rules={[{ required: true, message: 'Пожалуйста, введите единицу измерения' }]}
>
<Input placeholder="Введите единицу измерения" />
</Form.Item>
<Form.Item
name="count"
label="Количество"
rules={[{ required: true, message: 'Пожалуйста, введите количество' }]}
>
<Input placeholder="Введите количество" type="number" />
</Form.Item>
</Form>
</Modal>
</div>
);
};