<?php 


function appFilterDate($filter,$col,$refix_if_not_empty = " AND "){
	$sql = "";
	if(!empty($filter["periode"])){
		switch($filter["periode"]){
			case "1":
				$sql .=" DATE($col)=DATE(NOW())";
			break;
			case "2":
				$date = db::parseDate(date("Y-m-d"),"Y-m-d",-1);
				$sql .=" DATE($col)=DATE('$date')";
			break;
			case "3":
				$date = db::parseDate($filter["periode_date"],"Y-m-d");
				$sql .=" DATE($col)=DATE('$date')";
			break;
			case "4":
				$sql .=" (YEAR($col)=YEAR(NOW()) AND YEARWEEK(DATE($col),1)>=YEARWEEK(DATE(NOW()),1))";
			break;
			case "5":
				$sql .=" (YEAR($col)=YEAR(NOW()) AND MONTH(DATE($col))>=MONTH(NOW()))";
			break;
			case "6":
				$sql .=" YEAR($col)=YEAR(NOW())";
			break;
			case "7":
				$date_debut = db::parseDate($filter["periode_de"],"Y-m-d");
				$date_fin = db::parseDate($filter["periode_a"],"Y-m-d");
				$sql .=" (DATE($col)>=DATE('$date_debut') AND  DATE($col)<=DATE('$date_fin'))";
			break;
		}
	}
	if (!empty($sql)) {
		return " ".trim($refix_if_not_empty)." ".trim($sql)." ";
	}
	return "";
}

Route::isJs(function(){
	ob_start();
	?>
	<div style="display: flex;">
		<div class="form-group avs">
			<label style="display:block" class="control-label">{{label || 'Date'}}</label>
			<select class="form-control avs" ng-model="ngModel.periode" ng-change="changed()" ng-init="ngModel.periode=''">
				<option value="">Tout</option>
				<option value="1">Aujourd'hui</option>
				<option value="2">Hier</option>
				<option value="3">Date Spécifique</option>
				<option value="4">Cette Semaine</option>
				<option value="5">Ce Mois</option>
				<option value="6">Cette Année</option>
				<option value="7">Periode Spécifique</option>
			</select>
		</div>
		<div class="form-group avs" ng-show="ngModel.periode == '3'">
			<label style="display:block" class="control-label">Date</label>
			<input ng-change="changed()" ng-model="d1" type="date" class="form-control avs" name="date" mydate="ngModel.periode_date" placeholder="Date">
		</div>
		<div class="form-group avs" ng-show="ngModel.periode == '7'">
			<label style="display:block" class="control-label">De</label>
			<input ng-change="changed()" ng-model="d2" type="date" class="form-control avs" name="date" mydate="ngModel.periode_de" placeholder="Date début">
		</div>
		<div class="form-group avs" ng-show="ngModel.periode == '7'">
			<label style="display:block" class="control-label">à</label>
			<input ng-change="changed()" ng-model="d3" type="date" class="form-control avs" name="date" mydate="ngModel.periode_a" placeholder="Date fin">
		</div>
	</div>
	<?php
	$code=ob_get_clean();
	?>
	<script>
		app.directive('filterDate', [function () {
			return {
				restrict: 'EA',
				transclude : false,
				replace : false,
				template : <?= json_encode(preg_replace("/\t|\n|\r/i", "", $code)) ?>,
				scope : {
					ngModel : "=",
					onChange : "&?",
					label : "@?"
				},
				controller : function($scope){
					if (!$scope.ngModel) {
						$scope.ngModel = {periode : ""};
					}
					$scope.$watch("ngModel",function(){
						if (!$scope.ngModel) {
							$scope.ngModel = {periode : ""};
						}
					});
					$scope.changed = function(){
						if($scope.onChange){
							setTimeout(function(){
								$scope.onChange({});
								$scope.$apply();
							},100);
						}
					}
				},
				link: function (scope, iElement, iAttrs) {
					
				}
			};
		}]);
	</script>
	<?php
});

