<?php

use MeowCrew\CancellationOffers\Offer\Entity\DiscountOffer\DiscountOffer;
use MeowCrew\CancellationOffers\Offer\Entity\Offer;

defined('ABSPATH') || exit;

/**
 * Available variables
 *
 * @var string $take_discount_url
 * @var string $cancel_subscription_url
 * @var bool $is_preview
 * @var Offer $offer
 * @var WC_Subscription $subscription
 * @var string $popup_style
 * @var string $popup_layout
 * @var string $background_image
 */

$labels = apply_filters('cancellation_offers/frontend/popup/labels', array(
	'survey' => array(
		'continue_button' => __('Continue', 'cancellation-surveys-offers-woocommerce-subscriptions'),
		'never_mind_button' => __(
			'Never mind, I\'ve changed my mind',
			'cancellation-surveys-offers-woocommerce-subscriptions'
		),
		'textarea_placeholder' => __('Please specify', 'cancellation-surveys-offers-woocommerce-subscriptions'),
		// translators: %d is the minimum number of characters required for the text answer.
		'textarea_required' => sprintf(__(
				'Required (minimum %d characters)',
				'cancellation-surveys-offers-woocommerce-subscriptions'
			),
			$offer->getSurvey()->getTextAnswerMinimumLength()
		),
	),
), $offer, $subscription);

$isAnyDiscountOfferApplicable = false;

foreach ($offer->getDiscountOffers() as $discountOffer) {
	if ($discountOffer->isApplicableForSubscription($subscription)) {
		$isAnyDiscountOfferApplicable = true;
		break;
	}
}

$accentColor = $offer->getSettings()->getAccentColor();
?>

