<?
$defaultTimeZone='UTC';
if(date_default_timezone_get()!=$defaultTimeZone){
	date_default_timezone_set($defaultTimeZone);
}

$current_month_array = array();
$current_month_appointment_array = array();
$current_month_appointment_html_array = array();
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'view_calendar'){	
	$year_month = $_REQUEST['year_month'];
	$year_year = $_REQUEST['year_year'];
}else{
	$year_month = date('m');
	$year_year = date('Y');
}
$date_string = date('d')."-".$year_month."-".$year_year;
$first_day_of_current_month = $year_year."-".$year_month."-01";
$number_of_days_in_month = cal_days_in_month(CAL_GREGORIAN, $year_month, $year_year);
$last_day_of_current_month = $year_year."-".$year_month."-".$number_of_days_in_month;


for($i=1;$i<=$number_of_days_in_month;$i++){
	array_push($current_month_array,str_pad($i,2,0,STR_PAD_LEFT)."-".$year_month."-".$year_year);
}
?>
<link rel="stylesheet" href="../css/SimpleCalendar.css" />
<section class="content-header">
  <h1>
	Appointments and Reminders
  </h1>
</section>
<section class="content">
	<div class="row">
		<div class="col-xs-12">
			<div class="box">
				<div class="box-body">
					<form class="form-horizontal noprint"  id="view_calender_form" action="index?page=view_calendar_for_appointment_and_reminders&action=view_calendar" method="post">
						<div class="form-group">
						   <div class="col-sm-3" id="year_div">
						   <label for="Years" class="control-label">Years</label>
							<?
								$krscrm->getYearList("year_year",$year_year,"100%",false);
							?>
						   </div>
						   <div class="col-sm-3" id="month_div">
						   <label for="Months" class="control-label">Months</label>
							<?
								$krscrm->getYearMonth("year_month",$year_month,"100%",false);
							?>
						  </div>
						  
						  <div class="col-sm-6">
							<br>
							<a href="" onClick="window.print();" id="print_button" class="btn btn-default" style="float:right !important;"><i class="fa fa-print"></i> Print</a>
						  </div>
						</div>
					</form>
					<div>
					<?php
					//error_reporting(E_ALL ^ E_WARNING);
					require_once('../include/SimpleCalendar.php');

					$calendar = new SimpleCalendar();

					$calendar->setStartOfWeek('Monday');
					$calendar->setDate($date_string);
					
					$sql_for_appointment = "SELECT * FROM appointment WHERE appointment_date BETWEEN '$first_day_of_current_month 00:00:00' and '$last_day_of_current_month 23:59:59' ";
					$rs_for_appointment = mysql_query($sql_for_appointment);
					$result_for_appointment = mysql_num_rows($rs_for_appointment);
					if($result_for_appointment > 0){
						while($row_for_appointment = mysql_fetch_assoc($rs_for_appointment)){
							$appointment_id = $row_for_appointment['id'];
							$job_id = $row_for_appointment['job_id'];
							$admin_id = $row_for_appointment['admin_id'];
							$admin = Admin::getAdminsById($admin_id);
							$job = $krscrm->getEnqQuoteJobById($job_id);
							$appointment_date = utility::getFormatedDateReturn($row_for_appointment['appointment_date']);
							$appointment_time = $row_for_appointment['appointment_time'];
							
							
							$status_bar_color = "green";
							$status_bar_value = "A";
							
							$table_shift_caleder = "<div id='table_".$row_for_appointment['id']."' style='display:none;'><table>";
							$table_shift_caleder .= "<tr><td>Job : </td><td>$job</td></tr>";
							$table_shift_caleder .= "<tr><td>Consultant : </td><td>$admin</td></tr>";
							$table_shift_caleder .= "<tr><td>Date : </td><td>$appointment_date</td></tr>";
							$table_shift_caleder .= "<tr><td>Time : </td><td>$appointment_time</td></tr>";
							$table_shift_caleder .= "</table></div>";
							echo $table_shift_caleder;
							
							$status_bar = "<span class='pull-right badge bg-$status_bar_color'>$status_bar_value</span>";
							
							
			
							$calendar->addDailyHtml( "<span id=".$row_for_appointment['id'].">".$job_id."</span>$status_bar", $row_for_appointment["appointment_date"] , $row_for_appointment["appointment_date"], $holiday_in_time_format_array );
						}
					}
					
					$sql_for_reminder = "SELECT * FROM notes WHERE note_date BETWEEN '$first_day_of_current_month 00:00:00' and '$last_day_of_current_month 23:59:59' AND note_type = 1 ";
					$rs_for_reminder = mysql_query($sql_for_reminder);
					$result_for_reminder = mysql_num_rows($rs_for_reminder);
					if($result_for_reminder > 0){
						while($row_for_reminder = mysql_fetch_assoc($rs_for_reminder)){
							$reminder_id = $row_for_reminder['id'];
							$job_id = $row_for_reminder['job_id'];
							$admin_id = $row_for_reminder['admin_id'];
							$admin = Admin::getAdminsById($admin_id);
							$job = $krscrm->getEnqQuoteJobById($job_id);
							$reminder_date = utility::getFormatedDateReturn($row_for_reminder['note_date']);
							$reminder_time = $row_for_reminder['reminder_time'];
							
							
							$status_bar_color = "yellow";
							$status_bar_value = "R";
							
							$table_shift_caleder = "<div id='table_".$row_for_reminder['id']."' style='display:none;'><table>";
							$table_shift_caleder .= "<tr><td>Job : </td><td>$job</td></tr>";
							$table_shift_caleder .= "<tr><td>Consultant : </td><td>$admin</td></tr>";
							$table_shift_caleder .= "<tr><td>Date : </td><td>$reminder_date</td></tr>";
							$table_shift_caleder .= "<tr><td>Time : </td><td>$reminder_time</td></tr>";
							$table_shift_caleder .= "</table></div>";
							echo $table_shift_caleder;
							
							$status_bar = "<span class='pull-right badge bg-$status_bar_color'>$status_bar_value</span>";
							
							
			
							$calendar->addDailyHtml( "<span id=".$row_for_reminder['id'].">".$job_id."</span>$status_bar", $row_for_reminder["note_date"] , $row_for_reminder["note_date"], $holiday_in_time_format_array );
						}
					}

					$calendar->show(true);
					?>
					</div>
				</div>
			</div>
		</div>
	</div>
	<div class='triangle' style='width:0; height:0; border-bottom:10px solid #3c8dbc; border-left:5px solid transparent; border-right:5px solid transparent;display: none;'></div>
	<div id='floating_div' style='display: none;'></div>
