<?php
/**
 * View: Widget Products Settings tab.
 *
 * @package Ilabs\Firevert\Modules\PowerWidget
 *
 * @var WP_Post $post
 * @var bool    $firevert_power_widget_products_enabled
 * @var string  $firevert_power_widget_products_selection_mode
 * @var array   $firevert_power_widget_products_categories
 * @var array   $firevert_power_widget_products_selected
 * @var array   $firevert_power_widget_products_excluded_categories
 * @var array   $firevert_power_widget_products_excluded
 * @var bool    $firevert_power_widget_products_show_button
 * @var string  $firevert_power_widget_products_button_text
 * @var string  $firevert_power_widget_products_button_url
 */

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

use Ilabs\Firevert\Modules\PowerWidget\Services\Type\SectionType;
use Ilabs\Firevert\Core\Services\Helper\Integration\LanguageHelper;

firevert_view(
	'_partials/language-switcher.view.php',
	array(
		'languages' => LanguageHelper::get_available_languages(),
		'current'   => LanguageHelper::get_active_region_code(),
	)
);
?>

<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="firevert-form" data-module="power_widget" data-search-products-placeholder="<?php esc_attr_e( 'Search products', 'firevert' ); ?>" data-product-exists-message="<?php esc_attr_e( 'Product already exists', 'firevert' ); ?>" data-product-not-selected-message="<?php esc_attr_e( 'Please select a product', 'firevert' ); ?>" data-remove-button-text="<?php esc_attr_e( 'Remove', 'firevert' ); ?>" data-search-categories-placeholder="<?php esc_attr_e( 'Search categories', 'firevert' ); ?>">
	<?php wp_nonce_field( 'firevert_power_widget_save', 'firevert_power_widget_nonce' ); ?>
	<input type="hidden" name="action" value="firevert_save_power_widget">
	<input type="hidden" name="section" value="<?php echo esc_attr( SectionType::PRODUCTS ); ?>">
	<input type="hidden" name="post_ID" value="<?php echo esc_attr( $post->ID ); ?>">

	<div class="metabox-holder woocommerce">

		<div class="postbox wc-metabox">
			<div class="inside">
				<p>
					<label>
						<input type="checkbox" name="firevert_power_widget_products_enabled" value="1"
							<?php checked( $firevert_power_widget_products_enabled ); ?> />
						<?php esc_html_e( 'Enable product list?', 'firevert' ); ?>
					</label>
				</p>
			</div>
		</div>

		<div class="postbox wc-metabox">
			<div class="postbox-header">
				<h2 class="hndle"><?php esc_html_e( 'Select products for the list', 'firevert' ); ?></h2>
			</div>
			<div class="inside">
				<p>
					<label>
						<?php esc_html_e( 'Which products to select?', 'firevert' ); ?><br/>
						<select name="firevert_power_widget_products_selection_mode">
							<option value="sale" <?php selected( $firevert_power_widget_products_selection_mode, 'sale' ); ?>>
								<?php esc_html_e( 'All on sale', 'firevert' ); ?>
							</option>
							<option value="selected" <?php selected( $firevert_power_widget_products_selection_mode, 'selected' ); ?>>
								<?php esc_html_e( 'Selected', 'firevert' ); ?>
							</option>
						</select>
					</label>
				</p>

				<p>
					<strong><?php esc_html_e( 'Select product categories', 'firevert' ); ?></strong><br/>
				<div id="selected-categories-repeater">
					<?php
					if ( ! empty( $firevert_power_widget_products_categories ) ) :
						foreach ( $firevert_power_widget_products_categories as $category_id ) :
							$category = get_term( $category_id, 'product_cat' );
							if ( $category && ! is_wp_error( $category ) ) :
								?>
								<div class="repeater-item" style="margin-bottom: 5px; display: flex; align-items: center; gap: 10px;">
									<input type="hidden" name="firevert_power_widget_products_categories[]" value="<?php echo esc_attr( $category_id ); ?>" />
									<span class="category-name"><?php echo esc_html( $category->name ); ?></span>
									<button type="button" class="button remove-selected-category"><?php esc_html_e( 'Remove', 'firevert' ); ?></button>
								</div>
								<?php
							endif;
						endforeach;
					endif;
					?>
					<div id="add-selected-category-container" style="margin-top: 10px;">
						<select class="firevert-select2 firevert-select2-selected-categories" style="width: 300px;">
							<option value=""><?php esc_html_e( 'Select category', 'firevert' ); ?></option>
						</select>
						<button type="button" class="button add-selected-category" style="margin-left: 10px;"><?php esc_html_e( 'Add category', 'firevert' ); ?></button>
					</div>
				</div>
				</p>

				<p>
					<strong><?php esc_html_e( 'Selected products', 'firevert' ); ?></strong><br/>
				<div id="selected-products-repeater">
					<?php
					if ( ! empty( $firevert_power_widget_products_selected ) ) :
						foreach ( $firevert_power_widget_products_selected as $product_id ) :
							$product = wc_get_product( $product_id );
							if ( $product ) :
								?>
								<div class="repeater-item" style="margin-bottom: 5px; display: flex; align-items: center; gap: 10px;">
									<input type="hidden" name="firevert_power_widget_products_selected[]" value="<?php echo esc_attr( $product_id ); ?>" />
									<span class="product-name"><?php echo esc_html( $product->get_name() ); ?></span>
									<button type="button" class="button remove-selected-product"><?php esc_html_e( 'Remove', 'firevert' ); ?></button>
								</div>
								<?php
							endif;
						endforeach;
					endif;
					?>
					<div id="add-selected-product-container" style="margin-top: 10px;">
						<select class="firevert-select2 firevert-select2-selected-products" style="width: 300px;">
							<option value=""><?php esc_html_e( 'Select product', 'firevert' ); ?></option>
						</select>
						<button type="button" class="button add-selected-product" style="margin-left: 10px;"><?php esc_html_e( 'Add product', 'firevert' ); ?></button>
					</div>
				</div>
				</p>
			</div>
		</div>

		<div class="postbox wc-metabox">
			<div class="postbox-header">
				<h2 class="hndle"><?php esc_html_e( 'Excluded products', 'firevert' ); ?></h2>
			</div>
			<div class="inside">
				<p>
					<strong><?php esc_html_e( 'Exclude product categories', 'firevert' ); ?></strong><br/>
				<div id="excluded-categories-repeater">
					<?php
					if ( ! empty( $firevert_power_widget_products_excluded_categories ) ) :
						foreach ( $firevert_power_widget_products_excluded_categories as $category_id ) :
							$category = get_term( $category_id, 'product_cat' );
							if ( $category && ! is_wp_error( $category ) ) :
								?>
								<div class="repeater-item" style="margin-bottom: 5px; display: flex; align-items: center; gap: 10px;">
									<input type="hidden" name="firevert_power_widget_products_excluded_categories[]" value="<?php echo esc_attr( $category_id ); ?>" />
									<span class="category-name"><?php echo esc_html( $category->name ); ?></span>
									<button type="button" class="button remove-excluded-category"><?php esc_html_e( 'Remove', 'firevert' ); ?></button>
								</div>
								<?php
							endif;
						endforeach;
					endif;
					?>
					<div id="add-excluded-category-container" style="margin-top: 10px;">
						<select class="firevert-select2 firevert-select2-excluded-categories" style="width: 300px;">
							<option value=""><?php esc_html_e( 'Select category', 'firevert' ); ?></option>
						</select>
						<button type="button" class="button add-excluded-category" style="margin-left: 10px;"><?php esc_html_e( 'Add category', 'firevert' ); ?></button>
					</div>
				</div>
				</p>

				<p>
					<strong><?php esc_html_e( 'Excluded products', 'firevert' ); ?></strong><br/>
				<div id="excluded-products-repeater">
					<?php
					if ( ! empty( $firevert_power_widget_products_excluded ) ) :
						foreach ( $firevert_power_widget_products_excluded as $product_id ) :
							$product = wc_get_product( $product_id );
							if ( $product ) :
								?>
								<div class="repeater-item" style="margin-bottom: 5px; display: flex; align-items: center; gap: 10px;">
									<input type="hidden" name="firevert_power_widget_products_excluded[]" value="<?php echo esc_attr( $product_id ); ?>" />
									<span class="product-name"><?php echo esc_html( $product->get_name() ); ?></span>
									<button type="button" class="button remove-excluded-product"><?php esc_html_e( 'Remove', 'firevert' ); ?></button>
								</div>
								<?php
							endif;
						endforeach;
					endif;
					?>
					<div id="add-excluded-product-container" style="margin-top: 10px;">
						<select class="firevert-select2 firevert-select2-exclude-products" style="width: 300px;">
							<option value=""><?php esc_html_e( 'Select product', 'firevert' ); ?></option>
						</select>
						<button type="button" class="button add-excluded-product" style="margin-left: 10px;"><?php esc_html_e( 'Add product', 'firevert' ); ?></button>
					</div>
				</div>
				</p>
			</div>
		</div>

		<div class="postbox wc-metabox">
			<div class="postbox-header">
				<h2 class="hndle"><?php esc_html_e( 'Product list button', 'firevert' ); ?></h2>
			</div>
			<div class="inside">
				<p>
					<label>
						<input type="checkbox" name="firevert_power_widget_products_show_button" value="1"
							<?php checked( $firevert_power_widget_products_show_button ); ?> />
						<?php esc_html_e( 'Enable button under product list', 'firevert' ); ?>
					</label>
				</p>
				<p>
					<label for='firevert_power_widget_products_button_text'>
						<?php echo esc_html( firevert_label_with_lang( __( 'Button text', 'firevert' ) ) ); ?>
					</label><br/>
					<input type="text" name="firevert_power_widget_products_button_text"
						id="firevert_power_widget_products_button_text"
						value="<?php echo esc_attr( $firevert_power_widget_products_button_text ?? '' ); ?>" />
				</p>
				<p>
					<label for='firevert_power_widget_products_button_url'>
						<?php echo esc_html( firevert_label_with_lang( __( 'Where should the button link to?', 'firevert' ) ) ); ?>
					</label><br/>
					<input type="url" name="firevert_power_widget_products_button_url"
						id="firevert_power_widget_products_button_url"
						value="<?php echo esc_url( $firevert_power_widget_products_button_url ); ?>" />
				</p>
			</div>
		</div>
	</div>

	<?php submit_button( __( 'Save', 'firevert' ) ); ?>
</form>