<div id="cancellation-offer-popup"
	class="cancellation-offer-popup--<?php echo esc_attr($popup_style); ?> cancellation-offer-popup--layout-<?php echo esc_attr($popup_layout); ?>"
	style="--cos-accent-color: <?php echo esc_attr($accentColor); ?>;"
	data-is-preview="<?php echo esc_html(wc_bool_to_string($is_preview)); ?>"
	data-is-survey-enabled="<?php echo esc_html(wc_bool_to_string($offer->getSurvey()->isEnabled())); ?>"
	data-survey-is-text-answer-required="<?php echo esc_html(wc_bool_to_string($offer->getSurvey()->isTextAnswerRequired())); ?>"
	data-survey-textarea-min-length="<?php echo esc_attr($offer->getSurvey()->getTextAnswerMinimumLength()); ?>"
	data-survey-textarea-max-length="<?php echo esc_attr($offer->getSurvey()->getTextAnswerMaximumLength()); ?>"
	data-is-discount-offer-enabled="<?php echo esc_html(wc_bool_to_string($isAnyDiscountOfferApplicable)); ?>"
	data-cancel-subscription-url="<?php echo esc_attr($cancel_subscription_url); ?>"
	data-take-discount-url="<?php echo esc_attr($take_discount_url); ?>">

		<?php if ('split' === $popup_layout): ?>
			<div class="cancellation-offer-popup__sidebar"
				style="background-image: url('<?php echo esc_url($background_image); ?>');">
			</div>
			<div class="cancellation-offer-popup__content">
		<?php endif; ?>

		<div style="display: none" class="cancellation-offer-popup-survey">

			<?php do_action('cancellation_offers/frontend/popup/before_title', $offer, $subscription, $labels); ?>

			<?php if ($offer->getSurvey()->getTitle()): ?>
				<h2 class="cancellation-offer-popup-survey__title" style="text-align: center">
					<?php echo esc_html($offer->getSurvey()->getTitle()); ?>
				</h2>
			<?php endif; ?>

			<?php do_action('cancellation_offers/frontend/popup/after_title', $offer, $subscription, $labels); ?>

			<div class="cancellation-offer-popup-survey__description" style="text-align: center">
				<?php echo wp_kses_post(do_shortcode($offer->getSurvey()->getDescription())); ?>
			</div>

			<div class="cancellation-offer-popup-survey__questions">
				<div class="cancellation-offer-popup-survey-items">
					<?php foreach ($offer->getSurvey()->getItems() as $item): ?>
						<div class="cancellation-offer-popup-survey-item" tabindex="0" role="button" aria-pressed="false"
							data-slug="<?php echo esc_attr($item->getSlug()); ?>"
							data-is-text-answer-enabled="<?php echo esc_attr(wc_bool_to_string($item->isTextAnswerEnabled())); ?>"
							data-is-discount-offer-enabled="<?php echo esc_attr(wc_bool_to_string($item->isDiscountOfferEnabled($offer))); ?>">

							<div class="cancellation-offer-popup-survey-item__checkbox">
								<div
									class="cancellation-offer-popup-survey-item-checkbox cancellation-offer-popup-survey-item--active">
								</div>
							</div>

							<div class="cancellation-offer-popup-survey-item__text">
								<b><?php echo esc_attr($item->getTitle()); ?></b>
							</div>
						</div>
					<?php endforeach; ?>
				</div>

				<?php
				do_action(
					'cancellation_offers/frontend/popup/after_survey_questions',
					$offer,
					$subscription,
					$labels
				);
				?>

				<div style="margin-top: 20px; display:none; width: 100%"
					class="cancellation-offer-popup-survey-items__textarea">
					<div>
						<textarea style="width: 100%" minlength="5" maxlength="10000"
							placeholder="<?php echo esc_html($labels['survey']['textarea_placeholder']); ?>"
							rows="3"></textarea>
					</div>

					<?php if ($offer->getSurvey()->isTextAnswerRequired()): ?>
						<div class="cancellation-offer-popup-survey-items__textarea__requirements"
							style="display: flex; justify-content: space-between">

							<div class="cancellation-offer-popup-survey-items__textarea__required-text">
								<small><?php echo esc_html($labels['survey']['textarea_required']); ?></small>
							</div>

							<div class="cancellation-offer-popup-survey-items__textarea__character-count">
								<small>
									<span>0 / 10000</span>
								</small>
							</div>
						</div>
					<?php endif; ?>

				</div>
			</div>

			<?php do_action('cancellation_offers/frontend/popup/after_survey_block', $offer, $subscription, $labels); ?>

			<div class="cancellation-offer-popup-survey__buttons">
				<div>
					<button class="button primary-button cancellation-offer-popup-survey__continue-button" disabled>
						<?php echo esc_html($labels['survey']['continue_button']); ?>
					</button>
				</div>
				<div>
					<a href="#" role="button" class="cancellation-offer-popup-survey__dismiss-button">
						<?php echo esc_html($labels['survey']['never_mind_button']); ?>
					</a>
				</div>

				<?php do_action('cancellation_offers/frontend/popup/survey_buttons', $offer, $subscription, $labels); ?>
			</div>
		</div>

		<?php

		foreach ($offer->getDiscountOffers() as $index => $discountOffer) {
			/**
 			* Available variable in the scope of this loop
			* @var DiscountOffer $discountOffer
			 */

			 if (!$discountOffer->isApplicableForSubscription($subscription) ) {
				continue;
			}
			 ?>
			 	<div style="display: none" class="cancellation-offer-popup-discount-offer"
				data-offer-index="<?php echo esc_attr($index); ?>"
				data-survey-slugs="<?php echo esc_attr(implode(',', $discountOffer->getSurveyOptions())); ?>">

				<?php
				do_action('cancellation_offers/frontend/popup/before_discount_title', $offer, $subscription, $labels);
				?>

				<?php if ($discountOffer->getTitle()): ?>

					<h2 class="cancellation-offer-popup-discount-offer__title" style="text-align: center">
						<?php echo esc_html($discountOffer->getTitle()); ?>
					</h2>

				<?php endif; ?>

				<?php do_action('cancellation_offers/frontend/popup/after_discount_title', $offer, $subscription, $labels); ?>

				<div class="cancellation-offer-popup-discount-offer__description" style="text-align: center">
					<?php echo wp_kses_post(do_shortcode($discountOffer->getDescription())); ?>
				</div>

				<?php
				do_action(
					'cancellation_offers/frontend/popup/after_discount_description',
					$offer,
					$subscription,
					$labels
				);
				?>

				<div class="cancellation-offer-popup-discount-offer__buttons" style="text-align: center; margin-top: 20px">
					<div>
						<button class="button primary-button cancellation-offer-popup-discount-offer__continue-button">
							<?php echo esc_html($discountOffer->getApplyDiscountButtonLabel()); ?>
						</button>
					</div>
					<br>
					<div>
						<a href="#" class="cancellation-offer-popup-discount-offer__dismiss-button">
							<?php echo esc_html($discountOffer->getCancelSubscriptionButtonLabel()); ?>
						</a>
					</div>

					<?php do_action('cancellation_offers/frontend/popup/discount_buttons', $offer, $subscription, $labels); ?>
				</div>

			</div>
			 <?php
		}
		?>

		<?php if ('split' === $popup_layout): ?>
		</div>
	<?php endif; ?>