import{d as B,aa as E,g as d,A as m,J as g,H as R,B as p,j as v,O as y,a8 as A,t as n,r as M,c as u}from"./index-DOFLUERq.js";import{getRecords as _,updateRecord as w,deleteAttachment as x}from"./common-DEIQtiPI.js";import{I as k}from"./iconify-p99pbjf9.js";import D from"./FileUploadDialog-D4tAA3a7.js";import{_ as S}from"./_plugin-vue_export-helper-DlAUqK2U.js";const V={key:0,class:"media-section"},$={class:"image-grid"},j=["src","alt"],z={key:2,class:"image-overlay"},H=["title"],J=B({__name:"ImageSection",props:{metaFields:{},record:{},pageMeta:{},canEdit:{type:Boolean},entity:{},id:{}},emits:["open-gallery","refresh"],setup(l,{emit:I}){const e=l,h=I,s=E(),r=M(!1),i=u(()=>e.pageMeta?.image_field),F=u(()=>{const a=i.value;return a?.trim()?e.metaFields.some(t=>t.fieldname===a):!1}),c=u(()=>{const a=i.value;return a?String(e.record[a]||"").trim():""}),f=u(()=>!!c.value);function U(){e.canEdit&&(r.value=!0)}async function C(){r.value=!1;const a=i.value;if(a)try{const t=await _("File",[["attached_to_pagetype","=",e.entity||""],["attached_to_name","=",e.id||""]],["file_url"],"creation desc",0,1);if(t.success&&t.data?.length){const o=t.data[0];e.record[a]=o.file_url,await w(e.entity||"",e.id||"",{[a]:o.file_url}),h("refresh"),s.success(n("Image uploaded successfully"))}}catch(t){s.error(t.message||n("Upload failed"))}}async function N(){const a=i.value;if(!a||!e.entity||!e.id)return;const t=c.value;try{if(s.loading(n("Removing..."),{duration:0}),t){const o=await _("File",[["attached_to_pagetype","=",e.entity],["attached_to_name","=",e.id],["file_url","=",t]],["name"]);if(o.success&&o.data?.length)for(const b of o.data)await x(b.name)}e.record[a]="",await w(e.entity||"",e.id||"",{[a]:""}),s.destroyAll(),h("refresh")}catch(o){s.destroyAll(),s.error(o.message||n("Remove failed"))}}return(a,t)=>F.value?(d(),m("div",V,[g("div",$,[g("div",{class:R(["image-card",{"has-image":f.value}])},[f.value?(d(),m("img",{key:0,src:c.value,alt:i.value,onClick:t[0]||(t[0]=o=>a.$emit("open-gallery",c.value))},null,8,j)):l.canEdit?(d(),m("div",{key:1,class:"upload-placeholder",onClick:U},[p(v(k),{icon:"tabler:upload",class:"upload-icon"})])):y("",!0),l.canEdit&&f.value?(d(),m("div",z,[g("button",{class:"delete-btn",title:v(n)("Remove"),onClick:A(N,["stop"])},[p(v(k),{icon:"tabler:x"})],8,H)])):y("",!0)],2)]),p(D,{show:r.value,"onUpdate:show":t[1]||(t[1]=o=>r.value=o),pagetype:l.entity,pagename:l.id,pagefield:i.value,onUploaded:C},null,8,["show","pagetype","pagename","pagefield"])])):y("",!0)}}),P=S(J,[["__scopeId","data-v-a3ef3bf8"]]);export{P as default};
