<?php
/**
 * Warranty Product Data Panel Template
 *
 * @package WarrantyCart
 * @version 2.1.0
 */

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound,WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Template variables and WooCommerce hooks are local scope and don't need prefixing

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

$warranty_status        = $args['warranty_status'] ?? 'disabled';
$product_type           = $args['product_type'] ?? 'additional_service';
$warranty_duration      = $args['warranty_duration'] ?? '';
$warranty_duration_unit = $args['warranty_duration_unit'] ?? 'years';
$coverage_details       = $args['coverage_details'] ?? '';
$associated_warranty    = $args['associated_warranty'] ?? '';
$service_priority       = $args['service_priority'] ?? 'optional';
$applies_to             = $args['applies_to'] ?? 'none';
$applies_to_options     = $args['applies_to_options'] ?? array();
$applies_categories     = $args['applies_categories'] ?? array();
$applies_products       = $args['applies_products'] ?? array();
$product_categories     = $args['product_categories'] ?? array();
$included               = $args['included'] ?? false;
$pricing_mode           = $args['pricing_mode'] ?? 'fixed';
$pricing_modes          = $args['pricing_modes'] ?? array();
$percentage             = $args['percentage'] ?? '';
$price_min              = $args['price_min'] ?? '';
$price_max              = $args['price_max'] ?? '';
$main_warranties        = $args['main_warranties'] ?? array();
?>

