<?php
$prefix = 'rex_feed_';
$icon = '../assets/icon/icon-svg/icon-question.php';
$product_categories_url = 'https://rextheme.com/docs/wpfm-category-filter-generating-product-feed/?utm_source=plugin&utm_medium=category_filter_link&utm_campaign=pfm_plugin';
$product_tag_url = 'https://rextheme.com/docs/wpfm-tag-filter-generating-product-feed/?utm_source=plugin&utm_medium=tag_filter_link&utm_campaign=pfm_plugin';
$product_brand_url = 'https://rextheme.com/docs/how-to-use-brands-filter-when-generating-product-feed/';
?>

<div id="rex-feed-product-taxonomies-contents">
	<div id="rex-feed-product-cats" style="display: none">
        <div class="rex-feed-product-cats">
            <label for="<?php echo esc_attr($prefix) . 'cats';?>">
                <?php esc_html_e('Product Categories', 'rex-product-feed')?>
                <span class="rex_feed-tooltip">
                    <?php include plugin_dir_path(__FILE__) . $icon;?>
                    <p>
                        <?php esc_html_e('Add or exclude products by WooCommerce categories.', 'rex-product-feed')?>
                    </p>
                </span>
                <?php
                $saved_val = get_post_meta( $feed_id, '_rex_feed_cats_check_all_btn', true );
                $checked = $saved_val ? ' checked' : '';
                ?>
                <input id="rex_feed_cats_check_all_btn" name="rex_feed_cats_check_all_btn" type="checkbox" <?php echo $checked?>>
                <label for="rex_feed_cats_check_all_btn"><?php esc_html_e('Check All', 'rex-product-feed')?></label>
            </label>

            <a href="<?php echo esc_url($product_categories_url)?>" target="_blank">
                <?php esc_html_e('View Doc', 'rex-product-feed')?>
            </a>

        </div>

		<ul id="<?php echo esc_attr($prefix) . 'cats';?>">
			<?php
			$terms      = get_terms( array( 'taxonomy' => 'product_cat' ) );
			$terms      = is_array( $terms ) ? $terms : array();
			$post_terms = wp_get_post_terms( $feed_id, 'product_cat', array( 'fields' => 'slugs' ) );
			$post_terms = is_array( $post_terms ) ? $post_terms : array();
			$index      = 1;

            if ( empty( $terms ) ) {
                echo '<li>';
                echo '<label for="'. esc_attr($prefix) . 'tags' . esc_attr($index++) . '">'.esc_html__('No Categories', 'rex-product-feed').'</label>';
                echo '</li>';
            }
            else {
                foreach( $terms as $term ) {
                    $checked = in_array( $term->slug, $post_terms) ? ' checked' : '';
                    echo '<li>';
                    echo '<input type="checkbox" class="' . esc_attr($prefix) . 'cats' . '" id="'. esc_attr($prefix) . 'cats' . esc_attr($index) . '" name="'. esc_attr($prefix) . 'cats[]' . '" value="'. esc_attr($term->slug) .'" ' .esc_attr($checked). '>';
                    echo '<label for="'. esc_attr($prefix) . 'cats' . esc_attr($index++) . '">'.esc_html__($term->name, 'rex-product-feed').'</label>';
                    echo '</li>';
                }
            }
			?>
		</ul>
	</div>

	<div id="rex-feed-product-tags" style="display: none">
        <div class="rex-feed-product-tags">
            <label for="<?php echo esc_attr($prefix) . 'tags';?>">
                <?php esc_html_e('Product Tags', 'rex-product-feed')?>
                <span class="rex_feed-tooltip">
                    <?php include plugin_dir_path(__FILE__) . $icon;?>
                    <p>
                        <?php esc_html_e('Add or exclude products by WooCommerce tags.', 'rex-product-feed')?>
                    </p>
                </span>
                <?php
                $saved_val = get_post_meta( $feed_id, '_rex_feed_tags_check_all_btn', true );
                $checked = $saved_val ? ' checked' : '';
                ?>
                <input id="rex_feed_tags_check_all_btn" name="rex_feed_tags_check_all_btn" type="checkbox" <?php echo $checked?>>
                <label for="rex_feed_tags_check_all_btn"><?php esc_html_e('Check All', 'rex-product-feed')?></label>
            </label>

            <a href="<?php echo esc_url($product_tag_url)?>" target="_blank">
                <?php esc_html_e('View Doc', 'rex-product-feed')?>
            </a>

        </div>

		<ul id="<?php echo esc_attr($prefix) . 'tags';?>">
			<?php
			$terms      = get_terms( array( 'taxonomy' => 'product_tag' ) );
			$terms      = is_array( $terms ) ? $terms : array();
            $post_terms = wp_get_post_terms( $feed_id, 'product_tag', array( 'fields' => 'slugs' ) );
			$post_terms = is_array( $post_terms ) ? $post_terms : array();
			$index      = 1;

            if ( empty( $terms ) ) {
                echo '<li>';
                echo '<label for="'. esc_attr($prefix) . 'tags' . esc_attr($index++) . '">'.esc_html__('No Terms', 'rex-product-feed').'</label>';
                echo '</li>';
            }
            else {
                foreach( $terms as $term ) {
                    $checked = isset( $term->slug ) && in_array( $term->slug, $post_terms) ? ' checked' : '';
                    echo '<li>';
                    echo '<input type="checkbox" class="' . esc_attr($prefix) . 'tags' . '" id="'. esc_attr($prefix) . 'tags' . esc_attr($index) . '" name="'. esc_attr($prefix) . 'tags[]' . '" value="'. esc_attr($term->slug) .'" ' .esc_attr($checked). '>';
                    echo '<label for="'. esc_attr($prefix) . 'tags' . esc_attr($index++) . '">'.esc_html__($term->name, 'rex-product-feed').'</label>';
                    echo '</li>';
                }
            }
			?>
		</ul>
	</div>

    <div id="rex-feed-product-brands" style="display: none">
        <div class="rex-feed-product-brands">
            <label for="<?php echo esc_attr($prefix) . 'brands';?>">
                <?php esc_html_e('Product Brands', 'rex-product-feed')?>
                <span class="rex_feed-tooltip">
                    <?php include plugin_dir_path(__FILE__) . $icon;?>
                    <p>
                        <?php esc_html_e('Add or exclude products by WooCommerce brands.', 'rex-product-feed')?>
                    </p>
                </span>
                <?php
                $saved_val = get_post_meta( $feed_id, '_rex_feed_brands_check_all_btn', true );
                $checked = $saved_val ? ' checked' : '';
                ?>
                <input id="rex_feed_brands_check_all_btn" name="rex_feed_brands_check_all_btn" type="checkbox" <?php echo $checked?>>
                <label for="rex_feed_brands_check_all_btn"><?php esc_html_e('Check All', 'rex-product-feed')?></label>
            </label>

            <a href="<?php echo esc_url($product_brand_url)?>" target="_blank">
                <?php esc_html_e('View Doc', 'rex-product-feed')?>
            </a>

        </div>

        <ul id="<?php echo esc_attr($prefix) . 'brands'; ?>">
            <?php
            $brands = get_terms( array(
                'taxonomy'   => 'product_brand'
            ) );
            $brands      = is_array( $brands ) ? $brands : array();
            $post_brands = wp_get_post_terms( $feed_id, 'product_brand', array( 'fields' => 'slugs' ) );
            $post_brands = is_array( $post_brands ) ? $post_brands : array();
            $index       = 1;

            if ( empty( $brands ) ) {
                echo '<li>';
                echo '<label for="'. esc_attr($prefix) . 'brands' . esc_attr($index++) . '">'.esc_html__('No Brands Found', 'rex-product-feed').'</label>';
                echo '</li>';
            } else {
                foreach ( $brands as $brand ) {
                    $checked = in_array( $brand->slug, $post_brands ) ? ' checked' : '';
                    echo '<li>';
                    echo '<input type="checkbox" class="' . esc_attr($prefix) . 'brands' . '" id="' . esc_attr($prefix) . 'brands' . esc_attr($index) . '" name="' . esc_attr($prefix) . 'brands[]' . '" value="' . esc_attr($brand->slug) . '"' . $checked . '>';
                    echo '<label for="' . esc_attr($prefix) . 'brands' . esc_attr($index++) . '">' . esc_html( $brand->name ) . '</label>';
                    echo '</li>';
                }
            }
            ?>
        </ul>


    </div>

</div>