
<div class="wrapper wrapper-content animated fadeInRight">
	<div class="row">
		<div class="col-lg-12">
			<div class="ibox float-e-margins ">
				<div class="ibox-title">
					<a type="button" class="btn btn-xs btn-skin btn-primary pull-right" href="<?=$this->agent->referrer();?>">Back</a> 
					<h5 id="sub_title"></h5>
					<div class="ibox-tools">
					</div>
				</div>
				<div class="ibox-content">
					<div class="row" id="menu_privilege">
						<?= $this->menu->get_menu_privilege(0,0,$id)?>
					</div>
					<hr>
					<button class="btn btn-primary" type="button" onClick="privilegeSave()"><i class="fa fa-check"></i>&nbsp;Save</button>
				</div>
			</div>
		</div>
	</div>
</div>
<script>
	$(document).ready(function(){
        $('#menu_privilege').jstree({
			"checkbox" : {
			  "keep_selected_style" : false,
			  "three_state": true
			},
            'core' : {
                'check_callback' : true
            },
            'plugins' : [ 'types', 'dnd','checkbox' ],
            'types' : {
                'default' : {
                    'icon' : 'fa fa-folder'
                },
                'html' : {
                    'icon' : 'fa fa-file-code-o'
                },
                'svg' : {
                    'icon' : 'fa fa-file-picture-o'
                },
                'css' : {
                    'icon' : 'fa fa-file-code-o'
                },
                'img' : {
                    'icon' : 'fa fa-file-image-o'
                },
                'js' : {
                    'icon' : 'fa fa-file-text-o'
                }

            }
        });
		var nodes = $("#menu_privilege").jstree(true).get_selected("full", true);
		$.each(nodes,function(i, node){
		  if(node.children.length >0){
			// console.log(node,"not leaf");
		  }
		  else{
			console.log(node,"leaf");
			// $('#menu_privilege').jstree().create_node(node.id, {
			  // "id": node.id+"p3"+i,
			  // "text": "Parent-3"
			// }, "last", function() {
			// $('#menu_privilege').jstree().close_node({"id":node.id});
			// });
		  }  
		});
	});
	function privilegeSave()
	{
		var selectedMenu = [];
		var selectedMenuParent = [];
		var selectedElms = $('#menu_privilege').jstree('get_selected', true);
		var selectedElmsParent = $('#menu_privilege').jstree('get_top_selected', true);
		$.each(selectedElms, function() {
			selectedMenu.push(this.id);
		});
		$.each(selectedElms, function() {
			selectedMenuParent.push(this.parents);
		});
		$.each(selectedElmsParent, function() {
			selectedMenu.push(this.id);
		});
		$.ajax({
			type: "POST",
			url: site_url+"settings/privilege/save/",
			data:  { 
			idOperatorType : '<?=$id?>'
			,selectedMenu:selectedMenu,
			selectedMenuParent:selectedMenuParent},
			dataType: 'json',
			success: function(response)
			{
				swal(response['header']||'success', response['message']||'', "success").then(() => {
					window.location = site_url + "settings/privilege/input/" + response['idOperatorType'];
				});
			},
			error: function( jqXhr, textStatus, errorThrown ){
			swal('error', jqXhr['responseJSON']['message']||'', "error");
			}
	   });
	}
</script>