<?php
/**
 * Discount Code Input Widget - Render Methods
 *
 * @package GGM_Commerce
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Render the premium widget content.
 *
 * @param GGM_Commerce_Widget_Discount_Code_Input $widget The widget instance.
 */
function ggmc_discount_code_input_render_premium_content( $widget ) {
	$settings = $widget->get_settings_for_display();

	// Check if Discounts class is available.
	if ( ! class_exists( 'GGM_Commerce_Discounts' ) ) {
		echo '<p>' . esc_html__( 'Discount functionality is not available.', 'ggm-commerce' ) . '</p>';
		return;
	}

	$discounts_handler = GGM_Commerce_Discounts::get_instance();
	$applied_discount  = $discounts_handler->get_applied_discount();

	$nonce        = wp_create_nonce( 'ggmc_discount_nonce' );
	$is_collapsed = 'yes' === $settings['collapsible'] && ! $applied_discount;
	?>
	<div class="ggmc-discount-input-wrapper" data-nonce="<?php echo esc_attr( $nonce ); ?>">
		<?php if ( 'yes' === $settings['show_label'] ) : ?>
			<div class="ggmc-discount-label <?php echo $settings['collapsible'] === 'yes' ? 'collapsible' : ''; ?>" <?php echo $settings['collapsible'] === 'yes' ? 'style="cursor: pointer;"' : ''; ?>>
				<?php echo esc_html( $settings['label_text'] ); ?>
				<?php if ( 'yes' === $settings['collapsible'] ) : ?>
					<span class="ggmc-discount-toggle"><?php echo $is_collapsed ? '+' : '-'; ?></span>
				<?php endif; ?>
			</div>
		<?php endif; ?>

		<div class="ggmc-discount-form-container" <?php echo $is_collapsed ? 'style="display: none;"' : ''; ?>>
			<?php if ( $applied_discount ) : ?>
				<div class="ggmc-applied-discount">
					<span class="ggmc-discount-code-applied">
						<?php echo esc_html( $applied_discount->code ); ?>
					</span>
					<button type="button" class="ggmc-discount-remove-btn" title="<?php esc_attr_e( 'Remove discount', 'ggm-commerce' ); ?>">
						&times;
					</button>
				</div>
			<?php else : ?>
				<div class="ggmc-discount-form">
					<input type="text" class="ggmc-discount-input" placeholder="<?php echo esc_attr( $settings['input_placeholder'] ); ?>" />
					<button type="button" class="ggmc-discount-apply-btn">
						<?php echo esc_html( $settings['button_text'] ); ?>
					</button>
				</div>
			<?php endif; ?>
			<div class="ggmc-discount-message" style="display: none;"></div>
		</div>
	</div>

	<?php // Styles moved to widgets.css (structural) and professional-defaults.css (design). ?>

	<script>
	(function($) {
		$(document).ready(function() {
			var $wrapper = $('.ggmc-discount-input-wrapper');

			// Toggle collapsible.
			$wrapper.find('.ggmc-discount-label.collapsible').on('click', function() {
				var $container = $(this).siblings('.ggmc-discount-form-container');
				var $toggle = $(this).find('.ggmc-discount-toggle');
				$container.slideToggle(200);
				$toggle.text($toggle.text() === '+' ? '-' : '+');
			});

			// Apply discount.
			$wrapper.find('.ggmc-discount-apply-btn').on('click', function() {
				var $btn = $(this);
				var $input = $wrapper.find('.ggmc-discount-input');
				var code = $input.val().trim();
				var nonce = $wrapper.data('nonce');

				if (!code) {
					showMessage('error', '<?php echo esc_js( __( 'Please enter a discount code.', 'ggm-commerce' ) ); ?>');
					return;
				}

				$btn.prop('disabled', true).text('<?php echo esc_js( __( 'Applying...', 'ggm-commerce' ) ); ?>');

				$.post(ggm_commerce_ajax.ajax_url, {
					action: 'ggmc_apply_discount',
					nonce: nonce,
					code: code,
					cart_total: window.ggmc_cart_total || 0
				}, function(response) {
					if (response.success) {
						showMessage('success', response.data.message);
						// Refresh page or update cart.
						setTimeout(function() {
							location.reload();
						}, 1000);
					} else {
						showMessage('error', response.data.message);
						$btn.prop('disabled', false).text('<?php echo esc_js( $settings['button_text'] ); ?>');
					}
				}).fail(function() {
					showMessage('error', '<?php echo esc_js( __( 'Request failed. Please try again.', 'ggm-commerce' ) ); ?>');
					$btn.prop('disabled', false).text('<?php echo esc_js( $settings['button_text'] ); ?>');
				});
			});

			// Remove discount.
			$wrapper.find('.ggmc-discount-remove-btn').on('click', function() {
				var nonce = $wrapper.data('nonce');

				$.post(ggm_commerce_ajax.ajax_url, {
					action: 'ggmc_remove_discount',
					nonce: nonce
				}, function(response) {
					if (response.success) {
						location.reload();
					}
				});
			});

			function showMessage(type, message) {
				var $message = $wrapper.find('.ggmc-discount-message');
				$message.removeClass('success error').addClass(type).text(message).show();
			}
		});
	})(jQuery);
	</script>
	<?php
}
