<?php 





ce("utilisateurs")
->tpl(function(){
?>
<div ng-cloak>
	<div class="tablepagination">
		<div ng-show="loading" class="loading">Chargement ...</div>
		<div class="filter">
			<div class="form-group avs" style="flex:2;margin:0 2px;">
				<label style="display:block" class="control-label">Recherche</label>
				<input type="text" class="form-control avs" ng-model="filter.q" ng-enter="actPage()" placeholder="Recherche">
			</div>
			<button class="btn btn-default" ng-click="actPage()"><span class="glyphicon glyphicon-search"></span></button>
			<button class="btn btn-primary" ng-click="add({titre:''})"> <span class="glyphicon glyphicon-plus"></span> Ajouter</button>
		</div>
		<div style="border: 1px solid #ccc;min-height: 300px;margin-bottom: 5px;background: #fff;overflow-x: auto;">
			<table class="table table-striped table-hover" style="margin: 0">
				<tr>
					<th style="width:30px;cursor: pointer;">
						<span ng-hide="checkedAll" ng-click="checkedAllFn()" class="glyphicon glyphicon-ok-circle"></span>
						<span ng-show="checkedAll" ng-click="checkedAllFn()" class="glyphicon glyphicon-remove-circle"></span>
					</th>
					<th style="width: 10px;">ID</th>
					<th>Nom</th>
					<th>Login</th>
					<th>Type</th>
					<th style="width: 10px;">Active</th>
					<th style="width: 10px;"></th>
				</tr>
				<tr ng-repeat="item in data" ng-class="{'success':item.$checked,'warning':item.$loading,'info':item.type == 'Administrateur'}" style="cursor: pointer;">
					<td ng-click="item.$check()" style="vertical-align: middle;">
						<span ng-show="item.$checked" class="glyphicon glyphicon-ok-circle"></span>
					</td>
					<td ng-click="item.$check()">{{item.id}}</td>
					<td ng-click="item.$check()">{{item.nom}}</td>
					<td ng-click="item.$check()">{{item.login}}</td>
					<td ng-click="item.$check()">{{item.type}}</td>
					<td style="width:10px;white-space:nowrap;text-align:right;">
						<?php if (Droit("modifier-utilisateur")->ok()): ?>
						<toggle ng-hide="item.type == 'Administrateur'" ng-model="item.etat" ng-change="item.$action('activation',value)"></toggle>
						<?php endif ?>
					</td>
					<td style="width:10px;white-space:nowrap;text-align:right;">
						<?php if (Droit("droit-utilisateur")->ok()): ?>
						<button ng-hide="item.type == 'Administrateur'" class="btn btn-xs" ng-click="item.$show('dialogdroit')"> <span class="glyphicon glyphicon-briefcase"></span> Droits</button>
						<?php endif ?>
						<?php if (Droit("modifier-utilisateur")->ok()): ?>
						<button class="btn btn-xs" ng-click="item.$edit()"><span class="glyphicon glyphicon-pencil"></span></button>
						<?php endif ?>
						<?php if (Droit("supprimer-utilisateur")->ok()): ?>
						<button class="btn btn-xs" ng-click="item.$delete()"><span class="glyphicon glyphicon-trash"></span></button>
						<?php endif ?>

					</td>
				</tr>
			</table>
		</div>
		<dialog ng-model="edit" titre="Utilisateurs">
			<div class="row">
				<div class="col-md-8">
					<div class="form-group avs">
						<label style="display:block" class="control-label">Nom</label>
						<input type="text" class="form-control avs" name="nom" ng-model="item.nom" placeholder="Nom">
					</div>
				</div>
				<div class="col-md-4">
					<div class="form-group avs">
						<label style="display:block" class="control-label">Type</label>
						<select class="form-control avs" name="type" ng-model="item.type">
							<option>Administrateur</option>
							<?php 
							$config = storge("config");
							foreach ($config["roles"] as $key => $value) {
								?>
							<option><?= $value["titre"] ?></option>
								<?php
							}
							?>
						</select>
					</div>
				</div>
				<div class="col-md-6">
					<div class="form-group avs">
						<label style="display:block" class="control-label">Login</label>
						<input type="text" class="form-control avs" name="login" ng-model="item.login" placeholder="Login">
					</div>
				</div>
				<div class="col-md-6">
					<div class="form-group avs">
						<label style="display:block" class="control-label">Mot de passe</label>
						<input type="text" class="form-control avs" name="password" ng-model="item.password" placeholder="Mot de passe">
					</div>
				</div>
			</div>
		</dialog>
		<?php if (Droit("supprimer-utilisateur")->ok()): ?>
		<button ng-hide="isselect" class="btn btn-default pull-right" ng-disabled="checkedList.length==0" ng-click="deleteAll()"><span class="glyphicon glyphicon-trash"></span> Supprimer</button>
		<?php endif ?>
		<!-- <button ng-hide="isselect" class="btn btn-default pull-right" ng-click="popup(print_url)" style="margin-right: 7px;"><span class="glyphicon glyphicon-print"></span> Imprimer</button> -->
		<pagination length="nbrPage" page="page" ng-page="goToPage(page)"></pagination>
		<div style="clear: both;"></div>
	</div>
		<dialog ng-model="dialogdroit" titre="Gestion des droits pour : {{item.nom}}" width="800px" btn-fermer="true" btn-valider="true" ng-valider="item.$save(fermer)">
			<droit ng-model="item.data" nom="{{item.nom}}"></droit>
		</dialog>



</div>
<?php
})
->body(function(){
?>
<script>
	app.directive('utilisateurs', function ($mdDialog) {
		return {
			restrict: 'EA',
			template:{{inject-tpl}},
			scope:{checkedList:"=?",isselect:"=?",refrech:"=?",multiSelect:"@?"},
			replace:true,
			transclude:true,
			controller:function($scope){
				$scope.scopename = "utilisateurs";
				$scope.filter = {qFilterBy : ["titre","id"]};
				// $scope.multiSelect = false; $scope.checkBy = "id"; $scope.checkedList, item.$check(true|false|) item.$checked == true | false
				// $scope.lists = "checked,favorie".split(",");
				$scope.checkBy = "id";
				$scope.multiSelect = false;
				$scope.goToPage = function(page){
					var obj = {page:page};
					//-----------
					obj.filter = $scope.filter;
					//-----------
					$scope.apiData("getdataapiutilisateurs",obj);
				}
				$scope.checkedAllFn = function(){
					$scope.checkedAll = !$scope.checkedAll;
					for (var i = 0; i < $scope.data.length; i++) {
						$scope.data[i].$check($scope.checkedAll); 
					}
				}
				$scope.deleteAll = function(){
					if(!confirm( "Voulez vous vraiment supprimer "+$scope.checkedList.length+" enregistrements ?")) return;
					$scope.action('deleteall',$scope.checkedList,function(){
						$scope.checkedList = [];
						$scope.actPage();
					});
				}
				$scope.droit = <?= Droit::json() ?>;

				initPaginateTable($scope,null,$mdDialog);
			},
			compile: function(element, attrs){
				if(attrs.multiSelect == "true") attrs.multiSelect = true;
				if(attrs.multiSelect == "false") attrs.multiSelect = false;
				if (!attrs.multiSelect)  attrs.multiSelect = false; 
			},
			link: function (scope, iElement, iAttrs) {
				
			}
		};
	});
</script>
<?php
});


