<?php

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

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

$custom_offers = gigneo_option( '_gigneo_ts_service_custom_offers', true );

if ( false === $custom_offers ) {
	return;
}

$custom_offers = gigneo_post_meta( $p_id, '_gigneo_pm_custom_offers' );
$custom_offers = ! empty( $custom_offers ) ? $custom_offers : array();
?>
<div id="gn-edit-service-offers" class="gn-edit-service-section gn-edit-service-offers">
	<div class="gn-head">
		<div class="gn-heading-area">
			<h3 class="gn-title"><?php echo esc_html__( 'Custom offers', 'gigneo' ); ?></h3>
			<p class="gn-description"><?php echo esc_html__( 'Add tailored offers to personalise your service.', 'gigneo' ); ?></p>
		</div>
		<div class="gn-actions">
			<button type="button" class="gn-link gn-link-primary gn-add-custom-offer">
				<i class="hi hi-plus" aria-hidden="true"></i>
				<?php echo esc_html__( 'Add offer', 'gigneo' ); ?>
			</button>
		</div>
	</div>
	<div class="gn-plan-tiers">
		<div class="gn-plan-tiers-content">
			<div class="gn-plan-tier-tabs-info">
				<div class="gn-form-fields">
					<?php
					if ( ! empty( $custom_offers ) && is_array( $custom_offers ) ) {
						foreach ( $custom_offers as $index => $offer ) {
							$offer_name = ! empty( $offer['name'] ) ? $offer['name'] : '';
							?>
							<div class="gn-form-field gn-plan-field gn-plan-field-offer gn-plan-field-offer-<?php echo esc_attr( $index ); ?>">
								<label class="gn-required-field-label" for="gn-custom-offer-title-<?php echo intval( $index ); ?>">
									<?php echo esc_html__( 'Title', 'gigneo' ); ?>
								</label>
								<input id="gn-custom-offer-title-<?php echo intval( $index ); ?>" class="gn-required-field" type="text"
										name="custom_offers[<?php echo intval( $index ); ?>][name]"
										autocomplete="off" value="<?php echo esc_attr( $offer_name ); ?>">
								<button type="button" class="gn-btn gn-remove-custom-offer" data-gn-offer-id="<?php echo esc_attr( $index ); ?>">
									<i class="hi hi-trash" aria-hidden="true"></i>
								</button>
							</div>
							<?php
						}
					}
					?>
				</div>
			</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
						if ( ! empty( $custom_offers ) && is_array( $custom_offers ) ) {
							foreach ( $custom_offers as $index => $offer ) {
								$offer_name = ! empty( $offer['name'] ) ? $offer['name'] : '';
								?>
								<div class="gn-form-field gn-plan-field gn-plan-field-offer gn-plan-field-offer-<?php echo esc_attr( $index ); ?>">
									<div class="gn-checkbox">
										<input id="gn-plan-one-<?php echo intval( $index ); ?>" type="checkbox" value="yes" name="custom_offers[<?php echo intval( $index ); ?>][plan_one]" <?php echo ! empty( $offer['plan_one'] ) ? 'checked' : ''; ?>>
										<label for="gn-plan-one-<?php echo intval( $index ); ?>"></label>
									</div>
								</div>
								<?php
							}
						}
						?>
					</div>
				</div>
				<!-- Standard -->
				<div class="gn-tab-content gn-tab-content-plan-standard">
					<div class="gn-form-fields">
						<?php
						if ( ! empty( $custom_offers ) && is_array( $custom_offers ) ) {
							foreach ( $custom_offers as $index => $offer ) {
								$offer_name = ! empty( $offer['name'] ) ? $offer['name'] : '';
								?>
								<div class="gn-form-field gn-plan-field gn-plan-field-offer gn-plan-field-offer-<?php echo esc_attr( $index ); ?>">
									<div class="gn-checkbox">
										<input id="gn-plan-two-<?php echo intval( $index ); ?>" type="checkbox" value="yes" name="custom_offers[<?php echo intval( $index ); ?>][plan_two]" <?php echo ! empty( $offer['plan_two'] ) ? 'checked' : ''; ?>>
										<label for="gn-plan-two-<?php echo intval( $index ); ?>"></label>
									</div>
								</div>
								<?php
							}
						}
						?>
					</div>
				</div>
				<!-- Premium -->
				<div class="gn-tab-content gn-tab-content-plan-premium">
					<div class="gn-form-fields">
						<?php
						if ( ! empty( $custom_offers ) && is_array( $custom_offers ) ) {
							foreach ( $custom_offers as $index => $offer ) {
								$offer_name = ! empty( $offer['name'] ) ? $offer['name'] : '';
								?>
								<div class="gn-form-field gn-plan-field gn-plan-field-offer gn-plan-field-offer-<?php echo esc_attr( $index ); ?>">
									<div class="gn-checkbox">
										<input id="gn-plan-three-<?php echo intval( $index ); ?>" type="checkbox" value="yes" name="custom_offers[<?php echo intval( $index ); ?>][plan_three]" <?php echo ! empty( $offer['plan_three'] ) ? 'checked' : ''; ?>>
										<label for="gn-plan-three-<?php echo intval( $index ); ?>"></label>
									</div>
								</div>
								<?php
							}
						}
						?>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<!--Custom Offer Input-->
<script type="text/template" id="tmpl-gn-custom-offer-input">
	<div class="gn-form-field gn-plan-field gn-plan-field-offer gn-plan-field-offer-{{data.index}}">
		<label class="gn-required-field-label" for="gn-custom-offer-title-{{data.index}}">
			<?php echo esc_html__( 'Title', 'gigneo' ); ?>
		</label>
		<input id="gn-custom-offer-title-{{data.index}}" class="gn-required-field" type="text"
				name="custom_offers[{{data.index}}][name]"
				autocomplete="off" value="">
		<button type="button" class="gn-btn gn-remove-custom-offer" data-gn-offer-id="{{data.index}}">
			<i class="hi hi-trash" aria-hidden="true"></i>
		</button>
	</div>
</script>
<!--Custom Offer Checkbox-->
<script type="text/template" id="tmpl-gn-custom-offer-checkbox">
	<div class="gn-form-field gn-plan-field gn-plan-field-offer gn-plan-field-offer-{{data.index}}">
		<div class="gn-checkbox">
			<input id="gn-plan-{{data.plan}}-{{data.index}}" type="checkbox" value="yes" name="custom_offers[{{data.index}}][plan_{{data.plan}}]">
			<label for="gn-plan-{{data.plan}}-{{data.index}}"></label>
		</div>
	</div>
</script>