<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

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

$multi_tier = gigneo_post_meta( $service_id, '_gigneo_pm_price_multi_tier' );

if ( 'yes' !== $multi_tier ) {
	return;
}

$service_price_plans = gigneo_service_price_plan();
$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' );

$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'] ?? '',
		);
	}
}
?>
<div class="gn-section gn-plan-comparison-section">
	<div class="gn-head">
		<h3 class="gn-title"><?php echo esc_html__( 'Pricing Comparison', 'gigneo' ); ?></h3>
	</div>
	<div class="gn-body">
		<div class="gn-table-wrapper gn-table-comparison">
			<table class="gn-table">
				<thead>
				<tr>
					<th class="gn-feature"><h6 class="gn-title"><?php echo esc_html__( 'Features', 'gigneo' ); ?></h6></th>
					<th class="gn-plan gn-plan-one">
						<h6 class="gn-title"><?php echo esc_html( 'custom' === $title_type ? $plan_one_title : $service_price_plans['one'] ); ?></h6>
						<h4 class="gn-price"><?php echo esc_html( $plan_one_price ); ?></h4>
					</th>
					<th class="gn-plan gn-plan-two">
						<h6 class="gn-title"><?php echo esc_html( 'custom' === $title_type ? $plan_two_title : $service_price_plans['two'] ); ?></h6>
						<h4 class="gn-price"><?php echo esc_html( $plan_two_price ); ?></h4>
					</th>
					<th class="gn-plan gn-plan-three">
						<h6 class="gn-title"><?php echo esc_html( 'custom' === $title_type ? $plan_three_title : $service_price_plans['three'] ); ?></h6>
						<h4 class="gn-price"><?php echo esc_html( $plan_three_price ); ?></h4>
					</th>
				</tr>
				</thead>
				<tbody>
				<tr>
					<td class="gn-feature">
						<h6 class="gn-title"><?php echo esc_html__( 'Delivery Time', 'gigneo' ); ?></h6>
					</td>
					<td class="gn-plan gn-plan-one">
						<?php if ( ! empty( $plan_one_delivery_time ) && ! empty( $plan_one_time_type ) ) { ?>
							<span class="gn-value"><?php echo esc_html( gigneo_delivery_time( $plan_one_delivery_time, $plan_one_time_type ) ); ?></span>
						<?php } ?>
					</td>
					<td class="gn-plan gn-plan-two">
						<?php if ( ! empty( $plan_two_delivery_time ) && ! empty( $plan_two_time_type ) ) { ?>
							<span class="gn-value"><?php echo esc_html( gigneo_delivery_time( $plan_two_delivery_time, $plan_two_time_type ) ); ?></span>
						<?php } ?>
					</td>
					<td class="gn-plan gn-plan-three">
						<?php if ( ! empty( $plan_three_delivery_time ) && ! empty( $plan_three_time_type ) ) { ?>
							<span class="gn-value"><?php echo esc_html( gigneo_delivery_time( $plan_three_delivery_time, $plan_three_time_type ) ); ?></span>
						<?php } ?>
				</tr>
				<tr>
					<td class="gn-feature">
						<h6 class="gn-title"><?php echo esc_html__( 'Revisions', 'gigneo' ); ?></h6>
					</td>
					<td class="gn-plan gn-plan-one">
						<?php if ( ! empty( $plan_one_revisions ) ) { ?>
							<span class="gn-value"><?php echo esc_html( $plan_one_revisions ); ?></span>
						<?php } ?>
					</td>
					<td class="gn-plan gn-plan-two">
						<?php if ( ! empty( $plan_two_revisions ) ) { ?>
							<span class="gn-value"><?php echo esc_html( $plan_two_revisions ); ?></span>
						<?php } ?>
					</td>
					<td class="gn-plan gn-plan-three">
						<?php if ( ! empty( $plan_three_revisions ) ) { ?>
							<span class="gn-value"><?php echo esc_html( $plan_three_revisions ); ?></span>
						<?php } ?>
					</td>
				</tr>
				<?php
				foreach ( $features as $feature ) {
					?>
					<tr>
						<td class="gn-feature">
							<h6 class="gn-title"><?php echo esc_html( $feature['title'] ); ?></h6>
						</td>
						<td class="gn-plan gn-plan-one gn-<?php echo esc_html( ! empty( $feature['plan_one'] ) ? 'active' : 'inactive' ); ?>">
							<i class="hi hi-<?php echo esc_html( ! empty( $feature['plan_one'] ) ? 'check' : 'x-mark' ); ?>"></i>
						</td>
						<td class="gn-plan gn-plan-two gn-<?php echo esc_html( ! empty( $feature['plan_two'] ) ? 'active' : 'inactive' ); ?>">
							<i class="hi hi-<?php echo esc_html( ! empty( $feature['plan_two'] ) ? 'check' : 'x-mark' ); ?>"></i>
						</td>
						<td class="gn-plan gn-plan-three gn-<?php echo esc_html( ! empty( $feature['plan_three'] ) ? 'active' : 'inactive' ); ?>">
							<i class="hi hi-<?php echo esc_html( ! empty( $feature['plan_three'] ) ? 'check' : 'x-mark' ); ?>"></i>
						</td>
					</tr>
					<?php
				}
				?>
				</tbody>
			</table>
		</div>
	</div>
</div>