<?php
/**
 * General Settings Tab
 */
if ( ! defined( 'ABSPATH' ) ) exit;

function spar_settings_tab_general() {
	$defaults = spar_settings_default();
	$options = get_option( 'spar_options', $defaults );
	$options = array_merge( $defaults, $options );
	$earn_options         = isset( $options['earn'] ) && is_array( $options['earn'] ) ? $options['earn'] : [];
	$order_earning_enabled = ! empty( $earn_options['order']['enabled'] ) || ! empty( $earn_options['order_fixed']['enabled'] );
	$show_on_product = ! empty( $options['show_on_product'] );
	$show_on_product_loop = ! empty( $options['show_on_product_loop'] );
	$product_message      = $options['product_points_message'] ?? '';
	$product_loop_message = $options['product_loop_points_message'] ?? '';
	$product_default_message = function_exists( 'spar_get_default_points_message' )
		? spar_get_default_points_message( 'single' )
		: esc_html__( 'Earn {points} {points_label_lower} by purchasing this product', 'simple-points-and-rewards' );
	$product_loop_default_message = function_exists( 'spar_get_default_points_message' )
		? spar_get_default_points_message( 'loop' )
		: esc_html__( 'Earn {points} reward points from this product', 'simple-points-and-rewards' );

	$placeholder_items = array(
		'{points}'             => esc_html__( 'Replaced with the numeric points amount.', 'simple-points-and-rewards' ),
		'{points_label}'       => esc_html__( 'Replaced with the configured points label.', 'simple-points-and-rewards' ),
		'{points_label_lower}' => esc_html__( 'Replaced with the lowercase points label.', 'simple-points-and-rewards' ),
	);

	// Add {points_value} placeholder when Points Discount on Checkout is enabled.
	if ( ! empty( $options['redeem_individual_enabled'] ) ) {
		$placeholder_items['{points_value}'] = esc_html__( 'Replaced with the monetary value of the earned points based on the "Points Discount on Checkout" settings.', 'simple-points-and-rewards' );
	}

	ob_start();
	?>
	<div class="spar-placeholder-help" role="note">
		<strong><?php esc_html_e( 'Available placeholders:', 'simple-points-and-rewards' ); ?></strong>
		<ul>
			<?php foreach ( $placeholder_items as $token => $description ) : ?>
				<li><code><?php echo esc_html( $token ); ?></code> &ndash; <?php echo esc_html( $description ); ?></li>
			<?php endforeach; ?>
		</ul>
	</div>
	<?php
	$placeholder_details_markup = ob_get_clean();
	?>
	<h3><?php esc_html_e( 'General Settings', 'simple-points-and-rewards' ); ?></h3>
	<p><label><?php esc_html_e( 'Rewards Label:', 'simple-points-and-rewards' ); ?> <input type="text" name="rewards_label" value="<?php echo esc_attr( $options['rewards_label'] ); ?>" /></label></p>
	<p style="margin-bottom: -5px;"><label><?php esc_html_e( 'Points Label:', 'simple-points-and-rewards' ); ?> <input type="text" name="points_label" value="<?php echo esc_attr( $options['points_label'] ); ?>" /></label></p>
	
	<div id="spar-product-points-display-section"<?php echo $order_earning_enabled ? '' : ' style="display:none;"'; ?>>
	<h3><?php esc_html_e( 'Product Pages: Points Display', 'simple-points-and-rewards' ); ?></h3>
	<p><label><div class="spar-toggle-switch"><input type="checkbox" name="show_on_product" <?php checked( $show_on_product ); ?> /><span class="spar-toggle-slider"></span></div> <?php esc_html_e( 'Show points on product pages', 'simple-points-and-rewards' ); ?></label></p>
	<div class="spar-toggle-extra-settings spar-product-toggle<?php echo $show_on_product ? '' : ' spar-hidden'; ?>" data-toggle-input="show_on_product">
		<p>
			<label for="product_points_location"><?php esc_html_e( 'Product points display location:', 'simple-points-and-rewards' ); ?></label>
			<select name="product_points_location" id="product_points_location">
				<?php
				$locations = function_exists( 'spar_get_product_points_location_options' ) ? spar_get_product_points_location_options() : array();
				$selected_location = $options['product_points_location'] ?? 'summary_after_price';
				foreach ( $locations as $location_key => $location_data ) {
					$label = isset( $location_data['label'] ) ? $location_data['label'] : $location_key;
					printf(
						'<option value="%1$s" %2$s>%3$s</option>',
						esc_attr( $location_key ),
						selected( $selected_location, $location_key, false ),
						esc_html( $label )
					);
				}
				?>
			</select>
		</p>
		<p>
			<label for="product_points_message"><?php esc_html_e( 'Product points message:', 'simple-points-and-rewards' ); ?></label>
			<input type="text" name="product_points_message" id="product_points_message" value="<?php echo esc_attr( $product_message ); ?>" placeholder="<?php echo esc_attr( $product_default_message ); ?>" class="regular-text" />
			<small class="description"><?php esc_html_e( 'Leave blank to use the default message.', 'simple-points-and-rewards' ); ?></small>
		</p>
		<?php echo wp_kses_post( $placeholder_details_markup ); ?>
		<?php $product_points_text_color = isset( $options['product_points_text_color'] ) ? sanitize_hex_color( $options['product_points_text_color'] ) : ''; ?>
		<p style="margin-top: 16px;">
			<label for="spar-product-points-text-color"><?php esc_html_e( 'Text Color:', 'simple-points-and-rewards' ); ?></label>
			<input id="spar-product-points-text-color" type="color" name="product_points_text_color" value="<?php echo esc_attr( $product_points_text_color ?: '#495057' ); ?>" />
			<small class="description"><?php esc_html_e( 'Controls the font color of the points message on product pages.', 'simple-points-and-rewards' ); ?></small>
		</p>
		<?php $product_points_bg_color = isset( $options['product_points_bg_color'] ) ? sanitize_hex_color( $options['product_points_bg_color'] ) : ''; ?>
		<p style="margin-top: 10px;">
			<label for="spar-product-points-bg-color"><?php esc_html_e( 'Background Color:', 'simple-points-and-rewards' ); ?></label>
			<input id="spar-product-points-bg-color" type="color" name="product_points_bg_color" value="<?php echo esc_attr( $product_points_bg_color ?: '#eaf1fa' ); ?>" />
			<small class="description"><?php esc_html_e( 'Controls the background color of the points message on product pages.', 'simple-points-and-rewards' ); ?></small>
		</p>
	</div>
	<p><label><div class="spar-toggle-switch"><input type="checkbox" name="show_on_product_loop" <?php checked( $show_on_product_loop ); ?> /><span class="spar-toggle-slider"></span></div> <?php esc_html_e( 'Show points on category/shop product listings', 'simple-points-and-rewards' ); ?></label></p>
	<div class="spar-toggle-extra-settings spar-loop-toggle<?php echo $show_on_product_loop ? '' : ' spar-hidden'; ?>" data-toggle-input="show_on_product_loop">
		<p>
			<label for="product_loop_points_location"><?php esc_html_e( 'Catalog points display location:', 'simple-points-and-rewards' ); ?></label>
			<select name="product_loop_points_location" id="product_loop_points_location">
				<?php
				$loop_locations = function_exists( 'spar_get_product_loop_points_location_options' ) ? spar_get_product_loop_points_location_options() : array();
				$selected_loop_location = $options['product_loop_points_location'] ?? 'loop_after_title';
				foreach ( $loop_locations as $location_key => $location_data ) {
					$label = isset( $location_data['label'] ) ? $location_data['label'] : $location_key;
					printf(
						'<option value="%1$s" %2$s>%3$s</option>',
						esc_attr( $location_key ),
						selected( $selected_loop_location, $location_key, false ),
						esc_html( $label )
					);
				}
				?>
			</select>
		</p>
		<p>
			<label for="product_loop_points_message"><?php esc_html_e( 'Catalog points message:', 'simple-points-and-rewards' ); ?></label>
			<input type="text" name="product_loop_points_message" id="product_loop_points_message" value="<?php echo esc_attr( $product_loop_message ); ?>" placeholder="<?php echo esc_attr( $product_loop_default_message ); ?>" class="regular-text" />
			<small class="description"><?php esc_html_e( 'Leave blank to use the default message.', 'simple-points-and-rewards' ); ?></small>
		</p>
		<?php echo wp_kses_post( $placeholder_details_markup ); ?>
		<?php $product_loop_points_text_color = isset( $options['product_loop_points_text_color'] ) ? sanitize_hex_color( $options['product_loop_points_text_color'] ) : ''; ?>
		<p style="margin-top: 10px;">
			<label for="spar-product-loop-points-text-color"><?php esc_html_e( 'Text Color:', 'simple-points-and-rewards' ); ?></label>
			<input id="spar-product-loop-points-text-color" type="color" name="product_loop_points_text_color" value="<?php echo esc_attr( $product_loop_points_text_color ?: '#495057' ); ?>" />
			<small class="description"><?php esc_html_e( 'Controls the font color of the points message on category/shop listings.', 'simple-points-and-rewards' ); ?></small>
		</p>
		<?php $product_loop_points_bg_color = isset( $options['product_loop_points_bg_color'] ) ? sanitize_hex_color( $options['product_loop_points_bg_color'] ) : ''; ?>
		<p style="margin-top: 10px;">
			<label for="spar-product-loop-points-bg-color"><?php esc_html_e( 'Background Color:', 'simple-points-and-rewards' ); ?></label>
			<input id="spar-product-loop-points-bg-color" type="color" name="product_loop_points_bg_color" value="<?php echo esc_attr( $product_loop_points_bg_color ?: '#eaf1fa' ); ?>" />
			<small class="description"><?php esc_html_e( 'Controls the background color of the points message on category/shop listings.', 'simple-points-and-rewards' ); ?></small>
		</p>
	</div>
	<p><label><div class="spar-toggle-switch"><input type="checkbox" name="link_to_rewards" <?php checked( ! empty( $options['link_to_rewards'] ) ); ?> /><span class="spar-toggle-slider"></span></div> <?php esc_html_e( 'Link points text to rewards page', 'simple-points-and-rewards' ); ?></label></p>
	</div><!-- /#spar-product-points-display-section -->
	
	<h3><?php esc_html_e( 'Terms and Conditions', 'simple-points-and-rewards' ); ?></h3>
	<p><label><div class="spar-toggle-switch"><input type="checkbox" name="terms_enabled" id="spar_terms_enabled" <?php checked( ! empty( $options['terms_enabled'] ) ); ?> /><span class="spar-toggle-slider"></span></div> <?php esc_html_e( 'Enable Terms and Conditions', 'simple-points-and-rewards' ); ?></label></p>
	<p class="description"><?php esc_html_e( 'When enabled, a "Terms and Conditions" link will be displayed at the bottom of the rewards dashboard that opens a popup with your custom terms.', 'simple-points-and-rewards' ); ?></p>
	<div class="spar-toggle-extra-settings<?php echo ! empty( $options['terms_enabled'] ) ? '' : ' spar-hidden'; ?>" data-toggle-input="terms_enabled">
		<h4 style="margin-top: 0; margin-bottom: 10px;"><?php esc_html_e( 'Terms Display Settings', 'simple-points-and-rewards' ); ?></h4>
		<p>
			<label for="terms_link_text"><?php esc_html_e( 'Link Text:', 'simple-points-and-rewards' ); ?></label>
			<input type="text" name="terms_link_text" id="terms_link_text" value="<?php echo esc_attr( $options['terms_link_text'] ?? '' ); ?>" placeholder="<?php esc_attr_e( 'Terms and Conditions', 'simple-points-and-rewards' ); ?>" class="regular-text" />
			<small class="description"><?php esc_html_e( 'Leave blank to use the default localised text.', 'simple-points-and-rewards' ); ?></small>
		</p>
		<p style="margin-top: 0px;">
			<label><div class="spar-toggle-switch"><input type="checkbox" name="terms_show_dashboard" <?php checked( isset( $options['terms_show_dashboard'] ) ? (bool) $options['terms_show_dashboard'] : true ); ?> /><span class="spar-toggle-slider"></span></div> <?php esc_html_e( 'Show on rewards dashboard', 'simple-points-and-rewards' ); ?></label>
		</p>
		<p style="margin-top: -10px;">
			<label><div class="spar-toggle-switch"><input type="checkbox" name="terms_show_widget" <?php checked( isset( $options['terms_show_widget'] ) ? (bool) $options['terms_show_widget'] : true ); ?> /><span class="spar-toggle-slider"></span></div> <?php esc_html_e( 'Show on rewards widget', 'simple-points-and-rewards' ); ?></label>
		</p>
		<h4 style="margin-top: 20px;"><?php esc_html_e( 'Terms Content', 'simple-points-and-rewards' ); ?></h4>
		<p class="spar-terms-generate-row">
			<button type="button" id="spar-generate-terms-btn" class="button button-secondary">
				✨ <?php esc_html_e( 'Generate from Current Settings', 'simple-points-and-rewards' ); ?>
			</button>
			<span id="spar-generate-terms-spinner" class="spinner" style="float:none;visibility:hidden;margin:0 6px;vertical-align:middle;"></span>
			<span id="spar-generate-terms-msg" style="vertical-align:middle;"></span>
		</p>
		<p class="description" style="margin-top:-4px;margin-bottom:10px;"><?php esc_html_e( 'This generates a basic template based on your current plugin settings. Please review and adapt the content to suit your specific business requirements before publishing, and seek legal advice if required.', 'simple-points-and-rewards' ); ?></p>
		<?php
		$saved_terms_content = isset( $options['terms_content'] ) && '' !== $options['terms_content'] ? $options['terms_content'] : '';
		if ( '' === $saved_terms_content ) {
			$terms_default = '<h3>' . esc_html__( 'Points & Rewards Program – Terms and Conditions', 'simple-points-and-rewards' ) . '</h3>

<p>' . esc_html__( 'By participating in our Points & Rewards Program, you agree to the following terms and conditions.', 'simple-points-and-rewards' ) . '</p>

<h4>' . esc_html__( '1. Eligibility', 'simple-points-and-rewards' ) . '</h4>
<p>' . esc_html__( 'The rewards program is available to registered customers only. Participation is free and open to all registered account holders.', 'simple-points-and-rewards' ) . '</p>

<h4>' . esc_html__( '2. Earning Points', 'simple-points-and-rewards' ) . '</h4>
<p>' . esc_html__( 'Points are awarded for qualifying actions such as purchases, account sign-up, referrals, and other activities as outlined on the Earn Points section of your rewards dashboard. Points are credited to your account once the qualifying action is confirmed.', 'simple-points-and-rewards' ) . '</p>

<h4>' . esc_html__( '3. Redeeming Points', 'simple-points-and-rewards' ) . '</h4>
<p>' . esc_html__( 'Accumulated points may be redeemed for rewards such as discount vouchers as listed in the Claim Rewards section. Points have no cash value and cannot be exchanged for cash.', 'simple-points-and-rewards' ) . '</p>

<h4>' . esc_html__( '4. Points Validity', 'simple-points-and-rewards' ) . '</h4>
<p>' . esc_html__( 'Points remain valid while your account is active. We reserve the right to introduce expiry policies with advance notice to participants.', 'simple-points-and-rewards' ) . '</p>

<h4>' . esc_html__( '5. Account Suspension', 'simple-points-and-rewards' ) . '</h4>
<p>' . esc_html__( 'We reserve the right to suspend or terminate a customer\'s participation in the rewards program, and to void accumulated points, in cases of suspected fraud, misuse, or breach of these terms.', 'simple-points-and-rewards' ) . '</p>

<h4>' . esc_html__( '6. Program Changes', 'simple-points-and-rewards' ) . '</h4>
<p>' . esc_html__( 'We reserve the right to modify, suspend, or discontinue the rewards program at any time. Any significant changes will be communicated to participants in advance where reasonably possible.', 'simple-points-and-rewards' ) . '</p>

<h4>' . esc_html__( '7. Governing Law', 'simple-points-and-rewards' ) . '</h4>
<p>' . esc_html__( 'These terms are governed by applicable local laws. By participating in the program, you agree to these terms and any future amendments.', 'simple-points-and-rewards' ) . '</p>

<p><em>' . esc_html__( 'Last updated: ', 'simple-points-and-rewards' ) . date_i18n( get_option( 'date_format' ) ) . '</em></p>';
		} // end if no saved content
		$terms_content = '' !== $saved_terms_content ? $saved_terms_content : $terms_default;
		wp_editor(
			$terms_content,
			'terms_content',
			array(
				'textarea_name' => 'terms_content',
				'media_buttons' => false,
				'textarea_rows' => 40,
				'editor_height' => 400,
				'teeny'         => false,
				'quicktags'     => true,
			)
		);
		?>
	</div>
	
	<?php
}
