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


if(isset($_REQUEST['delete']) && $_REQUEST['delete'] != ''){
	mysql_query("DELETE FROM appointment WHERE id='".$_REQUEST['delete']."' ");
}	

$where = "";
$next_page_condition 	= "";
$admin_id_search 	= isset($_REQUEST['admin_id_search']) ? $_REQUEST['admin_id_search'] : "";
if(isset($admin_id_search) && $admin_id_search !=""){
	$where .= " AND admin_id LIKE '%$admin_id_search%' ";
	$next_page_condition 	.= "&admin_id_search=$admin_id_search";
}

$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');
}

$next_page_condition 	.= "&action=view_calendar";
$next_page_condition 	.= "&year_month=".$year_month;
$next_page_condition 	.= "&year_year=".$year_year;


$date_string = "01-".$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 style="float:left;margin-bottom:10px;">
	Appointment Management
  </h1>
  <div style="floar:right;float:right;">
	<a href="<?php echo $siteURL ?>/appointment/index?page=view_appointment_for_admin"><i class="fa fa-list"></i> <span>List View</span></a>
  </div>
</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&action=view_calendar" method="post">
						<div class="form-group">
						   <div class="col-sm-2" id="year_div">
						   <label for="Years" class="control-label">Years</label>
							<?
								$krscrm->getYearList("year_year",$year_year,"100%",false);
							?>
						   </div>
						   <div class="col-sm-4" id="month_div" style="height: 60px;">
						   <label for="Months" class="control-label">Months</label>
							   <div class="col-sm-12">
								<span id="left_scroll" style="float: left;margin: 8px 9px 0 0;cursor:pointer;"> <i class="fa fa-fw fa-arrow-left"></i></span>
								<?
									$krscrm->getYearMonth("year_month",$year_month,"78%",false);
								?>
								<span id="right_scroll" style="float: left;margin: 8px 0 0 9px;cursor:pointer;"> <i class="fa fa-fw fa-arrow-right"></i></span>
							   </div>
						   </div>
						   <div class="col-sm-3" id="month_div">
						   <label for="Months" class="control-label">Consultant</label>
							<?
								Admin::getAdminList("admin_id_search",$admin_id_search,"100%",false,"form-control");
							?>
						   </div>
						  
						  <div class="col-sm-3">
							<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 *,TIME_FORMAT(appointment_time, '%H:%i') as appointment_time,TIME_FORMAT(appointment_time_to, '%H:%i') as appointment_time_to FROM appointment WHERE appointment_date BETWEEN '$first_day_of_current_month 00:00:00' and '$last_day_of_current_month 23:59:59' $where ORDER BY appointment_time ASC ";
					$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'];
							$company_name = utility::getTableFieldById("job","company_name","id",$row_for_appointment['job_id']);
							$admin_id = $row_for_appointment['admin_id'];
							$admin = Admin::getAdminsById($admin_id);
							$admin_initial = Admin::getAdminsInitialById($admin_id);
							$job = $krscrm->getEnqQuoteJobById($job_id);
							$appointment_date = utility::getFormatedDateReturn($row_for_appointment['appointment_date']);
							$appointment_time = $row_for_appointment['appointment_time'];
							$appointment_time_to = $row_for_appointment['appointment_time_to'];
							$is_full_day = $row_for_appointment['is_full_day'];
							$appointment_text = $row_for_appointment['appointment_text'];
							$appointment_location = $row_for_appointment['appointment_location'];
							
							
							$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>";
							if($is_full_day == 0){
							$time_for_span = $appointment_time." - ".$appointment_time_to;
							$table_shift_caleder .= "<tr><td>Time From : </td><td>$appointment_time</td></tr>";
							$table_shift_caleder .= "<tr><td>Time To : </td><td>$appointment_time_to</td></tr>";
							}else{
							$time_for_span = "Full Day";
							$table_shift_caleder .= "<tr><td>Time : </td><td>Full Day</td></tr>";
							}
							$table_shift_caleder .= "<tr><td>Location : </td><td>$appointment_location</td></tr>";
							$table_shift_caleder .= "<tr><td>Description : </td><td>$appointment_text</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 style='font-size:13px' id=".$row_for_appointment['id'].">".$krscrm->getInitial8CharOfCompanyName($company_name)." <span style='font-size:10px;'>".$admin_initial." (".$time_for_span.")</span><div style='border:0px solid;width:30px;height:13px;float:right;'><a onclick=\" javascript:return confirm('Are you sure! You want to delete');\" href='".$config->frontEndURL."/admin/appointment/index?page=view_calendar_for_appointment&delete=".$row_for_appointment['id'].$next_page_condition."' class='fa fa-times
