import React, { useEffect, useState } from "react"; import { Modal, Skeleton, Table, Select, Form, Input, Divider, Popconfirm, InputNumber, Button, message } from "antd"; import useInitial from "@/hooks/useInitail"; import { PlusOutlined } from "@ant-design/icons"; import { fetchComps, getshoplist } from "../../api"; import st from "../../style.less"; import { edit } from "@/pages/ware/Spu/SkuManage/api"; interface Props{ onChange?:(data:BearCostSetting.TableList[])=>void; value?: BearCostSetting.TableList[]; } const { Column } = Table; const ShareRateItemlist = ({onChange, value=[]}:Props) => { const typeList = [{id: 1, name: '门店'}, {id: 2, name: '往来单位'}]; const [form] = Form.useForm(); const [data, setData] = useState({}); const [item, setItem] = useState({}); const [tableList, setTableList] = useState([]); const [compsList, setCompsList] = useState([] as BearCostSetting.Comp[]); const [shoplist, setShoplist] = useState([]); const [visible, setVisible] = useState(false); useEffect(() => { if (!data.type) return; fetchComps() .then(res => { res.data && setCompsList([...res.data]); }) .catch(err => { message.error("操作失败"); }); getshoplist() .then(res => { res.data && setShoplist([...res.data]); }) .catch(err => { message.error("操作失败"); }); }, [data.type]); /** * @description: 关闭Modal * @param {*} * @return {*} */ const closeModal = () => { setVisible(false); form.resetFields(); }; /** * @description: 编辑 * @param {BearCostSetting} item * @return {*} */ const edit = (item:BearCostSetting.TableList) => { const {type, shareRate, id, name, key} = item; console.log('type-->', type); const shopname = typeList.filter(i => i.id === type)[0].name; // if (type === 1) { // const _shoplist = shoplist.filter(i => i.id !== id); // setShoplist([..._shoplist]); // } else if (type === 2) { // const _compsList = compsList.filter(i => i.id !== id); // setCompsList([..._compsList]); // } setData({...data, type}); form.setFieldsValue({ type: {value: type, label: shopname}, id: {value: id, label: name}, shareRate, key }); }; const _delete = (row:BearCostSetting.TableList) => { const { key} =row; const _tableList = tableList.filter(i => i.key !== key); onChange && onChange?.([..._tableList]); // value = []; setTableList([..._tableList]); }; /** * @description: 分摊form * @param {any} feildValue * @return {*} */ function handleItemSave(feildValue: any) { console.log('feildValue-->', feildValue); const {id, type, shareRate, key} = feildValue; const _tableList = tableList.filter(i => { return i?.key !== key; }); // const _total = tableList.reduce((total, item) => { // if (item.shareRate)total += item.shareRate; // return total; // }, 0); // if (_total + shareRate >100) { // message.error("分摊比例大于100%"); // return; // } const tableItem = {id: id.value, type: type.value, name: id.label, shareRate, key: _tableList.length +1}; onChange && onChange?.([..._tableList, tableItem]); setTableList([..._tableList, tableItem]); closeModal(); } return ( <>
`${index}`} size="small" > t || "-"} /> (t && `${t}%`) || "-"} /> ( { console.log('index', index); edit(row); setVisible(true); }} > 编辑 _delete(row)} okText="确定" cancelText="取消"> { e.preventDefault(); }} style={{ color: "red" }} > 删除 )} />
); }; export default ShareRateItemlist;