<?php

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

if ( ! class_exists( 'EDITH_Field_product_select' ) ) {
	/**
	 * Extending Edith framework for adding new Product Select Field
	 */
	class EDITH_Field_product_select extends EDITH_Fields {

		/**
		 * Fields Render
		 *
		 * @return void
		 */
		public function render() {

			$args = wp_parse_args(
				$this->field,
				array(
					'disabled' => true,
				)
			);

			echo wp_kses_post( $this->field_before() );

			$this->value      = ( ! empty( $this->value ) ) ? $this->value : $this->field['default'];
			$disabled_options = ( ! empty( $this->value['disabled'] ) ) ? $this->value['disabled'] : array();

			$default_product_ids = get_posts(
				array(
					'numberposts'  => 10,
					'post_type'    => 'product',
					'fields'       => 'ids',
					'post__not_in' => array_unique( isset( $disabled_options ) && ! empty( $disabled_options ) ? array_merge( array_keys( $disabled_options ), array( get_the_id() ) ) : array( get_the_id() ) ),
				)
			);

			$default_products = array();

			if ( isset( $default_product_ids ) && ! empty( $default_product_ids ) ) {
				foreach ( $default_product_ids as $default_product_id ) {
					$default_products[ $default_product_id ] = esc_html( get_the_title( $default_product_id ) );
				}
			}

			echo '<div class="edith-sorter" data-depend-id="' . esc_attr( $this->field['id'] ) . '"></div>';

			echo '<input type="hidden" name="' . esc_attr( $this->field_name( '[enabled]' ) ) . '" class="edith-product-select-option-name" />';
			echo '<input type="hidden" value="' . esc_attr( get_the_id() ) . '" class="edith-current-product-id" />';

			echo '<div class="edith-product-select-wrapper">';

			$chosen_settings                       = array();
			$chosen_settings['data']['type']       = 'categories';
			$chosen_settings['data']['nonce']      = wp_create_nonce( 'edith_chosen_ajax_nonce' );
			$chosen_settings['data']['query_args'] = array( 'taxonomy' => 'product_cat' );
			?>
			<div class="edith-product-select-header">
				<input type="text" placeholder="<?php echo esc_html__( 'Search Products', 'finch-cart-product-recommendations' ); ?>" class="edith-product-select-search" />
				<div class="edith-product-select-search-category">
					<select class="edith-product-select-category edith-chosen edith-chosen-ajax" data-placeholder="<?php echo esc_html__( 'Select Product Categories', 'finch-cart-product-recommendations' ); ?>" data-chosen-settings="<?php echo esc_attr( json_encode( $chosen_settings ) ); ?>"> <?php // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode ?>
						<option value=""></option>
					</select>
				</div>
			</div>
			<?php

			echo '<div class="edith-product-select-modules">';

			echo ( $args['disabled'] ) ? '<div class="edith-modules edith-product-select-options">' : '';

			echo '<ul class="edith-enabled">';
			if ( ! empty( $default_products ) ) {
				foreach ( $default_products as $key => $value ) {
					$product       = wc_get_product( $key );
					$product_image = $product->get_image( array( 50, 50 ) );
					echo '<li><input type="hidden" name="' . esc_attr( $this->field_name( '[enabled][' . $key . ']' ) ) . '" value="' . esc_attr( $value ) . '"/><label>' . wp_kses_post( $product_image ) . esc_attr( $value ) . '</label></li>';
				}
			}
			echo '</ul>';

			// Check for hide/show disabled section.
			if ( $args['disabled'] ) {

				echo '</div>';

				echo '<div class="edith-modules edith-product-select-options-selected">';
				echo '<ul class="edith-disabled">';
				if ( ! empty( $disabled_options ) ) {
					foreach ( $disabled_options as $key => $value ) {
						$product       = wc_get_product( $key );
						$product_image = $product->get_image( array( 50, 50 ) );
						echo '<li><input type="hidden" name="' . esc_attr( $this->field_name( '[disabled][' . $key . ']' ) ) . '" value="' . esc_attr( $value ) . '"/><label>' . wp_kses_post( $product_image ) . esc_attr( $value ) . '</label></li>';
					}
				}
				echo '</ul>';
				echo '</div>';

			}

			echo '</div>';
			echo '</div>';

			echo wp_kses_post( $this->field_after() );
		}

		/**
		 * Fields Scripts Enqueue
		 *
		 * @return void
		 */
		public function enqueue() {

			if ( ! wp_script_is( 'jquery-ui-sortable' ) ) {
				wp_enqueue_script( 'jquery-ui-sortable' );
			}

			if ( ! wp_script_is( 'finch-cart-edith-custom-script' ) ) {
				wp_enqueue_script( 'finch-cart-edith-custom-script' );
			}

			if ( ! wp_style_is( 'finch-cart-edith-custom-style' ) ) {
				wp_enqueue_style( 'finch-cart-edith-custom-style' );
			}
		}
	}
}
