<?php
/**
 * View: Widget Product Targeting Settings.
 *
 * @package Ilabs\Firevert\Modules\PromoTimeWidget
 *
 * @var WP_Post $post
 * @var bool    $firevert_promo_time_widget_all_products
 * @var array   $firevert_promo_time_widget_product_categories
 * @var array   $firevert_promo_time_widget_selected_products
 * @var array   $firevert_promo_time_widget_excluded_categories
 * @var array   $firevert_promo_time_widget_excluded_products
 */

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

use Ilabs\Firevert\Modules\PromoTimeWidget\Services\Type\SectionType;

$firevert_promo_time_widget_product_categories  = $firevert_promo_time_widget_product_categories ?? array();
$firevert_promo_time_widget_selected_products   = $firevert_promo_time_widget_selected_products ?? array();
$firevert_promo_time_widget_excluded_categories = $firevert_promo_time_widget_excluded_categories ?? array();
$firevert_promo_time_widget_excluded_products   = $firevert_promo_time_widget_excluded_products ?? array();

?>

<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="firevert-form" data-module="promo_time_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_promo_time_widget_save', 'firevert_promo_time_widget_nonce' ); ?>
	<input type="hidden" name="action" value="firevert_save_promo_time_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_promo_time_widget_all_products" value="1"
							<?php checked( true, $firevert_promo_time_widget_all_products ); ?> />
						<?php esc_html_e( 'Enable for all products', '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>
					<strong><?php esc_html_e( 'Select product categories', 'firevert' ); ?></strong><br/>
				<div id="selected-categories-repeater">
					<?php
					if ( ! empty( $firevert_promo_time_widget_product_categories ) ) :
						foreach ( $firevert_promo_time_widget_product_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_promo_time_widget_product_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_promo_time_widget_selected_products ) ) :
						foreach ( $firevert_promo_time_widget_selected_products 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_promo_time_widget_selected_products[]" 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_promo_time_widget_excluded_categories ) ) :
						foreach ( $firevert_promo_time_widget_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_promo_time_widget_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_promo_time_widget_excluded_products ) ) :
						foreach ( $firevert_promo_time_widget_excluded_products 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_promo_time_widget_excluded_products[]" 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>

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