<?php
include "includes.php";

if(isset($_POST["action"]) && $_POST["action"] == "reschedule_appointment") {
	verifyRequiredParams(array("api_key", "order_id", "notes", "date", "time"));
	if (isset($_POST["api_key"]) && $_POST["api_key"] == $objsettings->get_option("ct_api_key")) {
		$id = $order = $_POST["order_id"];
		$notes = $_POST["notes"];
		$dates = $_POST["date"];
		$timess = $_POST["time"];
		$booking_status = "RS";
		$read_status = "U";
		$lastmodify = date("Y-m-d H:i:s");
		$datetime_withmaxtime = "";
		if ($getmaximumbooking != "") {
			$datetime_withmaxtime = strtotime("+".$getmaximumbooking." month", strtotime(date("Y-m-d")));
		}
		if (strtotime($dates) <= $datetime_withmaxtime || $datetime_withmaxtime == "") {
			$dat = $dates." ".$timess;
			$finaldate = date("Y-m-d H:i:s", strtotime($dat));
			$objuserdetails->reschedule_booking($finaldate, $order, $booking_status, $read_status, $lastmodify);
			$serializedData = $objuserdetails->get_user_notes($order);
			$data = unserialize(base64_decode($serializedData[0]));
			if (array_key_exists("notes", $data)) {
				$data["notes"] = $notes;
			}
			$serializedData = base64_encode(serialize($data));
			$objuserdetails->update_notes($order, $serializedData); /* code for email and sms */
			$orderdetail = $objdashboard->getclientorder_api($id);
			$clientdetail = $objdashboard->clientemailsender($id);
			$admin_company_name = $objsettings->get_option("ct_company_name");
			$setting_date_format = $objsettings->get_option("ct_date_picker_date_format");
			$setting_time_format = $objsettings->get_option("ct_choose_time_format");
			$booking_date = str_replace($english_date_array,$selected_lang_label,date($setting_date_format, strtotime($clientdetail["booking_date_time"])));
			if ($setting_time_format == 12) {
				$booking_time = str_replace($english_date_array,$selected_lang_label,date("h:i A", strtotime($clientdetail["booking_date_time"])));
			} else {
				$booking_time = date("H:i", strtotime($clientdetail["booking_date_time"]));
			}
			$company_name = $objsettings->get_option("ct_email_sender_name");
			$company_email = $objsettings->get_option("ct_email_sender_address");
			$service_name = $clientdetail["title"];
			if ($admin_email == "") {
				$admin_email = $clientdetail["email"];
			}
			$price = $general->ct_price_format($orderdetail[2], $symbol_position, $decimal); /* methods */
			$units = $label_language_values["none"];
			$methodname = $label_language_values["none"];
			$hh = $booking->get_methods_ofbookings($orderdetail[4]);
			$count_methods = mysqli_num_rows($hh);
			$hh1 = $booking->get_methods_ofbookings($orderdetail[4]);
			if ($count_methods > 0) {
				while ($jj = mysqli_fetch_array($hh1)) {
					if ($units == $label_language_values["none"]) {
						$units = $jj["units_title"]."-".$jj["qtys"];
					} else {
						$units = $units.",".$jj["units_title"]."-".$jj["qtys"];
					}
					$methodname = $jj["method_title"];
				}
			} /* Add ons */
			$addons = $label_language_values["none"];
			$hh = $booking->get_addons_ofbookings($orderdetail[4]);
			while ($jj = mysqli_fetch_array($hh)) {
				if ($addons == $label_language_values["none"]) {
					$addons = $jj["addon_service_name"]."-".$jj["addons_service_qty"];
				} else {
					$addons = $addons.",".$jj["addon_service_name"]."-".$jj["addons_service_qty"];
				}
			} /* Guest user */
			if ($orderdetail[4] == 0) {
				$gc = $objdashboard->getguestclient($orderdetail[4]);
				$temppp = unserialize(base64_decode($gc[5]));
				$temp = str_replace("\\", "", $temppp);
				$vc_status = $temp["vc_status"];
				if ($vc_status == "N") {
					$final_vc_status = $label_language_values["no"];
				}
				elseif($vc_status == "Y") {
					$final_vc_status = $label_language_values["yes"];
				} else {
					$final_vc_status = "N/A";
				}
				$p_status = $temp["p_status"];
				if ($p_status == "N") {
					$final_p_status = $label_language_values["no"];
				}
				elseif($p_status == "Y") {
					$final_p_status = $label_language_values["yes"];
				} else {
					$final_p_status = "N/A";
				}
				$client_name = $gc[2];
				$client_email = $gc[3];
				$client_phone = $gc[4];
				$firstname = $client_name;
				$lastname = "";
				$booking_status = $orderdetail[6];
				$final_vc_status;
				$final_p_status;
				$payment_status = $orderdetail[5];
				$client_address = $temp["address"];
				$client_notes = $temp["notes"];
				$client_status = $temp["contact_status"];
				$client_city = $temp["city"];
				$client_state = $temp["state"];
				$client_zip = $temp["zip"];
			} else /*Registered user */ {
				$c = $objdashboard->getguestclient($orderdetail[4]);
				$temppp = unserialize(base64_decode($c[5]));
				$temp = str_replace("\\", "", $temppp);
				$vc_status = $temp["vc_status"];
				if ($vc_status == "N") {
					$final_vc_status = $label_language_values["no"];
				}
				elseif($vc_status == "Y") {
					$final_vc_status = $label_language_values["yes"];
				} else {
					$final_vc_status = "N/A";
				}
				$p_status = $temp["p_status"];
				if ($p_status == "N") {
					$final_p_status = $label_language_values["no"];
				}
				elseif($p_status == "Y") {
					$final_p_status = $label_language_values["yes"];
				} else {
					$final_p_status = "N/A";
				}
				$client_phone_no = $c[4];
				$client_phone_length = strlen($client_phone_no);
				if ($client_phone_length > 6) {
					$client_phone = $client_phone_no;
				} else {
					$client_phone = "N/A";
				}
				$client_namess = explode(" ", $c[2]);
				$cnamess = array_filter($client_namess);
				$ccnames = array_values($cnamess);
				if (sizeof((array)$ccnames) > 0) {
					$client_first_name = $ccnames[0];
					if (isset($ccnames[1])) {
						$client_last_name = $ccnames[1];
					} else {
						$client_last_name = "";
					}
				} else {
					$client_first_name = "";
					$client_last_name = "";
				}
				if ($client_first_name == "" && $client_last_name == "") {
					$firstname = "User";
					$lastname = "";
					$client_name = $firstname." ".$lastname;
				}
				elseif($client_first_name != "" && $client_last_name != "") {
					$firstname = $client_first_name;
					$lastname = $client_last_name;
					$client_name = $firstname." ".$lastname;
				}
				elseif($client_first_name != "") {
					$firstname = $client_first_name;
					$lastname = "";
					$client_name = $firstname." ".$lastname;
				}
				elseif($client_last_name != "") {
					$firstname = "";
					$lastname = $client_last_name;
					$client_name = $firstname." ".$lastname;
				}
				$client_notes = $temp["notes"];
				if ($client_notes == "") {
					$client_notes = "N/A";
				}
				$client_status = $temp["contact_status"];
				if ($client_status == "") {
					$client_status = "N/A";
				}
				$client_email = $c[3];
				$payment_status = $orderdetail[5];
				$final_vc_status;
				$final_p_status;
				$client_address = $temp["address"];
				$client_city = $temp["city"];
				$client_state = $temp["state"];
				$client_zip = $temp["zip"];
			}
			$searcharray = array("{{service_name}}", "{{booking_date}}", "{{business_logo}}", "{{business_logo_alt}}", "{{client_name}}", "{{methodname}}", "{{units}}", "{{addons}}", "{{client_email}}", "{{phone}}", "{{payment_method}}", "{{vaccum_cleaner_status}}", "{{parking_status}}", "{{notes}}", "{{contact_status}}", "{{address}}", "{{price}}", "{{admin_name}}", "{{firstname}}", "{{lastname}}", "{{app_remain_time}}", "{{reject_status}}", "{{company_name}}", "{{booking_time}}", "{{client_city}}", "{{client_state}}", "{{client_zip}}", "{{company_city}}", "{{company_state}}", "{{company_zip}}", "{{company_country}}", "{{company_phone}}", "{{company_email}}", "{{company_address}}", "{{admin_name}}");
			$replacearray = array($service_name, $booking_date, $business_logo, $business_logo_alt, $client_name, $methodname, $units, $addons, $client_email, $client_phone, $payment_status, $final_vc_status, $final_p_status, $client_notes, $client_status, $client_address, $price, $get_admin_name, $firstname, $lastname, "", "", $admin_company_name, $booking_time, $client_city, $client_state, $client_zip, $company_city, $company_state, $company_zip, $company_country, $company_phone, $company_email, $company_address, $get_admin_name);
			if ($gc_hook->gc_purchase_status() == "exist") {
				if ($_POST["gc_event_id"] != "none" && $_POST["gc_staff_event_id"] != "none" && $_POST["pid"] != "none") {
					if ($objsettings->get_option("ct_gc_status_configure") == "Y" && $objsettings->get_option("ct_gc_status") == "Y") {
						echo $gc_hook->gc_reschedule_booking_ajax_hook();
					}
				}
			} /* Client Email Template */
			$emailtemplate->email_subject = "Appointment Rescheduled by you";
			$emailtemplate->user_type = "C";
			$clientemailtemplate = $emailtemplate->readone_client_email_template_body();
			if ($clientemailtemplate[2] != "") {
				$clienttemplate = base64_decode($clientemailtemplate[2]);
			} else {
				$clienttemplate = base64_decode($clientemailtemplate[3]);
			}
			$subject = $clientemailtemplate[1];
			if ($objsettings->get_option("ct_client_email_notification_status") == "Y" && $clientemailtemplate[4] == "E") {
				$client_email_body = str_replace($searcharray, $replacearray, $clienttemplate);
				if ($objsettings->get_option("ct_smtp_hostname") != "" && $objsettings->get_option("ct_email_sender_name") != "" && $objsettings->get_option("ct_email_sender_address") != "" && $objsettings->get_option("ct_smtp_username") != "" && $objsettings->get_option("ct_smtp_password") != "" && $objsettings->get_option("ct_smtp_port") != "") {
					$mail->IsSMTP();
				} else {
					$mail->IsMail();
				}
				$mail->SMTPDebug = 0;
				$mail->IsHTML(true);
				$mail->From = $company_email;
				$mail->FromName = $company_name;
				$mail->Sender = $company_email;
				$mail->AddAddress($client_email, $client_name);
				$mail->Subject = $subject;
				$mail->Body = $client_email_body;
				$mail->send();
				$mail->ClearAllRecipients();
			} /* Admin Email Template */
			$emailtemplate->email_subject = "Appointment Rescheduled By Customer";
			$emailtemplate->user_type = "A";
			$adminemailtemplate = $emailtemplate->readone_client_email_template_body();
			if ($adminemailtemplate[2] != "") {
				$admintemplate = base64_decode($adminemailtemplate[2]);
			} else {
				$admintemplate = base64_decode($adminemailtemplate[3]);
			}
			$adminsubject = $adminemailtemplate[1];
			if ($objsettings->get_option("ct_admin_email_notification_status") == "Y" && $adminemailtemplate[4] == "E") {
				$admin_email_body = str_replace($searcharray, $replacearray, $admintemplate);
				if ($objsettings->get_option("ct_smtp_hostname") != "" && $objsettings->get_option("ct_email_sender_name") != "" && $objsettings->get_option("ct_email_sender_address") != "" && $objsettings->get_option("ct_smtp_username") != "" && $objsettings->get_option("ct_smtp_password") != "" && $objsettings->get_option("ct_smtp_port") != "") {
					$mail_a->IsSMTP();
				} else {
					$mail_a->IsMail();
				}
				$mail_a->SMTPDebug = 0;
				$mail_a->IsHTML(true);
				$mail_a->From = $company_email;
				$mail_a->FromName = $company_name;
				$mail_a->Sender = $company_email;
				$mail_a->AddAddress($admin_email, $get_admin_name);
				$mail_a->Subject = $adminsubject;
				$mail_a->Body = $admin_email_body;
				$mail_a->send();
				$mail_a->ClearAllRecipients();
			} /*SMS SENDING CODE*/ /*GET APPROVED SMS TEMPLATE*/ 
			
			/* MESSAGEBIRD CODE */
		if($settings->get_option("ct_sms_messagebird_status") == "Y"){
			if ($settings->get_option('ct_sms_messagebird_send_sms_to_client_status') == "Y"){
				$template = $objdashboard->gettemplate_sms("RS", "C");
				$phone = $client_phone;
				if ($template[4] == "E"){
					if ($template[2] == ""){
						$message = base64_decode($template[3]);
					}
					else{
						$message = base64_decode($template[2]);
					}
				}
				$messagebird_apikey =$settings->get_option("ct_sms_messagebird_account_apikey");     

				$message = str_replace($searcharray, $replacearray, $message);

				require_once(dirname(dirname(__FILE__)).'/messagebird/vendor/autoload.php');
				$MessageBird = new \MessageBird\Client($messagebird_apikey);

				$Message = new \MessageBird\Objects\Message();
				$Message->originator = 'MessageBird';
				$Message->recipients = $phone;
				$Message->body = $message;

				$res = $MessageBird->messages->create($Message);
				var_dump($res);
				$Balance = $MessageBird->balance->read();
			}
			
			if ($settings->get_option('ct_sms_messagebird_send_sms_to_admin_status') == "Y"){
				$template = $objdashboard->gettemplate_sms("RS", "A");
				$phone = $settings->get_option('ct_sms_messagebird_admin_phone');;
				if ($template[4] == "E"){
					if ($template[2] == ""){
						$message = base64_decode($template[3]);
					}
					else{
						$message = base64_decode($template[2]);
					}
				}
				$messagebird_apikey =$settings->get_option("ct_sms_messagebird_account_apikey");     

				$message = str_replace($searcharray, $replacearray, $message);

				require_once(dirname(dirname(__FILE__)).'/messagebird/vendor/autoload.php');
				$MessageBird = new \MessageBird\Client($messagebird_apikey);

				$Message = new \MessageBird\Objects\Message();
				$Message->originator = 'MessageBird';
				$Message->recipients = $phone;
				$Message->body = $message;

				$res = $MessageBird->messages->create($Message);
				var_dump($res);
				$Balance = $MessageBird->balance->read();
			}
			
	  }
			/* TEXTLOCAL CODE */
			if ($objsettings->get_option("ct_sms_textlocal_status") == "Y") {
				if ($objsettings->get_option("ct_sms_textlocal_send_sms_to_client_status") == "Y") {
					$template = $objdashboard->gettemplate_sms("RS", "C");
					$phone = $client_phone;
					if ($template[4] == "E") {
						if ($template[2] == "") {
							$message = base64_decode($template[3]);
						} else {
							$message = bas64_decode($template[2]);
						}
					}
					$message = str_replace($searcharray, $replacearray, $message);
					$data = "username=".$textlocal_username."&hash=".$textlocal_hash_id."&message=".$message."&numbers=".$phone."&test=0";
					$ch = curl_init("http://api.textlocal.in/send/?");
					curl_setopt($ch, CURLOPT_POST, true);
					curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
					$result = curl_exec($ch);
					curl_close($ch);
				}
				if ($objsettings->get_option("ct_sms_textlocal_send_sms_to_admin_status") == "Y") {
					$template = $objdashboard->gettemplate_sms("RS", "A");
					$phone = $objsettings->get_option("ct_sms_textlocal_admin_phone");
					if ($template[4] == "E") {
						if ($template[2] == "") {
							$message = base64_decode($template[3]);
						} else {
							$message = base64_decode($template[2]);
						}
					}
					$message = str_replace($searcharray, $replacearray, $message);
					$data = "username=".$textlocal_username."&hash=".$textlocal_hash_id."&message=".$message."&numbers=".$phone."&test=0";
					$ch = curl_init("http://api.textlocal.in/send/?");
					curl_setopt($ch, CURLOPT_POST, true);
					curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
					$result = curl_exec($ch);
					curl_close($ch);
				}
			} /*PLIVO CODE*/
			if ($objsettings->get_option("ct_sms_plivo_status") == "Y") {
				if ($objsettings->get_option("ct_sms_plivo_send_sms_to_client_status") == "Y") {
					$auth_id = $objsettings->get_option("ct_sms_plivo_account_SID");
					$auth_token = $objsettings->get_option("ct_sms_plivo_auth_token");
					$p_client = new Plivo\ RestAPI($auth_id, $auth_token, "", "");
					$template = $objdashboard->gettemplate_sms("RS", "C");
					$phone = $client_phone;
					if ($template[4] == "E") {
						if ($template[2] == "") {
							$message = base64_decode($template[3]);
						} else {
							$message = base64_decode($template[2]);
						}
						$client_sms_body = str_replace($searcharray, $replacearray, $message); /* MESSAGE SENDING CODE THROUGH PLIVO */
						$params = array("src" => $objsettings->get_option("ct_sms_plivo_sender_number"), "dst" => $phone, "text" => $client_sms_body, "method" => "POST");
						$response = $p_client->send_message($params); /* MESSAGE SENDING CODE ENDED HERE*/
					}
				}
				if ($objsettings->get_option("ct_sms_plivo_send_sms_to_admin_status") == "Y") {
					$auth_id = $objsettings->get_option("ct_sms_plivo_account_SID");
					$auth_token = $objsettings->get_option("ct_sms_plivo_auth_token");
					$p_admin = new Plivo\ RestAPI($auth_id, $auth_token, "", "");
					$template = $objdashboard->gettemplate_sms("RS", "A");
					$phone = $admin_phone_plivo;
					if ($template[4] == "E") {
						if ($template[2] == "") {
							$message = base64_decode($template[3]);
						} else {
							$message = base64_decode($template[2]);
						}
						$client_sms_body = str_replace($searcharray, $replacearray, $message);
						$params = array("src" => $objsettings->get_option("ct_sms_plivo_sender_number"), "dst" => $phone, "text" => $client_sms_body, "method" => "POST");
						$response = $p_admin->send_message($params); /* MESSAGE SENDING CODE ENDED HERE*/
					}
				}
			}
			if ($objsettings->get_option("ct_sms_twilio_status") == "Y") {
				if ($objsettings->get_option("ct_sms_twilio_send_sms_to_client_status") == "Y") {
					$AccountSid = $objsettings->get_option("ct_sms_twilio_account_SID");
					$AuthToken = $objsettings->get_option("ct_sms_twilio_auth_token");
					$twilliosms_client = new Services_Twilio($AccountSid, $AuthToken);
					$template = $objdashboard->gettemplate_sms("RS", "C");
					$phone = $client_phone;
					if ($template[4] == "E") {
						if ($template[2] == "") {
							$message = base64_decode($template[3]);
						} else {
							$message = base64_decode($template[2]);
						}
						$client_sms_body = str_replace($searcharray, $replacearray, $message); /*TWILIO CODE*/
						$message = $twilliosms_client->account->messages->create(array("From" => $company_phone, "To" => $phone, "Body" => $client_sms_body));
					}
				}
				if ($objsettings->get_option("ct_sms_twilio_send_sms_to_admin_status") == "Y") {
					$AccountSid = $objsettings->get_option("ct_sms_twilio_account_SID");
					$AuthToken = $objsettings->get_option("ct_sms_twilio_auth_token");
					$twilliosms_admin = new Services_Twilio($AccountSid, $AuthToken);
					$template = $objdashboard->gettemplate_sms("RS", "A");
					$phone = $admin_phone_twilio;
					if ($template[4] == "E") {
						if ($template[2] == "") {
							$message = base64_decode($template[3]);
						} else {
							$message = base64_decode($template[2]);
						}
						$client_sms_body = str_replace($searcharray, $replacearray, $message); /*TWILIO CODE*/
						$message = $twilliosms_admin->account->messages->create(array("From" => $company_phone, "To" => $phone, "Body" => $client_sms_body));
					}
				}
			}
			if ($objsettings->get_option("ct_nexmo_status") == "Y") {
				if ($objsettings->get_option("ct_sms_nexmo_send_sms_to_client_status") == "Y") {
					$template = $objdashboard->gettemplate_sms("RS", "C");
					$phone = $client_phone;
					if ($template[4] == "E") {
						if ($template[2] == "") {
							$message = base64_decode($template[3]);
						} else {
							$message = base64_decode($template[2]);
						}
						$ct_nexmo_text = str_replace($searcharray, $replacearray, $message);
						$res = $nexmo_client->send_nexmo_sms($phone, $ct_nexmo_text);
					}
				}
				if ($objsettings->get_option("ct_sms_nexmo_send_sms_to_admin_status") == "Y") {
					$template = $objdashboard->gettemplate_sms("RS", "A");
					$phone = $objsettings->get_option("ct_sms_nexmo_admin_phone_number");
					if ($template[4] == "E") {
						if ($template[2] == "") {
							$message = base64_decode($template[3]);
						} else {
							$message = base64_decode($template[2]);
						}
						$ct_nexmo_text = str_replace($searcharray, $replacearray, $message);
						$res = $nexmo_admin->send_nexmo_sms($phone, $ct_nexmo_text);
					}
				}
			} /*SMS SENDING CODE END*/ /* code for email and sms */
			send_staff_email_sms($id,"RS",$objdashboard,$setting,$booking,$english_date_array,$selected_lang_label,$admin_email,$general);
			$valid = ["status" => "true", "statuscode" => 200, "response" => $label_language_values["your_appointment_rescheduled_successfully"]];
			setResponse($valid);
		} else {
				$invalid = ["status" => "false", "statuscode" => 404, "response" => $label_language_values["sorry_we_are_not_available"]];
				setResponse($invalid);
		}
	} else {
		$invalid = ["status" => "false", "statuscode" => 404, "response" => $label_language_values["api_key_mismatch"]];
		setResponse($invalid);
	}
}

?>