import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react' import styles from './index.module.scss' import { useParams } from 'react-router-dom' import { Button, DatePicker, Input, Modal, Select } from 'antd' import MyPopconfirm from '@/components/MyPopconfirm' import history, { btnFlagFu2 } from '@/utils/history' import { MessageFu } from '@/utils/message' import { statusObj } from '@/utils/tableData' import X3auditInfo from '@/pages/X_stock/X3auditInfo' import ZflowTable from '@/components/ZflowTable' import MyTable, { MyTableMethods } from '@/components/MyTable' import { EXbtnFu } from '@/utils/EXBtn' import { D7_APIcreate, D7_APIdel, D7_APIgetInfo, D7_APIsaveApply, D7_APIsaveAudit, D7_APIsaveCreate, D7_APIsaveDraft } from '@/store/action/D7check' import { ID7CheckItem } from '../types' import { useDispatch, useSelector } from 'react-redux' import { RootState } from '@/store' import { D2_APIgetList } from '@/store/action/D2storSet' import { StocktakingModal } from '../components/StocktakingModal' import { D7CHECK_COLLECTION_COLUMNS } from '../constants' import dayjs from 'dayjs' import { areAllCheckersFilled } from '@/utils/objects' import { C1GoodType } from '@/pages/A3_ledger/C1ledger/type' import { BatchFillingModal } from '../components/BatchFillingModal' import { EXPORT_WORD_ENUM } from '@/utils/exportTemplates' export const pageTitTxtObj = { 1: '新增', 2: '编辑', 3: '审批', 4: '查看' } function D7edit() { const { key, id } = useParams() // key:1 新增 2编辑 3审批 4查看 // 滚到顶部 const sollrDom = useRef(null) // 顶部数据 const [topInfo, setTopInfo] = useState({} as ID7CheckItem) const { list: storageIdArr } = useSelector((state: RootState) => state.D2storSet.tableInfo) const [stocktakingVisible, setStocktakingVisible] = useState(false) const [batchFillingVisible, setBatchFillingVisible] = useState(false) const [collectionList, setCollectionList] = useState([]) const tableRef = useRef(null) const delSnapIdsRef = useRef([]) // 变更分库缩写 清空 藏品清单 const [isModalOpen, setIsModalOpen] = useState(0) const dispatch = useDispatch() useEffect(() => { dispatch(D2_APIgetList({ pageNum: 1, pageSize: 99999 })) }, [dispatch]) // 创建订单 const creatFu = useCallback(async () => { const res = await D7_APIcreate() if (res.code === 0) { setTopInfo({ ...res.data, date: dayjs().format('YYYY-MM-DD') }) } }, []) // 获取详情 const getInfoFu = useCallback( async (iid = id) => { const res = await D7_APIgetInfo(iid) if (res.code === 0) { const data = res.data setTopInfo(data) // 藏品清单快照信息id对比 const arrTemp: any = [] const snapsTemp = data.snaps || [] snapsTemp.forEach((v: any) => { const { cusForm, ...obj } = JSON.parse(v.snap || '{}') if (obj.id) obj.id2 = v.id if (cusForm) tableRef.current?.form.setFieldsValue(cusForm) arrTemp.push(obj) }) setCollectionList(arrTemp) } }, [id] ) useEffect(() => { if (key === '1') creatFu() else getInfoFu() if (sollrDom.current) sollrDom.current.scrollTop = 0 }, [creatFu, getInfoFu, key]) // 富文本的ref const ZRichTextRef = useRef(null) // 审批意见的ref const ZAuditRef = useRef(null) const pageTitTxt = useMemo(() => { return Reflect.get(pageTitTxtObj, key) }, [key]) const checkDataFu = useCallback(() => { if (!topInfo.date) { MessageFu.warning('请选择盘点日期') return true } if (!topInfo.num) { MessageFu.warning('请填写盘点单编号') return true } if (!topInfo.storageId) { MessageFu.warning('请选择分库') return true } if (!topInfo.reason) { MessageFu.warning('请填写盘点事由') return true } if (!collectionList.length) { MessageFu.warning('请添加盘点范围') return true } const tableInputVals = tableRef.current?.form.getFieldsValue() if (!areAllCheckersFilled('checker', tableInputVals)) { MessageFu.warning(`请填写盘点人`) return true } return false }, [topInfo, collectionList]) // 审批的sta const [auditSta, setAuDitSta] = useState('') // 新增的底部按钮点击 const btnClickFu = useCallback( async (val: '草稿' | '创建' | '保存' | '审批') => { if (checkDataFu()) return if (val === '审批') { // console.log('审批信息富文本', rtf2) if (!auditSta) { if (sollrDom.current) sollrDom.current.scrollTop = 0 return MessageFu.warning('请选择审批结果') } const rtf2 = ZAuditRef.current?.resData() const res = await D7_APIsaveAudit({ orderId: topInfo.id, rtfOpinion: rtf2, status: auditSta === '同意' ? 1 : 2 }) if (res.code === 0) { MessageFu.success('审批成功') // 跳详情页 history.push(`/check_edit/4/${topInfo.id}`) } } else { const tableValues = tableRef.current?.form.getFieldsValue() const rtf1 = ZRichTextRef.current?.fatherBtnOkFu() || { flag: true } // console.log('申请信息富文本', JSON.stringify(rtf1.val || '')) const obj = { ...topInfo, goodsIds: collectionList.map(v => v.id).join(','), delSnapIds: delSnapIdsRef.current.length ? delSnapIdsRef.current : '', snaps: collectionList.map(i => ({ goodsId: i.id, id: i.id2 ? i.id2 : null, orderId: topInfo.id, statusCheck: tableValues[`${i.id}-statusCheck`], snap: JSON.stringify({ ...i, cusForm: tableValues }) })), rtf: JSON.stringify(rtf1.val || '') } // console.log(123, obj) // if (1 + 1 === 2) return if (val === '草稿') { // 存草稿 当前页保存 不跳转 const res = await D7_APIsaveDraft(obj) if (res.code === 0) { MessageFu.success('草稿保存成功') getInfoFu(topInfo.id) } } else { const res = val === '创建' ? await D7_APIsaveCreate(obj) : await D7_APIsaveApply(obj) if (res.code === 0) { MessageFu.success(`${val}成功`) // 跳到详情页 history.push(`/check_edit/4/${topInfo.id}`) } } } }, [auditSta, checkDataFu, topInfo, collectionList, getInfoFu] ) // 查看的按钮创建-提交-撤回 const lookBtnFu = useCallback( async (val: '创建' | '提交') => { const tableValues = tableRef.current?.form.getFieldsValue() const rtf1 = ZRichTextRef.current?.fatherBtnOkFu() || { flag: true } // console.log('申请信息富文本', JSON.stringify(rtf1.val || '')) const obj = { ...topInfo, goodsIds: collectionList.map(v => v.id).join(','), delSnapIds: delSnapIdsRef.current.length ? delSnapIdsRef.current : '', snaps: collectionList.map(i => ({ goodsId: i.id, id: i.id2 ? i.id2 : null, orderId: topInfo.id, statusCheck: tableValues[`${i.id}-statusCheck`], snap: JSON.stringify({ ...i, cusForm: tableValues }) })), rtf: JSON.stringify(rtf1.val || '') } const res = val === '创建' ? await D7_APIsaveCreate(obj) : await D7_APIsaveApply(obj) if (res.code === 0) { if (sollrDom.current) sollrDom.current.scrollTop = 0 MessageFu.success(val + '成功') getInfoFu() } }, [getInfoFu, topInfo, collectionList] ) // 查看模式点击删除 const delFu = useCallback(async () => { const res = await D7_APIdel(id) if (res.code === 0) { MessageFu.success('删除成功') history.push('/check') } }, [id]) // 查看模式点击审批 编辑 const lookJumpFu = useCallback( (val: '审批' | '编辑') => { history.push(`/check_edit/${val === '审批' ? 3 : 2}/${id}`) MessageFu.success(`已跳转至${val}页面`) }, [id] ) // 查看模式下的按钮 const lookBtn = useMemo(() => { return ( <> {btnFlagFu2(topInfo)['创建'] ? ( ) : null} {btnFlagFu2(topInfo)['提交'] ? ( ) : null} {btnFlagFu2(topInfo)['审批'] ? ( ) : null} {btnFlagFu2(topInfo)['编辑'] ? ( ) : null} {btnFlagFu2(topInfo)['重新提交'] ? ( ) : null} {EXbtnFu(topInfo, [EXPORT_WORD_ENUM.COLLECTION_INVENTORY])} {btnFlagFu2(topInfo)['删除'] ? ( delFu()} Dom={ } /> ) : null} ) }, [delFu, lookBtnFu, lookJumpFu, topInfo]) // 申请记录 const [auditsShow, setAuditsShow] = useState(false) const handleStocktakingOk = (list: any[]) => { delSnapIdsRef.current.push(...collectionList.map(i => i.id2)) setCollectionList(list) } const handleBatchFillingOk = (form: any) => { const temp: Record = {} collectionList.forEach(item => { temp[`${item.id}-checker`] = form.checker temp[`${item.id}-remark`] = form.remark }) tableRef.current?.form.setFieldsValue(temp) } return (
藏品盘点-{pageTitTxt}
{['3'].includes(key) ? ( setAuDitSta(val)} ref={ZAuditRef} /> ) : null} {/* 表单字段、附件等 */}
申请信息 {key === '1' ? null : ( )}
*盘点日期:
{topInfo.id && ( { setTopInfo({ ...topInfo, date: e.format('YYYY-MM-DD') }) }} /> )}
*盘点单编号:
setTopInfo({ ...topInfo, num: e.target.value })} readOnly={['3', '4'].includes(key)} placeholder='请输入内容' maxLength={30} showCount />
*分库缩写:
setTopInfo({ ...topInfo, reason: e.target.value })} readOnly={['3', '4'].includes(key)} placeholder='请输入内容' maxLength={200} showCount />
{/* 盘点范围 */}
盘点范围
{!['3', '4'].includes(key) && (
)}
{ if (item.id2 && !delSnapIdsRef.current.includes(item.id2)) delSnapIdsRef.current.push(item.id2) const temp = [...collectionList] temp.splice(idx, 1) setCollectionList(temp) }} /> ) } } ] } />
{/* 申请流程 */} {auditsShow ? ( setAuditsShow(false)} /> ) : null}
{/* 底部按钮 */}
{['3', '4'].includes(key) && topInfo.audits && topInfo.audits.length && [2, 3, 4].includes(topInfo.status) ? ( ) : null} {key === '4' ? ( lookBtn ) : ( <> {key === '3' ? ( ) : ( )} {key === '1' ? ( ) : null} history.push('/check')} /> )}
{/* 分库缩写改变的时候校验一下 */}
) } const MemoD7edit = React.memo(D7edit) export default MemoD7edit