layui.use(['table','jquery','form'],function(){
	var $=layui.jquery;
	var table=layui.table;
	var formx=layui.form;
	var getWorkCenters=function(depid,is_producing_line){
		$.get('admin.php?cmd=getWorkCenters&depid='+depid+'&is_producing_line='+is_producing_line,function(data){
			var html='';
			
			if(is_producing_line==1){

				for(var y in data.producingLines){
					let department=data.producingLines[y];
					html+='<div class="layui-row" style="display:flex;flex-wrap:wrap;justify-content:flex-start"> ';
					html+='<div class="layui-col-md2" style="display:flex;justify-content:flex-end">';
					html+='<input type="checkbox" name="producing_line['+department['DepId']+'][checked]" value="1"  title="'+department['DepName']+'"   /></div>';
					html+='<div class="layui-col-md2">';
					html+='<select name="producing_line['+department['DepId']+'][CenterId]" >';
						for( var z in data.workcenters){
							let workcenter=data.workcenters[z];
							html+='<option value="'+workcenter['CenterId']+'"  >'+workcenter['CenterName']+'</option>';					
						}
					html+='</select>';
					html+='</div>';
					html+='</div>';
				
				}
			
			}else{
				
				for(var x in data.workcenters){
					html+='<input type="checkbox" name="workcenter[]" value="'+data.workcenters[x]['CenterId']+'"  title="'+data.workcenters[x]['CenterName']+'" '+(data.workcenters[x]['checked']==true?'checked':'')+'   />';
				}

				
			}			

				$('#workcenters').html(html);		
				formx.render('checkbox');
				formx.render('select');
				
		},'json')		

	}
	formx.on('select(producing_department)',function(data){
		let is_producing_line=$('[name="is_producing_line"]').val();
		getWorkCenters(data.value,is_producing_line);
	});
	
	formx.on('select(is_producing_line)',function(data){	
		
		let depid=$('[name="producing_department"]').val();
		getWorkCenters(depid,data.value);	
	});

})