import{d as ne,c as $,r as c,z as x,o as se,A as le,e as r,f as C,s as oe,g as L,k as v,h as p,j as n,J as ie,w as I,C as j,F as ce,H as re,B as q,n as o,p as ue,a6 as P,t as m,q as K,K as A,a7 as H}from"./index-a0da86f0.js";import{I as F}from"./iconify-56cbf3ff.js";import{getRecords as de}from"./common-fa44b7a0.js";import{p as J}from"./slug-3db6ff8f.js";import{_ as fe}from"./_plugin-vue_export-helper-c27b6911.js";const ve={class:"field-label"},pe={key:0,class:"required"},_e={class:"input-wrapper"},me={key:0,class:"link-buttons"},he={key:0,class:"dropdown-menu"},ke={class:"dropdown-content"},ye=["onClick"],ge={key:0,class:"create-icon"},we={class:"item-label"},Ce={key:0,class:"empty-state"},be={key:1,class:"loading-state"},$e=ne({__name:"Link",props:{df:{},record:{},canEdit:{type:Boolean},ctx:{}},setup(N){const l=N,Q=$(()=>l.df.label_layout||"vertical"),u=c(""),h=c(""),k=c(""),B=c(!1),d=c(!1),y=c([]),b=c(!1),O=c({}),g=c({}),i=$(()=>l.df.options||""),S=$(()=>!!u.value),G=$(()=>l.df.max_options||20);x(()=>l.record[l.df.fieldname],async e=>{e!==u.value&&(u.value=e||"",e&&i.value?h.value=await W(i.value,e):h.value="")},{immediate:!0}),x(u,e=>{l.record[l.df.fieldname]!==e&&(l.record[l.df.fieldname]=e)});async function M(e){var t;if(!e)return{};if(O.value[e])return O.value[e];try{const s=((t=(await A.get(`/api/data/PageType/${encodeURIComponent(e)}`,{headers:H(),withCredentials:!0})).data)==null?void 0:t.data)||{};return O.value[e]=s,s}catch(a){return console.error("获取页面类型配置失败:",a),{}}}async function W(e,t){var s;if(!e||!t)return t;const a=`${e}_${t}`;if(g.value[a])return g.value[a];try{const f=await M(e),_=f.title_field||"name",D=f.show_title_field_in_link||!1;if(_==="name"||!D)return g.value[a]=t,t;const V=(((s=(await A.get(`/api/data/${encodeURIComponent(e)}/${encodeURIComponent(t)}`,{headers:H(),withCredentials:!0})).data)==null?void 0:s.data)||{})[_]||t;return g.value[a]=V,V}catch(f){return console.error("获取记录标题失败:",f),t}}function T(){return{label:`${m("Create")}${m(i.value)}`,value:"create_new",type:"create"}}async function R(e=""){if(i.value){b.value=!0;try{const a=(await M(i.value)).title_field||"name",s=["name"];a!=="name"&&s.push(a);const f=[];e.trim()&&(a!=="name"?f.push([a,"like",`%${e}%`]):f.push(["name","like",`%${e}%`]));const _=await de(i.value,f,s,"modified desc",0,G.value);if(_.success&&_.data){const D=_.data.map(w=>{const E=a==="name"?w.name:w[a]||w.name;return{label:E,value:w.name,title:E}});y.value=[...D,T()]}else y.value=[T()]}catch(t){console.error("搜索选项失败:",t),y.value=[T()]}finally{b.value=!1}}}function X(e){k.value=e,d.value&&R(e)}async function z(){l.canEdit&&(d.value||(await R(k.value),d.value=!0))}async function Y(e){if(e.type==="create"){const t=J(i.value),a=Math.random().toString(36).substring(2,12),s=`new-${t}-${a}`;window.open(`/app/${t}/${s}`,"_blank"),d.value=!1;return}u.value=e.value,h.value=e.title||e.label,k.value="",d.value=!1}function Z(){u.value="",h.value="",k.value="",d.value=!1}function ee(){if(!u.value||!i.value)return;const e=J(i.value);window.open(`/app/${e}/${u.value}`,"_blank")}function ae(){B.value=S.value}function te(){B.value=!1}function U(e){e.target.closest(".link-field-container")||(d.value=!1)}return se(()=>{document.addEventListener("click",U)}),le(()=>{document.removeEventListener("click",U)}),(e,t)=>(o(),r("div",{class:q(["field-wrapper",`layout-${Q.value}`])},[C("label",ve,[oe(L(e.ctx.t(e.df.label||e.df.fieldname))+" ",1),e.df.reqd?(o(),r("span",pe,"*")):v("",!0)]),C("div",{class:"link-field-container",onMouseenter:ae,onMouseleave:te},[C("div",_e,[p(n(ue),{value:S.value?h.value:k.value,placeholder:e.ctx.t(e.df.fieldname),disabled:!e.canEdit,"onUpdate:value":X,onClick:z,onFocus:z,class:"link-input"},null,8,["value","placeholder","disabled"]),B.value&&S.value?(o(),r("div",me,[e.canEdit?(o(),ie(n(K),{key:0,size:"small",quaternary:"",onClick:j(Z,["stop"]),title:n(m)("Clear"),class:"action-btn"},{icon:I(()=>[p(n(P),null,{default:I(()=>[p(n(F),{icon:"mdi:close"})]),_:1})]),_:1},8,["title"])):v("",!0),p(n(K),{size:"small",quaternary:"",onClick:j(ee,["stop"]),title:n(m)("Open Detail"),class:"action-btn"},{icon:I(()=>[p(n(P),null,{default:I(()=>[p(n(F),{icon:"mdi:arrow-right"})]),_:1})]),_:1},8,["title"])])):v("",!0)]),d.value?(o(),r("div",he,[C("div",ke,[(o(!0),r(ce,null,re(y.value,a=>(o(),r("div",{key:a.value,class:q(["dropdown-item",{"create-item":a.type==="create"}]),onClick:s=>Y(a)},[a.type==="create"?(o(),r("span",ge,[p(n(F),{icon:"mdi:plus"})])):v("",!0),C("span",we,L(a.label),1)],10,ye))),128)),y.value.length===0&&!b.value?(o(),r("div",Ce,L(n(m)("No Data")),1)):v("",!0),b.value?(o(),r("div",be,L(n(m)("Loading")),1)):v("",!0)])])):v("",!0)],32)],2))}});const Le=fe($e,[["__scopeId","data-v-1284a7de"]]),De=Object.freeze(Object.defineProperty({__proto__:null,default:Le},Symbol.toStringTag,{value:"Module"}));export{De as _};
