import{getRecords as x,deleteAttachment as E}from"./common-DEIQtiPI.js";import B from"./FileUploadDialog-D4tAA3a7.js";import{d as F,A as c,J as o,_ as I,P as u,O as _,K as N,B as O,H as P,r as h,c as i,g as s}from"./index-DOFLUERq.js";import{_ as V}from"./_plugin-vue_export-helper-DlAUqK2U.js";const A={class:"field-label"},D={key:0,class:"required"},R={class:"attach-container"},M={class:"file-link-wrap"},$=["title","href"],j={key:0,class:"actions"},q={key:0,class:"actions actions-left"},z={key:1,class:"field-value"},L=F({__name:"Attach",props:{df:{},record:{},canEdit:{type:Boolean},ctx:{}},setup(l){const e=l,p=h(!1),r=i(()=>String(e.record?.[e.df.fieldname]||"")),d=h(!1),b=i(()=>e.df.label_layout||"vertical"),y=i(()=>e.ctx?.entity&&(e.ctx?.entity.value??e.ctx?.entity)||e.record?.pagetype||""),m=i(()=>e.ctx?.id&&(e.ctx?.id.value??e.ctx?.id)||e.record?.name||""),g=i(()=>e.df?.fieldname||""),k=i(()=>{const t=r.value;if(!t)return"";try{const n=new URL(t,window.location.origin).pathname,f=n.substring(n.lastIndexOf("/")+1);return decodeURIComponent(f||t)}catch{const n=t.lastIndexOf("/");return n>=0?decodeURIComponent(t.substring(n+1)):t}});function w(){e.canEdit&&(d.value=!0)}async function U(){d.value=!1;try{const t=await x("File",[["attached_to_pagetype","=",y.value],["attached_to_name","=",m.value]],["file_url"],"creation desc",0,1);if(t.success&&t.data?.length){const a=t.data[0];e.record[g.value]=a.file_url;try{typeof e.ctx?.saveSilent=="function"?await e.ctx.saveSilent():typeof e.ctx?.save=="function"&&await e.ctx.save()}catch{}}}catch{}}async function C(){if(!e.canEdit)return;const t=e.df.fieldname,a=e.record[t];if(a)try{const n=e.ctx?.entity&&(e.ctx?.entity.value??e.ctx?.entity)||"",f=e.ctx?.id&&(e.ctx?.id.value??e.ctx?.id)||"";if(n&&f){const v=await x("File",[["attached_to_pagetype","=",n],["attached_to_name","=",f],["file_url","=",a]],["name"]);if(v.success&&v.data?.length)for(const S of v.data)await E(S.name)}}catch{}e.record[t]="";try{typeof e.ctx?.saveSilent=="function"?e.ctx.saveSilent():typeof e.ctx?.save=="function"&&e.ctx.save()}catch{}}return(t,a)=>(s(),c("div",{class:P(["field-wrapper",`layout-${b.value}`])},[o("label",A,[I(u(l.ctx.t(l.df.label||l.df.fieldname))+" ",1),l.df.reqd?(s(),c("span",D,"*")):_("",!0)]),o("div",R,[r.value?(s(),c("div",{key:0,class:"attach-line",onMouseenter:a[0]||(a[0]=n=>p.value=!0),onMouseleave:a[1]||(a[1]=n=>p.value=!1)},[a[3]||(a[3]=o("span",{class:"link-icon"},[o("i",{class:"fa-regular fa-file"})],-1)),o("span",M,[o("a",{class:"file-link",title:r.value,href:r.value,target:"_blank",rel:"noopener"},u(k.value||r.value),9,$)]),l.canEdit?(s(),c("div",j,[o("button",{class:"action",type:"button",onClick:C},u(l.ctx.t?l.ctx.t("Clear"):"Clear"),1)])):_("",!0)],32)):(s(),c(N,{key:1},[l.canEdit?(s(),c("div",q,[o("button",{class:"action",type:"button",onClick:w},u(l.ctx.t?l.ctx.t("Upload"):"Upload"),1)])):(s(),c("span",z,"—"))],64)),O(B,{show:d.value,"onUpdate:show":a[2]||(a[2]=n=>d.value=n),pagetype:y.value,pagename:m.value,pagefield:g.value,onUploaded:U},null,8,["show","pagetype","pagename","pagefield"])])],2))}}),T=V(L,[["__scopeId","data-v-c27fe432"]]),Q=Object.freeze(Object.defineProperty({__proto__:null,default:T},Symbol.toStringTag,{value:"Module"}));export{Q as _};
