<?php
/**
 * Template: tiered buy box.
 *
 * Dostupna promenljiva: $box (niz sa svim podacima).
 * Tema može da override-uje ovaj fajl kopiranjem u woocommerce/tiered-buy-box.php.
 *
 * Elementi nose dvojne klase: nove `drbu-*` (primarne) i legacy `pg-*`
 * (fallback za postojeće teme). Uključivanje varijanti (radio toggle) se
 * oslanja na data-v atribut i definisano je u CSS fajlu plugina.
 *
 * @package discount-rules-box-upsell
 */

defined( 'ABSPATH' ) || exit;

if ( empty( $box ) || ! is_array( $box ) ) {
	return;
}

$drbu_product_id    = $box['product_id'];
$drbu_product       = $box['product'];
$drbu_base          = $box['base'];
$drbu_settings      = $box['settings'];
$drbu_tiers         = $box['tiers'];
$drbu_strings       = $box['strings'];
$drbu_prices        = $box['prices'];
$drbu_links         = $box['links'];
$drbu_sfx           = $box['sfx'];
$drbu_radio_ids     = $box['radio_ids'];
$drbu_best_index    = $box['best_index'];
$drbu_discount_type = $box['discount_type'];

$drbu_tier_count = isset( $box['tier_count'] ) ? max( 2, min( 3, (int) $box['tier_count'] ) ) : 3;

// Opcije prikaza — sve su podrazumevano uključene.
$drbu_enabled = array_merge(
	array(
		'free_shipping' => true,
		'footer_badges' => true,
		'savings'       => true,
		'best_value'    => true,
		'per_unit'      => true,
		'was_price'     => true,
	),
	isset( $box['enabled'] ) && is_array( $box['enabled'] ) ? $box['enabled'] : array()
);

$drbu_wrap_classes = 'pg-buy drbu-buy';
foreach ( array( 'free_shipping', 'footer_badges', 'savings', 'best_value', 'per_unit', 'was_price' ) as $drbu_opt ) {
	if ( empty( $drbu_enabled[ $drbu_opt ] ) ) {
		$drbu_wrap_classes .= ' drbu-no-' . str_replace( '_', '-', $drbu_opt );
	}
}

