<?
$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']."' ");
	$table_name = 'appointment';
	$uploaddir = '../document/';
	$id	= $_REQUEST['delete'];
	
	$sql = "SELECT * FROM documents WHERE appointment_id = '$id' ";
	$rs = mysql_query($sql);
	if(mysql_num_rows($rs) > 0){
		while($data = mysql_fetch_assoc($rs)){
			$document_id = $data["id"];
			$document_name = $data["document_name"];
			$old_image	=	$uploaddir.$document_name;	
			if(file_exists($old_image)){
				mysql_query("DELETE FROM documents WHERE id = '$document_id' ");
				unlink($old_image);
			}
		}
	}
	
	mysql_query("DELETE FROM $table_name WHERE id = $id ");
	
	$common_appointment_id	= $_REQUEST['common_appointment_id'];
	$apply_to_coming_event	= $_REQUEST['apply_to_coming_event'];
	
	if($apply_to_coming_event==1){
		$sel_qry = "SELECT * FROM $table_name WHERE common_appointment_id='".$common_appointment_id."' AND appointment_date>='".date('Y-m-d H:i:s')."' ";
		$sel_sql = mysql_query($sel_qry);
		if(mysql_num_rows($sel_sql)){
			while($app_row=mysql_fetch_array($sel_sql)){
				$id = $app_row['id'];
				$sql = "SELECT * FROM documents WHERE appointment_id = '$id' ";
				$rs = mysql_query($sql);
				if(mysql_num_rows($rs) > 0){
					while($data = mysql_fetch_assoc($rs)){
						$document_id = $data["id"];
						$document_name = $data["document_name"];
						$old_image	=	$uploaddir.$document_name;	
						if(file_exists($old_image)){
							mysql_query("DELETE FROM documents WHERE id = '$document_id' ");
							unlink($old_image);
						}
					}
				}
				mysql_query("DELETE FROM $table_name WHERE id = $id ");
			}
		}
	}
}	

