<?php

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

global $post;
$rule_data = get_post_meta( $post->ID, 'pchfw_rule_data', true );
$hide_price         = isset( $rule_data['hide_price'] ) ? $rule_data['hide_price'] : '';
$hide_price_text    = isset( $rule_data['hide_price_text'] ) ? $rule_data['hide_price_text'] : '';
$hide_cart_button   = isset( $rule_data['hide_cart_button'] ) ? $rule_data['hide_cart_button'] : '';
$show_custom_button   = isset( $rule_data['show_custom_button'] ) ? $rule_data['show_custom_button'] : '';

$custom_button_label = isset( $rule_data['custom_button_label'] ) ? $rule_data['custom_button_label'] : '';
$custom_button_link  = isset( $rule_data['custom_button_link'] ) ? $rule_data['custom_button_link'] : '';
$select_type = isset( $rule_data['select_type'] ) ? $rule_data['select_type'] : 'product';
$include_product = isset( $rule_data[ 'include_' . $select_type ]['toggle'] ) ? $rule_data[ 'include_' . $select_type ]['toggle'] : 'all';
$include_product_ids = isset( $rule_data[ 'include_' . $select_type ][ $include_product ] ) ? $rule_data[ 'include_' . $select_type ][ $include_product ] : array();
$excluded_product_ids = isset( $rule_data[ 'exclude_' . $select_type ] ) ? $rule_data[ 'exclude_' . $select_type ] : array();


?>