$drbu_per_unit_label = isset( $drbu_strings['per_unit_label'] ) ? $drbu_strings['per_unit_label'] : '';
$drbu_total_label    = isset( $drbu_strings['total_label'] ) ? $drbu_strings['total_label'] : '';
$drbu_pack_total     = isset( $drbu_strings['pack_total_label'] ) ? $drbu_strings['pack_total_label'] : '';
$drbu_savings_label  = isset( $drbu_strings['savings_label'] ) ? $drbu_strings['savings_label'] : '';
$drbu_in_stock       = isset( $drbu_strings['in_stock_label'] ) ? $drbu_strings['in_stock_label'] : '';
$drbu_best_label     = isset( $drbu_strings['best_label'] ) ? $drbu_strings['best_label'] : '';
?>
<div class="<?php echo esc_attr( $drbu_wrap_classes ); ?>" data-key="<?php echo esc_attr( $drbu_sfx ); ?>">
	<?php foreach ( $drbu_radio_ids as $drbu_i => $drbu_rid ) : ?>
		<input class="pg-radio drbu-radio" type="radio" name="<?php echo esc_attr( $drbu_sfx . '-pack' ); ?>" id="<?php echo esc_attr( $drbu_rid ); ?>" data-v="<?php echo esc_attr( $drbu_i + 1 ); ?>"<?php echo ( $drbu_i === $drbu_best_index ) ? ' checked="checked"' : ''; ?>>
	<?php endforeach; ?>

	<div class="pg-head drbu-head">
		<?php foreach ( array_slice( $drbu_prices, 0, $drbu_tier_count, true ) as $drbu_i => $drbu_p ) : ?>
			<div class="pg-price-v pg-v<?php echo esc_attr( $drbu_i + 1 ); ?> drbu-price-v drbu-v<?php echo esc_attr( $drbu_i + 1 ); ?>" data-v="<?php echo esc_attr( $drbu_i + 1 ); ?>">
				<span class="pg-num drbu-num"><?php echo wp_kses_post( $drbu_p['unit_html'] ); ?></span>
				<?php if ( ! empty( $drbu_enabled['per_unit'] ) ) : ?>
					<span class="pg-per drbu-per"><?php echo esc_html( $drbu_per_unit_label ); ?></span>
				<?php endif; ?>
				<?php if ( ! empty( $drbu_enabled['was_price'] ) && $drbu_i > 0 && $drbu_p['save'] > 0.005 ) : ?>
					<s class="pg-was drbu-was"><?php echo wp_kses_post( $drbu_p['base_html'] ); ?></s>
				<?php endif; ?>
			</div>
		<?php endforeach; ?>
		<span class="pg-instock drbu-instock"><?php echo esc_html( $drbu_in_stock ); ?></span>
	</div>

	<?php if ( ! empty( $drbu_enabled['free_shipping'] ) ) : ?>
		<?php foreach ( array_slice( $drbu_prices, 0, $drbu_tier_count, true ) as $drbu_i => $drbu_p ) : ?>
			<div class="pg-ship-v pg-ship-<?php echo esc_attr( $drbu_i + 1 ); ?> drbu-ship-v drbu-ship-<?php echo esc_attr( $drbu_i + 1 ); ?><?php echo $drbu_p['ship_free'] ? ' pg-free drbu-free' : ''; ?>" data-v="<?php echo esc_attr( $drbu_i + 1 ); ?>"><?php echo wp_kses_post( $drbu_p['ship_text'] ); ?></div>
		<?php endforeach; ?>
	<?php endif; ?>

	<div class="pg-packs drbu-packs">
		<?php foreach ( array_slice( $drbu_tiers, 0, $drbu_tier_count, true ) as $drbu_i => $drbu_tier ) : ?>
			<?php
			$drbu_p              = isset( $drbu_prices[ $drbu_i ] ) ? $drbu_prices[ $drbu_i ] : array();
			$drbu_rid            = isset( $drbu_radio_ids[ $drbu_i ] ) ? $drbu_radio_ids[ $drbu_i ] : '';
			$drbu_tier_label     = isset( $drbu_tier['label'] ) ? $drbu_tier['label'] : '';
			$drbu_tier_label     = str_replace( '{qty}', (string) ( isset( $drbu_p['qty'] ) ? $drbu_p['qty'] : ( $drbu_i + 1 ) ), $drbu_tier_label );
			$drbu_note           = isset( $drbu_tier['note'] ) ? (string) $drbu_tier['note'] : '';
			$drbu_note           = str_replace( '{qty}', (string) ( isset( $drbu_p['qty'] ) ? $drbu_p['qty'] : ( $drbu_i + 1 ) ), $drbu_note );
			$drbu_note_has_save  = ( false !== strpos( $drbu_note, '{save}' ) );
			$drbu_formatted_save = ( ! empty( $drbu_savings_label ) ? esc_html( $drbu_savings_label ) . ' ' : '' ) . $drbu_p['save_html'];

			$drbu_note = str_replace( '{d' . ( $drbu_i + 1 ) . '}', $drbu_p['discount_display'], $drbu_note );
			if ( ! empty( $drbu_enabled['savings'] ) && $drbu_p['save'] > 0.005 ) {
				$drbu_note = str_replace( '{save}', $drbu_formatted_save, $drbu_note );
			} else {
				$drbu_note = str_replace( '{save}', '', $drbu_note );
			}
			$drbu_note    = str_replace( '{price}', $drbu_p['total_html'], $drbu_note );
			$drbu_is_best = ( $drbu_i === $drbu_best_index );
			?>
			<label class="pg-pack drbu-pack<?php echo $drbu_is_best ? ' pg-is-best drbu-is-best' : ''; ?>" for="<?php echo esc_attr( $drbu_rid ); ?>" data-v="<?php echo esc_attr( $drbu_i + 1 ); ?>"><span class="pg-tile drbu-tile">
				<span class="pg-badges-wrap drbu-badges-wrap">
					<?php if ( ! empty( $drbu_enabled['free_shipping'] ) && $drbu_p['ship_free'] && '' !== $drbu_p['ship_flag_text'] ) : ?>
						<span class="pg-shipflag drbu-shipflag">✓ <?php echo esc_html( $drbu_p['ship_flag_text'] ); ?></span>
					<?php endif; ?>
					<?php if ( $drbu_is_best && ! empty( $drbu_enabled['best_value'] ) ) : ?>
						<span class="pg-flag drbu-flag"><?php echo esc_html( $drbu_best_label ); ?></span>
					<?php endif; ?>
				</span>
				<span class="pg-dot drbu-dot"></span>
				<span class="pg-pbody drbu-pbody">
					<b><?php echo esc_html( $drbu_tier_label ); ?></b>
					<span class="pg-pnote drbu-pnote"><?php echo wp_kses_post( $drbu_note ); ?><?php if ( ! empty( $drbu_enabled['savings'] ) && ! $drbu_note_has_save && $drbu_p['save'] > 0.005 ) : ?> <b><?php echo esc_html( $drbu_savings_label ); ?> <?php echo wp_kses_post( $drbu_p['save_html'] ); ?></b><?php endif; ?></span>
					<?php if ( ! empty( $drbu_enabled['per_unit'] ) ) : ?>
						<span class="pg-punit drbu-punit"><?php echo wp_kses_post( $drbu_p['unit_html'] ); ?> <?php echo esc_html( $drbu_per_unit_label ); ?></span>
					<?php endif; ?>
				</span>
				<span class="pg-pprice drbu-pprice"><?php echo wp_kses_post( $drbu_p['total_html'] ); ?><?php if ( ! empty( $drbu_enabled['was_price'] ) && $drbu_i > 0 && $drbu_p['save'] > 0.005 ) : ?><s><?php echo wp_kses_post( $drbu_p['was_html'] ); ?></s><?php endif; ?></span>
			</span></label>
		<?php endforeach; ?>
	</div>

	<div class="pg-cta-wrap drbu-cta-wrap">
		<?php foreach ( array_slice( $drbu_prices, 0, $drbu_tier_count, true ) as $drbu_i => $drbu_p ) : ?>
			<?php
			$drbu_cta_key        = 'cta' . ( $drbu_i + 1 );
			$drbu_cta            = isset( $drbu_strings[ $drbu_cta_key ] ) ? $drbu_strings[ $drbu_cta_key ] : '';
			$drbu_formatted_save = ( ! empty( $drbu_savings_label ) ? esc_html( $drbu_savings_label ) . ' ' : '' ) . $drbu_p['save_html'];
			$drbu_cta            = str_replace( '{price}', $drbu_p['total_html'], $drbu_cta );
			$drbu_cta            = str_replace( '{save}', ( $drbu_p['save'] > 0.005 ? $drbu_formatted_save : '' ), $drbu_cta );
			$drbu_cta            = str_replace( '{qty}', (string) $drbu_p['qty'], $drbu_cta );
			$drbu_cta            = str_replace( '{unit_price}', $drbu_p['unit_html'], $drbu_cta );
			?>
			<a class="pg-cta-v pg-cta-<?php echo esc_attr( $drbu_i + 1 ); ?> drbu-cta-v drbu-cta-<?php echo esc_attr( $drbu_i + 1 ); ?>" href="<?php echo esc_url( $drbu_links[ $drbu_i ] ); ?>" data-v="<?php echo esc_attr( $drbu_i + 1 ); ?>"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="9" cy="21" r="1"></circle><circle cx="20" cy="21" r="1"></circle><path d="M1 1h4l2.7 13.4a2 2 0 0 0 2 1.6h9.7a2 2 0 0 0 2-1.6L23 6H6"></path></svg> <?php echo wp_kses_post( $drbu_cta ); ?></a>
		<?php endforeach; ?>
	</div>

	<?php if ( ! empty( $drbu_enabled['footer_badges'] ) ) : ?>
		<?php
		$drbu_badge_lines = preg_split( '/\r\n|\r|\n/', trim( isset( $drbu_strings['footer_badges'] ) ? $drbu_strings['footer_badges'] : '' ) );
		$drbu_badge_lines = array_values( array_filter( $drbu_badge_lines, 'strlen' ) );
		?>
		<?php if ( ! empty( $drbu_badge_lines ) ) : ?>
			<div class="pg-pay drbu-pay">
				<?php foreach ( $drbu_badge_lines as $drbu_badge ) : ?>
					<span><span class="pg-pi drbu-pi"><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"></polyline></svg></span> <?php echo esc_html( $drbu_badge ); ?></span>
				<?php endforeach; ?>
			</div>
		<?php endif; ?>
	<?php endif; ?>
</div>
