<?php  

include(dirname(dirname(dirname(__FILE__)))."/objects/class_connection.php");
include(dirname(dirname(dirname(__FILE__))).'/objects/class_users.php');
include(dirname(dirname(dirname(__FILE__))).'/objects/class_order_client_info.php');
include(dirname(dirname(dirname(__FILE__))).'/objects/class_setting.php');
include(dirname(dirname(dirname(__FILE__))).'/objects/class_booking.php');
include(dirname(dirname(dirname(__FILE__)))."/objects/class_adminprofile.php");			   
include(dirname(dirname(dirname(__FILE__)))."/objects/class_dashboard.php");			   
include(dirname(dirname(dirname(__FILE__)))."/objects/class_front_first_step.php");			   
include(dirname(dirname(dirname(__FILE__)))."/objects/class_gc_hook.php");

$con = new cleanto_db();
$conn = $con->connect();
$objadminprofile = new cleanto_adminprofile();
$objadminprofile->conn = $conn;
$user=new cleanto_users();
$order_client_info=new cleanto_order_client_info();
$objdashboard=new cleanto_dashboard();
$setting = new cleanto_setting();
$setting->conn = $conn;
$booking = new cleanto_booking();
$booking->conn = $conn;
$getdateformat=$setting->get_option('ct_date_picker_date_format');
$time_format = $setting->get_option('ct_time_format');
$user->conn=$conn;
$order_client_info->conn=$conn;
$objdashboard->conn=$conn;
$first_step=new cleanto_first_step();
$first_step->conn=$conn;
$gc_hook = new cleanto_gcHook();
$gc_hook->conn = $conn;

$lang = $setting->get_option("ct_language");
$label_language_values = array();
$language_label_arr = $setting->get_all_labelsbyid($lang);
if(isset($_POST['action']) && $_POST['action']=='add_customer_registers'){	
	$user->user_email=$_POST['ct_email'];
	$user->user_pwd=md5($_POST['ct_password']);
	$user->first_name=$_POST['ct_first_name'];
	$user->last_name=$_POST['ct_last_name'];
	$user->phone=$_POST['ct_phone'];
	$user->address=$_POST['ct_address'];
	$user->zip=$_POST['ct_zip_code'];
	$user->city=$_POST['ct_city'];
	$user->state=$_POST['ct_state'];
	$user->usertype=serialize(array('client'));	
	$add_customer_register=$user->add_customer_register(); 
}
if(isset($_POST['ct_email'])){
	$user->user_email = trim(strip_tags(mysqli_real_escape_string($conn, $_POST['ct_email'])));
	$check_customer_email_existing = $user->check_customer_email_existing();
	if($check_customer_email_existing > 0){
		echo 'false';
	}else{
		echo "true";
	}
} 

