<?php 




Route::isJs(function(){
	ob_start();
	?>
	<div style="position: relative;">
		<form ng-submit="$event.preventDefault();submit();" ng-class="{'loading':loading}">
			<div ng-show="loading" style="position: absolute;top: 0;right: 0;bottom: 0;left: 0;display: flex;align-items: center;justify-content: center;z-index: 1000;background: rgba(255,255,255,0.5);">
				<md-progress-circular md-mode="indeterminate" md-diameter="48"></md-progress-circular>
			</div>
			<ng-transclude></ng-transclude>
		</form>
	</div>
	<?php
	$code=ob_get_clean();
	?>
	<script>
		app.directive('wForm', [function () {
			return {
				restrict: 'E',
				transclude : {
					frmSuccess : "?wFormSuccess",
					frmError : "?wFormError"
				},
				replace : true,
				template : <?= json_encode(preg_replace("/\t|\n|\r/i", "", $code)) ?>,
				scope : {
					ngModel   : "=" ,
					api       : "@" ,
					onInit    : "&?",
					onReset   : "&?",
					onSuccess : "&?",
					onError   : "&?",
					onEnd     : "&?"
				},
				controller : function($scope){
					$scope.ngModel = $scope;
					$scope.loading = false;
					$scope.data = {};
					$scope.res = {};
					$scope.scopeName = "wForm";
					$scope.formSuccess = null;
					this.setFormSuccess = function($s){
						$scope.formSuccess = $s;
					}
					$scope.formError = null;
					this.setFormError = function($s){
						$scope.formError = $s;
					}
					$scope.reset = function(){
						$scope.data = {};
						$scope.res = {};
						if($scope.onReset) $scope.onReset();
					}
					$scope.submit = function(data){
						$scope.loading = true;
						api($scope.api,$scope.data,function(rep){
							$scope.loading = false;
							if(rep && rep.data){
								for(var x in rep.data){
									$scope.data[x] = rep.data[x];
								}
							}
							$scope.res = {};
							if(rep && rep.res){
								$scope.res = rep.res;
							}
							if (rep && (rep.ok || rep.success || rep.etat || rep.valid || rep === true)) {
								if($scope.onSuccess) $scope.onSuccess();
							}else{
								if($scope.onError) $scope.onError();
							}
							if($scope.onEnd) $scope.onEnd();
							$scope.$apply();
						});
					}
					setTimeout(function(){
						if($scope.onInit) $scope.onInit();
						if($scope.onReset) $scope.onReset();
						$scope.$apply();
					},200);
				},
				link: function ($scope, iElement, iAttrs) {

				}
			};
		}]);
		app.directive('wFormSuccess', [function () {
			return {
				restrict: 'E',
				require : '^wForm',
				transclude : true,
				replace : true,
				template : "<div><ng-transclude></ng-transclude></div>",
				scope : {hideform : "=?"},
				controller : function($scope){
					
				},
				link: function ($scope, iElement, iAttrs, wFormCtrl) {
					wFormCtrl.setFormSuccess($scope);
				}
			};
		}]);
		app.directive('wFormError', [function () {
			return {
				restrict: 'E',
				require : '^wForm',
				transclude : true,
				replace : true,
				template : "<div><ng-transclude></ng-transclude></div>",
				scope : {hideform : "=?"},
				controller : function($scope){
					
				},
				link: function ($scope, iElement, iAttrs, wFormCtrl) {
					wFormCtrl.setFormError($scope);
				}
			};
		}]);
	</script>
	<?php
});
