<?php
$day_size 			= '20'; // px
$date_today			= date('Y-m-d');

if(isset($_POST['month_before'])){ $month_before = $_POST['month_before']; } else { $month_before = "1"; }
if(isset($_POST['month_after'])){ $month_after = $_POST['month_after']; } else { $month_after = "3"; }

$_date_start 	= date('Y-m-d', strtotime('-'.$month_before.' month', strtotime($date_today)));
$_date_end 		= date('Y-m-d', strtotime('+'.$month_after.' month', strtotime($date_today)));

function days_in($date_s, $date_e){
	$start = strtotime($date_s);
	$end = strtotime($date_e);
	$days_between = ceil(abs($end - $start) / 86400);
	if($date_e != '' OR $date_e > '0'){
		return $days_between;
	} else {
		return '1';
	}
}
function days_before($date_s, $date_e){

	$datetimeFrom 	= DateTime::createFromFormat( 'Y-m-d', $date_e );
	$datetimeTo 	= DateTime::createFromFormat( 'Y-m-d', $date_s );
	if($date_e != ''){
		if($datetimeTo < $datetimeFrom){
			return $datetimeFrom->diff( $datetimeTo )->format( '%a' );
		} else {
			return '-' . $datetimeFrom->diff( $datetimeTo )->format( '%a' );
		}
	} else {
		return '0';
	}
}

$_monthsList = array("01"=>"Январь","02"=>"Февраль","03"=>"Март","04"=>"Апрель","05"=>"Май", "06"=>"Июнь","07"=>"Июль","08"=>"Август","09"=>"Сентябрь","10"=>"Октябрь","11"=>"Ноябрь","12"=>"Декабрь");
?>

<style>
	.timelist .u-item {
		width:<?php echo days_in($_date_start, $_date_end)*$day_size;?>px;
	}
	.line_today {
		width:<?php echo $day_size ?>px;
	}
	.ruler {
		width:<?php echo days_in($_date_start, $_date_end)*$day_size;?>px;
	}
</style>

<div class="clear"></div>

<ul class="timelist">
	<li class="ruler">
		<div class="months" style="width:<?php echo days_in($_date_start, $_date_end)*$day_size;?>px;">
		<?php 
		$start = new DateTime($_date_start);
		$end = new DateTime(date('Y-m-d', strtotime('+1 month', strtotime($_date_end))));
		$step = new DateInterval('P1M');
		$period = new DatePeriod($start, $step, $end);
		$n = 0;
		foreach($period as $datetime) {
			$n++;
			if($n == 1){
				echo '<div class="month" style="width:calc('.(date('t', strtotime($datetime->format("Y-m-d")))-date('j', strtotime($datetime->format("Y-m-d")))+1)*$day_size.'px - 0px);">' . $_monthsList[$datetime->format("m")].' '.$datetime->format("Y") . '</div>';
			} elseif($n == ($month_before+$month_after+1)){
				echo '<div class="month" style="width:calc('.(days_in($datetime->format("Y-m-01"), $_date_end))*$day_size.'px - 0px);">' . $_monthsList[$datetime->format("m")].' '.$datetime->format("Y") . '</div>';
			} else {
				echo '<div class="month" style="width:calc('.date('t', strtotime($datetime->format("Y-m-d")))*$day_size.'px - 0px);">' . $_monthsList[$datetime->format("m")].' '.$datetime->format("Y") . '</div>';
			}
		}
		?>
		</div>
		<div class="days" style="width:<?php echo days_in($_date_start, $_date_end)*$day_size;?>px;">
		<?php 
		$start = new DateTime($_date_start);
		$end = new DateTime($_date_end);
		$step = new DateInterval('P1D');
		$period = new DatePeriod($start, $step, $end);
		foreach($period as $datetime) {
		  echo '<div class="day" style="width:calc('.$day_size.'px - 0px);">' . $datetime->format("d") . '</div>';
		}
		?>
		</div>
	</li>
	<?php
	$dom = new DOMDocument;
	$dom->load($baseU);
	$xpath_u = new DOMXPath($dom);
	$_u = $xpath_u->query('/users/user');
	foreach ($_u as $u) {
	
	$UserId 			= $u->getAttribute('id');
	$UserLogin 			= $u->getAttribute('login');
	$UserPhoto			= $u->getElementsByTagName("userphoto")->item(0)->nodeValue;
	$UserName			= $u->getElementsByTagName("username")->item(0)->nodeValue;
	$UserSurName		= $u->getElementsByTagName("usersurname")->item(0)->nodeValue;
	$UserSurNameSM		= mb_substr($u->getElementsByTagName("usersurname")->item(0)->nodeValue, 0, 1,'utf-8').'.';
	
	echo "<li class=\"u-item\" id=\"u_$UserId\">";
			if(file_exists('./crmdata/upload/_user-photo/' . $UserPhoto) and $UserPhoto != ''){
				echo '<a href="/'.$folder.'/users/?view='.$UserId.'" title="'.get_user_name($UserId, 'small').'" class="user" style="background:url(\'/'.$folder.'/crmdata/upload/_user-photo/' . $UserPhoto . '?'.rand(1,1000).'        \') center center;"></a>';
			} else {
				echo '<a href="/'.$folder.'/users/?view='.$UserId.'" title="'.get_user_name($UserId, 'small').'" class="user empty" style="background:' . get_color2($UserLogin) . ';">' . mb_substr($UserSurName, 0, 1,'utf-8') . mb_substr($UserName, 0, 1,'utf-8') . '</a>';
			}
	echo "</a>";
	
	
	$doc = new DOMDocument;
	$doc->load($baseV);
	$xpath_v = new DOMXPath($doc);
	$_v = $xpath_v->query('/vacations/vacation[@status="+" and @user="'.$UserLogin.'"]');
	if($_v->length > 0){
		echo '<ul class="tasklist">';
		foreach ($_v as $v) {
			$n++;
			$VacId 		= $v->getAttribute('id');
			$VacType 		= $v->getAttribute('type');
			$VacUser 		= $v->getAttribute('user');
			$VacDateStart 	= $v->getElementsByTagName("dateS")->item(0)->nodeValue;
			$VacDateEnd 	= $v->getElementsByTagName("dateE")->item(0)->nodeValue;
			$VacComment 	= $v->getElementsByTagName("com")->item(0)->nodeValue;
			echo '
			<li class="t-item" id="t_$VacId" style="z-index:'.$n.';width:calc('.(days_in($VacDateStart, $VacDateEnd) + 1)*$day_size.'px - 0px);left:'.days_before($_date_start, $VacDateStart)*$day_size.'px;border-bottom:5px solid'; if ($VacType == 'vac-3') {echo '#d861ca';} elseif ($VacType == 'vac-2') {echo '#e00000';} elseif ($VacType == 'vac-4') {echo '#ffbf00';} elseif ($VacType == 'vac-5') {echo '#3fa33f';} elseif ($VacType == 'vac-1') {echo '#337ab7';} else {echo '#dddddd';} echo ';">
			<div><i class="fa fa-fw fa-file-text-o" aria-hidden="true"></i> <b><a href="/'.$folder.'/vacations/?id='.$VacId.'">Заявление №'.$VacId.'</a></b></div>
			<div><i class="fa fa-fw fa-calendar" aria-hidden="true"></i> с '.$VacDateStart.' по '.$VacDateEnd.' ('. timediff($VacDateStart, $VacDateEnd).' дн.)</div>
			<div><i class="fa fa-fw fa-commenting" aria-hidden="true"></i> '.mb_substr(strip_tags($VacComment), 0, 70, 'UTF-8') . '...'.'</div>
			</li>';
		}
		echo "</ul>";
	}
	
	echo "</li>";
	} ?>
	<li class="line_today" style="left:<?php echo days_before($_date_start, $date_today)*$day_size;?>px;"></li>
