<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$service_id = $args['p_id'] ?? get_the_ID();

$profile_id          = gigneo_active_profile_id();
$author_id           = get_post_field( 'post_author', $service_id );
$author_profile_id   = gigneo_profile_id( $author_id, 'freelancer' );
$service_price_plans = gigneo_service_price_plan();
$delivery_time_types = gigneo_delivery_time_types();

$multi_tier = gigneo_post_meta( $service_id, '_gigneo_pm_price_multi_tier' );
$title_type = gigneo_option( '_gigneo_ts_service_pricing_title', 'custom' );

//Plan 1
$plan_one_title         = gigneo_post_meta( $service_id, '_gigneo_pm_price_plan_one_title' );
$plan_one_title         = ! empty( $plan_one_title ) ? $plan_one_title : '';
$plan_one_description   = gigneo_post_meta( $service_id, '_gigneo_pm_price_plan_one_description' );
$plan_one_description   = ! empty( $plan_one_description ) ? $plan_one_description : '';
$plan_one_time_type     = gigneo_post_meta( $service_id, '_gigneo_pm_price_plan_one_delivery_time_type' );
$plan_one_delivery_time = gigneo_post_meta( $service_id, '_gigneo_pm_price_plan_one_delivery_time' );
$plan_one_delivery_time = ! empty( $plan_one_delivery_time ) ? intval( $plan_one_delivery_time ) : 0;
$plan_one_revisions     = gigneo_post_meta( $service_id, '_gigneo_pm_price_plan_one_revisions' );
$plan_one_revisions     = ! empty( $plan_one_revisions ) ? $plan_one_revisions : esc_html__( 'Unlimited', 'gigneo' );
$plan_one_price         = gigneo_post_meta( $service_id, '_gigneo_pm_price_plan_one_price' );
$plan_one_price         = ! empty( $plan_one_price ) ? gigneo_price_format( $plan_one_price, true ) : '';
$plan_one_featured      = gigneo_post_meta( $service_id, '_gigneo_pm_price_plan_one_featured' );

// Plan 2
$plan_two_title         = gigneo_post_meta( $service_id, '_gigneo_pm_price_plan_two_title' );
$plan_two_title         = ! empty( $plan_two_title ) ? $plan_two_title : '';
$plan_two_description   = gigneo_post_meta( $service_id, '_gigneo_pm_price_plan_two_description' );
$plan_two_description   = ! empty( $plan_two_description ) ? $plan_two_description : '';
$plan_two_time_type     = gigneo_post_meta( $service_id, '_gigneo_pm_price_plan_two_delivery_time_type' );
$plan_two_delivery_time = gigneo_post_meta( $service_id, '_gigneo_pm_price_plan_two_delivery_time' );
$plan_two_delivery_time = ! empty( $plan_two_delivery_time ) ? $plan_two_delivery_time : '';
$plan_two_revisions     = gigneo_post_meta( $service_id, '_gigneo_pm_price_plan_two_revisions' );
$plan_two_revisions     = ! empty( $plan_two_revisions ) ? $plan_two_revisions : esc_html__( 'Unlimited', 'gigneo' );
$plan_two_price         = gigneo_post_meta( $service_id, '_gigneo_pm_price_plan_two_price' );
$plan_two_price         = ! empty( $plan_two_price ) ? gigneo_price_format( $plan_two_price, true ) : '';
$plan_two_featured      = gigneo_post_meta( $service_id, '_gigneo_pm_price_plan_two_featured' );

