<?php

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

$order_id = $args['p_id'] ?? '';
if ( ! $order_id ) {
	return;
}
$freelancer_profile_id = $args['freelancer_profile_id'] ?? '';
$profile_id            = $args['profile_id'] ?? '';
$date_format           = $args['date_format'] ?? '';
$time_format           = $args['time_format'] ?? '';

$order_status      = get_post_status( $order_id );
$is_active_order   = in_array( $order_status, array( 'publish', 'delivered', true ), true );
$is_finished_order = in_array( $order_status, array( 'completed', 'cancelled', true ), true );

if ( ! $is_active_order && ! $is_finished_order ) {
	return;
}

if ( $is_active_order ) {
	$current_stage          = gigneo_post_meta( $order_id, '_gigneo_pm_activity_stage' );
	$start_date             = gigneo_post_meta( $order_id, '_gigneo_pm_started_at_gmt' );
	$extended_delivery_date = gigneo_post_meta( $order_id, '_gigneo_pm_extended_delivery_date_time_gmt' );
	$initial_delivery_date  = gigneo_post_meta( $order_id, '_gigneo_pm_delivery_at_gmt' );
	$delivery_date          = ! empty( $extended_delivery_date ) ? $extended_delivery_date : $initial_delivery_date;
	$delivery_date          = gigneo_datetime( $delivery_date );

	$delivery_ts = strtotime( $delivery_date );
	$now         = new DateTime( 'now', wp_timezone() );
	$now->setTimezone( new DateTimeZone( 'UTC' ) );
	$now_gmt    = $now->format( 'Y-m-d H:i:s' );
	$now_gmt    = gigneo_datetime( $now_gmt );
	$now_gmt_ts = strtotime( $now_gmt );

	if ( $now_gmt_ts < $delivery_ts - 60 ) {
		$delivery_status = 'pending';
	} elseif ( $now_gmt_ts <= $delivery_ts + 60 ) {
		$delivery_status = 'delivered';
	} else {
		$delivery_status = 'delayed';
	}

	// Calculate time diff
	$diff    = max( 0, $delivery_ts - $now_gmt_ts );
	$days    = floor( $diff / DAY_IN_SECONDS );
	$hours   = floor( ( $diff % DAY_IN_SECONDS ) / HOUR_IN_SECONDS );
	$minutes = floor( ( $diff % HOUR_IN_SECONDS ) / MINUTE_IN_SECONDS );
	$seconds = $diff % MINUTE_IN_SECONDS;
} else {
	$delivery_date   = '';
	$now_gmt         = '';
	$delivery_status = 'delivered';
	$days            = 0;
	$hours           = 0;
	$minutes         = 0;
	$seconds         = 0;
}

function gigneo_pad_num( $num ): string {
	return str_pad( (string) $num, 2, '0', STR_PAD_LEFT );
}
?>
<div class="gn-order-detail-side">
	<div class="gn-order-section gn-order-delivery">
		<div class="gn-body">
			<div id="gn-order-delivery-countdown" class="gn-delivery-countdown gn-status-<?php echo esc_attr( $delivery_status ); ?>"<?php echo $is_active_order ? ' data-gn-now="' . esc_attr( $now_gmt ) . '" data-gn-delivery="' . esc_attr( $delivery_date ) . '"' : ''; ?>>
				<div class="gn-countdown-item"><span
							id="gn-days"><?php echo esc_html( gigneo_pad_num( $days ) ); ?></span><small><?php echo esc_html__( 'Days', 'gigneo' ); ?></small>
				</div>
				<div class="gn-countdown-item"><span
							id="gn-hours"><?php echo esc_html( gigneo_pad_num( $hours ) ); ?></span><small><?php echo esc_html__( 'Hours', 'gigneo' ); ?></small>
				</div>
				<div class="gn-countdown-item"><span
							id="gn-minutes"><?php echo esc_html( gigneo_pad_num( $minutes ) ); ?></span><small><?php echo esc_html__( 'Min', 'gigneo' ); ?></small>
				</div>
				<div class="gn-countdown-item"><span
							id="gn-seconds"><?php echo esc_html( gigneo_pad_num( $seconds ) ); ?></span><small><?php echo esc_html__( 'Sec', 'gigneo' ); ?></small>
				</div>
			</div>
		</div>
	</div>
</div>
