<?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'] ?? '';
$employer_profile_id   = $args['profile_id'] ?? '';
$profile_id            = $args['profile_id'] ?? '';
$date_format           = $args['date_format'] ?? '';
$time_format           = $args['time_format'] ?? '';


$order_location         = gigneo_post_meta( $order_id, '_gigneo_pm_location' );
$order_location         = ! empty( $order_location ) ? $order_location : array();
$order_id_format        = gigneo_format_id( $order_id, 'order', false );
$order_post             = get_post( $order_id );
$start_date_raw         = gigneo_post_meta( $order_id, '_gigneo_pm_started_at_gmt' );
$start_date             = ! empty( $start_date_raw ) ? gigneo_datetime( $start_date_raw, $date_format ) : esc_html__( 'None', 'gigneo' );
$delivery_type          = gigneo_post_meta( $order_id, '_gigneo_pm_delivery_type' ); // remote/onsite
$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_raw      = ! empty( $extended_delivery_date ) ? $extended_delivery_date : $initial_delivery_date;
$delivery_date          = ! empty( $delivery_date_raw ) ? gigneo_datetime( $delivery_date_raw, $date_format ) : esc_html__( 'None', 'gigneo' );
$sub_total              = gigneo_post_meta( $order_id, '_gigneo_pm_subtotal' );
$total                  = gigneo_post_meta( $order_id, '_gigneo_pm_total' );
$proposal_id            = gigneo_post_meta( $order_id, '_gigneo_pm_proposal_id' );
$job_type               = gigneo_post_meta( $order_id, '_gigneo_pm_delivery_type' );
$revision_used          = gigneo_post_meta( $order_id, '_gigneo_pm_revision_used' );
$revision_used          = ! empty( $revision_used ) ? intval( $revision_used ) : 0;
$location_type          = ! empty( $order_location['type'] ) ? $order_location['type'] : '';
?>
<div class="gn-order-section gn-order-payment-overview">
	<div class="gn-head">
		<h3 class="gn-title"><?php echo esc_html__( 'Contract overview', 'gigneo' ); ?></h3>
		<?php if ( ! empty( $proposal_id ) ) { ?>
			<a class="gn-link gn-link-primary" href="<?php echo esc_url( get_edit_post_link( $proposal_id ) ); ?>" target="_blank">
				<?php echo esc_html__( 'View proposal', 'gigneo' ); ?>
				<i class="hi hi-arrow-up-right" aria-hidden="true"></i>
			</a>
		<?php } ?>
	</div>
	<div class="gn-body">
		<div class="gn-stats-wrapper">
			<div class="gn-stat">
				<h4 class="gn-label"><?php echo esc_html__( 'Order amount', 'gigneo' ); ?></h4>
				<span class="gn-value"><?php echo esc_html( gigneo_price_format( $total, true ) ); ?></span>
			</div>
			<div class="gn-stat">
				<h4 class="gn-label"><?php echo esc_html__( 'Job Type', 'gigneo' ); ?></h4>
				<span class="gn-value"><?php echo esc_html( ucfirst( $job_type ) ); ?></span>
			</div>
			<div class="gn-stat">
				<h4 class="gn-label"><?php echo esc_html__( 'Start date', 'gigneo' ); ?></h4>
				<p class="gn-value"><?php echo esc_html( $start_date ); ?></p>
			</div>
			<div class="gn-stat">
				<h4 class="gn-label"><?php echo esc_html__( 'Due date', 'gigneo' ); ?></h4>
				<p class="gn-value"><?php echo esc_html( $delivery_date ); ?></p>
			</div>
		</div>
	</div>
</div>

<?php
//Location
if ( ! empty( $order_location ) ) {
	$countries        = gigneo_allowed_countries();
	$states           = gigneo_allowed_states();
	$from_location    = $order_location['location']['from'] ?? array();
	$to_location      = $order_location['location']['to'] ?? array();
	$country_state    = explode( ':', $order_location['country_state'] );
	$country          = ! empty( $country_state[0] ) ? $country_state[0] : '';
	$country          = ! empty( $country ) && ! empty( $countries[ $country ] ) ? $countries[ $country ] : '';
	$state            = ! empty( $order_location['country_state'] ) && ! empty( $states[ $order_location['country_state'] ] ) ? $states[ $order_location['country_state'] ] : '';
	$location_address = ! empty( $location['address'] ) ? $location['address'] : '';
	$city_term        = ! empty( $order_location['city'] ) ? get_term_by( 'slug', $order_location['city'], 'gigneo_city' ) : '';
	$city_name        = $city_term->name ?? '';
	?>
	<div class="gn-order-section gn-order-general-overview">
		<div class="gn-order-sub-section gn-locations">
			<h3 class="gn-title"><?php echo esc_html__( 'Location', 'gigneo' ); ?></h3>
			<div class="gn-location gn-from-location">
				<span class="gn-icon"><i class="hi hi-briefcase" aria-hidden="true"></i></span>
				<div class="gn-info">
					<h6 class="gn-title">
						<?php
						printf(
							// translators: %s is the profile type, e.g., Freelancer.
							esc_html__( '%s location', 'gigneo' ),
							esc_html( gigneo_freelancer_singular() )
						)
						?>
					</h6>
					<p class="gn-address"><?php echo esc_html( implode( ', ', array_filter( array( $order_location['area'] ?? '', $order_location['zip_code'] ?? '', $city_name, $state, $country ) ) ) ); ?></p>
					<p class="gn-friendly-address"><?php echo esc_html( $from_location['friendly_address'] ?? '' ); ?></p>
				</div>
			</div>
			<?php if ( 'on_location' === $location_type ) { ?>
				<div class="gn-location gn-to-location">
					<span class="gn-icon"><i class="hi hi-home" aria-hidden="true"></i></span>
					<div class="gn-info">
						<h6 class="gn-title">
							<?php
							printf(
								// translators: %s is the profile type, e.g., Employer.
								esc_html__( '%s location', 'gigneo' ),
								esc_html( gigneo_employer_singular() )
							)
							?>
						</h6>
						<p class="gn-address"><?php echo esc_html( implode( ', ', array_filter( array( $location_address, $state, $country ) ) ) ); ?></p>
						<p class="gn-friendly-address"><?php echo esc_html( $to_location['friendly_address'] ?? '' ); ?></p>
					</div>
				</div>
			<?php } ?>
		</div>
	</div>
<?php } ?>