if ($language_label_arr[1] != "" || $language_label_arr[3] != "" || $language_label_arr[4] != "" || $language_label_arr[5] != "")
{
	$default_language_arr = $setting->get_all_labelsbyid("en");
	if($language_label_arr[1] != ''){
		$label_decode_front = base64_decode($language_label_arr[1]);
	}else{
		$label_decode_front = base64_decode($default_language_arr[1]);
	}
	if($language_label_arr[3] != ''){
		$label_decode_admin = base64_decode($language_label_arr[3]);
	}else{
		$label_decode_admin = base64_decode($default_language_arr[3]);
	}
	if($language_label_arr[4] != ''){
		$label_decode_error = base64_decode($language_label_arr[4]);
	}else{
		$label_decode_error = base64_decode($default_language_arr[4]);
	}
	if($language_label_arr[5] != ''){
		$label_decode_extra = base64_decode($language_label_arr[5]);
	}else{
		$label_decode_extra = base64_decode($default_language_arr[5]);
	}

	$label_decode_front_unserial = unserialize($label_decode_front);
	$label_decode_admin_unserial = unserialize($label_decode_admin);
	$label_decode_error_unserial = unserialize($label_decode_error);
	$label_decode_extra_unserial = unserialize($label_decode_extra);
    
	$label_language_arr = array_merge($label_decode_front_unserial,$label_decode_admin_unserial,$label_decode_error_unserial,$label_decode_extra_unserial);
	
	foreach($label_language_arr as $key => $value){
		$label_language_values[$key] = urldecode($value);
	}
}
else
{
	$default_language_arr = $setting->get_all_labelsbyid("en");
    
	$label_decode_front = base64_decode($default_language_arr[1]);
	$label_decode_admin = base64_decode($default_language_arr[3]);
	$label_decode_error = base64_decode($default_language_arr[4]);
	$label_decode_extra = base64_decode($default_language_arr[5]);
	
	$label_decode_front_unserial = unserialize($label_decode_front);
	$label_decode_admin_unserial = unserialize($label_decode_admin);
	$label_decode_error_unserial = unserialize($label_decode_error);
	$label_decode_extra_unserial = unserialize($label_decode_extra);
    
	$label_language_arr = array_merge($label_decode_front_unserial,$label_decode_admin_unserial,$label_decode_error_unserial,$label_decode_extra_unserial);
	
	foreach($label_language_arr as $key => $value){
		$label_language_values[$key] = urldecode($value);
	}
}
if(isset($_POST['getclient_bookings_details'])){
	/*new file include*/
	include(dirname(dirname(dirname(__FILE__))).'/assets/lib/date_translate_array.php');
    /* client id */
    $user->user_id = $_POST['id'];
    $clientinfo = $user->readone();
    $client_id = $clientinfo[0];
    /* get all bookings of the selected client */
    $clientbooking = $user->get_user_all_bookings();
    $cnti = 1;
    while($cc = mysqli_fetch_array($clientbooking)){
        if($cc['booking_status']=='A')
		{
			$booking_stats=$label_language_values['active'];
		}
		elseif($cc['booking_status']=='C')
		{
			$booking_stats=$label_language_values['confirm'];
		}
		elseif($cc['booking_status']=='R')
		{
			$booking_stats=$label_language_values['reject'];
		}
		elseif($cc['booking_status']=='RS')
		{
			$booking_stats=$label_language_values["rescheduled"];
		}
		elseif($cc['booking_status']=='CC')
		{
			$booking_stats=$label_language_values['cancel_by_client'];
		}
		elseif($cc['booking_status']=='CS')
		{
			$booking_stats=$label_language_values['cancelled_by_service_provider'];
		}
		elseif($cc['booking_status']=='CO')
		{
			$booking_stats=$label_language_values['completed'];
		}
		else
		{
			$cc['booking_status']=='MN';
			$booking_stats=$label_language_values['mark_as_no_show'];
		}
           ?>
        <tr>
            <td><?php echo $cnti;?></td>
            <td><?php
            	$s_name = [];
            	$services = $user->get_booking_service_name($cc['order_id']);
            	while($service = mysqli_fetch_array($services)){
            		$s_name[] = $service['sname'];
            	}
            	echo implode(', ', $s_name);
            ?></td>
           <?php 
			if($time_format == 12){
			?>
			<td><?php echo str_replace($english_date_array,$selected_lang_label,date($getdateformat." h:i A",strtotime($cc['booking_date_time'])));?></td>
			<?php 
			}else{
			?>
            <td><?php echo str_replace($english_date_array,$selected_lang_label,date($getdateformat." H:i",strtotime($cc['booking_date_time'])));?></td>
			<?php 
			}
			?>
            <td><?php echo $booking_stats;?></td>
            <td><?php  if($cc['pna'] != 0){ echo $cc['c_payment_method'];} else { echo "Free";};?></td>
            <td>
                <?php 
                /* methods */
                $units = $label_language_values['none'];
                $methodname=$label_language_values['none'];
                $hh = $booking->get_methods_ofbookings($cc['order_id']);
                $count_methods = mysqli_num_rows($hh);
                $hh1 = $booking->get_methods_ofbookings($cc['order_id']);
                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'];
                    }
                }
                $addons = $label_language_values['none'];
                $hh = $booking->get_addons_ofbookings($cc['order_id']);
                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'];
                    }
                }
                ?>
                <b><?php echo $label_language_values['methods'];?></b> - <?php  echo $methodname;?>
                <br>
                <b><?php echo $label_language_values['units'];?></b> - <?php  echo $units;?>
                <br>
                <b><?php echo $label_language_values['addons'];?></b> - <?php  echo $addons;?>
            </td>
			<td> 
			<?php  
			
			$booking->order_id = $cc['order_id'];
			$staff_status_detail = $booking->staff_status_read_one_by_or_id();
			if(mysqli_num_rows($staff_status_detail) > 0){
				while($row = mysqli_fetch_assoc($staff_status_detail)){
					$objadminprofile->id = $row['staff_id'];
					$result_staff_info = $objadminprofile->readone();
					
					?>
					
					<b><?php  echo $result_staff_info['fullname'];?></b> - <?php  if($row['status']=='A'){echo $label_language_values['accept'];}else{echo $label_language_values['decline'];}?> <br>
					
					<?php  
					
				}
			}
			/* if($booking_details['staff_ids'] != ""){
				for($i=0;$i<sizeof((array)$staff_ids);$i++){
					$objadminprofile->id = $staff_ids[$i];
					$result_staff_info = $objadminprofile->readone();
					$booking->staff_id = $staff_ids[$i];
					$staff_status_details = $booking->staff_status_read_one_by_or_id();
					?>
					
					<b><?php  echo $result_staff_info['fullname'];?></b> - <?php  if($staff_status_details['status']=='A'){echo $label_language_values['accept'];}else{echo $label_language_values['decline'];}?> <br>
					
					<?php  
				}
			} */
			?>
			<!--b><?php  echo $result_staff_info['fullname'];?></b> - <?php  if($staff_status_details['status']=='A'){echo $label_language_values['accept'];}else{echo $label_language_values['decline'];}?>--></td>
        </tr>
    <?php 
    $cnti++;}?>
