<?php
add_action('wp_ajax_load_more_terms_func', 'load_more_terms_func');
add_action('wp_ajax_nopriv_load_more_terms_func', 'load_more_terms_func'); // For non-logged-in users if needed
function load_more_terms_func()
{
    // Get parameters
    $taxonomy = isset($_POST['taxonomy']) ? sanitize_text_field($_POST['taxonomy']) : '';
    $parent_id = isset($_POST['parent_id']) ? intval($_POST['parent_id']) : 0;
    $paged = isset($_POST['paged']) ? intval($_POST['paged']) : 1;
    $per_page = isset($_POST['per_page']) ? intval($_POST['per_page']) : 5;

    if (empty($taxonomy) || $parent_id <= 0) {
        wp_send_json_error('Invalid taxonomy.');
        wp_die();
    }

    // Query terms
    $args = array(
        'taxonomy' => $taxonomy,
        'hide_empty' => false,
        'parent' => $parent_id,
        'number' => $per_page,
        'offset' => ($paged - 1) * $per_page,
    );
    $child_terms = get_terms($args);

    if (is_wp_error($child_terms) || empty($child_terms)) {
        wp_send_json_error('No more terms.');
        wp_die();
    }

    // Return terms
    ob_start();
    foreach ($child_terms as $key => $child) {
        tdg_render_post_and_term_items('term', $child->taxonomy, $child->term_id);
    }
    $html = ob_get_clean();

    $total_terms = get_terms([
        'taxonomy' => $taxonomy,
        'hide_empty' => false,
        'parent' => $parent_id,
        'fields' => 'count',
    ]);
    $has_more = ($paged * $per_page) < $total_terms;

    wp_send_json_success(['html' => $html, 'has_more' => $has_more]);

    die;
}

add_action('wp_ajax_load_more_fabric_func', 'load_more_fabric_func');
add_action('wp_ajax_nopriv_load_more_fabric_func', 'load_more_fabric_func'); // For non-logged-in users if needed
function load_more_fabric_func()
{
    // Get parameters
    $post_type = isset($_POST['post_type']) ? sanitize_text_field($_POST['post_type']) : 'custom-fabrication';
    $taxonomy = isset($_POST['taxonomy']) ? sanitize_text_field($_POST['taxonomy']) : '';
    $term = isset($_POST['term']) ? sanitize_text_field($_POST['term']) : '';
    $paged = isset($_POST['paged']) ? intval($_POST['paged']) : 1;
    $current_id = isset($_POST['current_id']) ? intval($_POST['current_id']) : '';

    // Query
    $args = array(
        'post_type' => $post_type,
        'posts_per_page' => 6,
        'paged' => $paged,
    );
    if ($taxonomy && $term) {
        $args['tax_query'] = array(
            array(
                'taxonomy' => $taxonomy,
                'terms' => $term,
                'field' => 'slug',
            )
        );
    }
    if( !empty($current_id) ){
        $args['post__not_in'] = array( $current_id );
    }
    $query = new WP_Query($args);

    // Return terms
    ob_start();
    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();
            $post_id = get_the_ID();
            tdg_render_post_and_term_items('post', '', $post_id);
        }
        wp_reset_postdata();
    }
    $html = ob_get_clean();

    wp_send_json_success(['html' => $html]);

    die;
}

