layui.define(['jquery','form'],function(j){var k='selectN';var $=layui.jquery;var l=layui.form;var m=function(h){this.selected=[];this.values=[];this.names=[];this.lastValue='';this.lastName='';this.isSelected=false;this.config={elem:'',data:[],num:3,pid:'pid',method:'get',url:'',selected:[],tips:'请选择',search:false,width:null,last:false,verify:'',filter:'',name:'',delimiter:',',field:['id','title'],status:'status',children:'children',formFilter:'selectN',};if(this.config.url){this.config.data=this.getData(url)}this.config=$.extend(this.config,h);this.setTips=function(){var o=this,c=o.config;if(Object.prototype.toString.call(c.tips)!='[object Array]'){return c.tips}else{var i=$(c.elem).find('select').length;return c.tips.hasOwnProperty(i)?c.tips[i]:'请选择'}};this.setSearch=function(){var o=this,c=o.config;if(Object.prototype.toString.call(c.search)!='[object Array]'){return c.search===true?'lay-search ':' '}else{var i=$(c.elem).find('select').length;if(c.search.hasOwnProperty(i)){return c.search[i]===true?'lay-search ':' '}}return' '};this.setWidth=function(){var o=this,c=o.config;if(Object.prototype.toString.call(c.width)!='[object Array]'){return/^\d+$/.test(c.width)?'style="width:'+c.width+'px;" ':' '}else{var i=$(c.elem).find('select').length;if(c.width.hasOwnProperty(i)){return/^\d+$/.test(c.width[i])?'style="width:'+c.width[i]+'px;" ':' '}}};this.createSelect=function(a){var o=this,c=o.config,f=c.field;var b='';b+='<div class="layui-input-inline" '+o.setWidth()+'>';b+=' <select '+o.setSearch()+'lay-filter="'+c.filter+'">';b+='  <option value="">'+o.setTips()+'</option>';for(var i=0;i<a.length;i++){var d=a[i][c.status]==0?'disabled="" ':'';b+='  <option '+d+'value="'+a[i][f[0]]+'">'+ (a[i][f[1]])+'</option>'}b+=' </select>';b+='</div>';return b};this.getOptionData=function(a,b,c){var f=this.config.field;var d=null;for(var i=0;i<b.length;i++){if('undefined'==typeof a[b[i]]){d=null;break}else if('undefined'==typeof a[b[i]][this.config.children]){d=null;break}else{d=a[b[i]][this.config.children]}}if(!d){d=this.getData(this.config.url+'?'+this.config.pid+'='+c)}return d};this.set=function(a){var o=this,c=o.config;$E=$(c.elem);var b=c.verify==''?'':'lay-verType="tips" lay-verify="'+c.verify+'" ';var d='<div style="height:0px;width:0px;overflow:hidden"><input '+b+'name="'+c.name+'"></div>';d+=o.createSelect(c.data);$E.html(d);a=typeof a=='undefined'?c.selected:a;a=a&&typeof a=='string'?c.selected.split(','):a;var e=[];for(var i=0;i<a.length;i++){$E.find('select:last').val(a[i]);var f=$E.find('select:last').get(0).selectedIndex-1;e.push(f);var g=o.getOptionData(c.data,e,a[i]);if(g&&g.length>0&&c.num>=2){var d=o.createSelect(g);$E.append(d)}}l.render('select',c.formFilter);o.getSelected()};this.change=function(a){var o=this,c=o.config;var b=a.parent();b.nextAll('div.layui-input-inline').remove();var d=[];b.parent().find('select').each(function(){d.push($(this).get(0).selectedIndex-1)});num=$(c.elem).find('select[lay-filter="'+c.name+'"]').length;var e=a.val();var f=o.getOptionData(c.data,d,e);if(f&&f.length>0&&num+1<=c.num){var g=o.createSelect(f);b.after(g);l.render('select',c.formFilter)}o.getSelected()};this.getSelected=function(){var o=this,c=o.config;var b=[];var d=[];var e=[];$E=$(c.elem);$E.find('select').each(function(){var a={};var v=$(this).val();var n=$(this).find('option:selected').text();a.value=v;a.name=n;b.push(v);d.push(n);e.push(a)});o.selected=e;o.values=b;o.names=d;o.lastValue=$E.find('select:last').val();o.lastName=$E.find('option:selected:last').text();o.isSelected=o.lastValue==''?false:true;var f=c.last===true?o.lastValue:o.values.join(c.delimiter);$E.find('input[name="'+c.name+'"]').val(f)};this.getData=function(b){var d;$.ajax({url:b,dataType:'json',async:false,success:function(a){d=a.data},error:function(){console.error(k+' hint：候选数据ajax请求错误 ');d=false}});return d}};m.prototype.render=function(){var o=this,c=o.config;$E=$(c.elem);if($E.length==0){console.error(k+' hint：找不到容器 '+c.elem);return false}o.config.field=typeof c.field==='string'?c.field.split(','):c.field;if(typeof c.data===''||(typeof c.data==='object'&&c.data.length==0)){var b=o.getData(c.url);if(b===false){console.error(k+' hint：缺少分类数据');return false}o.config.data=b}c.filter=c.filter==''?c.elem.replace('#','').replace('.',''):c.filter;c.name=c.name==''?c.elem.replace('#','').replace('.',''):c.name;o.config=c;o.set();l.on('select('+c.filter+')',function(a){o.change($(a.elem))});$E.find('input[name="'+c.name+'"]').focus(function(){var t=$(c.elem).offset().top;$('html,body').scrollTop(t-200);$(c.elem).find('select:last').addClass('layui-form-danger');setTimeout(function(){$(c.elem).find('select:last').removeClass('layui-form-danger')},3000)})};j(k,function(a){var b=new m(a);b.render();return b})});