<?php 
}
elseif(isset($_POST['guest'])){
	/*new file include*/
	include(dirname(dirname(dirname(__FILE__))).'/assets/lib/date_translate_array.php');
    /* get all bookings of the selected client */
    $order_id = $_POST['orderid'];
    $clientbooking = $user->get_bookings_guests($_POST['orderid'],$_POST['email']);
    $cntgi = 1;
        while($cc = mysqli_fetch_array($clientbooking)){
            if($cc['booking_status']=='A')
            {
                $booking_stats=$label_language_values['active'];
            }
            elseif($cc['booking_status']=='C')
            {
                $booking_stats=$label_language_values['confirm'];
            }
            elseif($cc['booking_status']=='R')
            {
                $booking_stats=$label_language_values['reject'];
            }
            elseif($cc['booking_status']=='RS')
            {
                $booking_stats=$label_language_values["rescheduled"];
            }
            elseif($cc['booking_status']=='CC')
            {
                $booking_stats=$label_language_values['cancel_by_client'];
            }
            elseif($cc['booking_status']=='CS')
            {
                $booking_stats=$label_language_values['cancelled_by_service_provider'];
            }
            elseif($cc['booking_status']=='CO')
            {
                $booking_stats=$label_language_values['completed'];
            }
            else
            {
                $cc['booking_status']=='MN';
                $booking_stats=$label_language_values['mark_as_no_show'];
            }
            ?>
            <tr>
                <td><?php echo $cntgi;?></td>
                <td><?php echo $cc['sname'];?></td>
                <?php 
				if($time_format == 12){
				?>
				<td><?php echo str_replace($english_date_array,$selected_lang_label,date("".$getdateformat." h:i A",strtotime($cc['booking_date_time'])));?></td>
				<?php 
				}else{
				?>
				<td><?php echo str_replace($english_date_array,$selected_lang_label,date("".$getdateformat." H:i",strtotime($cc['booking_date_time'])));?></td>
				<?php 
				}
				?>
                <td><?php echo $booking_stats;?></td>
                <td><?php  if($cc['pna'] != 0){ echo $cc['c_payment_method'];} else { echo "Free";};?></td>
                <td>
                    <?php 
                    /* methods */
                    $units = $label_language_values['none'];
                    $methodname=$label_language_values['none'];
                    $hh = $booking->get_methods_ofbookings($order_id);
                    $count_methods = mysqli_num_rows($hh);
                    $hh1 = $booking->get_methods_ofbookings($order_id);
                    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'];
                        }
                    }
                    $addons = $label_language_values['none'];
                    $hh = $booking->get_addons_ofbookings($order_id);
                    while($jj = mysqli_fetch_array($hh)){
                        if($addons == $label_language_values['none']){
                            $addons = $jj['addon_service_name'];
                        }
                        else
                        {
                            $addons = $addons.",".$jj['addon_service_name'];
                        }
                    }
                    ?>
                    <b><?php echo $label_language_values['methods'];?></b> - <?php  echo $methodname;?>
                    <br>
                    <b><?php echo $label_language_values['units'];?></b> - <?php  echo $units;?>
                    <br>
                    <b><?php echo $label_language_values['addons'];?></b> - <?php  echo $addons;?>
                </td>
            </tr>
        <?php 
       $cntgi++; }?>
