<?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'] ?? '';

//location
$order_location = gigneo_post_meta( $order_id, '_gigneo_pm_location' );
$order_location = ! empty( $order_location ) ? $order_location : array();
$location_type  = ! empty( $order_location['type'] ) ? $order_location['type'] : '';

$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' );
$delivery_type          = ! empty( $delivery_type ) ? $delivery_type : 'remote';
$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' );

$plan_data          = gigneo_post_meta( $order_id, '_gigneo_pm_price_plan' );
$service_price      = ! empty( $plan_data['price'] ) ? gigneo_price_format( $plan_data['price'], true ) : '';
$features           = ! empty( $plan_data['features'] ) ? $plan_data['features'] : array();
$custom_offers      = ! empty( $plan_data['custom_offers'] ) ? $plan_data['custom_offers'] : array();
$delivery_time      = ! empty( $plan_data['delivery_time'] ) ? intval( $plan_data['delivery_time'] ) : '';
$delivery_time_type = ! empty( $plan_data['delivery_time_type'] ) ? $plan_data['delivery_time_type'] : '';
$all_features       = array_merge( $features, $custom_offers );

$addons    = gigneo_post_meta( $order_id, '_gigneo_pm_addons' );
$addons    = ! empty( $addons ) ? $addons : array();
$time_list = gigneo_delivery_time_types( '', false );
$sub_total = gigneo_post_meta( $order_id, '_gigneo_pm_subtotal' );
$total     = gigneo_post_meta( $order_id, '_gigneo_pm_total' );

$total_revision = ! empty( $plan_data['revisions'] ) ? $plan_data['revisions'] : esc_html__( 'Unlimited', 'gigneo' );
$revision_used  = gigneo_post_meta( $order_id, '_gigneo_pm_revision_used' );
$revision_used  = ! empty( $revision_used ) ? intval( $revision_used ) : 0;
$revision_left  = is_numeric( $total_revision ) ? intval( $total_revision ) - $revision_used : 0;

?>
	<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>
		</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__( 'Type', 'gigneo' ); ?></h4>
					<span class="gn-value"><?php echo 'remote' === $delivery_type ? esc_html__( 'Remote', 'gigneo' ) : esc_html__( 'Onsite', 'gigneo' ); ?></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>
					<span class="gn-value"></span>
				</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>

	<div class="gn-order-section gn-order-general-overview">
		<div class="gn-order-sub-section gn-includes">
			<h3 class="gn-title"><?php echo esc_html__( 'Whats included', 'gigneo' ); ?></h3>
			<ul class="gn-feature-list">
				<li class="gn-feature gn-delivery-time">
					<i class="hi hi-clock" aria-hidden="true"></i>
					<span class="gn-title">
					<?php
					printf(
					// translators: %s is the delivery time, e.g., 3 days.
						esc_html__( 'Delivery in %s', 'gigneo' ),
						'<strong>' . esc_html( gigneo_delivery_time( $delivery_time, $delivery_time_type ) ) . '</strong>'
					);
					?>
				</span>
				</li>
				<li class="gn-feature gn-revision">
					<i class="hi hi-arrow-path" aria-hidden="true"></i>
					<span class="gn-label">
					<?php
					printf(
					// translators: %1$s is the number of revisions & %2$s is the round text
						esc_html__( '%1$s %2$s of revisions', 'gigneo' ),
						'<strong>' . esc_html( $total_revision ) . '</strong>',
						'<strong>' . esc_html__( 'round', 'gigneo' ) . '</strong>'
					)
					?>
				</span>
				</li>
				<?php
				if ( ! empty( $all_features ) ) {
					foreach ( $all_features as $feature ) {
						?>
						<li class="gn-feature">
							<i class="hi hi-check" aria-hidden="true"></i>
							<span class="gn-label"><?php echo esc_html( $feature ); ?></span>
						</li>
						<?php
					}
				}
				?>
			</ul>
		</div>
		<?php
		//Addons
		if ( ! empty( $addons ) ) {
			?>
			<div class="gn-order-sub-section gn-addons">
				<h3 class="gn-title"><?php echo esc_html__( 'Add-ons', 'gigneo' ); ?></h3>
				<ul class="gn-addon-list">
					<?php
					foreach ( $addons as $addon ) {
						$addon_qty                = ! empty( $addon['quantity'] ) ? $addon['quantity'] : 1;
						$addon_price              = ! empty( $addon['price'] ) ? gigneo_price_format( $addon['price'] * $addon_qty, true ) : 0;
						$addon_delivery_time      = ! empty( $addon['delivery_time'] ) ? $addon['delivery_time'] * $addon_qty : 1;
						$addon_delivery_time_type = ! empty( $addon['time_type'] ) ? $addon['time_type'] : '';
						?>
						<li class="gn-addon">
							<div class="gn-addon-info">
								<?php if ( ! empty( $addon['title'] ) ) { ?>
									<h5 class="gn-title">
										<?php echo esc_html( $addon['title'] . ( $addon_qty > 1 ? " (x$addon_qty)" : '' ) ); ?>
									</h5>
								<?php } ?>
								<?php if ( ! empty( $addon['description'] ) ) { ?>
									<p class="gn-description"><?php echo esc_html( $addon['description'] ); ?></p>
								<?php } ?>
							</div>
						</li>
						<?php
					}
					?>
				</ul>
			</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 ) && $countries[ $country ] ? $countries[ $country ] : '';
			$state            = ! empty( $order_location['country_state'] ) && $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-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>
		<?php } ?>
	</div>