<?php

use Gigneo\Modules;

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

$service_id    = $args['service_id'] ?? 0;
$plan          = $args['plan'] ?? 'one';
$delivery_type = $args['delivery_type'] ?? '';
$location_id   = $args['location_id'] ?? '';
$location      = $args['location'] ?? array();
$address       = $args['address'] ?? '';
$addons        = $args['addons'] ?? array();

$profile_id  = gigneo_active_profile_id();
$price_plans = gigneo_service_price_plan();
$plan        = in_array( $plan, array( 'one', 'two', 'three' ), true ) ? $plan : 'one';
$title_type  = gigneo_option( '_gigneo_ts_service_pricing_title', 'custom' );
$plan_title  = gigneo_post_meta( $service_id, "_gigneo_pm_price_plan_{$plan}_title" );
$plan_title  = 'custom' === $title_type ? $plan_title : $price_plans[ $plan ];
$plan_price  = gigneo_post_meta( $service_id, "_gigneo_pm_price_plan_{$plan}_price" );

$service_type   = gigneo_service_type( $service_id );
$locations      = gigneo_post_meta( $service_id, '_gigneo_pm_locations' );
$service_addons = gigneo_post_meta( $service_id, '_gigneo_pm_addons' );

$countries = gigneo_allowed_countries();
$states    = gigneo_allowed_states();

$distance_type  = gigneo_option( '_gigneo_ts_map_distance_type', 'kilometers' );
$travel_charges = gigneo_option( '_gigneo_ts_map_travel_charges', 10 );

$order_subtotal = $plan_price;
$order_total    = $order_subtotal;

