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