"use strict";const e=require("../../common/vendor.js"),a=require("../../request/api.js"),t=require("../../utils/upload.js");if(!Array){(e.resolveComponent("up-textarea")+e.resolveComponent("up-input")+e.resolveComponent("up-datetime-picker")+e.resolveComponent("up-checkbox")+e.resolveComponent("u-popup")+e.resolveComponent("up-upload")+e.resolveComponent("wd-signature"))()}Math||((()=>"../../uni_modules/uview-plus/components/u-textarea/u-textarea.js")+(()=>"../../uni_modules/uview-plus/components/u-input/u-input.js")+(()=>"../../uni_modules/uview-plus/components/u-datetime-picker/u-datetime-picker.js")+(()=>"../../uni_modules/uview-plus/components/u-checkbox/u-checkbox.js")+(()=>"../../uni_modules/uview-plus/components/u-popup/u-popup.js")+(()=>"../../uni_modules/uview-plus/components/u-upload/u-upload.js")+(()=>"../../node-modules/wot-design-uni/components/wd-signature/wd-signature.js"))();const l={__name:"rectification",setup(l){const n=e.ref(""),r=e.ref(""),u=e.ref(""),o=e.ref(!1),s=e.ref(300),i=e.ref(300),c=e.ref(!0),v=e.ref(""),d=e.ref(""),f=e.ref(340),g=e.ref(null),m=e.ref(!0),p=e.ref(!1),h=()=>{m.value=!0,g.value&&g.value.clear()},y=e=>{const a=e?t.toSubmitFileUrl(e):"";if(!a)return c.value=!0,d.value="",v.value="",void(m.value=!0);d.value=a,v.value=a,c.value=!1,m.value=!1},w=()=>{console.error("签名图片加载失败:",v.value),e.index.showToast({title:"签名图片加载失败",icon:"none"})},S=()=>{m.value=!0,c.value=!0,v.value="",d.value="",e.nextTick$1((()=>{g.value&&g.value.clear()}))},I=e.reactive({rectifyPlan:"",rectificationMeasures:"",controlMeasures:"",rectifyResult:"",planCost:"",actualCost:""}),x=e.ref(""),C=e.ref(!1),M=e.ref(Date.now()),T=e.ref(""),b=e=>{if(!e)return"";const a=new Date(e);if(Number.isNaN(a.getTime()))return"";return`${a.getFullYear()}-${String(a.getMonth()+1).padStart(2,"0")}-${String(a.getDate()).padStart(2,"0")} ${String(a.getHours()).padStart(2,"0")}:${String(a.getMinutes()).padStart(2,"0")}:${String(a.getSeconds()).padStart(2,"0")}`},P=e=>{if(!e)return null;const a=decodeURIComponent(String(e)).trim().replace("T"," ").replace(/-/g,"/"),t=new Date(a).getTime();return Number.isNaN(t)?null:t},R=e=>{const a=P(e);a?x.value=b(a):e&&(x.value=String(e).trim().replace("T"," "))},D=e=>null==e||""===e?[]:Array.isArray(e)?e.map((e=>String(e))).filter(Boolean):String(e).split(",").map((e=>String(e).trim())).filter(Boolean),N=(e,a)=>{const t=new Map;return(a||[]).forEach((e=>{(e.users||[]).forEach((a=>{t.set(String(a.userId),O(a,e))}))})),e.map((e=>t.get(String(e)))).filter(Boolean)},k=(e,a)=>{const t=new Map(a.map((e=>[e.id,e])));return e.forEach((e=>{const a=String(e);if(t.has(a))return;const l=$.value.find((e=>String(e.userId)===a));var n;l&&t.set(a,(n=l,{id:String(n.userId),name:J(n),deptName:n.deptName||""}))})),e.map((e=>t.get(String(e)))).filter(Boolean)},A=e=>{M.value=e.value,T.value=b(e.value),C.value=!1},L=e.ref([]),_=e.ref([]),$=e.ref([]),j=e.ref(!1),U=e.ref([]),B=e.ref([]),z=e.ref(0),E=e.ref([]),q=e.ref(!1),H=e.ref([]),V=e.ref([]),F=e.ref(0),W=e.ref([]),J=e=>e.postName?`${e.nickName}_${e.postName}`:e.nickName||"",O=(e,a)=>({id:String(e.userId),name:J(e),deptName:a.deptName}),G=e=>0===e.length?"":e.length<=2?e.map((e=>e.name)).join("、"):`${e[0].name}等${e.length}人`,Y=e.computed((()=>G(B.value))),K=e.computed((()=>G(V.value))),Q=e.computed((()=>{const e=L.value[z.value];return(null==e?void 0:e.users)||[]})),X=e.computed((()=>{const e=le(E.value);return G(e)})),Z=e.computed((()=>new Set(E.value.map((e=>String(e)))))),ee=e.computed((()=>{const e=_.value[F.value];return(null==e?void 0:e.users)||[]})),ae=e.computed((()=>{const e=ne(W.value);return G(e)})),te=e.computed((()=>new Set(W.value.map((e=>String(e)))))),le=e=>{let a=N(e,L.value);if(a.length[e.id,e])));t.forEach((e=>{l.has(e.id)||l.set(e.id,e)})),a=e.map((e=>l.get(String(e)))).filter(Boolean)}return k(e,a)},ne=e=>{let a=N(e,_.value);return k(e,a)},re=e=>{B.value=le(e)},ue=e=>{V.value=ne(e)},oe=e=>{var a;if(!(null==(a=e.users)?void 0:a.length))return 0;const t=new Set(E.value.map(String));return e.users.filter((e=>t.has(String(e.userId)))).length},se=e=>{var a;if(!(null==(a=e.users)?void 0:a.length))return 0;const t=new Set(W.value.map(String));return e.users.filter((e=>t.has(String(e.userId)))).length};const ie=()=>{E.value=[...U.value];const e=L.value.findIndex((e=>{var a;return(null==(a=e.users)?void 0:a.length)>0}));z.value=e>=0?e:0,j.value=!0},ce=()=>{j.value=!1},ve=()=>{W.value=[...H.value];const e=_.value.findIndex((e=>{var a;return(null==(a=e.users)?void 0:a.length)>0}));F.value=e>=0?e:0,q.value=!0},de=()=>{q.value=!1},fe=()=>{U.value=E.value.map((e=>String(e))),re(U.value),j.value=!1},ge=()=>{H.value=W.value.map((e=>String(e))),ue(H.value),q.value=!1},me=async()=>{try{const e=await a.getDepartmentPersonUsers();0===e.code&&e.data&&(L.value=e.data,U.value.length>0&&re(U.value))}catch(e){console.error("获取安全管理人员列表失败:",e)}},pe=async()=>{console.log("当前hazardId:",n.value);try{const e=await a.getDeptUsersWithSubordinates({hazardId:n.value});0===e.code&&e.data&&(_.value=e.data,H.value.length>0&&ue(H.value),console.log("整改责任人部门树:",_.value))}catch(e){console.error("获取整改责任人列表失败:",e)}},he=async()=>{await Promise.all([me(),pe()]),U.value.length>0&&re(U.value)},ye=e.ref([]),we=e.getCurrentInstance(),{afterRead:Se,deletePic:Ie}=t.createUploadListHandlers(ye,{watermark:{canvasId:"watermarkCanvas",canvasWidthRef:s,canvasHeightRef:i,instance:we}}),xe=async()=>{const l=ye.value.filter((e=>"success"===e.status)).map((e=>t.buildAttachmentItem(e))),s={hazardId:n.value,assignId:r.value,rectifyPlan:I.rectifyPlan,rectificationMeasures:I.rectificationMeasures,controlMeasures:I.controlMeasures,rectifyResult:I.rectifyResult,planCost:Number(I.planCost)||0,actualCost:Number(I.actualCost)||0,attachments:l,manageIds:U.value.map((e=>Number(e))),memberIds:H.value.map((e=>Number(e))),rectifyTime:T.value||b(M.value),signPath:d.value||""};u.value&&(s.rectifyId=u.value);try{const t=await a.submitRectification(s);e.index.hideLoading(),0===t.code?(Ae(!1),e.index.showToast({title:o.value?"保存成功":"提交成功",icon:"success"}),setTimeout((()=>{e.index.navigateBack()}),1500)):e.index.showToast({title:t.msg||(o.value?"保存失败":"提交失败"),icon:"none"})}catch(i){e.index.hideLoading(),console.error("提交整改失败:",i),e.index.showToast({title:"操作失败",icon:"none"})}finally{p.value=!1}},Ce=async()=>{if(I.rectifyPlan)if(I.rectificationMeasures)if(I.controlMeasures)if(I.rectifyResult)if(T.value)if(0!==B.value.length)if(0!==V.value.length)if(c.value){if(!g.value||m.value)return void e.index.showToast({title:"请进行电子签名",icon:"none"});p.value=!0,e.index.showLoading({title:"正在提交...",mask:!0}),g.value.confirm()}else{if(!d.value)return void e.index.showToast({title:"请进行电子签名",icon:"none"});p.value=!0,e.index.showLoading({title:"正在提交...",mask:!0}),await xe()}else e.index.showToast({title:"请选择整改责任人",icon:"none"});else e.index.showToast({title:"请选择安全管理人员",icon:"none"});else e.index.showToast({title:"请选择实际整改时间",icon:"none"});else e.index.showToast({title:"请输入整改完成情况",icon:"none"});else e.index.showToast({title:"请输入管控措施",icon:"none"});else e.index.showToast({title:"请输入整改措施",icon:"none"});else e.index.showToast({title:"请输入整改方案",icon:"none"})},Me=async()=>{try{e.index.showLoading({title:"加载中..."});const l=await a.getRectifyDetail({rectifyId:u.value});if(e.index.hideLoading(),0===l.code&&l.data){const a=l.data;I.rectifyPlan=a.rectifyPlan||"",I.rectificationMeasures=a.rectificationMeasures||"",I.controlMeasures=a.controlMeasures||"",I.rectifyResult=a.rectifyResult||"",I.planCost=a.planCost?String(a.planCost):"",I.actualCost=a.actualCost?String(a.actualCost):"",a.deadline&&R(a.deadline),a.rectifyTime&&(e=>{const a=P(e);a?(M.value=a,T.value=b(a)):e&&(T.value=String(e).trim().replace("T"," "))})(a.rectifyTime);const u=(e=>e&&(e.signPath||e.signUrl||e.signature||e.signatureUrl||e.signaturePath)||"")(a);console.log("整改详情签名路径:",u),y(u),n.value=a.hazardId||"",r.value=a.assignId||"",$.value=[...Array.isArray(a.managers)?a.managers:[],...Array.isArray(a.members)?a.members:[]];const o=(e=>{const a=D(e.manageIds??e.managerIds);return a.length>0?a:Array.isArray(e.managers)&&e.managers.length>0?e.managers.map((e=>String(e.userId))).filter(Boolean):[]})(a),s=D(a.memberIds);o.length>0&&(U.value=o),s.length>0?H.value=s:a.rectifierId&&(H.value=[String(a.rectifierId)]),await he(),a.attachments&&a.attachments.length>0&&(ye.value=a.attachments.map((e=>t.mapServerFileToUploadItem(e)))),e.index.setNavigationBarTitle({title:"编辑整改信息"})}}catch(l){e.index.hideLoading(),console.error("获取整改详情失败:",l),e.index.showToast({title:"获取详情失败",icon:"none"})}},Te=e.ref(!1),be=async()=>{if(n.value){Te.value=!0;try{const t=await a.getHiddenDangerDetail({hazardId:n.value,assignId:r.value});if(0!==t.code||!t.data)return void e.index.showToast({title:"获取隐患详情失败",icon:"none"});const{title:l,description:u}=t.data,o=await a.generateRectifyPlan({title:l,description:u});0===o.code&&o.data?(o.data.rawResponse&&(I.rectifyPlan=o.data.rawResponse),e.index.showToast({title:"AI生成完成",icon:"success",duration:2e3})):e.index.showToast({title:o.msg||"AI生成失败",icon:"none"})}catch(t){console.error("AI生成整改方案失败:",t),e.index.showToast({title:"AI生成失败,请重试",icon:"none"})}finally{Te.value=!1}}else e.index.showToast({title:"缺少隐患信息",icon:"none"})},Pe=e.ref(!1),Re=e.ref(!1),De=e.ref(!1),Ne=e.ref([]),ke=()=>`draft_rectify_${n.value||""}_${u.value||""}`,Ae=(a=!0)=>{const t=ke();e.index.removeStorageSync(t),Pe.value=!1,Re.value=!1,De.value=!0,I.rectifyPlan="",I.rectificationMeasures="",I.controlMeasures="",I.rectifyResult="",I.planCost="",I.actualCost="",ye.value=[],d.value="",v.value="",c.value=!0,Ne.value=[],g.value&&g.value.clear(),e.nextTick$1((()=>{De.value=!1})),a&&e.index.showToast({title:"草稿已清空",icon:"none"})};return e.watch((()=>[I.rectifyPlan,I.rectificationMeasures,I.controlMeasures,I.rectifyResult,I.planCost,I.actualCost,ye.value,d.value,Ne.value]),(()=>{(n.value||u.value)&&(()=>{if(De.value)return;const a=ke();if(!(I.rectifyPlan||I.rectificationMeasures||I.controlMeasures||I.rectifyResult||I.planCost||I.actualCost||ye.value.length>0||d.value||Ne.value.length>0))return e.index.removeStorageSync(a),void(Pe.value=!1);const t={formData:{rectifyPlan:I.rectifyPlan,rectificationMeasures:I.rectificationMeasures,controlMeasures:I.controlMeasures,rectifyResult:I.rectifyResult,planCost:I.planCost,actualCost:I.actualCost},fileList1:ye.value,signatureServerPath:d.value,signatureUrl:v.value,showCanvas:c.value,signaturePaths:Ne.value};e.index.setStorageSync(a,JSON.stringify(t)),Pe.value=!0})()}),{deep:!0}),e.onLoad((a=>{try{const a=e.index.getSystemInfoSync();f.value=a.windowWidth-40}catch(t){console.error("获取系统信息失败:",t)}a.hazardId&&(n.value=a.hazardId),a.assignId&&(r.value=a.assignId),a.rectifyId||he(),a.rectifyId?(u.value=a.rectifyId,o.value="1"===a.isEdit,Me()):((()=>{const a=ke(),l=e.index.getStorageSync(a);if(l)try{const a=JSON.parse(l);if(!(a.formData.rectifyPlan||a.formData.rectificationMeasures||a.formData.controlMeasures||a.formData.rectifyResult||a.formData.planCost||a.formData.actualCost||a.fileList1&&a.fileList1.length>0||a.signatureServerPath||a.signaturePaths&&a.signaturePaths.length>0))return;De.value=!0,I.rectifyPlan=a.formData.rectifyPlan||"",I.rectificationMeasures=a.formData.rectificationMeasures||"",I.controlMeasures=a.formData.controlMeasures||"",I.rectifyResult=a.formData.rectifyResult||"",I.planCost=a.formData.planCost||"",I.actualCost=a.formData.actualCost||"",ye.value=a.fileList1||[],Ne.value=a.signaturePaths||[],a.signatureServerPath||a.signatureUrl?y(a.signatureServerPath||a.signatureUrl):!1===a.showCanvas&&(c.value=!1),Pe.value=!0,Re.value=!0,Ne.value.length>0&&setTimeout((()=>{g.value&&(m.value=!1)}),450),e.nextTick$1((()=>{De.value=!1})),e.index.showToast({title:"已自动恢复您上次未提交的内容",icon:"none",duration:2500})}catch(t){console.error("解析草稿失败:",t),De.value=!1}})(),M.value=Date.now(),T.value=b(M.value)),a.deadline&&R(a.deadline)})),(a,l)=>e.e({a:Re.value},Re.value?{b:e.o((e=>Ae(!0)))}:{},{c:!Te.value},(Te.value,{}),{d:e.t(Te.value?"AI生成中...":"AI生成整改方案"),e:Te.value,f:Te.value,g:e.o(be),h:e.o((e=>I.rectifyPlan=e)),i:e.p({placeholder:"请输入内容",maxlength:-1,autoHeight:!0,modelValue:I.rectifyPlan}),j:e.o((e=>I.rectificationMeasures=e)),k:e.p({placeholder:"请输入整改措施",modelValue:I.rectificationMeasures}),l:e.o((e=>I.controlMeasures=e)),m:e.p({placeholder:"请输入管控措施",modelValue:I.controlMeasures}),n:e.o((e=>I.rectifyResult=e)),o:e.p({placeholder:"请输入内容",modelValue:I.rectifyResult}),p:e.o((e=>I.planCost=e)),q:e.p({placeholder:"请输入内容",type:"number",modelValue:I.planCost}),r:e.o((e=>I.actualCost=e)),s:e.p({placeholder:"请输入内容",type:"number",modelValue:I.actualCost}),t:e.t(x.value||"暂无"),v:x.value?"":1,w:e.t(T.value||"请选择实际整改时间"),x:T.value?"":1,y:e.o((e=>C.value=!0)),z:e.o(A),A:e.o((e=>C.value=!1)),B:e.o((e=>C.value=!1)),C:e.o((e=>M.value=e)),D:e.p({show:C.value,mode:"datetime",modelValue:M.value}),E:e.t(B.value.length>0?Y.value:"请选择安全管理人员(可多选)"),F:0===B.value.length?1:"",G:e.o(ie),H:e.t(V.value.length>0?K.value:"请选择整改责任人(可多选)"),I:0===V.value.length?1:"",J:e.o(ve),K:e.o(ce),L:E.value.length>0},E.value.length>0?{M:e.t(E.value.length),N:e.t(X.value)}:{},{O:e.f(L.value,((a,t,l)=>e.e({a:e.t(a.deptName),b:oe(a)>0},oe(a)>0?{c:e.t(oe(a))}:{},{d:"manager-dept-"+a.deptId,e:e.n({active:z.value===t}),f:e.o((e=>z.value=t),"manager-dept-"+a.deptId)}))),P:0===Q.value.length},0===Q.value.length?{}:{Q:e.f(Q.value,((a,t,l)=>({a:e.o((e=>function(e,a){const t=String(e);a?Z.value.has(t)||(E.value=[...E.value,t]):E.value=E.value.filter((e=>String(e)!==t))}(a.userId,e)),"manager-user-"+a.userId),b:"dcabdcae-8-"+l+",dcabdcae-7",c:e.p({usedAlone:!0,checked:Z.value.has(String(a.userId)),label:J(a),activeColor:"#2667E9",shape:"square"}),d:"manager-user-"+a.userId})))},{R:"manager-dept-users-"+z.value,S:e.o(ce),T:e.o(fe),U:e.o(ce),V:e.p({show:j.value,mode:"bottom",round:"20"}),W:e.o(de),X:W.value.length>0},W.value.length>0?{Y:e.t(W.value.length),Z:e.t(ae.value)}:{},{aa:e.f(_.value,((a,t,l)=>e.e({a:e.t(a.deptName),b:se(a)>0},se(a)>0?{c:e.t(se(a))}:{},{d:a.deptId,e:e.n({active:F.value===t}),f:e.o((e=>F.value=t),a.deptId)}))),ab:0===ee.value.length},0===ee.value.length?{}:{ac:e.f(ee.value,((a,t,l)=>({a:e.o((e=>function(e,a){const t=String(e);a?te.value.has(t)||(W.value=[...W.value,t]):W.value=W.value.filter((e=>String(e)!==t))}(a.userId,e)),"user-"+a.userId),b:"dcabdcae-10-"+l+",dcabdcae-9",c:e.p({usedAlone:!0,checked:te.value.has(String(a.userId)),label:J(a),activeColor:"#2667E9",shape:"square"}),d:"user-"+a.userId})))},{ad:"dept-users-"+F.value,ae:e.o(de),af:e.o(ge),ag:e.o(de),ah:e.p({show:q.value,mode:"bottom",round:"20"}),ai:e.o(e.unref(Se)),aj:e.o(e.unref(Ie)),ak:e.p({fileList:ye.value,name:"1",multiple:!0,imageMode:"aspectFill",maxCount:10}),al:!q.value&&!j.value&&!C.value},q.value||j.value||C.value?{}:{am:s.value,an:i.value,ao:s.value+"px",ap:i.value+"px"},{aq:c.value},c.value?{ar:e.o(h)}:{as:e.o(S)},{at:!c.value},c.value?{}:e.e({av:v.value},v.value?{aw:v.value,ax:e.o(w)}:{}),{ay:c.value&&!q.value&&!j.value&&!C.value},!c.value||q.value||j.value||C.value?{}:{az:e.sr(g,"dcabdcae-12",{k:"signatureRef"}),aA:e.o((a=>(async a=>{try{const{url:e}=await t.uploadToCloud(a);y(e),p.value&&await xe()}catch(l){p.value=!1,e.index.hideLoading(),console.error("签名上传失败:",l),e.index.showToast({title:"签名上传失败,请重试",icon:"none"})}})(a.tempFilePath))),aB:e.o((e=>m.value=!1)),aC:e.o((e=>m.value=!1)),aD:e.o((e=>m.value=!0)),aE:e.p({width:f.value,height:160,backgroundColor:"#f8f8f8",penColor:"#000000",lineWidth:3,enableHistory:!1})},{aF:e.t(o.value?"保存修改":"提交整改"),aG:e.o(Ce),aH:e.gei(a,"")})}},n=e._export_sfc(l,[["__scopeId","data-v-dcabdcae"]]);wx.createPage(n);