import{A as K,t as N}from"./index.Df8UJ10K.js";import{a as U,X as $,r as p,_ as J,f as P,W as Q,w as T,aj as k,J as q,o as d,K as V,c as h,R as W,a9 as X,b as z,Q as G,M as O,S as Y,P as Z,t as ee}from"./vue.D1BjjNmv.js";import{_ as le}from"./_plugin-vue_export-helper.DlAUqK2U.js";const te={class:"option-item"},ae={key:0,class:"empty-content"},oe={key:0,class:"no-data"},ne={key:1,class:"load-more-wrapper"},se={key:2,class:"no-more-data"},ce=U({__name:"index",props:{modelValue:{},tableConfig:{url:null,label:null,value:null,columns:[]},displayLabel:{}},emits:["update:modelValue","change"],setup(D,{emit:E}){const s=D,C=$(),I=E,{ui:M}=K(),v=M.formItem.injectFormItemContext(),B=p(),b=p(),l=p([]),f=p(!1),g=p(!1),y=p(""),u=J({page:1,limit:20,total:0}),o=P(()=>s.tableConfig),S=P(()=>l.value.length<u.total);let m=null;const R=async e=>{y.value=e,u.page=1;const a=s.modelValue?l.value.find(t=>t[o.value.value]===s.modelValue):null;l.value=[],a&&(l.value=[a]),await x(!1),a&&s.modelValue&&(l.value.find(c=>c[o.value.value]===s.modelValue)||(l.value=[a,...l.value]))},H=async()=>{g.value||!S.value||(u.page+=1,await x(!0))},x=async(e=!1)=>{const a=o.value.url,t={page:u.page,limit:u.limit,has_inventory:1};y.value&&(t.search=y.value);try{e?g.value=!0:f.value=!0;const c=await N({url:a,params:t});if(c.code===2e3){const r=c.data||[];if(u.total=c.total||0,e){const n=new Set(l.value.map(i=>i[o.value.value])),_=r.filter(i=>!n.has(i[o.value.value]));l.value=[...l.value,..._]}else{const n=s.modelValue?l.value.find(_=>_[o.value.value]===s.modelValue):null;l.value=r,n&&s.modelValue?l.value.find(i=>i[o.value.value]===s.modelValue)||(l.value=[n,...l.value]):s.modelValue&&!n&&(l.value.find(i=>i[o.value.value]===s.modelValue)||await w())}}}catch(c){console.error("获取设备列表失败:",c),e||(l.value=[])}finally{f.value=!1,g.value=!1}},w=async e=>{const a=e||s.modelValue;if(a){try{const t=await N({url:`${o.value.url}${a}/`,method:"get"});if(t.code===2e3&&t.data){const c=t.data;return l.value.find(n=>n[o.value.value]===c[o.value.value])||(l.value=[c,...l.value]),c}}catch(t){console.error("加载选中项失败:",t)}return null}},j=async e=>{e?(l.value.length===0&&(u.page=1,y.value="",await x(!1)),await ee(),A()):L()},A=()=>{if(!B.value)return;const e=document.querySelector(".deviceSelectorPopper .el-select-dropdown__wrap");e&&(m=a=>{const t=a.target,c=t.scrollTop,r=t.scrollHeight,n=t.clientHeight;r-c-n<20&&S.value&&!g.value&&!f.value&&H()},e.addEventListener("scroll",m))},L=()=>{if(m){const e=document.querySelector(".deviceSelectorPopper .el-select-dropdown__wrap");e&&m&&e.removeEventListener("scroll",m),m=null}};Q(()=>{L()});const F=async e=>{e&&(l.value.find(t=>t[o.value.value]===e)||await w(e)),I("update:modelValue",e),v==null||v.onChange(),v==null||v.onBlur(),I("change",e),C.onChange&&typeof C.onChange=="function"&&C.onChange(e)};return T(()=>s.modelValue,async e=>{b.value=e,e&&(l.value.find(t=>t[o.value.value]===e)||await w(e))},{immediate:!0}),T(()=>s.displayLabel,e=>{s.modelValue&&e&&l.value.length===0&&w()},{immediate:!0}),(e,a)=>{const t=k("el-option"),c=k("el-button"),r=k("el-select");return d(),q(r,{ref_key:"selectRef",ref:B,"popper-class":"deviceSelectorPopper",class:"deviceSelector",modelValue:b.value,"onUpdate:modelValue":a[0]||(a[0]=n=>b.value=n),placeholder:"请选择设备",onVisibleChange:j,clearable:"",filterable:"",remote:"","remote-method":R,loading:f.value,onChange:F},{empty:V(()=>[f.value?O("",!0):(d(),h("div",ae,[l.value.length===0?(d(),h("div",oe,"暂无数据")):S.value?(d(),h("div",ne,[Y(c,{link:"",type:"primary",onClick:H,loading:g.value},{default:V(()=>a[1]||(a[1]=[Z(" 加载更多 ")])),_:1},8,["loading"])])):(d(),h("div",se,"没有更多数据了"))]))]),default:V(()=>[(d(!0),h(W,null,X(l.value,n=>(d(),q(t,{key:n[o.value.value],label:n[o.value.label],value:n[o.value.value]},{default:V(()=>[z("div",te,G(n[o.value.label]),1)]),_:2},1032,["label","value"]))),128))]),_:1},8,["modelValue","loading"])}}}),de=le(ce,[["__scopeId","data-v-769660d3"]]);export{de as d};