// Plan 3
$plan_three_title         = gigneo_post_meta( $service_id, '_gigneo_pm_price_plan_three_title' );
$plan_three_title         = ! empty( $plan_three_title ) ? $plan_three_title : '';
$plan_three_description   = gigneo_post_meta( $service_id, '_gigneo_pm_price_plan_three_description' );
$plan_three_description   = ! empty( $plan_three_description ) ? $plan_three_description : '';
$plan_three_time_type     = gigneo_post_meta( $service_id, '_gigneo_pm_price_plan_three_delivery_time_type' );
$plan_three_delivery_time = gigneo_post_meta( $service_id, '_gigneo_pm_price_plan_three_delivery_time' );
$plan_three_delivery_time = ! empty( $plan_three_delivery_time ) ? $plan_three_delivery_time : '';
$plan_three_revisions     = gigneo_post_meta( $service_id, '_gigneo_pm_price_plan_three_revisions' );
$plan_three_revisions     = ! empty( $plan_three_revisions ) ? $plan_three_revisions : esc_html__( 'Unlimited', 'gigneo' );
$plan_three_price         = gigneo_post_meta( $service_id, '_gigneo_pm_price_plan_three_price' );
$plan_three_price         = ! empty( $plan_three_price ) ? gigneo_price_format( $plan_three_price, true ) : '';
$plan_three_featured      = gigneo_post_meta( $service_id, '_gigneo_pm_price_plan_three_featured' );
$service_status           = ! empty( $service_id ) ? get_post_status( $service_id ) : '';

$features = array();

//Category Features
$category_term_query = new WP_Term_Query(
	array(
		'taxonomy'   => 'gigneo_facet',
		'object_ids' => $service_id,
		'hide_empty' => false,
	)
);


if ( ! empty( $category_term_query->terms ) && ! is_wp_error( $category_term_query ) ) {
	foreach ( $category_term_query->terms as $c_term ) {
		$service_features = gigneo_term_meta( $c_term->term_id, '_gigneo_tm_service_features' );
		if ( ! empty( $service_features ) ) {
			foreach ( $service_features as $key => $feature ) {
				if ( empty( $feature['title'] ) ) {
					continue;
				}
				$features[] = array(
					'title'      => $feature['title'],
					'plan_one'   => gigneo_post_meta( $service_id, "_gigneo_pm_price_plan_one_feature_$c_term->slug_$key" ),
					'plan_two'   => gigneo_post_meta( $service_id, "_gigneo_pm_price_plan_two_feature_$c_term->slug_$key" ),
					'plan_three' => gigneo_post_meta( $service_id, "_gigneo_pm_price_plan_three_feature_$c_term->slug_$key" ),
				);
			}
		}
	}
}

//Custom Features
$custom_offers = gigneo_post_meta( $service_id, '_gigneo_pm_custom_offers' );

if ( ! empty( $custom_offers ) && is_array( $custom_offers ) ) {
	foreach ( $custom_offers as $offer ) {
		$offer_title = ! empty( $offer['name'] ) ? $offer['name'] : '';
		if ( empty( $offer_title ) ) {
			continue;
		}
		$features[] = array(
			'title'      => $offer_title,
			'plan_one'   => $offer['plan_one'] ?? '',
			'plan_two'   => $offer['plan_two'] ?? '',
			'plan_three' => $offer['plan_three'] ?? '',
		);
	}
}

//Service Type
$service_type          = gigneo_service_type( $service_id );
$service_type_text     = esc_html__( 'Remote', 'gigneo' );
$service_delivery_type = gigneo_post_meta( $service_id, '_gigneo_pm_delivery_type' );

if ( 'onsite' === $service_type ) {
	if ( 'both' === $service_delivery_type ) {
		$service_type_text = esc_html__( 'Onsite - Hybrid', 'gigneo' );
	} elseif ( 'visit_me' === $service_delivery_type ) {
		$service_type_text = sprintf(
			// translators: %s is the freelancer singular
			esc_html__( 'Onsite - To %s location', 'gigneo' ),
			esc_html( gigneo_freelancer_singular() )
		);
	} elseif ( 'on_location' === $service_delivery_type ) {
		$service_type_text = esc_html__( 'Onsite - To your location', 'gigneo' );
	}
}

//Locations
$service_locations = gigneo_post_meta( $service_id, '_gigneo_pm_locations' );
$countries         = gigneo_allowed_countries();
$states            = gigneo_allowed_states();
$profile_type      = gigneo_profile_type();