if ( in_array( 'addons', gigneo_active_modules(), true ) ) {
	$raw_addons = filter_input( INPUT_GET, 'addons' );

	if ( empty( $addons ) && isset( $raw_addons ) ) {
		$decoded_addons = is_string( $raw_addons ) ? json_decode( $raw_addons, true ) : $raw_addons;
		$addons         = array();

		if ( ! empty( $decoded_addons ) && is_array( $decoded_addons ) ) {
			foreach ( $decoded_addons as $key => $addon ) {
				$addons[ absint( $key ) ] = array(
					'id'       => isset( $addon['id'] ) ? absint( $addon['id'] ) : '',
					'quantity' => isset( $addon['quantity'] ) ? absint( $addon['quantity'] ) : 1,
				);
			}
		}
	}
}
$total_travel_charges = 0;
?>
<div class="gn-hire-summary gn-hire-service-summary">
	<h2 class="gn-title"><?php echo esc_html__( 'Order summary', 'gigneo' ); ?></h2>
	<div class="gn-package-info">
		<span class="gn-label">
		<?php
		// translators: %s is plan name
		printf( esc_html__( '%s package', 'gigneo' ), esc_html( $plan_title ) );
		?>
		</span>
		<span class="gn-value"><?php echo esc_html( gigneo_price_format( $plan_price ?? 0, true ) ); ?></span>
	</div>
	<?php
	//Addons
	if ( ! empty( $addons ) && is_array( $addons ) ) {
		?>
		<div class="gn-addons">
			<?php
			foreach ( $addons as $addon ) {

				if ( ! isset( $addon['id'] ) ) {
					continue;
				}

				$addon_id                 = intval( $addon['id'] );
				$addon_quantity           = $addon['quantity'] ? intval( $addon['quantity'] ) : 1;
				$addon_title              = $service_addons[ $addon_id ]['title'] ?? '';
				$addon_delivery_time      = intval( $service_addons[ $addon_id ]['delivery_time'] ) ?? 0;
				$addon_delivery_time_type = $service_addons[ $addon_id ]['time_type'] ?? '';
				$addon_price              = $service_addons[ $addon_id ]['price'] ?? 0;
				$addon_price              = ( $addon_price * $addon_quantity );
				$order_subtotal           = $order_subtotal + $addon_price;
				$order_total              = $order_subtotal;
				?>
				<div class="gn-addon">
					<span class="gn-label"><?php echo esc_html( $addon_title ); ?><?php echo esc_html( $addon_quantity > 1 ? " (x$addon_quantity)" : '' ); ?></span>
					<span class="gn-value"><?php echo esc_html( gigneo_price_format( $addon_price ?? 0, true ) ); ?></span>
				</div>
				<?php
			}
			?>
		</div>
		<?php
	}

	//Location
	if ( 'onsite' === $service_type && isset( $location_id ) && isset( $locations[ intval( $location_id ) ] ) ) {
		$from_location    = $locations[ intval( $location_id ) ];
		$country_state    = explode( ':', $from_location['country_state'] );
		$country          = ! empty( $country_state[0] ) ? $country_state[0] : '';
		$country          = ! empty( $country ) && $countries[ $country ] ? $countries[ $country ] : '';
		$state            = ! empty( $from_location['country_state'] ) && $states[ $from_location['country_state'] ] ? $states[ $from_location['country_state'] ] : '';
		$location_address = ! empty( $location['address'] ) ? $location['address'] : '';
		?>
		<div class="gn-section gn-summary-location">
			<div class="gn-head">
				<h3 class="gn-title"><?php echo esc_html__( 'Location details', 'gigneo' ); ?></h3>
			</div>
			<div class="gn-body">
				<div class="gn-locations">
					<div class="gn-location gn-from-location">
						<?php
						$city_term = ! empty( $from_location['city'] ) ? get_term_by( 'slug', $from_location['city'], 'gigneo_city' ) : '';
						$city_name = $city_term->name ?? '';
						?>
						<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 freelancer singular
									esc_html__( '%s location', 'gigneo' ),
									esc_html( gigneo_freelancer_singular() )
								);
								?>
							</h6>
							<p class="gn-address"><?php echo esc_html( implode( ', ', array_filter( array( $from_location['area'], $from_location['zip_code'], $city_name, $state, $country ) ) ) ); ?></p>
							<p class="gn-friendly-address"><?php echo esc_html( $from_location['address'] ); ?></p>
						</div>
					</div>
					<?php if ( 'on_location' === $delivery_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 echo esc_html__( 'Your location', 'gigneo' ); ?></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( $address ); ?></p>
							</div>
						</div>
					<?php } ?>
				</div>
				<?php
				if ( ! empty( $location['distance'] ) ) {
					?>
					<div class="gn-distance">
						<span class="gn-label"><?php echo esc_html__( 'Distance', 'gigneo' ); ?></span>
						<span class="gn-value">
							<?php echo esc_html( $location['distance'] ); ?>
							<?php echo 'kilometers' === $distance_type ? esc_html__( 'Km', 'gigneo' ) : esc_html__( 'Mile', 'gigneo' ); ?>
						</span>
					</div>
					<?php
					if ( ! empty( $travel_charges ) ) {
						$total_travel_charges = $location['distance'] * $travel_charges;
						$order_subtotal      += $total_travel_charges;
						?>
						<div class="gn-distance-rate">
						<span class="gn-label">
						<?php
						printf(
						// translators: %s is distance type
							esc_html__( 'Rate per %s', 'gigneo' ),
							'kilometers' === $distance_type ? esc_html__( 'Km', 'gigneo' ) : esc_html__( 'Mile', 'gigneo' )
						);
						?>
						</span>
							<span class="gn-value"><?php echo esc_html( gigneo_price_format( $travel_charges, true ) ); ?></span>
						</div>
						<div class="gn-distance-charges">
							<span class="gn-label"><?php echo esc_html__( 'Travel charges', 'gigneo' ); ?></span>
							<span class="gn-value"><?php echo esc_html( gigneo_price_format( $total_travel_charges, true ) ); ?></span>
						</div>
						<?php
					}
				}
				?>
			</div>
		</div>
		<?php
	}
	?>
	<div class="gn-section gn-pricing">
		<div class="gn-subtotal">
			<span class="gn-label"><?php echo esc_html__( 'Subtotal', 'gigneo' ); ?></span>
			<span class="gn-value"><?php echo esc_html( gigneo_price_format( $order_subtotal, true ) ); ?></span>
		</div>
		<?php

		$platform_commissions      = gigneo_commission( $order_total, 'service', $profile_id );
		$platform_commission       = ! empty( $platform_commissions['total'] ) ? $platform_commissions['total'] : 0;
		$platform_commission_title = gigneo_option( '_gigneo_ts_platform_fee_name', esc_html__( 'Platform fee', 'gigneo' ) );
		if ( ! empty( $platform_commission ) ) {
			$order_total = $order_subtotal + $platform_commission;
			?>
			<div class="gn-platform-commission">
				<span class="gn-label"><?php echo esc_html( $platform_commission_title ); ?></span>
				<span class="gn-value"><?php echo esc_html( gigneo_price_format( $platform_commission, true ) ); ?></span>
			</div>
			<?php
		}

		?>
		<div class="gn-total">
			<span class="gn-label"><?php echo esc_html__( 'Total', 'gigneo' ); ?></span>
			<span class="gn-value"><?php echo esc_html( gigneo_price_format( $order_total, true ) ); ?></span>
		</div>
	</div>
	<div class="gn-actions">
		<?php
		$privacy_page_id = gigneo_option( '_gigneo_ts_template_privacy_policy' );
		$terms_page_id   = gigneo_option( '_gigneo_ts_template_terms_and_condition' );
		$terms_url       = $terms_page_id ? esc_url( get_permalink( $terms_page_id ) ) : '#';
		$privacy_url     = $privacy_page_id ? esc_url( get_permalink( $privacy_page_id ) ) : '#';
		$terms_title     = $terms_page_id ? esc_html( get_the_title( $terms_page_id ) ) : esc_html__( 'Terms and Condition', 'gigneo' );
		$privacy_title   = $privacy_page_id ? esc_html( get_the_title( $privacy_page_id ) ) : esc_html__( 'Privacy Policy', 'gigneo' );
		?>
		<fieldset class="gn-form-fields">
			<div class="gn-form-field">
				<div class="gn-form-field-checkbox">
					<input id="gn-term-and-privacy" type="checkbox" class="gn-required-field" value="yes" name="term_and_privacy" />
					<label for="gn-term-and-privacy">
						<?php
						printf(
						// translators: 1. Terms and Condition URL, 2. Terms and Condition Title, 3. Privacy Policy URL, 4. Privacy Policy Title.
							wp_kses_post( __( 'I agree to the <a href="%1$s">%2$s</a> & <a href="%3$s">%4$s</a>', 'gigneo' ) ),
							esc_url( $terms_url ),
							esc_html( $terms_title ),
							esc_url( $privacy_url ),
							esc_html( $privacy_title )
						);
						?>
					</label>
				</div>
			</div>
		</fieldset>
		<button type="submit" class="gn-btn gn-btn-primary"><?php echo esc_html__( 'Confirm and pay', 'gigneo' ); ?></button>
		<p class="gn-description">
			<?php
			printf(
			// translators: %s is freelancer singular
				esc_html__( 'Fund the job upfront. %s gets paid once you are satisfied with the work.', 'gigneo' ),
				esc_html( gigneo_freelancer_singular() )
			);
			?>
		</p>
	</div>
</div>