<?php 


ce("droit")
->tpl(function(){
?>
<div> 
	<style type="text/css">
		.jstree .glyphicon.glyphicon-ok{
			color: green;
		}
		.jstree .glyphicon.glyphicon-remove{
			color: red;
		}
		.jstree .glyphicon.glyphicon-flag{
			color: blue;
		}
	</style>
	<paneltree 
		style="margin: -15px;"
		ng-model="ngModel" 
		current="current" 
		ng-init="btnsavetreetxt='Enregistrer';" 
		accept="accept" 
		width1="50%" 
		width2="50%" 
		jstree="jstree"
		min-height="300px"
		contextmenu = "false"
		dnd = "false"
		>
		<div class="row" ng-show="current && current.data.type=='droit'">
			<div class="col-xs-12">
				<h2 style="border-left: 5px solid #ade5ff; padding: 12px; padding-left: 12px; margin: 0;">{{current.text}}</h2>
				<hr>
			</div>
			<div class="col-xs-12">
				<div class="form-group avs">
					<label ng-hide="$parent.nom" style="display:block" class="control-label">>Droit de Ce utilisateur pour {{current.text}}</label>
					<label ng-show="$parent.nom" style="display:block" class="control-label">Droit de {{$parent.nom}} pour {{current.text}}</label>
					<select ng-change="$parent.updatedroit()" class="form-control avs" ng-model="current.data.value" ng-options="option for option in current.data.options"></select>
				</div>
			</div>
		</div>
		<div ng-hide="current || current.data.type!='droit'" style="display: flex;align-items: center;justify-content: center;height: 200px;color:#aaa">
			Aucun élément sélectionné
		</div>
	</paneltree>
</div>
<?php
})
->js(function(){
?>
<script>
	app.directive('droit', function () {
		return {
			restrict: 'EA',
			template:{{inject-tpl}},
			scope:{ngModel:"=",nom:"@?"},
			replace:true,
			controller:function($scope){
				$scope.initModule = function(module){
					var v = module.data.value;
					if (v && v.toLowerCase) {
						v = v.toLowerCase();
					}
					switch(v){
						case "oui":
						case "yes":
						case "ok":
						case "vrai":
						case "success":
						case "autoriser":
						case "autorisé":
						case "autorise":
							module.icon = "glyphicon glyphicon-ok";
						break;
						case "non":
						case "no":
						case "faut":
						case "echec":
						case "bloquer":
							module.icon = "glyphicon glyphicon-remove";
						break;
						case "demande":
							module.icon = "glyphicon glyphicon-flag";
						break;
						default:
							module.icon = "glyphicon glyphicon-file";
						break;
					}
					$scope.jstree.set_icon(module.id,module.icon);
				}
				$scope.initAll = function (list){
					if (list && list.length) {
						for (var i = 0; i < list.length; i++) {
							if (list[i].data.type == "droit") {
								$scope.initModule(list[i]);
							}
							$scope.initAll(list[i].children);
						}
					}
				}

				$scope.updatedroit = function(){
					if ($scope.current) {
						$scope.initModule($scope.current);
						$scope.accept();
					}
				}
			},
			link: function ($scope, iElement, iAttrs) {
				$scope.initAll($scope.ngModel);
				$scope.$watch("ngModel", function(){
					$scope.initAll($scope.ngModel);
				});
			}
		};
	});
</script>
<?php
});