$where = "";
$next_page_condition 	= "";
$admin_id_search 	= isset($_REQUEST['admin_id_search']) ? $_REQUEST['admin_id_search'] : $_SESSION['user_id'];
if(isset($admin_id_search) && $admin_id_search !=""){
	$where .= " AND admin_id ='$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">
					<div class="hideshowonmob">
					<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>
						   <?if(isset($_SESSION['is_consultant']) && $_SESSION['is_consultant'] !=""){?>
							  <div class="col-sm-3" id="month_div">
							  
							  </div>
						   <?}else{?>
							   <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>
					<button type="button" id="" name="" value=""  style="float:left;clear:right " class="btn btn-success hideshowonmob_btn">Show Filter</button>
					<button type="button" id="" name="" value=""  style="" class="btn btn-warning hideshowonmob_hide_btn">Hide Filter</button>
					&nbsp;<a href="<?php echo $siteURL ?>/appointment/index?page=view_filter_appointment_for_admin&filter_date=1" >
							<button type="button" id="" name="" value="" class="btn btn-success">Today</button>
							</a>
					&nbsp;<a href="<?php echo $siteURL ?>/appointment/index?page=view_filter_appointment_for_admin&filter_date=2">
							<button type="button" id="" name="" value="" class="btn btn-success">Week</button>
						  </a>
					&nbsp;<a href="<?php echo $siteURL ?>/appointment/index?page=view_calendar_for_appointment">
					<button type="button" id="" name="" value="" class="btn btn-success">Month</button></a>
					
					<div style="margin-top:10px">
					<?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']);
							$first_name1 = utility::getTableFieldById("job","first_name1","id",$row_for_appointment['job_id']);
							$last_name1 = utility::getTableFieldById("job","last_name1","id",$row_for_appointment['job_id']);
							$contact_name = ucwords($first_name1.' '.$last_name1);
							if(trim($contact_name)!=""){
								$contact_name = '('.$contact_name.') ';
							}
							$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>";
							$from = "";
							$page = 'view_enquiry_progress';
							
							$job_detail = utility::getTableRowById("job","id",$row_for_appointment['job_id'],'is_prospect,is_enquiry,is_quotation,is_job,to_quote,quoted,quotation_accepted,accepted_date_given,quotation_declined,completed_projects,completed_projects');
							
							if($job_detail['is_prospect']==1 && $job_detail['is_enquiry']==0 && $job_detail['is_quotation']==0 && $job_detail['is_job']==0 ){
								$from = 'view_prospect';
								$page = 'view_prospect_progress';
							}
							if($job_detail['is_enquiry']==1 && $job_detail['is_quotation']==0 && $job_detail['is_job']==0){
								$from = 'view_enquiry';
							}
							if($job_detail['is_quotation']==1 && $job_detail['is_job']==0 && $job_detail['to_quote']==1){
								$from = 'view_quotation';
							}
							if($job_detail['is_quotation']==1 && $job_detail['is_job']==0 && $job_detail['quoted']==1){
								$from = 'view_quotation';
							}
							if($job_detail['is_quotation']==1 && $job_detail['is_job']==0 && $job_detail['quotation_accepted']==1){
								$from = 'view_quotation';
							}
							if($job_detail['is_quotation']==1 && $job_detail['is_job']==0 && $job_detail['accepted_date_given']==1){
								$from = 'view_accepted_date_given';
							}							
							if($job_detail['is_quotation']==1 && $job_detail['is_job']==0 && $job_detail['quotation_declined']==1){
								$from = 'view_quotation';
							}
							if($job_detail['is_job']==1 ){
								$from = 'view_job';
							}
							if($job_detail['is_job']==1 && $job_detail['completed_projects']==1){
								$from = 'view_job';
							}
							$admin_color = utility::getTableFieldById("admin","manage_color","id",$row_for_appointment['admin_id']);
							
							$calendar->addDailyHtml( "<div class='event' style='background:".$admin_color.";padding:5px'><a href='../enquiry_management/index?page=".$page."&from=".$from."&id=".$row_for_appointment['job_id']."' class='edit_appointment' style='font-size:13px;cursor:pointer;color:#fff;' alt=".$row_for_appointment['id'].">".$krscrm->getInitial8CharOfCompanyName($company_name)." <span style='font-size:10px;cursor:pointer;' alt='".$row_for_appointment['id']."'>".$contact_name.$admin_initial." (".$time_for_span.")</span><div style='border:0px solid;width:30px;height:13px;float:right;'><a href=''  class='fa fa-pencil edit_appointment hide' 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></a><span class='delete'><a alt=".$row_for_appointment['common_appointment_id']." 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:17px;float:right;cursor:pointer;z-index: 999;position: relative;left: 30px;'></a></span><a href='' data-toggle='modal' data-target='#myModal' title='".$row_for_appointment['job_id']."' class='appointment fa fa-edit' alt='".$row_for_appointment['id']."' style='color:white;font-size:16px;float:right;cursor:pointer;z-index: 999;position: relative;left: 20px;'></a></div>$status_bar", $row_for_appointment["appointment_date"] , $row_for_appointment["appointment_date"], $holiday_in_time_format_array );
							/* $calendar->addDailyHtml( "<a href='../enquiry_management/index?page=view_enquiry_progress&from=view_accepted_date_given&id=".$row_for_appointment['job_id']."' class='edit_appointment' style='font-size:13px;cursor:pointer;color:#fff;' alt=".$row_for_appointment['id'].">".$krscrm->getInitial8CharOfCompanyName($company_name)." <span style='font-size:10px;cursor:pointer;' alt='".$row_for_appointment['id']."'>".$contact_name.$admin_initial." (".$time_for_span.")</span><div style='border:0px solid;width:30px;height:13px;float:right;'><a href=''  class='fa fa-pencil edit_appointment hide' 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></a><span class='delete'><a alt=".$row_for_appointment['common_appointment_id']." 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;z-index: 999;position: relative;left: 30px;'></a></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>
.hideshowonmob_btn{
	display:none;
	margin-bottom: 5px;
}
.hideshowonmob_hide_btn{
	display:none;
	margin-bottom: 5px;
}
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;
	}
	.hideshowonmob{
		display:none;
	}
	.hideshowonmob_btn{
		display:block;
	}
}

@media only screen and (max-width: 450px){
	#year_month{
		width:78% !important;
	}
}
</style>
<script type="text/javascript">		
$(document).ready(function() {
	//alert($(window).width());
	$('.delete a').click(function(event) {
		var common_appointment_id = $(this).attr("alt");
		if(confirm('Are you sure! You want to delete')){
			if(common_appointment_id>0){
				if(confirm('Do you want to delete all corresponding appointment.')){
					var href = this.href;
					//alert(href);
					// Don't follow the link
					//event.preventDefault();
					var new_url = href+'&common_appointment_id='+common_appointment_id+'&apply_to_coming_event=1';
					
					window.location = new_url;
					return false;
					
				}else{
					var href = this.href;
					//alert(href);
					// Don't follow the link
					//event.preventDefault();

					// Do the async thing
					startSomeAsyncThing(function() {
						// This is the completion callback for the asynchronous thing;
						// go to the link
						window.location = href;
					});
					
					return false;
				}
				
			}else{
				// Remember the link href
				var href = this.href;
				//alert(href);
				// Don't follow the link
				//event.preventDefault();

				// Do the async thing
				/* startSomeAsyncThing(function() {
					// This is the completion callback for the asynchronous thing;
					// go to the link
					window.location = href;
				}); */
			}
			
		}else{
			return false;
		}
	});
	$(".appointment").click(function(){	
		$("#myModalLabel").text("Appointment Management");
		var hide_top_left = "&hide_top=1&hide_left=1";
		var appoint_id = $(this).attr("alt");		
		var job_id = $(this).attr("title");			
		$(".modal_iframe").attr("src","<?=$config->frontEndURL?>/admin/appointment/index?page=view&action=edit&id="+appoint_id+"&source=j&job_id="+job_id+hide_top_left);
	});
	
	$(".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; ?>
		//alert("<? echo $_SESSION["update_not_done"]; ?>");
		$("#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(){
		//alert("<? echo $_SESSION["update_not_done"]; ?>");
		/* <? if($_SESSION["update_not_done"] == 1){ ?>
			if(!confirm("Do you want to close appointment.")){
				<? 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();
		}
	});
	
	$(".hideshowonmob_btn").on("click",function() {
		$(".hideshowonmob").show('slow');
		$(".hideshowonmob_hide_btn").show();
		$(".hideshowonmob_btn").hide();
	});
	
	$(".hideshowonmob_hide_btn").on("click",function() {
		$(".hideshowonmob").hide('slow');
		$(".hideshowonmob_hide_btn").hide();
		$(".hideshowonmob_btn").show();
	});
});
</script>