<table class="form-table">
	<?php wp_nonce_field('pchfw_metabox_nonce', 'pchfw_metabox_nonce'); ?>
	<tbody>
		<tr valign="top">
			<th scope="row" class="titledesc"><?php esc_html_e( 'Hide Price', 'price-cart-hider-for-woocommerce' ); ?></th>
			<td class="forminp forminp-checkbox">
				<label for="pchfw_rule_hide_price">
					<input name="pchfw_rule[hide_price]" id="pchfw_rule_hide_price" type="checkbox" value="1" <?php checked( $hide_price, '1' ); ?>>
					<?php esc_html_e( 'Enable to hide the product price.', 'price-cart-hider-for-woocommerce' ); ?>
				</label>
			</td>
		</tr>

		<tr valign="top">
			<th scope="row" class="titledesc"><?php esc_html_e( 'Replace Price Text', 'price-cart-hider-for-woocommerce' ); ?></th>
			<td class="forminp forminp-text">
				<textarea name="pchfw_rule[hide_price_text]" rows="4" cols="50" style="width: 350px;"><?php echo esc_textarea( $hide_price_text ); ?></textarea>
				<p class="description"><?php esc_html_e( 'Text to display instead of the price. Leave blank for no replacement.', 'price-cart-hider-for-woocommerce' ); ?></p>
			</td>
		</tr>

		<tr valign="top">
			<th scope="row" class="titledesc"><?php esc_html_e( 'Hide Add to Cart Button', 'price-cart-hider-for-woocommerce' ); ?></th>
			<td class="forminp forminp-checkbox">
				<label for="pchfw_rule_hide_cart_button">
					<input name="pchfw_rule[hide_cart_button]" id="pchfw_rule_hide_cart_button" type="checkbox" value="1" <?php checked( $hide_cart_button, '1' ); ?>>
					<?php esc_html_e( 'Enable to hide the add to cart button.', 'price-cart-hider-for-woocommerce' ); ?>
				</label>
			</td>
		</tr>

		<tr valign="top">
			<th scope="row" class="titledesc"><?php esc_html_e( 'Show Custom Button', 'price-cart-hider-for-woocommerce' ); ?></th>
			<td class="forminp forminp-checkbox">
				<label for="pchfw_rule_show_custom_button">
					<input name="pchfw_rule[show_custom_button]" id="pchfw_rule_show_custom_button" type="checkbox" value="1" <?php checked( $show_custom_button, '1' ); ?>>
					<?php esc_html_e( 'Show Custom Button on Prodct', 'price-cart-hider-for-woocommerce' ); ?>
				</label>
			</td>
		</tr>

		<tr valign="top">
			<th scope="row" class="titledesc"><?php esc_html_e( 'Custom Button Label', 'price-cart-hider-for-woocommerce' ); ?></th>
			<td class="forminp forminp-text">
				<input name="pchfw_rule[custom_button_label]" id="pchfw_rule_custom_button_label" type="text" value="<?php echo esc_attr( $custom_button_label ); ?>" style="width: 350px;">
				<p class="description"><?php esc_html_e( 'Set a custom label for the button.', 'price-cart-hider-for-woocommerce' ); ?></p>
			</td>
		</tr>

		<tr valign="top">
			<th scope="row" class="titledesc"><?php esc_html_e( 'Custom Button Link', 'price-cart-hider-for-woocommerce' ); ?></th>
			<td class="forminp forminp-text">
				<input name="pchfw_rule[custom_button_link]" id="pchfw_rule_custom_button_link" type="text" value="<?php echo esc_attr( $custom_button_link ); ?>" style="width: 350px;">
				<p class="description"><?php esc_html_e( 'Set a custom link for the button.', 'price-cart-hider-for-woocommerce' ); ?></p>
			</td>
		</tr>

		<tr valign="top">
			<th scope="row" class="titledesc"><?php echo esc_html__( 'Select Type', 'price-cart-hider-for-woocommerce' ); ?></th>
			<td class="forminp forminp-radio">
				<label for="wc_rule_data_product_type" class="wc_rules_product_label">
					<input name="pchfw_rule[select_type]" type="radio" id="wc_rule_data_product_type" class="selected-products" value="product" <?php echo checked( 'product', $select_type, false ); ?>>
					<?php echo esc_html__( 'Product', 'price-cart-hider-for-woocommerce' ); ?>
				</label>
				<label for="wc_rule_data_category_type" class="wc_rules_category_label">
					<input name="pchfw_rule[select_type]" id="wc_rule_data_category_type" value="category" class="selected-category" type="radio" <?php echo checked( 'category', $select_type, false ); ?>>
					<?php echo esc_html__( 'Category', 'price-cart-hider-for-woocommerce' ); ?>
				</label>
				<label for="wc_rule_data_tag_type" >
					<input name="pchfw_rule[select_type]" id="wc_rule_data_tag_type" value="tag" class="selected-tag" type="radio" <?php echo checked( 'tag', $select_type, false ); ?>>
					<?php echo esc_html__( 'Tag', 'price-cart-hider-for-woocommerce' ); ?>
				</label>
			</td> 
		</tr>

		<!-- Product Filter -->
		<tr class="filter-section" data-filter-type="product">
		<th scope="row" class="titledesc"><?php echo esc_html__( 'Include Products', 'price-cart-hider-for-woocommerce' ); ?></th>
			<td class="forminp forminp-checkbox">
				<label for="wc_rule_data_product_all" class="wc_rules_all_product_label">
					<input name="pchfw_rule[include_product][toggle]" id="wc_rule_data_product_all" type="radio" value="all" <?php checked( 'all', $include_product, true ); ?>><?php echo esc_html__( 'All', 'price-cart-hider-for-woocommerce' ); ?>
				</label>
				<label for="wc_rule_data_product_specific">
					<input name="pchfw_rule[include_product][toggle]" id="wc_rule_data_product_specific" type="radio" value="specific" <?php checked( 'specific', $include_product, true ); ?>><?php echo esc_html__( 'Specific Products', 'price-cart-hider-for-woocommerce' ); ?>
					<div class="rules-specific-container">
						<select class="wc-product-search" data-exclude_type="variable" multiple="multiple" id="wc_rule_data_product_search" name="pchfw_rule[include_product][specific][]" data-placeholder="<?php esc_attr_e( 'Search for a product&hellip;', 'price-cart-hider-for-woocommerce' ); ?>">
							<?php
							foreach ( $include_product_ids as $include_product_id ) {
								$product = wc_get_product( $include_product_id );
								if ( is_object( $product ) ) {
									echo '<option value="' . esc_attr( $include_product_id ) . '"' . selected( true, true, false ) . '>' . esc_html( wp_strip_all_tags( $product->get_formatted_name() ) ) . '</option>';
								}
							}
							?>
						</select>  
					</div>
				</label>
			</td>
			</tr>
			<tr class="filter-section exclude-filter" data-filter-type="product">
			<th scope="row" class="titledesc"><?php echo esc_html__( 'Exclude Products', 'price-cart-hider-for-woocommerce' ); ?></th>
			<td class="forminp forminp-checkbox">
				<label for="wc_rule_data_product_exclude">
					<select class="wc-product-search" data-exclude_type="variable" multiple="multiple" id="wc_rule_data_product_exclude" name="pchfw_rule[exclude_product][]" data-placeholder="<?php esc_attr_e( 'Search for a product&hellip;', 'price-cart-hider-for-woocommerce' ); ?>" > 
					<?php
					foreach ( $excluded_product_ids as $excluded_product_id ) {
						$product = wc_get_product( $excluded_product_id );
						if ( is_object( $product ) ) {
							echo '<option value="' . esc_attr( $excluded_product_id ) . '"' . selected( true, true, false ) . '>' . esc_html( wp_strip_all_tags( $product->get_formatted_name() ) ) . '</option>';
						}
					}
					?>
					</select>
				</label>
			</td>
		</tr>

		<!-- category Filter -->
		<tr class="filter-section" data-filter-type="category">
		<th scope="row" class="titledesc"><?php echo esc_html__( 'Include Category', 'price-cart-hider-for-woocommerce' ); ?></th>
			<td class="forminp forminp-checkbox">
				<label for="wc_rule_data_category_all" class="wc_rules_all_category_label">
					<input name="pchfw_rule[include_category][toggle]" id="wc_rule_data_category_all" type="radio" value="all" <?php checked( 'all', $include_product, true ); ?>><?php echo esc_html__( 'All', 'price-cart-hider-for-woocommerce' ); ?>
				</label>
				<label for="wc_rule_data_category_specific">
					<input name="pchfw_rule[include_category][toggle]" id="wc_rule_data_category_specific" type="radio" value="specific" <?php checked( 'specific', $include_product, true ); ?>><?php echo esc_html__( 'Specific Category', 'price-cart-hider-for-woocommerce' ); ?>
					<div class="rules-specific-container">
						<select class="wc-category-search" multiple="multiple" id="wc_rule_data_category_search" name="pchfw_rule[include_category][specific][]" data-placeholder="<?php esc_attr_e( 'Search for a category&hellip;', 'price-cart-hider-for-woocommerce' ); ?>" data-action="woocommerce_json_search_category" data-exclude="">
						<?php
						foreach ( $include_product_ids as $select_category ) {

							$product_cat = get_term_by( 'slug', $select_category, 'product_cat' );
							if ( is_object( $product_cat ) ) {

								echo '<option value="' . esc_attr($select_category) . '"' . selected( true, true, false ) . '>' . esc_html( $product_cat->name ) . '</option>';
							}
						}
						?>
							
						</select>  
					</div>
				</label>
			</td>
			</tr>
			<tr class="filter-section exclude-filter" data-filter-type="category">
			<th scope="row" class="titledesc"><?php echo esc_html__( 'Exclude Category', 'price-cart-hider-for-woocommerce' ); ?></th>
			<td class="forminp forminp-checkbox">
				<label for="wc_rule_data_category_exclude">
					<select class="wc-category-search" multiple="multiple" id="wc_rule_data_category_exclude" name="pchfw_rule[exclude_category][]" data-placeholder="<?php esc_attr_e( 'Search for a category&hellip;', 'price-cart-hider-for-woocommerce' ); ?>" data-action="woocommerce_json_search_category">
					<?php

					foreach ( $excluded_product_ids as $select_category ) {

						$product_cat = get_term_by( 'slug', $select_category, 'product_cat' );
						if ( is_object( $product_cat ) ) {

							echo '<option value="' . esc_attr($select_category) . '"' . selected( true, true, false ) . '>' . esc_html( $product_cat->name ) . '</option>';
						}
					}
					?>
						
				</select>
				</label>
			</td>
		</tr>
		
		<!-- tag Filter -->
		<tr class="filter-section" data-filter-type="tag">
		<th scope="row" class="titledesc"><?php echo esc_html__( 'Include Tag', 'price-cart-hider-for-woocommerce' ); ?></th>
			<td class="forminp forminp-checkbox">
				<label for="wc_rule_data_tag_all" class="wc_rules_all_tag_label">
					<input name="pchfw_rule[include_tag][toggle]" id="wc_rule_data_tag_all" type="radio" value="all" <?php checked( 'all', $include_product, true ); ?>><?php echo esc_html__( 'All', 'price-cart-hider-for-woocommerce' ); ?>
				</label>
				<label for="wc_rule_data_tag_specific">
					<input name="pchfw_rule[include_tag][toggle]" id="wc_rule_data_tag_specific" type="radio" value="specific" <?php checked( 'specific', $include_product, true ); ?>><?php echo esc_html__( 'Specific Tag', 'price-cart-hider-for-woocommerce' ); ?>
					
					<div class="rules-specific-container">
						<select class="select2 wc-enhanced-select" multiple="multiple" id="new-tag-product_tag" name="pchfw_rule[include_tag][specific][]" data-placeholder="<?php esc_attr_e( 'Search for a tag&hellip;', 'price-cart-hider-for-woocommerce' ); ?>" data-action="woocommerce_json_search_tag" data-exclude="">
						<?php

						$taxonomies = 'product_tag';
						$product_tags = get_terms( $taxonomies );

						foreach ( $product_tags as $values ) {
							$key = $values->slug;
							$value = $values->name;
							$selected = is_array( $include_product_ids ) && in_array( $key, $include_product_ids ) ? 'selected' : '';

							echo '<option value="' . esc_attr( $key ) . '" ' . esc_attr( $selected ) . '>' . esc_html( $value ) . '</option>';
						}
						?>
						</select>  
					</div>
				</label>
			</td>
			</tr>
			
			<tr class="filter-section exclude-filter" data-filter-type="tag">
			<th scope="row" class="titledesc"><?php echo esc_html__( 'Exclude Tag', 'price-cart-hider-for-woocommerce' ); ?></th>
			<td class="forminp forminp-checkbox">
				<label for="wc_rule_data_tag_exclude">
					<select class="select2 wc-enhanced-select" multiple="multiple" id="wc_rule_data_tag_exclude" name="pchfw_rule[exclude_tag][]" data-placeholder="<?php esc_attr_e( 'Search for a Tag&hellip;', 'price-cart-hider-for-woocommerce' ); ?>" data-action="woocommerce_json_search_tag" data-exclude=""> 
					<?php
					$taxonomies = 'product_tag';
					$product_tags = get_terms( $taxonomies );

					foreach ( $product_tags as $values ) {
						$key = $values->slug;
						$value = $values->name;
						$selected = is_array( $excluded_product_ids ) && in_array( $key, $excluded_product_ids ) ? 'selected' : '';

						echo '<option value="' . esc_attr( $key ) . '" ' . esc_attr( $selected ) . '>' . esc_html( $value ) . '</option>';
					}
					?>
					</select>
				</label>
			</td>
		</tr>

	</tbody>
</table>
