<?php

defined( 'ABSPATH' ) || exit;

use Quicker\Utils\Helper;

$product_tabs = array(
	array(
		'id'      => 'tab-direct-checkout',
		'label'   => esc_html__( 'Quick Checkout', 'quicker' ),
		'content' => function () use ( $enable_buy_now, $enable_single_buy_now, $custom_cart_url, $disable, $direct_checkout_categories, $direct_checkout_products, $direct_checkout_entire_store, $direct_checkout_scope, $direct_checkout_button_text, $enable_one_click_checkout, $one_click_button_text, $one_click_scope, $one_click_products ) {
			?>
			<h2 class="mt-0"><?php esc_html_e( 'Direct Checkout', 'quicker' ); ?></h2>
			<?php
			$args = array(
				'label'    => esc_html__( 'Enable Direct Checkout ', 'quicker' ),
				'id'       => 'enable_buy_now',
				'options'  => array(
					'no'         => esc_html__( 'No', 'quicker' ),
					'checkout'   => esc_html__( 'Checkout Page', 'quicker' ),
					'custom_url' => esc_html__( 'Custom Url', 'quicker' ),
				),
				'selected' => $enable_buy_now,
			);
			quicker_select_field( $args );
			$buy_now_cond = $enable_buy_now == 'custom_url' ? 'yes' : '';
			$args         = array(
				'label'           => esc_html__( 'Replace the cart URL with a custom URL', 'quicker' ),
				'condition_class' => Helper::instance()->d_none_class( $buy_now_cond ) . ' enable_buy_now',
				'id'              => 'custom_cart_url',
				'value'           => $custom_cart_url,
			);
			quicker_number_input_field( $args );
			// New: Direct Checkout Button Text
			$args = array(
				'label' => esc_html__( 'Direct Checkout Button Text', 'quicker' ),
				'id'    => 'direct_checkout_button_text',
				'value' => isset( $direct_checkout_button_text ) ? $direct_checkout_button_text : '',
				'condition_class' => Helper::instance()->d_none_class( ( isset( $enable_buy_now ) && $enable_buy_now !== 'no' ) ? 'yes' : '' ) . ' enable_buy_now_any',
			);
			quicker_number_input_field( $args );

			// New: Apply Direct Checkout for (scope)
			$args = array(
				'label'           => esc_html__( 'Apply Direct Checkout for', 'quicker' ),
				'id'              => 'direct_checkout_scope',
				'options'         => array(
					'entire'     => esc_html__( 'Entire store', 'quicker' ),
					'categories' => esc_html__( 'Product Categories', 'quicker' ),
					'products'   => esc_html__( 'Specific Products', 'quicker' ),
				),
				'selected'        => isset( $direct_checkout_scope ) ? $direct_checkout_scope : 'entire',
				'condition_class' => 'enable_buy_now_any direct_checkout_scope_control',
			);
			quicker_select_field( $args );

			// New: Limit direct checkout to selected product categories
			$args = array(
				'label'           => esc_html__( 'Direct Checkout for Product Categories', 'quicker' ),
				'id'              => 'direct_checkout_categories',
				// Shows when scope is categories (hidden by default otherwise)
				'condition_class' => Helper::instance()->d_none_class( ( isset( $direct_checkout_scope ) && $direct_checkout_scope === 'categories' ) ? 'yes' : '' ) . ' enable_buy_now_any scope-categories select2-multiple',
				'selected'        => is_array( $direct_checkout_categories ) ? $direct_checkout_categories : array(),
				'options'         => Helper::get_product_categories(),
				'select_type'     => 'multiple',
				'disable'         => $disable,
			);
			quicker_select_field( $args );

			// New: Limit direct checkout to specific products
			$args = array(
				'label'           => esc_html__( 'Direct Checkout for Specific Product', 'quicker' ),
				'id'              => 'direct_checkout_products',
				// Shows when scope is products (hidden by default otherwise)
				'condition_class' => Helper::instance()->d_none_class( ( isset( $direct_checkout_scope ) && $direct_checkout_scope === 'products' ) ? 'yes' : '' ) . ' enable_buy_now_any scope-products select2-multiple',
				'selected'        => is_array( $direct_checkout_products ) ? $direct_checkout_products : array(),
				'options'         => Helper::get_products(),
				'select_type'     => 'multiple',
				'disable'         => $disable,
			);
			quicker_select_field( $args );

			$args = array(
				'label'   => esc_html__( 'Direct Checkout from Single Page', 'quicker' ),
				'id'      => 'enable_single_buy_now',
				'checked' => $enable_single_buy_now,
				'condition_class' => Helper::instance()->d_none_class( ( isset( $enable_buy_now ) && $enable_buy_now !== 'no' ) ? 'yes' : '' ) . ' enable_buy_now_any',
			);
			quicker_checkbox_field( $args );
			?>
			<h2 class="mt-0"><?php esc_html_e( 'One Click Checkout', 'quicker' ); ?></h2>
			<?php
			// Pro feature: Enable One Click Checkout (always shown) as a select option
			$args = array(
				'label'   => esc_html__( 'Enable One Click Checkout', 'quicker' ),
				'id'      => 'enable_one_click_checkout',
				'options' => array(
					'no'       => esc_html__( 'No', 'quicker' ),
					'replace'  => esc_html__( 'Replace Add to Cart Button', 'quicker' ),
					'add_new'  => esc_html__( 'Add a Buy Now Button Next After "Add to Cart" Button', 'quicker' ),
				),
				'disable'  => $disable,
				'selected' => isset( $enable_one_click_checkout ) && $enable_one_click_checkout !== '' ? $enable_one_click_checkout : 'no',
			);
			quicker_select_field( $args );

			// Show additional one-click fields when enabled (enable_one_click_checkout !== 'no')
			$show_one_click = ( isset( $enable_one_click_checkout ) && $enable_one_click_checkout !== 'no' ) ? 'yes' : '';
			$args = array(
				'label'           => esc_html__( 'Buy Now Button Text', 'quicker' ),
				'id'              => 'one_click_button_text',
				'value'           => isset( $one_click_button_text ) ? $one_click_button_text : '',
				'condition_class' => Helper::instance()->d_none_class( $show_one_click ) . ' enable_one_click_checkout',
				'data_label'      => '',
				'placeholder'     => '',
			);
			quicker_number_input_field( $args );

			$args = array(
				'label'           => esc_html__( 'Buy Now for', 'quicker' ),
				'id'              => 'one_click_scope',
				'options'         => array(
					'all'      => esc_html__( 'All Products', 'quicker' ),
					'products' => esc_html__( 'Specific Products', 'quicker' ),
				),
				'condition_class' => Helper::instance()->d_none_class( $show_one_click ) . ' enable_one_click_checkout',
				'selected'        => isset( $one_click_scope ) ? $one_click_scope : 'all',
			);
			quicker_select_field( $args );

			// Show product picker when Buy Now for = Specific Products
			$one_click_scope_products_visible = ( isset( $enable_one_click_checkout ) && $enable_one_click_checkout !== 'no' && isset( $one_click_scope ) && $one_click_scope === 'products' ) ? 'yes' : '';
			$args = array(
				'label'           => esc_html__( 'Products', 'quicker' ),
				'id'              => 'one_click_products',
				'condition_class' => Helper::instance()->d_none_class( $one_click_scope_products_visible ) . ' enable_one_click_checkout one-click-settings one-click-scope-products select2-multiple',
				'selected'        => isset( $one_click_products ) && is_array( $one_click_products ) ? $one_click_products : array(),
				'options'         => Helper::get_products(),
				'select_type'     => 'multiple',
				'disable'         => $disable,
			);
			quicker_select_field( $args );

		},
	),
	array(
		'id'      => 'tab-add-cart',
		'label'   => esc_html__( 'Add Cart Button', 'quicker' ),
		'content' => function () use ( $add_cart_text, $ajax_add_to_cart, $disable, $default_quantity ) {
			$args = array(
				'label' => esc_html__( 'Add Cart Button Text', 'quicker' ),
				'id'    => 'add_cart_text',
				'value' => $add_cart_text,
			);
			quicker_number_input_field( $args );
			$args = array(
				'label'       => esc_html__( 'Global Default Quantity', 'quicker' ),
				'id'          => 'default_quantity',
				'field_type'  => 'number',
				'wrapper_class' => 'single-block inline-wrapper',
				'value'       => $default_quantity,
				'extra_label' => esc_html__( 'Default quantity on single product page', 'quicker' ),
				'disable'     => $disable,
			);
			quicker_number_input_field( $args );
			$args = array(
				'label'          => esc_html__( 'Enable Ajax Add to Cart', 'quicker' ),
				'id'             => 'ajax_add_to_cart',
				'checkbox_label' => esc_html__( 'Enable AJAX Add to Cart functionality on all pages, and disable the direct checkout redirection from both the shop and single product pages to ensure proper functionality.', 'quicker' ),
				'checked'        => $ajax_add_to_cart,
				'disable'        => $disable,
			);
			quicker_checkbox_field( $args );
		},
	),
	array(
		'id'      => 'tab-product-addons',
		'label'   => esc_html__( 'Product Addons', 'quicker' ),
		'content' => function () use ( $enable_product_addons ) {
			$args = array(
				'label'          => esc_html__( 'Enable Product Addons', 'quicker' ),
				'id'             => 'enable_product_addons',
				'checked'        => $enable_product_addons,
				'checkbox_label' => esc_html__( 'Enable Product Addons in', 'quicker' ) .
					' <a href="' . esc_url( admin_url( 'edit.php?post_type=product' ) ) . '" target="_blank" style="color:#3263f8;text-decoration:underline;">' .
					esc_html__( 'Single Product Page.', 'quicker' ) . '</a>',
			);
			quicker_checkbox_field( $args );
		},
	),
);
?>
<div class="quicker-tabs-wrapper">
	<div class="quicker-vertical-tab-pane">
		<ul class="quicker-tabs vertical">
			<?php foreach ( $product_tabs as $i => $tab ) : ?>
				<li class="<?php echo esc_attr( $i === 0 ? 'active' : '' ); ?>" data-tab="<?php echo esc_attr( $tab['id'] ); ?>"><?php echo esc_html( $tab['label'] ); ?></li>
			<?php endforeach; ?>
		</ul>
	</div>
	<div class="quicker-vertical-tab">
		<?php foreach ( $product_tabs as $i => $tab ) : ?>
			<div class="quicker-tab-content<?php echo $i === 0 ? ' active' : ''; ?>" id="<?php echo esc_attr( $tab['id'] ); ?>">
				<?php $tab['content'](); ?>
			</div>
		<?php endforeach; ?>
	</div>
</div>