$map_enabled = false;
?>
<div class="gn-section gn-pricing-section">
	<div class="gn-service-pricing-tabs-wrapper">
		<?php if ( 'yes' === $multi_tier ) { ?>
			<div class="gn-service-pricing-tabs">
				<label class="gn-tab">
					<input type="radio" value="one" name="plan" checked="checked">
					<span class="gn-title"><?php echo esc_html( 'custom' === $title_type ? $plan_one_title : $service_price_plans['one'] ); ?></span>
				</label>
				<label class="gn-tab">
					<input type="radio" name="plan" value="two">
					<span class="gn-title"><?php echo esc_html( 'custom' === $title_type ? $plan_two_title : $service_price_plans['two'] ); ?></span>
				</label>
				<label class="gn-tab">
					<input type="radio" name="plan" value="three">
					<span class="gn-title"><?php echo esc_html( 'custom' === $title_type ? $plan_three_title : $service_price_plans['three'] ); ?></span>
				</label>
			</div>
		<?php } else { ?>
			<input type="hidden" name="plan" value="one">
		<?php } ?>
		<div class="gn-service-pricing-tabs-content">
			<div id="gn-service-pricing-tab-content-plan-one" class="gn-service-pricing-tab-content gn-active">
				<div class="gn-service-plan-details">
					<div class="gn-head">
						<div class="gn-heading-area">
							<h4 class="gn-title"><?php echo esc_html( 'custom' === $title_type ? $plan_one_title : $service_price_plans['one'] ); ?></h4>
							<p class="gn-description"><?php echo esc_html( $plan_one_description ); ?></p>
						</div>
						<div class="gn-pricing-area">
							<span class="gn-label"><?php echo esc_html__( 'Starting from', 'gigneo' ); ?></span>
							<span class="gn-value"><?php echo esc_html( $plan_one_price ); ?></span>
						</div>
					</div>
					<div class="gn-body">
						<ul class="gn-features">
							<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 delivery time
										esc_html__( 'Delivery in %s', 'gigneo' ),
										'<strong>' . esc_html( gigneo_delivery_time( $plan_one_delivery_time, $plan_one_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: revision 2: round
									esc_html__( '%1$s %2$s of revisions', 'gigneo' ),
									'<strong>' . esc_html( $plan_one_revisions ) . '</strong>',
									'<strong>' . esc_html__( 'round', 'gigneo' ) . '</strong>'
								)
								?>
									</span>
							</li>
							<?php foreach ( $features as $key => $feature ) { ?>
								<li class="gn-feature gn-custom-feature gn-custom-feature-<?php echo esc_attr( $key ); ?> gn-<?php echo esc_attr( ! empty( $feature['plan_one'] ) ? 'active' : 'inactive' ); ?>">
									<i class="hi hi-<?php echo esc_attr( ! empty( $feature['plan_one'] ) ? 'check' : 'x-mark' ); ?>"
										aria-hidden="true"></i>
									<span class="gn-title"><?php echo esc_html( $feature['title'] ); ?></span>
								</li>
							<?php } ?>
						</ul>
					</div>
				</div>
			</div>
			<div id="gn-service-pricing-tab-content-plan-two" class="gn-service-pricing-tab-content">
				<div class="gn-service-plan-details">
					<div class="gn-head">
						<div class="gn-heading-area">
							<h4 class="gn-title"><?php echo esc_html( 'custom' === $title_type ? $plan_two_title : $service_price_plans['two'] ); ?></h4>
							<p class="gn-description"><?php echo esc_html( $plan_two_description ); ?></p>
						</div>
						<div class="gn-pricing-area">
							<span class="gn-label"><?php echo esc_html__( 'Starting from', 'gigneo' ); ?></span>
							<span class="gn-value"><?php echo esc_html( $plan_two_price ); ?></span>
						</div>
					</div>
					<div class="gn-body">
						<ul class="gn-features">
							<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 delivery time
										esc_html__( 'Delivery in %s', 'gigneo' ),
										'<strong>' . esc_html( gigneo_delivery_time( $plan_two_delivery_time, $plan_two_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: revision 2: round
									esc_html__( '%1$s %2$s of revisions', 'gigneo' ),
									'<strong>' . esc_html( $plan_two_revisions ) . '</strong>',
									'<strong>' . esc_html__( 'round', 'gigneo' ) . '</strong>'
								)
								?>
									</span>
							</li>
							<?php foreach ( $features as $key => $feature ) { ?>
								<li class="gn-feature gn-custom-feature gn-custom-feature-<?php echo esc_attr( $key ); ?> gn-<?php echo esc_attr( ! empty( $feature['plan_two'] ) ? 'active' : 'inactive' ); ?>">
									<i class="hi hi-<?php echo esc_attr( ! empty( $feature['plan_two'] ) ? 'check' : 'x-mark' ); ?>"
										aria-hidden="true"></i>
									<span class="gn-title"><?php echo esc_html( $feature['title'] ); ?></span>
								</li>
							<?php } ?>
						</ul>
					</div>
				</div>
			</div>
			<div id="gn-service-pricing-tab-content-plan-three" class="gn-service-pricing-tab-content">
				<div class="gn-service-plan-details">
					<div class="gn-head">
						<div class="gn-heading-area">
							<h4 class="gn-title"><?php echo esc_html( 'custom' === $title_type ? $plan_three_title : $service_price_plans['three'] ); ?></h4>
							<p class="gn-description"><?php echo esc_html( $plan_three_description ); ?></p>
						</div>
						<div class="gn-pricing-area">
							<span class="gn-label"><?php echo esc_html__( 'Starting from', 'gigneo' ); ?></span>
							<span class="gn-value"><?php echo esc_html( $plan_three_price ); ?></span>
						</div>
					</div>
					<div class="gn-body">
						<ul class="gn-features">
							<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 delivery time
									esc_html__( 'Delivery in %s', 'gigneo' ),
									'<strong>' . esc_html( gigneo_delivery_time( $plan_three_delivery_time, $plan_three_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: revision 2: round
									esc_html__( '%1$s %2$s of revisions', 'gigneo' ),
									'<strong>' . esc_html( $plan_three_revisions ) . '</strong>',
									'<strong>' . esc_html__( 'round', 'gigneo' ) . '</strong>'
								)
								?>
									</span>
							</li>
							<?php foreach ( $features as $key => $feature ) { ?>
								<li class="gn-feature gn-custom-feature gn-custom-feature-<?php echo esc_attr( $key ); ?> gn-<?php echo esc_attr( ! empty( $feature['plan_three'] ) ? 'active' : 'inactive' ); ?>">
								<i class="hi hi-<?php echo esc_attr( ! empty( $feature['plan_three'] ) ? 'check' : 'x-mark' ); ?>"
									aria-hidden="true"></i>
								<span class="gn-title"><?php echo esc_html( $feature['title'] ); ?></span>
								</li>
							<?php } ?>
						</ul>
					</div>
				</div>
			</div>
		</div>
	</div>
	<div class="gn-service-type-wrapper">
		<span class="gn-label"><?php echo esc_html__( 'Service type', 'gigneo' ); ?></span>
		<span class="gn-value"><?php echo esc_html( $service_type_text ); ?></span>
	</div>
	<?php
	//Locations
	if ( 'remote' !== $service_type ) {
		?>
		<div class="gn-service-location-wrapper">
			<div class="gn-service-delivery-type">
				<?php
				if ( 'both' === $service_delivery_type ) {
					?>
					<fieldset class="gn-form-fields gn-delivery-type-both gn-onsite-delivery-type">
						<div class="gn-form-field">
							<div class="gn-radio">
								<input type="radio" id="delivery_type_to_location" class="gn-required-field" name="delivery_type" value="on_location" checked>
								<label for="delivery_type_to_location">
								<?php
								printf(
										// translators: %s is the freelancer singular
									esc_html__( '%s travels to you', 'gigneo' ),
									esc_html( gigneo_freelancer_singular() )
								);
								?>
								</label>
							</div>
							<div class="gn-radio">
								<input type="radio" id="delivery_type_visit_freelancer" class="gn-required-field" name="delivery_type" value="visit_me">
								<label for="delivery_type_visit_freelancer">
								<?php
								printf(
										// translators: %s is the freelancer singular
									esc_html__( "I'll will visit %s", 'gigneo' ),
									esc_html( gigneo_freelancer_singular() )
								);
								?>
								</label>
							</div>
						</div>
					</fieldset>
					<?php
				}
				if ( ! empty( $service_locations ) && is_array( $service_locations ) ) {
					?>
					<fieldset class="gn-form-fields">
					<?php
					foreach ( $service_locations as $index => $location ) {
						$area          = ! empty( $location['area'] ) ? $location['area'] : '';
						$address       = ! empty( $location['address'] ) ? $location['address'] : '';
						$city          = ! empty( $location['city'] ) ? $location['city'] : '';
						$zip           = ! empty( $location['zip_code'] ) ? $location['zip_code'] : '';
						$country_state = explode( ':', $location['country_state'] );
						$country_code  = ! empty( $country_state[0] ) ? $country_state[0] : '';
						$state_code    = ! empty( $country_state[1] ) ? $country_state[1] : '';
						$country       = ! empty( $country_code ) && $countries[ $country_code ] ? $countries[ $country_code ] : '';
						$state         = ! empty( $location['country_state'] ) && $states[ $location['country_state'] ] ? $states[ $location['country_state'] ] : '';
						$list_cities   = gigneo_cities( $state_code );
						$city          = $list_cities[ $city ] ?? '';
						$city          = ( 'None' === $city ) ? '' : $city;

						?>
						<div class="gn-service-location">
							<div class="gn-form-field">
								<div class="gn-radio">
								<input type="radio" class="gn-required-field" id="service-location-<?php echo esc_attr( $index ); ?>"
										name="location_key" value="<?php echo esc_attr( $index ); ?>">
								<label for="service-location-<?php echo esc_attr( $index ); ?>">
									<span class="gn-title">
										<?php echo esc_html( implode( ', ', array_filter( array( $city, $state, $country ) ) ) ); ?>
									</span>
									<span class="gn-description"><?php echo esc_html( implode( ', ', array_filter( array( $area, $zip, $address ) ) ) ); ?></span>
								</label>
							</div>
							</div>
							<div class="gn-location-details gn-hide">
								<?php
								if ( 'visit_me' !== $service_delivery_type ) {
									?>
								<div class="gn-form-field gn-location-address-field">
									<label for="gn-hire-address-<?php echo esc_attr( $index ); ?>" class="gn-required-field-label"><?php echo esc_html__( 'Your Address', 'gigneo' ); ?></label>
									<textarea id="gn-hire-address-<?php echo esc_attr( $index ); ?>" name="location[<?php echo esc_attr( $index ); ?>][address]" class="gn-not-required-field" placeholder="<?php echo esc_attr__( 'Enter your address details', 'gigneo' ); ?>" rows="5"></textarea>
								</div>
								<?php } ?>
							</div>
						</div>
						<?php
					}
					?>
					</fieldset>
					<?php
				}
				?>
			</div>
		</div>
	<?php } ?>
	<div class="gn-actions">
		<?php if ( 'publish' === $service_status ) { ?>
			<input type="hidden" name="service_id" value="<?php echo esc_attr( $service_id ); ?>">
			<button type="submit" class="gn-btn gn-btn-primary"><?php echo esc_html__( 'Proceed to hire', 'gigneo' ); ?></button>
		<?php } elseif ( 'paused' === $service_status ) { ?>
			<div class="gn-notices-wrapper">
				<div class="gn-notice gn-notice-danger">
					<div class="gn-notice-icon">
						<i class="hi hi-exclamation-circle" aria-hidden="true"></i>
					</div>
					<div class="gn-notice-info">
						<h3 class="gn-title"><?php echo esc_html__( 'Paused', 'gigneo' ); ?></h3>
						<p class="gn-description"><?php echo esc_html__( 'This service is currently paused by the author and is not accepting new orders.', 'gigneo' ); ?></p>
					</div>
				</div>
			</div>
		<?php } ?>
		<?php
		do_action(
			'gigneo_action_buttons',
			array(
				'member_id'  => $author_profile_id,
				'profile_id' => $profile_id,
				'context'    => 'pricing',
			)
		);
		?>
	</div>
	<div class="gn-payment-info">
		<span class="gn-secure-payment">
			<i class="hi hi-lock-closed" aria-hidden="true"></i>
			<?php echo esc_html__( 'Secure payments', 'gigneo' ); ?>
		</span>
		<span class="gn-escrow-protection">
			<i class="hi hi-shield-check" aria-hidden="true"></i>
			<?php echo esc_html__( 'Escrow protection', 'gigneo' ); ?>
		</span>
	</div>
</div>
