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