import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react' import styles from './index.module.scss' import { useParams } from 'react-router-dom' import { FourTableType } from '@/pages/B_enterTibet/B1collect/type' import { E4_APIcreate, E4_APIdel, E4_APIgetInfo, E4_APIrevocation, E4_APIsaveApply, E4_APIsaveAudit, E4_APIsaveCreate, E4_APIsaveDraft } from '@/store/action/E4repair' import { pageTitTxtObj } from '@/pages/D_storeManage/D4impStor/D4edit' import dayjs from 'dayjs' import { MessageFu } from '@/utils/message' import history, { btnFlagFu2, infoPageBackFu, openGoodsInfoFu } from '@/utils/history' import { C1GoodType } from '@/pages/A3_ledger/C1ledger/type' import classNames from 'classnames' import { Button, DatePicker, Input } from 'antd' import MyPopconfirm from '@/components/MyPopconfirm' import TextArea from 'antd/es/input/TextArea' import { EXbtnFu } from '@/utils/EXBtn' import X3auditInfo from '@/pages/X_stock/X3auditInfo' import { E4tableCgoods, statusObj } from '@/utils/tableData' import Z3upFiles from '@/components/Z3upFiles' import ZRichTexts from '@/components/ZRichTexts' import MyTable from '@/components/MyTable' import ZflowTable from '@/components/ZflowTable' import Y1cathet from '@/pages/Y_goodsDetails/Y1cathet' import ZGaddNow from '@/components/ZGaddNow' import ZupFileTable from '@/components/ZupFileTable' import { API_goodsInfo } from '@/store/action/C1ledger' import { EXPORT_WORD_ENUM } from '@/utils/exportTemplates' const tableArrTemp = [ { name: '损坏情况', key: 'txt1' }, { name: '修复意见', key: 'txt2' }, { name: '批准文号', key: 'txt3' } ] function E4edit() { const { key, id } = useParams() // key:1 新增 2编辑 3审批 4查看 // 滚到顶部 const sollrDom = useRef(null) // 顶部数据 const [topInfo, setTopInfo] = useState({} as FourTableType) // 藏品清单快照数据 const [snaps, setSnaps] = useState([]) const delSnapIdsRef = useRef([]) const snapsID2ref = useRef<{ goodsId: number; id: number }[]>([]) // 创建订单 const creatFu = useCallback(async () => { const res = await E4_APIcreate() if (res.code === 0) { setTopInfo(res.data) // 从藏品详情点击按钮进来 const urlAll = window.location.href if (urlAll.includes('?id=')) { const urlId = urlAll.split('?id=')[1] const res2 = await API_goodsInfo(Number(urlId)) if (res2.code === 0) { setSnaps([res2.data]) } } } }, []) // 获取详情 const getInfoFu = useCallback( async (id2?: number) => { const res = await E4_APIgetInfo(id2 || id) if (res.code === 0) { const data = res.data setTopInfo(data) // 设置富文本 ZRichTextRef.current?.ritxtShowFu(JSON.parse(data.rtf || '{}')) // 藏品清单快照信息id对比 const arrTemp: any = [] const snapsTemp = data.snaps || [] snapsTemp.forEach((v: any) => { snapsID2ref.current.push({ goodsId: v.goodsId, id: v.id }) const obj = JSON.parse(v.snap || '{}') if (obj.id) obj.id2 = v.id arrTemp.push(obj) }) setSnaps(arrTemp) } }, [id] ) useEffect(() => { if (key === '1') creatFu() else getInfoFu() if (sollrDom.current) sollrDom.current.scrollTop = 0 }, [creatFu, getInfoFu, key]) const pageTitTxt = useMemo(() => { return Reflect.get(pageTitTxtObj, key) }, [key]) const timeChange = useCallback( (e: any, key: 'date' | 'returnDate') => { setTopInfo({ ...topInfo, [key]: e ? dayjs(e).format('YYYY-MM-DD') : '' }) }, [topInfo] ) // 上传附件的ref const filesRef = useRef(null) // 备注的ref const ZRichTextRef = useRef(null) // 审批意见的ref const ZAuditRef = useRef(null) // 审批的sta const [auditSta, setAuDitSta] = useState('') // 字段的校验 const checkFu = useCallback(() => { if (!topInfo.authUnit) { MessageFu.warning('请输入交修部门') return true } if (!topInfo.date) { MessageFu.warning('请选择交修日期') return true } }, [topInfo]) // 新增的底部按钮点击 const btnClickFu = useCallback( async (val: '草稿' | '创建' | '保存' | '审批') => { if (checkFu()) { if (sollrDom.current) sollrDom.current.scrollTop = 0 return } if (val !== '草稿') { if (snaps.length === 0) return MessageFu.warning('请添加藏品') } 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 E4_APIsaveAudit({ orderId: topInfo.id, rtfOpinion: rtf2, status: auditSta === '同意' ? 1 : 2 }) if (res.code === 0) { MessageFu.success('审批成功') // 跳详情页 history.push(`/repair_edit/4/${topInfo.id}`) } } else { // 多个富文本 const rtf1 = ZRichTextRef.current?.fatherBtnOkFu() || { flag: true } // 上传附件 const filesRes = filesRef.current.filesIdRes() const obj = { ...topInfo, rtf: JSON.stringify(rtf1.val || ''), fileIds: filesRes.join(','), goodsIds: snaps.map(v => v.id).join(','), delSnapIds: delSnapIdsRef.current.length ? delSnapIdsRef.current : '', snaps: snaps.map(v => ({ goodsId: v.id, id: v.id2 ? v.id2 : null, orderId: topInfo.id, snap: JSON.stringify(v) })) } // console.log(123, obj.snaps) // if (1 + 1 === 2) return if (val === '草稿') { // 存草稿 当前页保存 不跳转 const res = await E4_APIsaveDraft(obj) if (res.code === 0) { MessageFu.success('草稿保存成功') getInfoFu(topInfo.id) } } else { const res = val === '创建' ? await E4_APIsaveCreate(obj) : await E4_APIsaveApply(obj) if (res.code === 0) { MessageFu.success(`${val}成功`) // 跳到详情页 history.push(`/repair_edit/4/${topInfo.id}`) } } } }, [auditSta, checkFu, getInfoFu, snaps, topInfo] ) // 打开侧边栏 const [cathet, setCathet] = useState(0) const startBtn = useMemo(() => { return [ { title: '藏品编号', render: (item: C1GoodType) => { return ( setCathet(item.id)} className={classNames('D1GtNum', item.id === cathet ? 'D1GtNumAc' : '')} > {item.num || '(空)'} ) } } ] }, [cathet]) // 表格的输入框改变 const tableInputChange = useCallback( (id: number, val: string, key: 'txt1') => { setSnaps( snaps.map(v => ({ ...v, [key]: v.id === id ? val : v[key] })) ) }, [snaps] ) const tableLastBtn = useMemo(() => { const arr: any = [] tableArrTemp.forEach(v => { arr.push({ title: v.name, width: 200, render: (item: C1GoodType) => { return (