<?php
	require_once 'cron/class.db.php';
	$usuario = (isset($_GET['usuario']) && $_GET['usuario'] != '') ? $_GET['usuario'] : '';
	$boat = (isset($_GET['boat']) && $_GET['boat'] != '') ? $_GET['boat'] : '';
	$anyo = (isset($_GET['anyo']) && $_GET['anyo'] != '') ? $_GET['anyo'] : '';
	
	if ($usuario > 0 && $boat > 0 ) {
		
		DataBase::getInstancia();
		
		$sql = 'SELECT * FROM credit_register WHERE user_id = '.$usuario.' AND boat_id = '.$boat.' AND date_year = '.$anyo.' ORDER BY date_month ASC';
		$resultados = DataBase::execute_query($sql,'S');


		$sql2 = 'SELECT month(date_start) as date_month, weekday(date_start) as dia, schedule_id, status_id, count(1) as contador FROM reservations WHERE user_id = '.$usuario.' AND boat_id = '.$boat.' AND year(date_start) = '.$anyo.' GROUP BY date_month, dia, schedule_id, status_id ORDER BY date_month ASC';
		$resultados2 = DataBase::execute_query($sql2,'S');

		$consumidos = array();
		
		foreach ( $resultados2 as $res ) {
		   if ($res['status_id'] > 0 && $res['status_id'] < 3){
			if ( $res['dia'] == 5 || ( $res['dia'] == 4 && ($res['schedule_id'] == 2 || $res['schedule_id'] == 4) ) || ( $res['dia'] == 6 && ($res['schedule_id'] == 1 || $res['schedule_id'] == 3) ) ){
				if ($res['schedule_id'] == 1 || $res['schedule_id'] == 3 ){
					$consumidos[$res['date_month']]['FS']['morning']  += intval($res['contador'], 10);
				}else{
					$consumidos[$res['date_month']]['FS']['afternoon']  += intval($res['contador'], 10);
				}
			}else{
				if ($res['schedule_id'] == 1 || $res['schedule_id'] == 3 ){
					$consumidos[$res['date_month']]['L']['morning']  += intval($res['contador'], 10);
				}else{
					$consumidos[$res['date_month']]['L']['afternoon']  += intval($res['contador'], 10);
				}
			}
                   }
		}
		
		if (count($resultados)>0) {
			foreach ($resultados as $item){
				
				$meses = array('Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre');

				$resto1 = (isset($consumidos[$item['date_month']]['L']['morning'])) ? intval($item['morning']) - intval($consumidos[$item['date_month']]['L']['morning']) : $item['morning'];
				$resto2 = (isset($consumidos[$item['date_month']]['L']['afternoon'])) ? intval($item['afternoon']) - intval($consumidos[$item['date_month']]['L']['afternoon']) : $item['afternoon'];
				$resto3 = (isset($consumidos[$item['date_month']]['FS']['morning'])) ? intval($item['festivo_morning']) - intval($consumidos[$item['date_month']]['FS']['morning']) : $item['festivo_morning'];
				$resto4 = (isset($consumidos[$item['date_month']]['FS']['afternoon'])) ? intval($item['festivo_afternoon']) - intval($consumidos[$item['date_month']]['FS']['afternoon']) : $item['festivo_afternoon'];

				echo '
				<div class="col-md-2">
					<div class="box">
						<div class="box-header with-border">
						  <h3 class="box-title text-center">'.$meses[($item['date_month'])-1].'-'.$item['date_year'].'</h3>
						</div>
						<div class="box-body">
							<div style="border:2px solid #605ca8">
							   <div>
								  <div style="text-align:center;padding:5px;"><i class="fa fa-sun-o"></i><input type="text" disabled="disabled" id="L_D_'.$anyo.'_'.$item['date_month'].'" value="'.$resto1.'" style="margin-left:5px;width:25px;text-align:center;"/></div>
							  </div>
							  <div>
								  <div style="text-align:center;padding:5px;"><i class="fa fa-moon-o"></i><input type="text" disabled="disabled" id="L_N_'.$anyo.'_'.$item['date_month'].'" value="'.$resto2.'" style="margin-left:5px;width:25px;text-align:center;"/></div>
							  </div>
							</div>
							<div style="border:2px solid #ff851b; margin-top:5px;">
							  <div>
								  <div style="text-align:center;padding:5px;"><i class="fa fa-sun-o"></i><input type="text" disabled="disabled" id="FS_D_'.$anyo.'_'.$item['date_month'].'" value="'.$resto3.'" style="margin-left:5px;width:25px;text-align:center;"/></div>
							  </div>
							  <div>
								  <div style="text-align:center;padding:5px;"><i class="fa fa-moon-o"></i><input type="text" disabled="disabled" id="FS_N_'.$anyo.'_'.$item['date_month'].'" value="'.$resto4.'" style="margin-left:5px;width:25px;text-align:center;"/></div>
							  </div>
							</div>
							
						</div>
					</div>			
				</div>';
				
			}
		}else{
			echo '<div class="callout callout-danger">Lo sentimos, su usuario no tiene permisos para la configuración de créditos para el '.$anyo.'.</div>';
		}
	}
?>