<div id="warranty_product_data" class="panel woocommerce_options_panel">

	<div class="options_group">
		<?php
		woocommerce_wp_checkbox(
			array(
				'id'          => 'warranty_cart_product_status',
				'label'       => __( 'Enable warranty features', 'warranty-cart' ),
				'description' => __( 'Check this box to enable warranty functionality for this product.', 'warranty-cart' ),
				'cbvalue'     => 'yes',
				'value'       => 'enabled' === $warranty_status ? 'yes' : '',
			)
		);
		?>
	</div>

	<div class="warranty-fields" style="<?php echo 'enabled' === $warranty_status ? '' : 'display: none;'; ?>">
		<div class="options_group">
			<?php
			woocommerce_wp_select(
				array(
					'id'          => 'warranty_cart_product_type',
					'value'       => $product_type,
					'label'       => __( 'Warranty product type', 'warranty-cart' ),
					'options'     => array(
						'main_warranty'      => __( 'Main warranty', 'warranty-cart' ),
						'additional_service' => __( 'Additional service', 'warranty-cart' ),
					),
					'desc_tip'    => 'true',
					'description' => __( 'Choose "Main warranty" for primary warranty offerings that customers can purchase directly. Choose "Additional service" for complementary services that extend or enhance main warranties.', 'warranty-cart' ),
				)
			);

			woocommerce_wp_text_input(
				array(
					'id'                => 'warranty_cart_duration',
					'value'             => $warranty_duration,
					'label'             => __( 'Warranty duration', 'warranty-cart' ),
					'data_type'         => 'number',
					'custom_attributes' => array(
						'step' => '1',
						'min'  => '1',
					),
					'desc_tip'          => 'true',
					'description'       => __( 'Enter the duration value for this warranty coverage period.', 'warranty-cart' ),
				)
			);

			woocommerce_wp_select(
				array(
					'id'          => 'warranty_cart_duration_unit',
					'value'       => $warranty_duration_unit,
					'label'       => __( 'Duration unit', 'warranty-cart' ),
					'options'     => array(
						'days'   => __( 'Days', 'warranty-cart' ),
						'months' => __( 'Months', 'warranty-cart' ),
						'years'  => __( 'Years', 'warranty-cart' ),
					),
					'desc_tip'    => 'true',
					'description' => __( 'Select the time unit for the warranty duration. This determines when the warranty expires.', 'warranty-cart' ),
				)
			);
			?>
		</div>

		<div class="options_group show_if_additional_service<?php echo 'additional_service' === $product_type ? '' : ' hidden'; ?>">
			<?php
			woocommerce_wp_select(
				array(
					'id'          => 'warranty_cart_associated_warranty',
					'value'       => $associated_warranty,
					'label'       => __( 'Link to main warranty', 'warranty-cart' ),
					'options'     => $main_warranties,
					'class'       => 'wc-enhanced-select',
					'desc_tip'    => 'true',
					'description' => __( 'Connect this additional service to a main warranty. Customers who purchase the selected main warranty will see this service as an optional add-on during checkout.', 'warranty-cart' ),
				)
			);

			woocommerce_wp_select(
				array(
					'id'          => 'warranty_cart_service_priority',
					'value'       => $service_priority,
					'label'       => __( 'Service presentation', 'warranty-cart' ),
					'options'     => array(
						'optional'    => __( 'Optional add-on', 'warranty-cart' ),
						'recommended' => __( 'Recommended add-on', 'warranty-cart' ),
						'required'    => __( 'Required service', 'warranty-cart' ),
					),
					'desc_tip'    => 'true',
					'description' => __( 'Control how prominently this service is displayed to customers. "Required" services are automatically added to cart, "Recommended" services are highlighted, and "Optional" services are shown as standard add-ons.', 'warranty-cart' ),
				)
			);
			?>
		</div>

		<div class="options_group">
			<?php
			woocommerce_wp_textarea_input(
				array(
					'id'          => 'warranty_cart_coverage_details',
					'value'       => $coverage_details,
					'label'       => __( 'Coverage description', 'warranty-cart' ),
					'desc_tip'    => 'true',
					'description' => __( 'Provide detailed information about what this warranty or service covers. This description helps customers understand the value and scope of coverage. It will be displayed on the warranty selection page and included in warranty certificates.', 'warranty-cart' ),
				)
			);
			?>
		</div>

		<div class="options_group warranty_cart_main_only">
			<?php
			woocommerce_wp_checkbox(
				array(
					'id'          => 'warranty_cart_included',
					'cbvalue'     => 'yes',
					'value'       => $included ? 'yes' : '',
					'label'       => __( 'Included free', 'warranty-cart' ),
					'description' => __( 'Give this cover with every product it applies to, at no charge. The customer is never asked to buy it: a record is written when their order completes, so they can see the cover and claim against it. The price settings below stop applying.', 'warranty-cart' ),
				)
			);

			woocommerce_wp_select(
				array(
					'id'            => 'warranty_cart_pricing_mode',
					'value'         => $pricing_mode,
					'label'         => __( 'Price', 'warranty-cart' ),
					'options'       => $pricing_modes,
					'desc_tip'      => 'true',
					'description'   => __( 'A fixed price suits a catalogue where everything costs about the same. A percentage suits one where it does not — £29 of cover is most of the value of a £40 kettle and a rounding error on a £2,000 television.', 'warranty-cart' ),
					'wrapper_class' => 'warranty_cart_sold_field',
				)
			);

			woocommerce_wp_text_input(
				array(
					'id'                => 'warranty_cart_percentage',
					'value'             => $percentage,
					'label'             => __( 'Percentage of the covered product', 'warranty-cart' ),
					'type'              => 'number',
					'custom_attributes' => array(
						'step' => '0.01',
						'min'  => '0',
						'max'  => '100',
					),
					'desc_tip'          => 'true',
					'description'       => __( 'The product\'s own price above is still used wherever no product is being covered — the [warranty_cart] selector page, for instance.', 'warranty-cart' ),
					'wrapper_class'     => 'warranty_cart_percentage_field warranty_cart_sold_field',
				)
			);

			woocommerce_wp_text_input(
				array(
					'id'                => 'warranty_cart_price_min',
					'value'             => $price_min,
					'label'             => __( 'Minimum price', 'warranty-cart' ),
					'type'              => 'number',
					'custom_attributes' => array(
						'step' => '0.01',
						'min'  => '0',
					),
					'desc_tip'          => 'true',
					'description'       => __( 'Optional. A percentage of a very cheap item is not worth administering.', 'warranty-cart' ),
					'wrapper_class'     => 'warranty_cart_percentage_field warranty_cart_sold_field',
				)
			);

			woocommerce_wp_text_input(
				array(
					'id'                => 'warranty_cart_price_max',
					'value'             => $price_max,
					'label'             => __( 'Maximum price', 'warranty-cart' ),
					'type'              => 'number',
					'custom_attributes' => array(
						'step' => '0.01',
						'min'  => '0',
					),
					'desc_tip'          => 'true',
					'description'       => __( 'Optional. A percentage of a very expensive item asks for more than anybody will pay.', 'warranty-cart' ),
					'wrapper_class'     => 'warranty_cart_percentage_field warranty_cart_sold_field',
				)
			);
			?>
		</div>

		<?php
		/*
		 * Where this cover is offered.
		 *
		 * Main warranties only. An additional service attaches to a warranty
		 * rather than to a product, so offering one on its own beside a kettle
		 * would sell a customer a support plan for cover they do not have.
		 */
		?>
		<div class="options_group warranty_cart_main_only">
			<?php
			woocommerce_wp_select(
				array(
					'id'          => 'warranty_cart_applies_to',
					'value'       => $applies_to,
					'label'       => __( 'Offer alongside', 'warranty-cart' ),
					'options'     => $applies_to_options,
					'desc_tip'    => 'true',
					'description' => __( 'Show this warranty on the product page of the items it covers, so a customer can add it while they are buying. Leave it unoffered to sell it only through the [warranty_cart] selector.', 'warranty-cart' ),
				)
			);
			?>

			<p class="form-field warranty_cart_applies_categories_field">
				<label for="warranty_cart_applies_categories"><?php \esc_html_e( 'Categories', 'warranty-cart' ); ?></label>
				<select
					id="warranty_cart_applies_categories"
					name="warranty_cart_applies_categories[]"
					class="wc-enhanced-select"
					multiple="multiple"
					style="width: 50%;"
				>
					<?php foreach ( $product_categories as $term ) : ?>
						<option
							value="<?php echo \esc_attr( (string) $term->term_id ); ?>"
							<?php selected( in_array( (int) $term->term_id, $applies_categories, true ) ); ?>
						>
							<?php echo \esc_html( $term->name ); ?>
						</option>
					<?php endforeach; ?>
				</select>
				<?php
				// wc_help_tip() escapes its own output.
				echo \wc_help_tip( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
					\esc_html__( 'Products in these categories, and in any category beneath them, will be offered this warranty.', 'warranty-cart' )
				);
				?>
			</p>

			<p class="form-field warranty_cart_applies_products_field">
				<label for="warranty_cart_applies_products"><?php \esc_html_e( 'Products', 'warranty-cart' ); ?></label>
				<select
					id="warranty_cart_applies_products"
					name="warranty_cart_applies_products[]"
					class="wc-product-search"
					multiple="multiple"
					style="width: 50%;"
					data-placeholder="<?php \esc_attr_e( 'Search for a product…', 'warranty-cart' ); ?>"
					data-action="woocommerce_json_search_products_and_variations"
				>
					<?php foreach ( $applies_products as $applies_product_id ) : ?>
						<?php $applies_product = \wc_get_product( $applies_product_id ); ?>
						<?php if ( $applies_product ) : ?>
							<option value="<?php echo \esc_attr( (string) $applies_product_id ); ?>" selected="selected">
								<?php echo \esc_html( \wp_strip_all_tags( $applies_product->get_formatted_name() ) ); ?>
							</option>
						<?php endif; ?>
					<?php endforeach; ?>
				</select>
				<?php
				// wc_help_tip() escapes its own output.
				echo \wc_help_tip( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
					\esc_html__( 'Choosing a variable product covers every one of its variations.', 'warranty-cart' )
				);
				?>
			</p>
		</div>

		<?php \do_action( 'warranty_cart_product_options_warranty' ); ?>
	</div>
</div>
<?php // phpcs:enable ?>
