<?php namespace MeowCrew\CancellationOffers\Offer\CPT\Columns;

use MeowCrew\CancellationOffers\Offer\Entity\Offer;
use MeowCrew\CancellationOffers\SurveyAnswers\SurveyAnswersPage\SurveyAnswersPage;

class SurveyAnswersColumn {
	
	public function getName(): string {
		return __( 'Top Survey Answers', 'cancellation-surveys-offers-woocommerce-subscriptions' );
	}
	
	public function render( Offer $offer ) {
		$surveyResults = $offer->getStatistics()->getSurveyResults();
		
		$surveyResults = array_slice( $surveyResults, 0, 3 );
		
		if ( empty( $surveyResults ) ) {
			esc_html_e( 'N/A', 'cancellation-surveys-offers-woocommerce-subscriptions' );
			
			return;
		}
		
		?>

		<div>
			<?php foreach ( $surveyResults as $surveyResult ) : ?>
				<div class="cancellation-offer-popup-survey__survey-statistics-item">
					<div class="cancellation-offer-popup-survey__survey-statistics-item__label">
						<b><?php echo esc_html( $surveyResult['answer'] ); ?></b>
						(<?php echo esc_html( $surveyResult['answer_count'] ); ?>)
					</div>
					<div class="cancellation-offer-popup-survey__survey-statistics-item__progress-bar">
						<div class="cancellation-offer-popup-survey__survey-statistics-item__progress-bar-inner"
							 style="width: <?php echo esc_html( $surveyResult['answer_count_rate'] ); ?>%">
							<?php echo esc_html( $surveyResult['answer_count_rate'] ); ?>%
						</div>
					</div>
				</div>
			<?php endforeach; ?>
		</div>
		
		<?php
		$seeAllSurveyAnswersURL = add_query_arg( array(
			'page'     => SurveyAnswersPage::PAGE_SLUG,
			'offer_id' => $offer->getId(),
		), admin_url( 'admin.php' ) );
		?>

		<div>
			<a href="<?php echo esc_attr( $seeAllSurveyAnswersURL ); ?>" target="_blank">
				<?php
				esc_html_e( 'See all survey answers',
					'cancellation-surveys-offers-woocommerce-subscriptions' );
				?>
				<svg
						style="
							width: 0.8rem;
							height: 0.8rem;
							stroke: currentColor;
							fill: none;"
						xmlns='http://www.w3.org/2000/svg'
						stroke-width='10' stroke-dashoffset='0'
						stroke-dasharray='0' stroke-linecap='round'
						stroke-linejoin='round' viewBox='0 0 100 100'>
					<polyline fill="none" points="40 20 20 20 20 90 80 90 80 60"/>
					<polyline fill="none" points="60 10 90 10 90 40"/>
					<line fill="none" x1="89" y1="11" x2="50" y2="50"/>
				</svg>
			</a>
		</div>
		<?php
	}
}