ce("utilisateursSelect")
->tpl(function(){
?>
<div>
	<div ng-hide="multi">
		<select-elm2 id="itemId" nom="itemName"  dialog="dialog_elm"
			col-id="id"
			col-nom="titre"
		></select-elm2>
		<dialog ng-model="dialog_elm" titre="Utilisateurs">
			<utilisateurs checked-list="dialog_elm.selected" isselect="true"></utilisateurs>
		</dialog>
	</div>
	<div ng-show="multi">
		<table class="table table-bordered" style="background:#efefef;width:100%">
			<tr>
				<th>Utilisateurs</th>
				<th>Note</th>
				<th style="width:10px;"></th>
			</tr>
			<tr ng-repeat="elm in ngModel">
				<td style="padding:2px;">
					<select-elm2 id="elm.id" nom="elm.titre"  dialog="dialogs[elm.$key]"
						col-id="id"
						col-nom="titre"
					></select-elm2>
					<dialog ng-model="dialogs[elm.$key]" titre="Utilisateurs">
						<utilisateurs checked-list="dialogs[elm.$key].selected" isselect="true"></utilisateurs>
					</dialog>
				</td>
				<td style="padding:2px;"><input style="height:34px;" type="text" class="form-control" ng-model="elm.note" placeholder="Note"></td>
				<td style="padding:2px;"><button style="height:34px;" class="btn btn-sm" ng-click="ngModel.splice($index,1)"><span class="glyphicon glyphicon-trash"></span></button></td>
			</tr>
			<tr>
				<td colspan="3">
					<center>
						<button class="btn btn-xs btn-primary" ng-click="ngModel=ngModel||[];ngModel.push({$key:getNewKey()})">Ajouter</button>
					</center>
				</td>
			</tr>
		</table>
	</div>
</div>
<?php
})
->body(function(){
?>
<script>
	app.directive('utilisateursSelect', function () {
		return {
			restrict: 'EA',
			template:{{inject-tpl}},
			scope:{ngModel:"=?",itemId:"=?",itemName:"=?",multi:"=?"},
			replace:true,
			transclude:true,
			controller:function($scope){
				$scope.dialogs = [];
				$scope.getNewKey = function(){
					return uniqid();
				}
			},
			link: function (scope, iElement, iAttrs) {
				
			}
		};
	});
</script>
<?php
});








