let waitSelectCounter=0;function getValueTypeParent(e){return e.is("select")?e.find("option:selected").val():"checkbox"===e.attr("type")&&e.prop("checked")?e.val():"radio"===e.attr("type")?e.find(":checked").val():e.is("input")||e.is("textarea")?e.val():""}function widgetSelectGetData(e,t){e.html("");let a={action:"select",activetab:e.closest("form").find('input[name="activetab"]').val(),field:e.attr("data-field"),fieldcode:e.attr("data-fieldcode"),fieldfilter:e.attr("data-fieldfilter"),fieldtitle:e.attr("data-fieldtitle"),required:"required"===e.attr("required")?1:0,source:e.attr("data-source"),term:getValueTypeParent(t)};$.ajax({method:"POST",url:window.location.href,data:a,dataType:"json",success:function(t){e.html(""),t.forEach((function(t){let a=t.key===e.attr("value")?"selected":"",l=null==t.key?"":t.key;e.append('<option value="'+l+'" '+a+">"+t.value+"</option>")})),e.change()},error:function(e,t,a){console.log("widgetSelectGetData AJAX ERROR"),console.log("status:",e.status),console.log("textStatus:",t),console.log("errorThrown:",a),console.log("responseText:",e.responseText),alert(e.status+" "+e.responseText)}})}$(document).ready((function(){$("select.select2").select2({width:"style",theme:"bootstrap-5"}),$(".parentSelect").each((function(){let e=$(this).attr("parent");if("undefined"===e||!1===e||""===e)return;let t=$(this),a=t.closest("form").find('[name="'+e+'"]');if(a.is("select")||["color","datetime-local","date","time"].includes(a.attr("type")))a.change((function(){widgetSelectGetData(t,a)}));else if("hidden"===a.attr("type")){let l=document.querySelector("[name='"+e+"']");l.addEventListener("change",(function(){widgetSelectGetData(t,a)}));let n=l.value;new MutationObserver((e=>{e.forEach((e=>{"attributes"===e.type&&"value"===e.attributeName&&l.value!==n&&(n=l.value,l.dispatchEvent(new Event("change")))}))})).observe(l,{attributes:!0})}else(a.is("input")||a.is("textarea"))&&a.keyup((async function(){waitSelectCounter++;let e=waitSelectCounter;if(await new Promise((e=>setTimeout(e,500))),e<waitSelectCounter)return!1;widgetSelectGetData(t,a)}));a.length>0&&widgetSelectGetData(t,a)}))}));