</section>


<style>
.badge{
	padding: 2px 3px;
}
.event{
	cursor:default;
}
.new_select{
    -webkit-border-radius: 0;
    border-radius: 0;
    background-color: white;
    font-family: inherit;
    border: 1px solid #cccccc;
    -webkit-box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.1);
    box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.1);
    color: rgba(0, 0, 0, 0.75);
    display: block;
    font-size: 0.875em;
    margin: 0 0 1em 0;
    padding: 0.5em;
    height: 2.3125em;
    width: 100%;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    -webkit-transition: -webkit-box-shadow 0.45s, border-color 0.45s ease-in-out;
    -moz-transition: -moz-box-shadow 0.45s, border-color 0.45s ease-in-out;
    transition: box-shadow 0.45s, border-color 0.45s ease-in-out;
}
@media print
{
	/*.noprint {display:none;}*/
	table.SimpleCalendar tbody td{
		height: 130px;
	}
	#year_div,#month_div{
		width:30%;
		float:left;
		clear:right;
	}
	#print_button{
		display:none;
	}
}
</style>
<script type="text/javascript">		
$(document).ready(function() {
	$('.event').hover(
         function (event) {
		   var span_id = $(this).children("span").attr("id");   
		   content = $("#table_"+span_id).html();
		   //alert(content);
			position = $(this).parent().position();	
			position_of_event = $(this).position();	
			position_top = position.top + position_of_event.top;
			//alert(position.top);			
			$("#floating_div").css({"padding":"10px","background":"#FFF","border":"1px solid #3c8dbc","width":"auto","position":"absolute","z-index":"9999","top":position_top+140,"left":position.left+160});
			$("#floating_div").html(content);
			$(".triangle").css({"position":"absolute","z-index":"9999","top":position_top+130,"left":position.left+260 });
			$(".triangle").show();
			$("#floating_div").show();
         }, 
         function () {
			$("#floating_div").css({"border":"0px solid #3c8dbc"});
			$("#floating_div").html("");
			$("#floating_div").hide();
			$(".triangle").hide();
         }
     );
	
	//$("#month_year").on("change",function() {
	$("#year_month,#year_year").on("change",function() {
		$("#view_calender_form").submit();
	});
});
</script>