<?php
/**
 * GFB Customer Appointment View Front-end
 *
 * @package GFB.
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

$appointments = APBGF_Appointment::apbgf_get_appointments_by_customer( $user_id );
if ( is_array( $appointments ) && count($appointments) > 0 ) {
	// echo '<pre>$appointments$appointments';
	// print_r( $appointments );
	// echo '</pre>';
	?>
	<table>
		<thead>
			<tr>
				<th><?php echo esc_html__('ID', 'appointment-and-booking-for-gravity-forms'); ?></th>
				<th><?php echo esc_html__('Date/Time', 'appointment-and-booking-for-gravity-forms'); ?></th>
				<th><?php echo esc_html__('Location', 'appointment-and-booking-for-gravity-forms'); ?></th>
				<th><?php echo esc_html__('Service', 'appointment-and-booking-for-gravity-forms'); ?></th>
				<th><?php echo esc_html__('Staff', 'appointment-and-booking-for-gravity-forms'); ?></th>
				<th><?php echo esc_html__('Slots', 'appointment-and-booking-for-gravity-forms'); ?></th>
				<th><?php echo esc_html__('Payment', 'appointment-and-booking-for-gravity-forms'); ?></th>
				<th><?php echo esc_html__('Status', 'appointment-and-booking-for-gravity-forms'); ?></th>
			</tr>    
		</thead>
		<tbody>        
		<?php
		foreach ( $appointments as $appointment ) {
			$date = get_post_meta( $appointment->ID, 'date', true );
			$date = apply_filters('apbgf_appointment_date_format', $date);
			$time = get_post_meta( $appointment->ID, 'time', true );
			$time = apply_filters('apbgf_appointment_time_format', $time);
			$location = get_post_meta( $appointment->ID, 'location_id', true );            
			$location = ( ! empty($location) && 'all' != $location ) ? get_the_title( $location ) : $location;
			$service = get_post_meta( $appointment->ID, 'service_id', true );            
			$service = get_the_title( $service );
			$staff = get_post_meta( $appointment->ID, 'staff_id', true );            
			$staff = get_the_title( $staff );
			$capacity = get_post_meta( $appointment->ID, 'slot', true );
			$cost = get_post_meta( $appointment->ID, 'cost', true );
			$cost = apbgf_price_format($cost);
			$appointment_status = get_post_meta( $appointment->ID, 'status', true );
			?>
			<tr>
				<td><?php echo esc_attr($appointment->ID); ?></td>
				<td><?php echo esc_attr($date) . ' ' . esc_attr($time); ?></td>
				<td>
					<?php
					if ('all' == $location) {
						echo '-';
					} else {
						echo esc_attr($location);
					}
					?>
				</td>
				<td>
					<?php 
					if ( 'other' === trim(strtolower($service)) ) {
						$service_other = get_post_meta( $appointment->ID, 'service_other', true );
						if ( ! empty(trim($service_other)) ) {
							echo esc_attr($service) . ' - ' . esc_attr( $service_other );    
						} else {
							echo esc_attr($service);
						}
					} else {
						echo esc_attr($service);
					}
					?>
				</td>
				<td><?php echo esc_attr($staff); ?></td>
				<td><?php echo esc_attr($capacity); ?></td>
				<td><?php echo esc_attr($cost); ?></td>
				<?php
				if ( 'pending' == $appointment_status ) {
					?>
					<td>                        
						<form class="apbgf-app-cancel-form" id="apbgf-app-cancel-form-<?php echo esc_attr($appointment->ID); ?>" action="" method="post">
							<input type="hidden" name="apbgf-app-id" value="<?php echo esc_attr($appointment->ID); ?>" />
							<input type="hidden" name="apbgf-status" value="cancelled" />                            
							<input type="submit" name="apbgf-change-status" value="<?php echo esc_html__('Cancel Request', 'appointment-and-booking-for-gravity-forms'); ?>">
						</form>
					</td>
					<?php
				} else {
					?>
					<td><?php echo esc_attr(strtoupper($appointment_status)); ?></td>
					<?php
				}
				?>
				
			</tr>
			<?php
		}
		?>
		</tbody>
	</table>
	<?php
} else {
	echo '<p>' . esc_html__('No appointment found.', 'appointment-and-booking-for-gravity-forms') . '</p>';
}

?>