import{M as f,c as r,e as l,l as w,v as m,i as d,f as c,F as y,p as g,k as p,j as n,q as o}from"./vue-Bqg3cjzV.js";import{_ as v}from"./index-CbGDU7fj.js";const K={name:"ZohoSearch",emits:["selected","update:modelValue"],props:{modelValue:Number,url:{type:String,required:!1},label:{type:String,required:!0},placeholder:{type:String,required:!0},loadDefaults:{type:Boolean,default:!1,required:!1},records:{type:Array,default:function(){return[]}},primaryKey:{type:String,default:"id"},fetchFlag:{type:String,default:"AUTOCOMPLETE_REQUEST"},defaultRow:{type:Object,default:function(){return{}}}},data:()=>({rows:[],searchKey:null,selectedRow:{},searchActive:!1}),computed:{...f({requestFlag:e=>e.requestFlag,isLoading:e=>e.isLoading}),isRequesting(){return this.isLoading&&this.requestFlag===this.fetchFlag},filteredRows(){let e=this.rows,t=this.searchKey&&this.searchKey.toLowerCase();return t&&(e=e.filter(s=>String(s[this.label]).toLowerCase().indexOf(t)>-1),e.length||t.trim().length>2&&(this.$store.commit("SET_AJAX_TASK",this.fetchFlag),(async s=>{let h=await this.$http.get(`${this.url}?query=${s}`);e=h.data.rows||h.data||[]})(t))),e}},created(){this.records.length&&(this.rows=[...this.records]),Object.keys(this.defaultRow).length&&(this.searchKey=this.defaultRow[this.label],this.selectedRow=this.defaultRow)},watch:{defaultRow(){Object.keys(this.defaultRow).length?(this.searchKey=this.defaultRow[this.label],this.selectedRow=this.defaultRow):(this.searchKey=null,this.selectedRow={})},modelValue(){if(!this.modelValue)this.searchKey=null,this.selectedRow={};else{let e=this.rows.find(t=>t[this.primaryKey]==this.modelValue);e&&(this.searchKey=e[this.label],this.selectedRow=e)}}},mounted(){this.loadDefaults&&this.getDefaultData()},methods:{handleFocusIn(e){e.target.nodeName=="BUTTON"?e.target.click():e.target.nodeName=="INPUT"&&(this.searchKey=null)},handleFocusOut(e){(e.target.nodeName=="INPUT"||e.target.classList.contains("search-item"))&&setTimeout(()=>{this.searchActive=!1},1)},getDefaultData(e=null){let t=e||this.url;this.$store.commit("SET_AJAX_TASK",this.fetchFlag),this.$http.get(t).then(s=>{s.data.rows?this.rows=[...this.rows,...s.data.rows]:this.rows=[...this.rows,...s.data]})},handleSearch(e){e.trim().length>0&&(this.$store.commit("SET_AJAX_TASK",this.fetchFlag),this.$http.get(`${this.url}?query=${e}`).then(t=>{t.data.rows?this.rows=t.data.rows:this.rows=t.data||[]}))},handleChange(e){this.searchKey=e[this.label],e[this.primaryKey]!=this.selectedRow[this.primaryKey]&&(this.selectedRow=e,this.$emit("selected",e),this.$emit("update:modelValue",e[this.primaryKey])),this.searchActive=!1},clearAll(){this.selectedRow={},this.rows=[]}}},R={class:"quick-search"},b={class:"dropdown show ac-dropdown ember-view"},F={class:"input-group btn-group"},A={class:"dropdown ember-view input-group-prepend w-100"},S={class:"auto-select ac-selected w-100","data-bs-toggle":"dropdown","data-bs-target":"#search-dropdown"},_=["placeholder"],T=["onClick"],k={key:0,version:"1.1",xmlns:"http://www.w3.org/2000/svg",x:"0",y:"0",viewBox:"0 0 512 512","xml:space":"preserve",class:"icon icon-ok icon-xs"},q={key:0,class:"py-2"};function N(e,t,s,h,O,i){return o(),r("div",{class:"search-field ember-view",onFocusout:t[2]||(t[2]=(...a)=>i.handleFocusOut&&i.handleFocusOut(...a)),onFocusin:t[3]||(t[3]=(...a)=>i.handleFocusIn&&i.handleFocusIn(...a))},[l("div",R,[l("div",b,[l("div",F,[l("div",A,[l("div",S,[w(l("input",{placeholder:s.placeholder,class:d(["form-control cursor-pointer search-key-input",{"rounded-bottom-0":e.searchActive,"is-loading":i.isRequesting}]),type:"text",onFocus:t[0]||(t[0]=a=>e.searchActive=!0),"onUpdate:modelValue":t[1]||(t[1]=a=>e.searchKey=a)},null,42,_),[[m,e.searchKey]])]),l("div",{class:d(["dropdown-menu ember-view scrollmenu w-100",{show:e.searchActive}])},[(o(!0),r(y,null,g(i.filteredRows,(a,u)=>(o(),r("button",{class:d(["dropdown-item search-item pl-7",{active:e.selectedRow[s.primaryKey]==a[s.primaryKey]}]),type:"button",onClick:V=>i.handleChange(a),key:"item"+u},[e.selectedRow[s.primaryKey]==a[s.primaryKey]?(o(),r("svg",k,[...t[4]||(t[4]=[l("path",{d:"M503.4 141.1c-5.6-5.6-12.9-8.6-20.8-8.6s-15.3 3.1-20.8 8.6L162.1 440.7 50.2 328.8c-11.5-11.5-30.2-11.5-41.6 0-11.5 11.5-11.5 30.2 0 41.6l132.8 132.9c5.6 5.5 13 8.5 20.8 8.5 7.9 0 15.5-3.2 20.9-8.7l320.4-320.4c11.4-11.5 11.4-30.2-.1-41.6z"},null,-1)])])):c("",!0),p(" "+n(a[s.label]),1)],10,T))),128)),!i.isRequesting&&!e.rows.length?(o(),r("span",q,n(e.$trans("No data to display")),1)):c("",!0)],2)])])])])],32)}const D=v(K,[["render",N],["__scopeId","data-v-2b3fb960"]]);export{D as Z};
