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

$p_id         = $args['p_id'] ?? '';
$current_step = $args['current_step'] ?? '';
$redirect_url = $args['redirect_url'] ?? '';

// Fetch all feature categories & their features
$facet_query = new WP_Term_Query(
	array(
		'taxonomy'   => 'gigneo_facet',
		'object_ids' => $p_id,
		'hide_empty' => false,
		'fields'     => 'all',
		'orderby'    => 'name',
	)
);

$all_features = array();

if ( ! is_wp_error( $facet_query ) && ! empty( $facet_query->terms ) ) {
	foreach ( $facet_query->terms as $facet ) {
		$features = gigneo_term_meta( $facet->term_id, '_gigneo_tm_service_features' );
		if ( ! empty( $features ) && is_array( $features ) ) {
			foreach ( $features as $key => $feature ) {
				if ( ! empty( $feature['title'] ) ) {
					$all_features[ "{$facet->slug}_$key" ] = $feature['title'];
				}
			}
		}
	}
}

// Get saved checkbox value
function gigneo_feature_checkbox( $post_id, $plan, $key ) {
	$value = gigneo_post_meta( $post_id, "_gigneo_pm_price_plan_{$plan}_feature_{$key}" );
	return 'yes' === $value ? 'checked' : '';
}
?>
<div id="gn-edit-service-features" class="gn-edit-service-section<?php echo esc_attr( empty( $all_features ) ? ' d-none' : '' ); ?>">
	<?php if ( ! empty( $all_features ) ) { ?>
		<div class="gn-head">
			<div class="gn-heading-area">
				<h3 class="gn-title"><?php esc_html_e( 'What’s included', 'gigneo' ); ?></h3>
				<p class="gn-description"><?php esc_html_e( 'Specify which features are included in each pricing tier', 'gigneo' ); ?></p>
			</div>
		</div>
		<div class="gn-plan-tiers">
			<div class="gn-plan-tiers-content">
				<div class="gn-plan-tier-tabs-info">
					<?php foreach ( $all_features as $key => $feature ) { ?>
						<h6 class="gn-plan-field-label gn-plan-field-feature">
							<?php echo esc_html( $feature ); ?>
						</h6>
					<?php } ?>
				</div>
				<div class="gn-plan-tier-tabs-content">
					<!-- Basic -->
					<div class="gn-tab-content gn-tab-content-plan-basic gn-active">
						<div class="gn-form-fields">
							<?php
							$counter = 0;
							foreach ( $all_features as $key => $feature ) {
								$checked = gigneo_feature_checkbox( $p_id, 'one', $key );
								?>
								<div class="gn-form-field gn-plan-field gn-plan-field-feature">
									<div class="gn-checkbox">
										<input id="gn-plan-one-feature-<?php echo esc_attr( $counter ); ?>" type="checkbox" value="yes" name="plan_one_feature_<?php echo esc_attr( $key ); ?>"<?php echo esc_html( $checked ); ?>>
										<label for="gn-plan-one-feature-<?php echo esc_attr( $counter ); ?>"></label>
									</div>
								</div>
								<?php
								++$counter;
							}
							?>
						</div>
					</div>
					<!-- Standard -->
					<div class="gn-tab-content gn-tab-content-plan-standard">
						<div class="gn-form-fields">
							<?php
							$counter = 0;
							foreach ( $all_features as $key => $feature ) {
								$checked = gigneo_feature_checkbox( $p_id, 'two', $key );
								?>
								<div class="gn-form-field gn-plan-field gn-plan-field-feature">
									<div class="gn-checkbox">
										<input
												id="gn-plan-two-feature-<?php echo esc_attr( $counter ); ?>"
												type="checkbox" value="yes" name="plan_two_feature_<?php echo esc_attr( $key ); ?>"<?php echo esc_html( $checked ); ?>
										>
										<label for="gn-plan-two-feature-<?php echo esc_attr( $counter ); ?>"></label>
									</div>
								</div>
								<?php
								++$counter;
							}
							?>
						</div>
					</div>
					<!-- Premium -->
					<div class="gn-tab-content gn-tab-content-plan-premium">
						<div class="gn-form-fields">
							<?php
							$counter = 0;
							foreach ( $all_features as $key => $feature ) {
								$checked = gigneo_feature_checkbox( $p_id, 'three', $key );
								?>
								<div class="gn-form-field gn-plan-field gn-plan-field-feature">
									<div class="gn-checkbox">
										<input id="gn-plan-three-feature-<?php echo esc_attr( $counter ); ?>" type="checkbox" value="yes" name="plan_three_feature_<?php echo esc_attr( $key ); ?>"<?php echo esc_html( $checked ); ?>>
										<label for="gn-plan-three-feature-<?php echo esc_attr( $counter ); ?>"></label>
									</div>
								</div>
								<?php
								++$counter;
							}
							?>
						</div>
					</div>
				</div>
			</div>
		</div>
	<?php } ?>
</div>