<?  global $vis_language;

	function vis_show_subcategories($confi_item_details,$call) {
		for($i=0;$i<count($confi_item_details);$i++) { 
				$ci_type .='<option value="'.$confi_item_details[$i]['item_id'].'">'.$confi_item_details[$i]['name'].'</option>';
				if(isset($confi_item_details[$i]['subcat']) && $confi_item_details[$i]['subcat']!=NULL) { 
					$ci_type .= vis_show_subcategories($confi_item_details[$i]['subcat'],'sub_cat'); 
				}
			}
		return $ci_type;
	}
?>
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title"><? echo$vis_language['TXTATTACHCI']; ?></h4>
        </div>
        <div id="parent_list" class="modal-body">
            <div class="row">
                <div class="col-sm-12">
                <section id="test" class="scrollable wrapper">
                <div class="row m-b">
                <div class="col-md-12">
                <form method="post" role="form" class="form-horizontal" name="vis_attach_ci_<?= $operation['vis_release_id'];?>" id="vis_attach_ci_<?= $operation['vis_release_id'];?>">
				<div class="form-group">
                   <div class="col-sm-6">
                   <select class="form-control selectpicker" data-container="body" data-size="auto" name="vis_configuration_item" id="vis_configuration_item" <? if(count($confi_item_details) > 10) echo 'data-live-search="true"' ?> onchange="vis_apply_filter();">
						<option value="0"><?=$vis_language['TXTITILSELECTCITYPE']?></option>
						<?php 
						/*
						for($p=0;$p<count($confi_item_details);$p++){ 
							echo '<option value="'.$confi_item_details[$p]['item_id'].'">'.$confi_item_details[$p]['name'].'</option>';
						}*/
						
						echo vis_show_subcategories($confi_item_details,'');
							
						?>
                      </select>
                   </div>
                   <div class="col-sm-6">
                   <input type="text" id="search_language_variables" onkeyup="vis_apply_filter();" placeholder="<?= $vis_language['TXTSEARCH'];?>" class="form-control">
                   </div>
				   <div class="col-sm-3"></div>
                </div>
                <div class="form-group">
                    <div class="col-sm-12">
                        <div id="configuration_item">
                        	<div style="text-align:center;"><?= $vis_language['TXTITILSELECTCITYPEORITEM'];?></div>
                        </div>                        
                    </div>
                </div>
				<input type="hidden" name="vis_release_id" id="vis_release_id" value="<?= $operation['vis_release_id'];?>">
				<input type="hidden" name="vis_attached_id" id="vis_attached_id" value="<?= $operation['attached_id'];?>">
              </form>
              </div>
              </div>
              </section>
              
              </div>
            </div>
        </div>
        <div class="modal-footer">
			<a class="btn btn-default" data-dismiss="modal"><? echo $vis_language['TXTCANCEL']; ?></a>
            <a class="btn btn-primary" onclick="$(this).visExecution('submit_form',{'vis_module':'releases', 'vis_operation':'attach_release_ci', 'vis_attach_ci':'success', 'vis_form':'vis_attach_ci_<?= $operation['vis_release_id'];?>', 'ci_for':'<?= $operation['ci_for'];?>'});"><? echo $vis_language['TXTATTACH']; ?></a>
        </div>
    </div>
</div>
<script type="text/javascript">
	$('.selectpicker').selectpicker();
	
	function vis_apply_filter(){
		
		var filter = $("#search_language_variables").val();
		var config_item = $("#vis_configuration_item").val();
		var data = {'vis_configuration_id':config_item, 'vis_filter':''+filter+'', 'attached_id':'<?= $operation['attached_id'];?>','vis_department':'<?=$operation['vis_department']?>', 'vis_domain_id':'<?= $operation['vis_domain_id'];?>'};
		
		if(filter.length <= 2 && config_item=='0'){
			$("#configuration_item").html('<div style="text-align:center;"><?= $vis_language['TXTGLOBALSEARCHERROR'];?></div>');
		}
		else{
			$.ajax({
				url: 'index.php?vis_module=problem&vis_operation=change_cofiguration',
				data: data,
				type: "POST",
				dataType: "json",
				success: function(data){
					$("#configuration_item").html(data.list);
				},
				error: function(xhr, ajaxOptions, thrownError) {$("#vis_global_msg_div").visInterface("load_global_message", "Error: "+xhr.status+" - "+thrownError);}
			});    
		}
		
	}
</script>
