<?php
/**
 * MeetingHub webex single Template sidebar
 *
 * @package MeetingHub
 */

?>

<div class="mhub-sidebar-col">
	<div class="mhub-sidebar">
		<div class="mhub-single-widget mhub-single-widget-countdown">
			<h4 class="mhub-widget-title"><?php esc_html_e( 'Time to go', 'meetinghub' ); ?></h4>
			<div class="mhub-widget-inner">
				<?php if ( ! isset( $webex_response['errors'] ) ) { ?>
					<?php if ( 'end' !== $meeting_status ) { ?>
						<?php if ( $is_recurring_meeting && ! empty( $recurring_next_occurrence['status'] ) ) { ?>
							<?php if ( 'scheduled' === $recurring_next_occurrence['status'] ) { ?>
								<div class="meetinghub_start_time" data-meeting-start-time="<?php echo esc_attr( $next_meeting_time ); ?>" data-meeting-time-offset="<?php echo esc_attr( $gmt_offset_val ); ?>" > </div>
							<?php } elseif ( 'running' === $recurring_next_occurrence['status'] ) { ?>
								<span class="mhub-countdown-wrapper">
									<span class="mhub-countdown-value"><?php esc_html_e( 'Meeting is running', 'meetinghub' ); ?></span>
									<span class="mhub-countdown-label"><?php esc_html_e( 'The recurring meeting is currently active', 'meetinghub' ); ?></span>
								</span>
							<?php } elseif ( 'series_completed' === $recurring_next_occurrence['status'] ) { ?>
								<span class="mhub-countdown-wrapper">
									<span class="mhub-countdown-value"><?php esc_html_e( 'Series completed', 'meetinghub' ); ?></span>
									<span class="mhub-countdown-label"><?php esc_html_e( 'This recurring meeting series has ended', 'meetinghub' ); ?></span>
								</span>
							<?php } ?>
						<?php } else { ?>
							<?php if ( $is_schedule ) { ?>
								<div class="meetinghub_start_time" data-meeting-start-time="<?php echo esc_attr( $next_meeting_time ); ?>" data-meeting-time-offset="<?php echo esc_attr( $gmt_offset_val ); ?>" > </div>
							<?php } else { ?>
								<span class="mhub-countdown-wrapper">
									<span class="mhub-countdown-value"><?php esc_html_e( 'Meeting is running', 'meetinghub' ); ?></span>
									<span class="mhub-countdown-label"><?php esc_html_e( 'The meeting is started and running', 'meetinghub' ); ?></span>
								</span>
							<?php } ?>
						<?php } ?>
					<?php } else { ?>
						<span class="mhub-countdown-wrapper">
							<span class="mhub-countdown-value"><?php esc_html_e( 'Meeting is finished', 'meetinghub' ); ?></span>
							<span class="mhub-countdown-label"><?php esc_html_e( 'This meeting has been ended by the host.', 'meetinghub' ); ?></span>
						</span>
					<?php } ?>
				<?php } else { ?>
					<span class="mhub-countdown-wrapper">
							<span class="mhub-countdown-label"><?php echo esc_html( $response_message ); ?></span>
					</span>

				<?php } ?>
			</div>
		</div>

		<?php if ( ! isset( $webex_response['errors'] ) ) { ?>
			<?php if ( current_user_can( 'manage_options' ) || current_user_can( 'tutor_instructor' ) ) { ?>
				<div class="mhub-single-widget mhub-single-widget-host-actions">
					<div class="mhub-widget-inner">
						<?php if ( 'end' !== $meeting_status ) { ?>
						<button  class="mhub-meeting-status" data-meeting-status="end" data-post-id="<?php echo esc_attr( $post->ID ); ?>" data-meeting-id="<?php echo esc_attr( $meeting_id ); ?>"><?php esc_html_e( 'End Meeting ?', 'meetinghub' ); ?>
						</button>
					<?php } else { ?>
						<button  class="mhub-meeting-status" data-meeting-status="resume" data-post-id="<?php echo esc_attr( $post->ID ); ?>" data-meeting-id="<?php echo esc_attr( $meeting_id ); ?>"><?php esc_html_e( 'Enable Meeting Join ?', 'meetinghub' ); ?>
						</button>
					<?php } ?>
						<p><?php esc_html_e( 'You are seeing this because you are the author of this meeting', 'meetinghub' ); ?></p>
					</div>
				</div>
			<?php } ?>
		<?php } ?>
			
		<div class="mhub-single-widget mhub-single-widget-detail">
			<h4 class="mhub-widget-title"><?php esc_html_e( 'Details', 'meetinghub' ); ?></h4>
			<div class="mhub-widget-inner">
				<dl>
					<dt><?php esc_html_e( 'Topic:', 'meetinghub' ); ?></dt>
					<dd><?php echo esc_html( the_title() ); ?></dd>
					<dt><?php esc_html_e( 'Hosted By:', 'meetinghub' ); ?></dt>
					<dd>
					<?php
						$author_id = get_post_field( 'post_author', $post->ID );
						$author_name = get_the_author_meta( 'display_name', $author_id );
						echo esc_html( $author_name );
					?>
					</dd>

					<?php if ( ! $is_recurring_meeting ) { ?>
						<dt><?php esc_html_e( 'Start Time:', 'meetinghub' ); ?></dt>
						<dd class="mhbu-tm">
						<?php
						echo esc_html( gmdate( 'l, F j, Y g:i A', strtotime( $start_time ) ) );
						?>
						</dd>
					<?php } ?>

					<?php if ( $is_recurring_meeting && ! empty( $recurring_display['is_recurring'] ) ) { ?>
						<dt><?php esc_html_e( 'Recurring Meeting schedule:', 'meetinghub' ); ?></dt>
						<dd style="margin-bottom:0;"> 
							<?php esc_html_e( 'From:', 'meetinghub' ); ?> 
							<span class="muhb-st"> <?php echo esc_html( $recurring_display['series_start'] ); ?></span>
						</dd>
						<?php if ( ! empty( $recurring_display['series_end'] ) ) { ?>
						<dd> 
							<?php esc_html_e( 'To:', 'meetinghub' ); ?> 
							<span class="muhb-et"> <?php echo esc_html( $recurring_display['series_end'] ); ?></span>
						</dd>
						<?php } ?>

						<dt><?php esc_html_e( 'Meeting recurrence:', 'meetinghub' ); ?></dt>
						<dd><?php echo esc_html( $recurring_display['description'] ); ?></dd>

						<dt><?php esc_html_e( 'Next Start Time:', 'meetinghub' ); ?></dt>
						<dd class="mhbu-tm">
							<?php
							if ( ! empty( $recurring_display['next_time'] ) ) {
								echo esc_html( $recurring_display['next_time'] );
							} else {
								echo esc_html__( 'Series completed', 'meetinghub' );
							}
							?>
						</dd>
					<?php } ?>

					<dt><?php esc_html_e( 'Duration:', 'meetinghub' ); ?></dt>
					<dd class="mhub-duration">
						<?php
						$display_duration = $is_recurring_meeting && ! empty( $recurring_display['duration_text'] ) 
							? $recurring_display['duration_text'] 
							: '';
						
						if ( empty( $display_duration ) ) {
							// Fallback to original duration formatting.
							if ( 60 <= $meeting_duration ) {
								$hours   = floor( $meeting_duration / 60 );
								$minutes = $meeting_duration % 60;

								// Get the correct translation for "hour" or "hours".
								$hour_text   = ( 1 == $hours ) ? __( 'hour', 'meetinghub' ) : __( 'hours', 'meetinghub' );
								$minute_text = ( 1 == $minutes ) ? __( 'minute', 'meetinghub' ) : __( 'minutes', 'meetinghub' );

								// Print hours.
								echo esc_html( number_format_i18n( $hours ) ) . ' ' . esc_html( $hour_text );

								// Print minutes if greater than 0.
								if ( $minutes > 0 ) {
									echo ' ' . esc_html( number_format_i18n( $minutes ) ) . ' ' . esc_html( $minute_text );
								}
							} else {
								// Get the correct translation for "minute" or "minutes".
								$minute_text = ( 1 == $meeting_duration ) ? __( 'minute', 'meetinghub' ) : __( 'minutes', 'meetinghub' );

								// Print minutes only.
								echo esc_html( number_format_i18n( $meeting_duration ) ) . ' ' . esc_html( $minute_text );
							}
						} else {
							echo esc_html( $display_duration );
						}
						?>
					</dd>

					<dt><?php esc_html_e( 'Current Timezone:', 'meetinghub' ); ?></dt>
					<dd class="mhub-tz">
					<?php
						echo esc_html( $time_zone );
					?>
					</dd>
					
				</dl>
			</div>
		</div>

		<?php
		if ( $mhub_is_pro_active && ! empty( $integration_settings ) && ! empty( $integration_settings['google_calendar'] ) ) {
			$calendar_file = MHUB_PRO_INCLUDES . '/Templates/sidebars/mhub-calander.php';
			if ( file_exists( $calendar_file ) ) {
				require_once $calendar_file;
			}
		}
		?>

		<div class="mhub-buy-btn-wpapper">
			<?php
			if ( ! empty( $product_id ) && 'yes' === $meeting_as_product && ! current_user_can( 'manage_options' ) ) {
				$product_link = get_permalink( $product_id );
				?>
				<a href="<?php echo esc_url( $product_link ); ?>" class="mhub-buy-button" target="__blank"> <?php esc_attr_e( 'Buy Now !', 'meetinghub' ); ?></a>
			
			<?php } ?>
		</div>
	</div>
</div>
