<?php 

app::api("uploadimgtmp",function($data){
	include_once __DIR__."/imagine.phar";
	$imgd = explode(",", $data["image"]);
	$imagine = new Imagine\Gd\Imagine();
	$img = $imagine->load(base64_decode($imgd[1]));
	if(!empty($data["ext"])){
		$ext = $data["ext"];
	}else{
		$type = explode(";", $imgd[0]);
		$type = explode(":", $type[0]);
		$type = explode("/", $type[1]);
		$ext = $type[1];
	}
	$dist = "tmp";
	if (!empty($data["dist"])) {
		$dist = app::slug($data["dist"]);
	}
	$filename = "public/images/$dist/".uniqid().".$ext";
	if (!empty($data["size"])) {
		$s = explode("-", $data["size"]);
		$s1 = explode("x", $s[0]);
		if (count($s1)==2) {
			if (is_numeric($s1[0]) && is_numeric($s1[1])) {
			  $size = new Imagine\Image\Box(floatval($s1[0]),floatval($s1[1]));
			  if(count($s)==1) $s[] = "inset";
			  $img = $img->thumbnail($size,$s[1]);
			}
		}
	}else{
		$size = new Imagine\Image\Box(1024,800);
		$img = $img->thumbnail($size,"inset");
	}
	if (!empty($data["titre"])) {
	  $dist = "public/images/$dist/";
	  $newfilename = $dist.app::Slug($data["titre"])."¤.".$ext;
	  $exe = true;
	  $i = 0;
	  while ($exe) {
	    $ff = $i==0?str_replace("¤", "", $newfilename):str_replace("¤", "-$i", $newfilename);
	    if (file_exists($ff) && $i<10000) {
	      $i += 1;
	    }else{
	      $exe = false;
	    }
	  }
	  $filename = $ff;
	}
	mkpath(dirname($filename));
	$img->save($filename,array('jpeg_quality' => 100,'png_compression_level' => 0));
	return ["src"=>"/$filename"];
});