' style='color:white;font-size:11px;float:right;cursor:pointer;'></a><a href=''  class='fa fa-pencil edit_appointment' style='color:white;font-size:11px;float:right;margin-right:3px;cursor:pointer;' data-toggle='modal' data-target='#myModal' alt='".$row_for_appointment['id']."'></a></div></span>$status_bar", $row_for_appointment["appointment_date"] , $row_for_appointment["appointment_date"], $holiday_in_time_format_array );
						}
					}
					$calendar->addDailyHtmlAdd( "<span style='font-size:20px;color:#3c8dbc;' title='Add'>+</span>", array(), $current_month_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>
table.SimpleCalendar{
	width: 100%;
}
.badge{
	padding: 2px 3px;
}
.event{
	display:block;
	float:left;
	width:100%;
	cursor:default;
}
#year_month{
	float:left;
	clear:right;
}
.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;
	}
}
@media only screen and (min-width: 1449px){
	#year_month{
		width:78% !important;
	}
}
@media only screen and (max-width: 1349px) and (min-width: 822px)  {
	#year_month{
		width:70% !important;
	}
}
@media only screen and (max-width: 822px) and (min-width: 600px)  {
	#year_month{
		width:65% !important;
	}
}
@media only screen and (max-width: 600px) and (min-width: 450px)  {
	#year_month{
		width:50% !important;
	}
}

@media only screen and (max-width: 640px){
	table.SimpleCalendar {
		width: 1506px;
		overflow-x: scroll;
	}
}

@media only screen and (max-width: 450px){
	#year_month{
		width:78% !important;
	}
}
</style>
<script type="text/javascript">		
$(document).ready(function() {
	//alert($(window).width());
	$(".add_appointment").click(function(){	
		$("#myModalLabel").text("Appointment Management");
		var hide_top_left = "&hide_top=1&hide_left=1";
		var app_date = $(this).attr("alt");		
		$(".modal_iframe").attr("src","<?=$config->frontEndURL?>/admin/appointment/index?page=add_appointment_for_calender&app_date="+app_date+hide_top_left);
	});
	$(".edit_appointment").click(function(){
		<? $_SESSION["update_not_done"] = 1; ?>
		$("#myModalLabel").text("Appointment Management");
		var hide_top_left = "&hide_top=1&hide_left=1";
		var id = $(this).attr("alt");		
		$(".modal_iframe").attr("src","<?=$config->frontEndURL?>/admin/appointment/index?page=add_appointment_for_calender&action=edit&id="+id+hide_top_left);
	});
	$(".close_button").click(function(){
		<? if($_SESSION["update_not_done"] == 1){ ?>
			if(!confirm("Do you want to close appointment without updating.")){
				<? unset($_SESSION["update_not_done"]);?>
				return false;
			}
		<? } ?>
		$(".modal_iframe").attr("src","");
		location.reload();
	});
	
	$(document).scrollTop(5); // scroll top to put header fix
	$('.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;
			scrollHeight = 100;
			top_position = 145;
			top_position_for_triangle = 135;
			if ($(document).scrollTop() >= scrollHeight) {
				top_position = 100;
				top_position_for_triangle = 90;
			}
			if($(window).width() < 640){
				top_position = top_position + 40;
				top_position_for_triangle = top_position_for_triangle + 40;
			}
			//alert(position.left);
			//alert($( window ).width());	
			left_position = ((position.left)*100 / $( window ).width()) + 2;
			left_position_for_triangle = ((position.left)*100 / $( window ).width()) + 10;
			$("#floating_div").css({"padding":"10px","background":"#FFF","border":"1px solid #3c8dbc","width":"auto","position":"absolute","z-index":"9999","top":position_top+top_position,"left":left_position+"%"});
			$("#floating_div").html(content);
			$(".triangle").css({"position":"absolute","z-index":"9999","top":position_top+top_position_for_triangle,"left":left_position_for_triangle+"%" });
			$(".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,#admin_id_search").on("change",function() {
		$("#view_calender_form").submit();
	});
	
	$("#left_scroll").on("click",function() {
		var month = $("#year_month").val();
		if(month != 1){
			var one_month_before = month - 1;
			if(one_month_before != 10 && one_month_before != 11 && one_month_before != 12){
				$("#year_month").val("0"+one_month_before);
			}else{
				$("#year_month").val(one_month_before);
			}
			$("#view_calender_form").submit();
		}
	});
	$("#right_scroll").on("click",function() {
		var month = $("#year_month").val();
		if(month != 12){
			var one_month_after = parseInt(month) + 1;
			if(one_month_after != 10 && one_month_after != 11 && one_month_after != 12){
				$("#year_month").val("0"+one_month_after);
			}else{
				$("#year_month").val(one_month_after);
			}
			
			$("#view_calender_form").submit();
		}
	});
});
</script>