<?php  }
elseif(isset($_POST['delete_guest_bookings'])){
$order_id = $_POST['orderid'];
    $user->delete_bookings_guestcustomers($order_id);
}
elseif(isset($_POST['delete_registered_bookings'])){
	$usersid = $_POST['usersid'];
    $user->delete_bookings_registeredcustomers($usersid);
}
elseif(isset($_POST['add_rec_status'])){
	$order_client_info->recurring_id = $_POST["rec_id"];
	$result = $order_client_info->add_rec_status();
	if($result){
		echo "1";
	}else{
		echo "0";
	}
}
elseif(isset($_POST['delete_rec_status'])){
	$order_client_info->recurring_id = $_POST["rec_id"];
	$result = $order_client_info->delete_one_rec_status();
	if($result){
		echo "1";
	}else{
		echo "0";
	}
}
elseif(isset($_POST['accept_rec_status'])){
	
$t_zone_value = $setting->get_option('ct_timezone');
$server_timezone = date_default_timezone_get();
if(isset($t_zone_value) && $t_zone_value!=''){
	$offset= $first_step->get_timezone_offset($server_timezone,$t_zone_value);
	$timezonediff = $offset/3600;
}else{
	$timezonediff =0;
}
if(is_numeric(strpos($timezonediff,'-'))){
	$timediffmis = str_replace('-','',$timezonediff)*60;
	$currDateTime_withTZ= strtotime("-".$timediffmis." minutes",strtotime(date('Y-m-d H:i:s')));
}else{
	$timediffmis = str_replace('+','',$timezonediff)*60;
	$currDateTime_withTZ = strtotime("+".$timediffmis." minutes",strtotime(date('Y-m-d H:i:s')));
}
	
	$secret_key = $setting->get_option('ct_stripe_secretkey');
	if($setting->get_option('ct_stripe_payment_form_status') == "on" && $secret_key != ""){
		$order_client_info->recurring_id = $_POST["rec_id"];
		$last_order_id = $order_client_info->read_last_order_id_by_rec_id();
		$booking->order_id = $last_order_id;
		$payment_detail_result = $booking->clone_payment_order_detail();
		$payment_detail = mysqli_fetch_assoc($payment_detail_result);
		$stripe_id = $payment_detail["transaction_id"];
		if($stripe_id != ""){
			require_once(dirname(dirname(dirname(__FILE__))).'/assets/stripe/stripe.php');
			try{
				\Stripe\Stripe::setApiKey($secret_key);
				$objsubscription = new \Stripe\Subscription;
				$sub_cancel = $objsubscription::retrieve($stripe_id);
				$sub_cancel->cancel();
			}	catch (Exception $e) {
				$error = $e->getMessage();
			}
		}
	}
	$order_client_info->recurring_id = $_POST["rec_id"];
	$all_order_ids = $order_client_info->read_all_by_rec_id();
	while($order_row = mysqli_fetch_assoc($all_order_ids)){
		$order_id = $order_row["order_id"];
		$booking->booking_id = $order_id;
		$old_booking_date_time = $booking->readone_order_date_time();
		if(strtotime($old_booking_date_time) >= $currDateTime_withTZ){
			$one_booking_details = $booking->clone_booking_order_detail();
			$row_one_booking_details = mysqli_fetch_assoc($one_booking_details);
			$id = $order_id;
			$pid = "";
			if($row_one_booking_details["staff_ids"] != ""){
				$sf_id_array = explode(",",$row_one_booking_details["staff_ids"]);
				$pid = $sf_id_array[0];
			}
			$gc_event_id = $row_one_booking_details['gc_event_id'];
			$gc_staff_event_id = $row_one_booking_details['gc_staff_event_id'];
			if($gc_hook->gc_purchase_status() == 'exist'){
				echo $gc_hook->gc_cancel_reject_booking_hook();
			}
			$objdashboard->delete_booking($order_id);
		}
	}
	$order_client_info->recurring_id = $_POST["rec_id"];
	$result = $order_client_info->accept_one_rec_status();
	if($result){
		echo "1";
	}else{
		echo "0";
	}
}
?>