Route::isCss(function(){
?>
<style>
.imgsce{
	display: inline-block;
	position: relative;
	/*width: 120px;
	height: 120px;*/
	cursor: pointer;
	text-align: center;
	border:1px solid #ccc;
	width: 100%;
	vertical-align: bottom;
}
.imgsce img{
	display: inline-block;
	/*margin:3px;*/
    vertical-align: top;
}
.imgselected {
	margin-right: 5px;
	margin-bottom: 5px;
}
.imgsceloading{
	position: absolute;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	z-index: 100000000;
	display: flex;
	justify-content: center;
	align-items: center;
	flex-direction: column;
	background: rgba(0,0,0,0.5);
	overflow: hidden;
}
.imgsce .del{
	position: absolute;
	top: 3px;
	right: 3px;

}
.progressbarimg{
	position: absolute;
	bottom: 0;
	left: 0;
	height: 5px;
	background: green;
	z-index: 1000;
	-webkit-transition: all 200ms ease-in-out;
	-moz-transition: all 200ms ease-in-out;
	-ms-transition: all 200ms ease-in-out;
	-o-transition: all 200ms ease-in-out;
	transition: all 200ms ease-in-out;
	-webkit-box-shadow: 0 -5px 10px 0 #30FF45;
	box-shadow: 0 -5px 10px 0 #30FF45;
}
</style>
<?php
});
Route::isJs(function(){
ce("imgs")
->tpl(function(){
?>
<span style="display:inline-block;">
	<span class="imgsce" ng-click="choseImage()" ng-if="max<=1" style="height:{{height}};width:{{width}};{{style}}">
		<span class="imgsceloading" ng-show="loading">
			<div style="color: #fff;"> <span class="glyphicon glyphicon-upload"></span> {{progress_value}} %</div>
			<div data-loader="ball-rotate"></div>
			<div class="progressbarimg" style="right: {{100 - progress_value}}%"></div>
		</span>
		<img style="height:100%;width:100%;" ng-show="!loading && src && src !=''" ng-src="{{src}}">
		<img style="height:100%;width:100%;" ng-show="loading" ng-src="{{elm.src}}">
		<button type="button" ng-click="clear()" ng-show="src && src !='' && !loading" class="btn btn-xs del"><span class="glyphicon glyphicon-trash"></span></button>
		<img style="max-height:100%;max-width:100%;" ng-hide="loading || (src && src !='')" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAIGNIUk0AAHolAACAgwAA+f8AAIDpAAB1MAAA6mAAADqYAAAXb5JfxUYAAAr8SURBVHja7J3bcuK6FkWnDcaYiyEhBFLN///UeWxSm0tDzNWAwT4PXUlV793V1TZgIxjjHSIbjUhaWlqykiT5n6SOpFgA8IktaVaW9CrJ530A/IeSLSniPQD8lsjmHQD8eZ4FAAgCgCAACAKAIAAIAoAg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C2KKLv5C5IpVLJVEIfHptWq1VIhnMhUaxOp8NUC1JNyzudTiF/20qS5IekQv56EASaz+ckxcHvO6dl6fn5We12u6gmzAoVRPp5mm65XCoMQ1ImQNLPPDLP8+T7/lklUO9CEIAbZsZOOsCtLdIBEAQAQQAQBABBAABBABAEAEEAEAQAQQAQBABBABAEAEEAAEEAEAQAQQAQBABBABAEAEEAEAQAQQAQBAAQBABBABAEAEEAEAQAQQAQBABBABAEABAEAEEAEAQAQQAQBABBABAEAEEAEAQAQQAAQQAQBABBABAEAEEAblQQh9cA8FucsqSJpJOkmPcB8MvgMfv/AICtcePFLGusAAAAAElFTkSuQmCC">
	</span>
	<span class="imgsce imgselected" ng-repeat="elm in src track by $index" ng-if="max>1" style="height:{{height}};width:{{width}};{{style}}">
		<span class="imgsceloading" ng-show="elm.loading">
			<div style="color: #fff;"> <span class="glyphicon glyphicon-upload"></span> {{elm.progress_value}} %</div>
			<div data-loader="ball-rotate"></div>
			<div class="progressbarimg" style="right: {{100 - elm.progress_value}}%"></div>
		</span>
		<img style="height:100%;width:100%;" ng-show="elm.src && elm.src !=''" ng-src="{{elm.src}}">
		<button type="button" ng-click="del($index)" ng-show="!elm.loading" class="btn btn-xs del"><span class="glyphicon glyphicon-trash"></span></button>
	</span>
	<span class="imgsce" ng-click="choseImage()" ng-show="max>1 && src.length<max" style="margin: 0 5px 5px 0;height:{{height}};width:{{width}};{{style}}">
		<img style="max-height: 100%;max-width: 100%;display: inline-block;" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAIGNIUk0AAHolAACAgwAA+f8AAIDpAAB1MAAA6mAAADqYAAAXb5JfxUYAAAr8SURBVHja7J3bcuK6FkWnDcaYiyEhBFLN///UeWxSm0tDzNWAwT4PXUlV793V1TZgIxjjHSIbjUhaWlqykiT5n6SOpFgA8IktaVaW9CrJ530A/IeSLSniPQD8lsjmHQD8eZ4FAAgCg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kooj7HHMXxKSLYuC2KKLv5C5IpVLJVEIfHptWq1VIhnMhUaxOp8NUC1JNyzudTiF/20qS5IekQv56EASaz+ckxcHvO6dl6fn5We12u6gmzAoVRPp5mm65XCoMQ1ImQNLPPDLP8+T7/lklUO9CEIAbZsZOOsCtLdIBEAQAQQAQBABBAABBABAEAEEAEAQAQQAQBABBABAEAEEAAEEAEAQAQQAQBABBABAEAEEAEAQAQQAQBAAQBABBABAEAEEAEAQAQQAQBABBABAEABAEAEEAEAQAQQAQBABBABAEAEEAEAQAQQAAQQAQBABBABAEAEEAblQQh9cA8FucsqSJpJOkmPcB8MvgMfv/AICtcePFLGusAAAAAElFTkSuQmCC">
	</span>
</span>
<?php
})
->js(function(){
?>
<script>
	app.factory('$img', [function () {
		return {
			upload:function(data,$scope,initcallback,pregresscallback,successcallback){
				var reader = new FileReader();
				reader.onload = function (e) {
					// initcallback(e);
					// $scope.$apply();
					var img = new Image();
          			img.src = e.target.result;
          			initcallback(img);

					var formData = new FormData();
					formData.append("image", e.target.result);
					var props = ["dist", "titre","size","ext"];
					for (var i = 0; i < props.length; i++) {
						if($scope[props[i]]){
							formData.append(props[i], $scope[props[i]]);
						}
					}
				    $.ajax({
				      xhr: function() {
				        var xhr = new window.XMLHttpRequest();
				        xhr.upload.addEventListener("progress", function(evt){
				          if (evt.lengthComputable) {
				            v = Math.ceil((evt.loaded / evt.total) * 100);
				            pregresscallback(v);
				            $scope.$apply();
				          }
				        }, false);
				        xhr.addEventListener("progress", function(evt){
				          if (evt.lengthComputable) {
				            v = Math.ceil((evt.loaded / evt.total) * 100);
				            pregresscallback(v);
				            $scope.$apply();
				          }
				        }, false);
				        return xhr;
				      },
				      url: "?api=uploadimgtmp&tokenapi="+window.tokenapi,
				      type: "POST",
				      data:  formData,
				      mimeType:"multipart/form-data",
				      contentType: false,
				      cache: false,
				      processData:false,
				      success: function(repdata, textStatus, jqXHR){
				      	var rep = JSON.parse(repdata);
						successcallback(rep.master);
						$scope.$apply();
				      },
				      error: function(jqXHR, textStatus, errorThrown){
				        successcallback({src:false});
				        $scope.$apply();
				      }
				    });
					$scope.$apply();
				}
				reader.readAsDataURL(data);
			}
		};
	}]);
	app.directive('imgs', function ($img) {
		return {
			restrict: 'AE',
			template:{{inject-tpl}},
			scope:{ngModel:"=",dist:"@",titre:"@",size:"@",ext:"@",height:"@",width:"@",style:"@",max:"=",init:"&"},
			replace:true,
			controller:function($scope){
				if($scope.ngModel instanceof Array){
					$scope.src = [];
					for (var i = 0; i < $scope.ngModel.length; i++) {
						$scope.src.push({
							src:$scope.ngModel[i],
							index:i,
							loading:false,
							progress_value:0
						});
					}
				}else{
					$scope.src = angular.copy($scope.ngModel);
				}
				$scope.max = $scope.max || 1;
				$scope.max = $scope.max * 1;
				$scope.$watch("ngModel",function(){
					if($scope.ngModel instanceof Array){
						$scope.src = [];
						for (var i = 0; i < $scope.ngModel.length; i++) {
							$scope.src.push({
								src:$scope.ngModel[i],
								index:i,
								loading:false,
								progress_value:0
							});
						}
					}else{
						$scope.src = angular.copy($scope.ngModel);
					}
				});
				if ($scope.max>1) {
					if($scope.src instanceof Array){

					}else{
						$scope.src = [];
					}
					if($scope.ngModel instanceof Array){

					}else{
				   		$scope.init();
				   		setTimeout(function(){
				   			if($scope.ngModel instanceof Array){
				   			}else{
				   				// $scope.ngModel = [];
				   			}
				   			$scope.$apply();
				   		},20);
					}
				}
				$scope.scopename = "imgs";
				$scope.progress_value = 0 ;
				$scope.loading = false;
				$scope.del = function(index){
					try {
						$scope.src.splice(index,1);
						$scope.ngModel.splice(index,1);
					} catch (e) {
						
					}
				}
				$scope.clear = function(){
					try {
						$scope.src = "";
						$scope.ngModel = "";
					} catch (e) {
						
					}
				}
				$scope.choseImage = function(){
				   var s = "";
				   var length = 0;
				   if ($scope.max>1) {
				   	s = "multiple";
				   	if($scope.src instanceof Array){

				   	}else{
				   		$scope.src = [];
				   	}
				   	if($scope.ngModel instanceof Array){

				   	}else{
				   		$scope.init();
				   		setTimeout(function(){
				   			if($scope.ngModel instanceof Array){
				   			}else{
				   				// $scope.ngModel = [];
				   			}
				   			$scope.$apply();
				   		},20);
				   	}
				   	length = $scope.src.length;
				   }
			       var file = $("<input type='file' accept='image/*' "+s+">");
			       file.change(function(e){
			       	var that = this;
			           for (var i = 0; i < Math.min(this.files.length,$scope.max-length); i++) {
			           	if (s == "multiple") {
			           		var elm = {};
			           		(function(elm,file){
								$img.upload(file,$scope,function(r){
									elm.loading = true;
									elm.src = r.src;
								},function(r){
									$scope.loading = true;
									elm.progress_value = r;
								},function(r){
									elm.loading = false;
									elm.src = r.src;
									$scope.ngModel[elm.index] = r.src;
								});			           			
			           		})(elm,that.files[i]);
			           		elm.index = $scope.src.length;
			           		$scope.src.push(elm);
			           	}else{
			           		var elm = {};
			           		(function(file,$scope,elm){
				              $img.upload(file,$scope,function(r){
				              	$scope.loading = true;
				              	$scope.progress = false;
				              	elm.src = r.src;
				              },function(r){
				              	$scope.loading = true;
				              	$scope.progress_value = r;
				              },function(r){
				              	$scope.loading = false;
				              	$scope.src = r.src;
				              	$scope.ngModel = r.src;
				              });
			           		})(this.files[i],$scope,elm);
			           		$scope.elm = elm;
			           		$scope.$apply();
			           	}
			           };
			       });
			       file.click();
				}
			},
			link: function (scope, iElement, iAttrs) {
				
			}
		};
	});
</script>
<?php
});
});