<?php

defined( 'ABSPATH' ) || exit;
use Quicker\Utils\Helper;

?>
<div class="quicker-tabs-wrapper">
	<div class="quicker-vertical-tab-pane">
		<?php
			$tabs = array(
				array(
					'id'     => 'min-max-tab',
					'label'  => esc_html__( 'Min/Max Order Amount', 'quicker' ),
					'active' => true,
				),
				array(
					'id'     => 'min-max-qty-tab',
					'label'  => esc_html__( 'Min/Max Order Quantity', 'quicker' ),
					'active' => false,
				),
				array(
					'id'     => 'cod-charge-tab',
					'label'  => esc_html__( 'Cash On Delivery Charge', 'quicker' ),
					'active' => false,
				),
				array(
					'id'     => 'extra-fees-tab',
					'label'  => esc_html__( 'Extra Fees', 'quicker' ),
					'active' => false,
				),
			);
			?>
		<ul class="quicker-tabs vertical">
			<?php foreach ( $tabs as $tab ) : ?>
				<li class="<?php echo esc_attr( $tab['active'] ? '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">
		<div class="quicker-tab-content active" id="min-max-tab">
			<?php
				$args = array(
					'label'   => esc_html__( 'Enable Min/Max Order Amount', 'quicker' ),
					'id'      => 'mix_max_order',
					'checked' => $mix_max_order,
				);
				quicker_checkbox_field( $args );
				$block_min_max_order = $mix_max_order == 'yes' ? '' : 'd-none';
				$args                = array(
					'label'           => esc_html__( 'Minimum Order Amount', 'quicker' ),
					'condition_class' => 'mix_max_order ' . $block_min_max_order,
					'wrapper_class'   => 'single-block inline-wrapper',
					'id'              => 'min_order_amount',
					'field_type'      => 'number',
					'disable'         => $disable,
					'extra_label'     => Helper::woo_currency(),
					'value'           => $min_order_amount,
				);
				quicker_number_input_field( $args );
				$args = array(
					'label'           => esc_html__( 'Maximum Order Amount', 'quicker' ),
					'condition_class' => 'mix_max_order ' . $block_min_max_order,
					'id'              => 'max_order_amount',
					'wrapper_class'   => 'single-block inline-wrapper',
					'field_type'      => 'number',
					'disable'         => $disable,
					'extra_label'     => Helper::woo_currency(),
					'value'           => $max_order_amount,
				);
				quicker_number_input_field( $args );

				$args = array(
					'label'           => esc_html__( 'Message', 'quicker' ),
					'condition_class' => 'mix_max_order ' . $block_min_max_order,
					'id'              => 'mix_max_order_msg',
					'wrapper_class'   => 'single-block',
					'value'           => $mix_max_order_msg,
					'placeholder'     => esc_html__( 'Example: Your order must be between {min} and {max}. Current: {subtotal}', 'quicker' ),
					'extra_label'     => esc_html__( 'Use {min}, {max}, {subtotal}', 'quicker' ),
					'rows'            => 3,
					'cols'            => 40,
					'disable'         => $disable,
				);
				quicker_textarea_field( $args );

				// User Roles
				$roles            = wp_roles();
				$role_options_amt = array( 'all' => esc_html__( 'All Roles', 'quicker' ) );
				foreach ( $roles->roles as $role_key => $role_data ) {
					$role_options_amt[ $role_key ] = $role_data['name'];
				}
				$args = array(
					'label'           => esc_html__( 'Apply to User Roles', 'quicker' ),
					'id'              => 'mix_max_order_roles',
					'condition_class' => 'mix_max_order ' . $block_min_max_order,
					'is_multiple'     => true,
					'select_type'     => 'multiple',
					'selected'        => isset( $mix_max_order_roles ) ? (array) $mix_max_order_roles : array(),
					'options'         => $role_options_amt,
					'data_label'      => 'mix_max_order_roles',
				);
				quicker_select_field( $args );

				?>
		</div>
		<div class="quicker-tab-content" id="min-max-qty-tab">
			<?php
				$args = array(
					'label'   => esc_html__( 'Enable Min/Max Order Quantity', 'quicker' ),
					'id'      => 'mix_max_order_qty',
					'checked' => $mix_max_order_qty,
				);
				quicker_checkbox_field( $args );
				$block_min_max_qty = $mix_max_order_qty == 'yes' ? '' : 'd-none';
				$args              = array(
					'label'           => esc_html__( 'Minimum Order Quantity', 'quicker' ),
					'condition_class' => 'mix_max_order_qty ' . $block_min_max_qty,
					'wrapper_class'   => 'single-block inline-wrapper',
					'id'              => 'min_order_quantity',
					'field_type'      => 'number',
					'disable'         => $disable,
					'extra_label'     => esc_html__( 'items', 'quicker' ),
					'value'           => $min_order_quantity,
				);
				quicker_number_input_field( $args );
				$args = array(
					'label'           => esc_html__( 'Maximum Order Quantity', 'quicker' ),
					'condition_class' => 'mix_max_order_qty ' . $block_min_max_qty,
					'id'              => 'max_order_quantity',
					'wrapper_class'   => 'single-block inline-wrapper',
					'field_type'      => 'number',
					'disable'         => $disable,
					'extra_label'     => esc_html__( 'items', 'quicker' ),
					'value'           => $max_order_quantity,
				);
				quicker_number_input_field( $args );
				$args = array(
					'label'           => esc_html__( 'Default Order Quantity', 'quicker' ),
					'condition_class' => 'mix_max_order_qty ' . $block_min_max_qty,
					'id'              => 'default_order_quantity',
					'wrapper_class'   => 'single-block inline-wrapper',
					'field_type'      => 'number',
					'disable'         => $disable,
					'extra_label'     => esc_html__( 'Pre-filled quantity on single product page', 'quicker' ),
					'value'           => $default_order_quantity,
				);
				quicker_number_input_field( $args );
				// Scope select
				$qty_scope_val = isset( $mix_max_qty_scope ) ? $mix_max_qty_scope : 'all';
				$args          = array(
					'label'           => esc_html__( 'Apply Restriction to', 'quicker' ),
					'id'              => 'mix_max_qty_scope',
					'condition_class' => 'mix_max_order_qty ' . $block_min_max_qty,
					'options'         => array(
						'all'        => esc_html__( 'All Products', 'quicker' ),
						'products'   => esc_html__( 'Specific Products', 'quicker' ),
						'categories' => esc_html__( 'Specific Categories', 'quicker' ),
					),
					'selected'        => $qty_scope_val,
					'disable'         => false,
				);
				quicker_select_field( $args );
				// Products / Categories – immediately below scope selector
				$qty_product_style = ( $qty_scope_val === 'products' )  ? '' : 'd-none';
				$qty_cat_style     = ( $qty_scope_val === 'categories' ) ? '' : 'd-none';
				?>
				<div class="mix_max_order_qty <?php echo esc_attr( $block_min_max_qty ); ?>">
				<?php
				$args = array(
					'label'       => esc_html__( 'Select Products', 'quicker' ),
					'id'          => 'mix_max_qty_products',
					'condition_class' => 'mix_max_qty_products ' . $qty_product_style,
					'is_multiple' => true,
					'select_type' => 'multiple',
					'selected'    => isset( $mix_max_qty_products ) ? (array) $mix_max_qty_products : array(),
					'options'     => Helper::get_all_products_for_select(),
					'data_label'  => 'mix_max_qty_products',
				);
				quicker_select_field( $args );

				$pro_unavailable = ! class_exists( 'QuickerPro' );
				$args = array(
					'label'       => esc_html__( 'Select Categories', 'quicker' ),
					'id'          => 'mix_max_qty_categories',
					'condition_class' => 'mix_max_qty_categories ' . $qty_cat_style,
					'is_multiple' => true,
					'select_type' => 'multiple',
					'selected'    => isset( $mix_max_qty_categories ) ? (array) $mix_max_qty_categories : array(),
					'options'     => Helper::get_product_categories(),
					'data_label'  => 'mix_max_qty_categories',
					'disable'     => $pro_unavailable,
				);
				quicker_select_field( $args );
				?>
				</div>
				<?php

				// User Roles
				$roles            = wp_roles();
				$role_options_qty = array( 'all' => esc_html__( 'All Roles', 'quicker' ) );
				foreach ( $roles->roles as $role_key => $role_data ) {
					$role_options_qty[ $role_key ] = $role_data['name'];
				}
				$args = array(
					'label'           => esc_html__( 'Apply to User Roles', 'quicker' ),
					'id'              => 'mix_max_qty_roles',
					'condition_class' => 'mix_max_order_qty ' . $block_min_max_qty,
					'is_multiple'     => true,
					'select_type'     => 'multiple',
					'selected'        => isset( $mix_max_qty_roles ) ? (array) $mix_max_qty_roles : array(),
					'options'         => $role_options_qty,
					'data_label'      => 'mix_max_qty_roles',
					'disable'         => $pro_unavailable,
				);
				quicker_select_field( $args );

				$args = array(
					'label'           => esc_html__( 'Message', 'quicker' ),
					'condition_class' => 'mix_max_order_qty ' . $block_min_max_qty,
					'id'              => 'mix_max_order_qty_msg',
					'wrapper_class'   => 'single-block',
					'value'           => $mix_max_order_qty_msg,
					'placeholder'     => esc_html__( 'Example: Your order must have between {min} and {max} items. Current: {qty}', 'quicker' ),
					'extra_label'     => esc_html__( 'Use {min}, {max}, {qty}', 'quicker' ),
					'rows'            => 3,
					'cols'            => 40,
					'disable'         => $disable,
				);
				quicker_textarea_field( $args );
			?>
		</div>
		<div class="quicker-tab-content" id="cod-charge-tab">
			<?php
			$args = array(
				'label'   => esc_html__( 'Enable Cash On Delivery Charge', 'quicker' ),
				'id'      => 'cod_charge',
				'checked' => $cod_charge,
			);
			quicker_checkbox_field( $args );

			$d_none_cod_charge = ( $cod_charge == 'no' ) ? 'd-none' : '';
			?>
			<div class="cod_charge <?php echo esc_attr( $d_none_cod_charge ); ?>">
			<?php
				$args = array(
					'label'   => esc_html__( 'Label', 'quicker' ),
					'id'      => 'cod_charge_label',
					'value'   => $cod_charge_label,
					'disable' => $disable,
				);
				quicker_number_input_field( $args );

				$args = array(
					'label'      => esc_html__( 'Charge', 'quicker' ),
					'id'         => 'cod_charge_amount',
					'field_type' => 'number',
					'value'      => $cod_charge_amount,
					'disable'    => $disable,
				);
				quicker_number_input_field( $args );
				?>
			<div class="form-label mt_2"><?php esc_html_e( 'Add Cash Delivery Charge Rules', 'quicker' ); ?></div>
			<?php
				$args = array(
					'label'      => esc_html__( 'Minimum Cart Total', 'quicker' ),
					'id'         => 'cod_min_cart',
					'wrapper_class'   => 'single-block inline-wrapper',
					'field_type' => 'number',
					'value'      => $cod_min_cart,
					'extra_label'=> Helper::woo_currency(),
					'disable'    => $disable,
				);
				quicker_number_input_field( $args );

				$args = array(
					'label'      => esc_html__( 'Charge', 'quicker' ),
					'id'         => 'cod_min_cart_charge',
					'wrapper_class'   => 'single-block inline-wrapper',
					'field_type' => 'number',
					'value'      => $cod_min_cart_charge,
					'extra_label'=> Helper::woo_currency(),
					'disable'    => $disable,
				);
				quicker_number_input_field( $args );

				$args = array(
					'label'      => esc_html__( 'Rule Message', 'quicker' ),
					'id'         => 'cod_min_cart_msg',
					'value'      => isset( $cod_min_cart_msg ) ? $cod_min_cart_msg : '',
					/* translators: %1$s: minimum cart amount, %2$s: delivery charge amount */
					'placeholder'=> esc_html__( 'Add items worth %1$s or more and enjoy delivery at just %2$s.', 'quicker' ),
					/* translators: %1$s: placeholder for minimum cart, %2$s: placeholder for charge */
					'extra_label'=> esc_html__( 'Use %1$s = min cart, %2$s = charge', 'quicker' ),
					'rows'       => 4,
					'cols'       => 32,
					'disable'    => $disable,
				);
				quicker_textarea_field( $args );

				?>
			</div>
		</div>
		<div class="quicker-tab-content" id="extra-fees-tab">
			<?php
			// Extra Fees Tab
			$args = array(
				'label'   => esc_html__( 'Enable Extra Fees on Checkout', 'quicker' ),
				'id'      => 'enable_extra_fees',
				'checked' => isset( $enable_extra_fees ) ? $enable_extra_fees : 'no',
			);
			quicker_checkbox_field( $args );
			$ef_none = ( isset( $enable_extra_fees ) && $enable_extra_fees === 'yes' ) ? '' : 'd-none';
			?>
			<div class="extra-fees-scope-wrapper <?php echo esc_attr( $ef_none ); ?>">
			<?php
			// Scope select
			$scope_val = isset( $extra_fees_scope ) ? $extra_fees_scope : 'all';
			$scope_options = array(
				'all'       => esc_html__( 'All Products', 'quicker' ),
				'products'  => esc_html__( 'Specific Products', 'quicker' ),
				'categories'=> esc_html__( 'Specific Categories', 'quicker' ),
				'cart_total'=> esc_html__( 'Cart Total', 'quicker' ),
				'cart_quantity'=> esc_html__( 'Cart Quantity', 'quicker' ),
			);

			// Flag to indicate whether Quicker Pro is available
			$pro_unavailable = ! class_exists( 'QuickerPro' );

			$args = array(
				'label'   => esc_html__( 'Apply Extra Fees to', 'quicker' ),
				'id'      => 'extra_fees_scope',
				'options' => $scope_options,
				'selected'=> $scope_val,
				'disable' => false,
				'disabled_options' => $pro_unavailable ? array( 'categories' , 'cart_total', 'cart_quantity' ) : array(),
			);
			quicker_select_field( $args );

			// Product multi-select (show when scope=products)
			$product_style = $scope_val === 'products' ? '' : 'd-none';
			$args = array(
				'label'           => esc_html__( 'Select Products', 'quicker' ),
				'id'              => 'extra_fees_products',
				'condition_class' => 'extra_fees_products ' . $product_style,
				'is_multiple'     => true,
				'select_type'     => 'multiple',
				'selected'        => isset( $extra_fees_products ) ? (array) $extra_fees_products : array(),
				'options'         => Helper::get_all_products_for_select(), // Need helper; fallback if undefined.
				'data_label'      => 'extra_fees_products'
			);
			if ( function_exists( 'quicker_select_field' ) ) {
				quicker_select_field( $args );
			}
			

			// Category multi-select (scope=categories)
			$cat_style = $scope_val === 'categories' ? '' : 'd-none';
			$args = array(
				'label'           => esc_html__( 'Select Categories', 'quicker' ),
				'id'              => 'extra_fees_categories',
				'condition_class' => 'extra_fees_categories ' . $cat_style,
				'is_multiple'     => true,
				'select_type'     => 'multiple',
				'selected'        => isset( $extra_fees_categories ) ? (array) $extra_fees_categories : array(),
				'options'         => Helper::get_product_categories(),
				'data_label'      => 'extra_fees_categories'
			);
			quicker_select_field( $args );

			// cart total multi-select (scope=cart_total)
			$cart_total_style = $scope_val === 'cart_total' ? '' : 'd-none';
			
			$args = array(
				'label'      => esc_html__( 'Subtotal', 'quicker' ),
				'id'         => 'extra_fees_cart_total',
				'wrapper_class'   => 'single-block inline-wrapper',
				'condition_class' => 'extra_fees_cart_total ' . $cart_total_style,
				'field_type' => 'number',
				'value'      => $extra_fees_cart_total,
				'extra_label'=> Helper::woo_currency(),
				'disable'    => $disable,
			);
			quicker_number_input_field( $args );

			// cart quantity multi-select (scope=cart_quantity)
			$cart_quantity_style = $scope_val === 'cart_quantity' ? '' : 'd-none';
			
			$args = array(
				'label'      => esc_html__( 'Cart Quantity', 'quicker' ),
				'id'         => 'extra_fees_cart_quantity',
				'condition_class' => 'extra_fees_cart_quantity ' . $cart_quantity_style,
				'field_type' => 'number',
				'value'      => $extra_fees_cart_quantity,
				'disable'    => $disable,
			);
			quicker_number_input_field( $args );

			// User Roles multi-select
			$roles = wp_roles();
			$role_options = array( 'all' => esc_html__( 'All Roles', 'quicker' ) );
			if ( ! empty( $roles->roles ) ) {
				foreach ( $roles->roles as $role_key => $role_data ) {
					$role_options[ $role_key ] = $role_data['name'];
				}
			}
			$args = array(
				'label'         => esc_html__( 'User Roles', 'quicker' ),
				'id'            => 'extra_fees_user_roles',
				'is_multiple'   => true,
				'select_type'   => 'multiple',
				'selected'      => isset( $extra_fees_user_roles ) ? (array) $extra_fees_user_roles : array(),
				'options'       => $role_options,
			);
			if ( function_exists( 'quicker_select_field' ) ) {
				quicker_select_field( $args );
			}

			// Fee Label
			$args = array(
				'label'   => esc_html__( 'Fee Label', 'quicker' ),
				'id'      => 'extra_fees_label',
				'value'   => isset( $extra_fees_label ) ? $extra_fees_label : esc_html__( 'Extra Fee', 'quicker' ),
			);
			quicker_number_input_field( $args );

			// Fee Amount
			$args = array(
				'label'       => esc_html__( 'Fee Amount', 'quicker' ),
				'id'          => 'extra_fees_amount',
				'wrapper_class'   => 'single-block inline-wrapper',
				'field_type'  => 'number',
				'value'       => isset( $extra_fees_amount ) ? $extra_fees_amount : '',
				'extra_label' => Helper::woo_currency(),
			);
			quicker_number_input_field( $args );

			$args = array(
				'label'       => esc_html__( 'Fee Description', 'quicker' ),
				'id'          => 'extra_fees_description',
				'value'       => isset( $extra_fees_description ) ? $extra_fees_description : '',
				'rows'        => 2,
				'cols'        => 40,
			);
			quicker_textarea_field( $args );
			?>
			</div>
		</div>
	</div>
</div>