import React, { useEffect, useMemo, useState } from "react"; import { InputNumber, Divider, Form, message, Table, Button, Row, Tag, Modal, } from "antd"; import { PlusOutlined } from "@ant-design/icons"; import * as API from "../../../api"; import styles from "../index.less"; import { useStore } from "../../../store"; import _ from "lodash"; import EditableCell from "./EditableCell"; import SeriesModal from "./SeriesModal"; import { history, useRequest } from "umi"; import { MAX_NUM } from "../../../entity"; const { Column } = Table; interface ShopTaskProps { form: any; } export default function ShopTask({ form }: ShopTaskProps) { const { shopTaskItem, isReadOnly, setShopSeriesRow, deleteShopSeriesRow, addShopSeriesRow, } = useStore(); const [seriesForm] = Form.useForm(); const [editingKey, setEditingKey] = useState(-1); // 编辑表格 key const [seriesVisible, setSeriesVisible] = useState(false); const saveShopSaleTaskHook = useRequest(API.saveShopSaleTask, { manual: true, throwOnError: true, }); // 车系去重 const selectedIds = useMemo(() => { if (!shopTaskItem) return []; return shopTaskItem.seriesTaskList.map((item) => String(item.seriesId)); }, [shopTaskItem]); // 计算车系任务总数自动填写 useEffect(() => { const total = shopTaskItem?.seriesTaskList.reduce( (total, currItem) => total + currItem.taskCount, 0 ); form.setFieldValue("seriesTaskCount", total); }, [shopTaskItem]); const isEditing = (record: API.SeriesTaskItem) => { return record.seriesId === Number(editingKey); }; const setSeriesRow = (id: number) => { seriesForm .validateFields() .then((row: any) => { setEditingKey(-1); setShopSeriesRow(id, row); }) .catch((error: any) => { message.error(error.message ?? "表单校验失败"); }); }; const editSeriesRow = (record: API.SeriesTaskItem) => { seriesForm.setFieldsValue({ ...record }); setEditingKey(record.seriesId); }; // 添加车系 const handleSelectSeries = (values: any) => { addShopSeriesRow(values); setSeriesVisible(false); }; const handleGoBack = () => { history.goBack(); // todo 提示是否有未保存的修改 }; const handleSaveTask = async () => { await form.validateFields(); const values = form.getFieldsValue(); const { taskId, ...other } = shopTaskItem!; saveShopSaleTaskHook .run({ ...other, ...values, id: taskId }) .then(() => { message.success("保存草稿成功"); }) .catch((error: any) => { message.error(error.message ?? "请求失败"); }); }; // 分配到门店和顾问 const autoAssignOneShop = async () => { await form.validateFields(); const values = form.getFieldsValue(); Modal.confirm({ title: ( 确认分配到 全部门店和顾问 吗? ), zIndex: 1002, onOk: async () => { const hide = message.loading("分配中,请稍候", 0); const { taskId, id, ...other } = shopTaskItem!; API.autoAssignOneShop({ ...other, ...values, orderTaskApplyId: taskId, orderShopTaskId: id, }) .then((res) => { message.success("分配成功"); }) .catch((error: any) => { message.error(error.message ?? "请求失败"); }) .finally(() => { hide(); }); }, }); }; const layout = { labelCol: { span: 5 }, wrapperCol: { span: 19 }, }; const components = { body: { cell: EditableCell, }, }; return ( <>