</ul>

<div class="row" style="margin:15px -15px 50px -15px;">
	<div class="col-lg-8 col-md-8 col-sm-12" style="vertical-align:middle;line-height:34px;">
		<i class="fa fa-fw fa-calendar" aria-hidden="true"></i> Период: <?php echo date('d', strtotime($_date_start));?> <?php echo $_monthsList[date('m', strtotime($_date_start))];?> <?php echo date('Y', strtotime($_date_start));?> - <?php echo date('d', strtotime($_date_end));?> <?php echo $_monthsList[date('m', strtotime($_date_end))];?> <?php echo date('Y', strtotime($_date_end));?>
	</div>
	<div class="col-lg-4 col-md-4 col-sm-12">
		<form method="POST">
			<div class="input-group input-group-xs">
				<span style="cursor:pointer;" class="input-group-addon" onclick="$('.timelist').scrollLeft(<?php echo days_before($_date_start, $date_today)*$day_size;?> - 100);"><i class="fa fa-fw fa-refresh" aria-hidden="true"></i></span>
				<select class="form-control" style="width:50%;" name="month_before" title="период до">
					<option value="1">1 месяц</option>
					<option value="3">3 месяца</option>
					<option value="6">6 месяцев</option>
					<option value="12">12 месяцев</option>
				</select>
				<select class="form-control" style="width:50%;" name="month_after" title="период после">
					<option value="1">1 месяц</option>
					<option value="3">3 месяца</option>
					<option value="6">6 месяцев</option>
					<option value="12">12 месяцев</option>
				</select>
				<span class="input-group-btn">
					<input type="submit" class="btn btn-md btn-default" name="update" value="Показать" />
				</span>
			</div>
		</form>
	</div>
</div>

<script>
$('.timelist').scroll(function () {
	var tl_scroll = $('.timelist').scrollLeft();
	$('.user').css('left',tl_scroll);
});
$(document).ready(function() {
	$('form select[name=month_before]').val('<?php echo $month_before;?>');
	$('form select[name=month_after]').val('<?php echo $month_after;?>');
	$('.timelist').scrollLeft(<?php echo days_before($_date_start, $date_today)*$day_size;?> - 100);
});
</script>