import{d as ne,w as T,o as le,f as se,A as d,J as g,_ as oe,P as x,O as _,i as D,j as l,t as o,am as z,C as L,a8 as R,an as U,B as y,K as ie,L as ce,H as N,c as $,r as v,g as i,ax as K,aI as V,a as A}from"./index-DOFLUERq.js";import{I as F}from"./iconify-p99pbjf9.js";import{getPageTypeMeta as ue}from"./common-DEIQtiPI.js";import{_ as re}from"./_plugin-vue_export-helper-DlAUqK2U.js";const de={class:"field-label"},ve={key:0,class:"required"},fe={class:"input-wrapper"},pe={key:2,class:"link-buttons"},_e={key:0,class:"dropdown-menu"},he={class:"dropdown-content"},me=["onClick"],ye={key:0,class:"create-icon"},ke={class:"item-label"},ge={key:0,class:"empty-state"},be={key:1,class:"loading-state"},we=ne({__name:"DynamicLink",props:{df:{},record:{},canEdit:{type:Boolean},ctx:{}},setup(p){const n=p,H=$(()=>n.df.label_layout||"vertical"),s=$(()=>{const e=n.df.options;return e&&n.record[e]||""}),c=v(""),h=v(""),m=v(""),I=v(!1),r=v(!1),k=v([]),b=v(!1),B=v({}),w=v({}),O=$(()=>!!c.value),J=$(()=>n.df.max_options||20);T(()=>n.record[n.df.fieldname],async e=>{e!==c.value&&(c.value=e||"",e&&s.value?h.value=await G(s.value,e):h.value="")},{immediate:!0}),T(c,(e,a)=>{n.record[n.df.fieldname]!==e&&(n.record[n.df.fieldname]=e,n.ctx?.onFieldChange&&e!==a&&n.ctx.onFieldChange(n.df.fieldname,e,a,n.record))}),T(s,(e,a)=>{e!==a&&c.value&&(c.value="",h.value="",m.value="",r.value=!1)});async function Q(e){if(!e)return{};if(B.value[e])return B.value[e];try{const a=await ue(e),t=a.success&&a.data?a.data:{};return B.value[e]=t,t}catch{return{}}}async function G(e,a){if(!e||!a)return a;const t=`${e}_${a}`;if(w.value[t])return w.value[t];try{const f=await Q(e),u=f.title_field||"name",C=f.show_title_field_in_link||!1;if(u==="name"||!C){const q=o(a);return w.value[t]=q,q}const te=((await A.get(`/api/data/${encodeURIComponent(e)}/${encodeURIComponent(a)}`)).data?.data||{})[u]||a,j=o(te);return w.value[t]=j,j}catch{return o(a)}}function S(){return{label:`${o("Create")}${o(s.value)}`,value:"create_new",type:"create"}}async function E(e=""){if(s.value){b.value=!0;try{const a=await A.post("/api/action/jingrow.desk.search.search_link",{pagetype:s.value,txt:e||"",page_length:J.value});if(a.data&&a.data.message){const f=a.data.message.filter(u=>u.value&&u.value!=="create_new__link_option"&&u.value!=="advanced_search__link_option").map(u=>{const C=u.label||u.value;return{label:o(C),value:u.value,title:o(C)}});k.value=[...f,S()]}else k.value=[S()]}catch{k.value=[S()]}finally{b.value=!1}}}function W(e){m.value=e,r.value&&E(e)}async function M(){n.canEdit&&s.value&&(r.value||(await E(m.value),r.value=!0))}async function X(e){if(e.type==="create"){const a=V(s.value),t=Math.random().toString(36).substring(2,12),f=`new-${a}-${t}`;window.open(`/app/${a}/${f}`,"_blank"),r.value=!1;return}c.value=e.value,h.value=e.title||e.label,m.value="",r.value=!1}function Y(){c.value="",h.value="",m.value="",r.value=!1}function Z(){if(!c.value||!s.value)return;const e=V(s.value);window.open(`/app/${e}/${c.value}`,"_blank")}function ee(){I.value=O.value}function ae(){I.value=!1}function P(e){e.target.closest(".link-field-container")||(r.value=!1)}return le(()=>{document.addEventListener("click",P)}),se(()=>{document.removeEventListener("click",P)}),(e,a)=>(i(),d("div",{class:N(["field-wrapper",`layout-${H.value}`])},[g("label",de,[oe(x(p.ctx.t(p.df.label||p.df.fieldname))+" ",1),p.df.reqd?(i(),d("span",ve,"*")):_("",!0)]),g("div",{class:"link-field-container",onMouseenter:ee,onMouseleave:ae},[g("div",fe,[s.value?(i(),D(l(z),{key:1,value:O.value?h.value:m.value,placeholder:"",disabled:!p.canEdit,"onUpdate:value":W,onClick:M,onFocus:M,class:"link-input"},null,8,["value","disabled"])):(i(),D(l(z),{key:0,value:c.value,placeholder:l(o)("Select a PageType first"),disabled:"",class:"link-input"},null,8,["value","placeholder"])),I.value&&O.value&&s.value?(i(),d("div",pe,[p.canEdit?(i(),D(l(U),{key:0,size:"small",quaternary:"",onClick:R(Y,["stop"]),title:l(o)("Clear"),class:"action-btn"},{icon:L(()=>[y(l(K),null,{default:L(()=>[y(l(F),{icon:"mdi:close"})]),_:1})]),_:1},8,["title"])):_("",!0),y(l(U),{size:"small",quaternary:"",onClick:R(Z,["stop"]),title:l(o)("Open Detail"),class:"action-btn"},{icon:L(()=>[y(l(K),null,{default:L(()=>[y(l(F),{icon:"mdi:arrow-right"})]),_:1})]),_:1},8,["title"])])):_("",!0)]),r.value?(i(),d("div",_e,[g("div",he,[(i(!0),d(ie,null,ce(k.value,t=>(i(),d("div",{key:t.value,class:N(["dropdown-item",{"create-item":t.type==="create"}]),onClick:f=>X(t)},[t.type==="create"?(i(),d("span",ye,[y(l(F),{icon:"mdi:plus"})])):_("",!0),g("span",ke,x(t.label),1)],10,me))),128)),k.value.length===0&&!b.value?(i(),d("div",ge,x(l(o)("No Data")),1)):_("",!0),b.value?(i(),d("div",be,x(l(o)("Loading")),1)):_("",!0)])])):_("",!0)],32)],2))}}),Ce=re(we,[["__scopeId","data-v-494aefeb"]]),Se=Object.freeze(Object.defineProperty({__proto__:null,default:Ce},Symbol.toStringTag,{value:"Module"}));export{Se as _};
