<?php 
/**
 * Register Projects post Type
 */
function project_codex_custom_init()
{
    $labels = array(
        'name' => 'Projects',
        'singular_name' => 'Projects',
        'add_new' => 'Add New',
        'add_new_item' => 'Add New Projects',
        'edit_item' => 'Edit Projects',
        'new_item' => 'New Projects',
        'all_items' => 'All Projects',
        'view_item' => 'View Projects',
        'search_items' => 'Search Projects',
        'not_found' => 'No Projects',
        'not_found_in_trash' => 'No Projects in Trash',
        'parent_item_colon' => '',
        'menu_name' => 'Projects'
    );
    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'projects'),
        'capability_type' => 'post',
        'has_archive' => true,
        'hierarchical' => false,
        'menu_position' => 24,
        'menu_icon' => 'dashicons-images-alt2',
        'supports' => array('title', 'editor', 'thumbnail')
    );
    register_post_type('projects', $args);
}
add_action('init', 'project_codex_custom_init');

function gl_create_my_taxonomy()
{
    register_taxonomy(
        'projects-category',
        'projects',
        array(
            'label' => __('Projects Categories', 'glorious'),
            'rewrite' => array('slug' => 'projects-category'),
            'hierarchical' => true,
        )
    );
}
add_action('init', 'gl_create_my_taxonomy');


    // Shortcode to display posts based on category ID
    function display_posts_by_category($atts) {


        wp_enqueue_style('project-style', get_stylesheet_directory_uri() . '/gl_post/css/projects.css');

        // Extract shortcode attributes
        $atts = shortcode_atts(array(
            'category_id' => 0,
        ), $atts, 'posts_by_category');

        // Generate dynamic ID for posts container
        $container_id = 'posts-container-' . $atts['category_id'];

        // Query posts based on category ID
        $args = array(
            'post_type' => 'projects',
            'posts_per_page' => 3,
            'paged' => 1, // Start from the first page
            'tax_query' => array(
                array(
                    'taxonomy' => 'projects-category',
                    'field' => 'term_id',
                    'terms' => $atts['category_id'],
                ),
            ),
        );
        $query = new WP_Query($args);
        // Output posts
        $output = '<div id="' . $container_id . '" class="posts-container">'; // Add dynamic ID
        if ($query->have_posts()) {
          
            while ($query->have_posts()) {
                $query->the_post();
                $location = get_field('location');
                $img1 = get_field('img1');
                $img2 = get_field('img2');
                $img3 = get_field('img3');
                $days = get_field('days');
                $man_power = get_field('man_power');
                $satisfied_clients = get_field('satisfied_clients');
                $technology = get_field('technology');
            
                $output .= '<div class="post-list">';
                $output .= '<div class="post-data">';
                if ($img1) {
                    $output .= '<div class="post-img"><img src="' . $img1 . '" alt=""></div>';
                }
                if ($img2) {
                    $output .= '<div class="post-img"><img src="' . $img2 . '" alt=""></div>';
                }
                $output .= '<div class="post-desc">';
                $output .= '<h3>' . esc_html(get_the_title()) . '</h3>';
                if ($location) {
                    $output .= '<div class="location">' . $location . '</div>';
                }
                $output .= '<button class="readMore">Read More</button>';
                $output .= '</div>';
                $output .= '</div>';
                $output .= '<div class="content-more">';
                $output .= '<div class="img3"><img src="' . $img3 . '" alt=""></div>';
                $output .= '<div class="content-more-dec">';
                $output .= '<div class="post-filed">';
                $output .= '<div><b>Days </b>' . esc_html($days) . '</div>';
                $output .= '<div><b>Man Power </b>' . esc_html($man_power) . '</div>';
                $output .= '<div><b>Satisfied Clients </b>' . esc_html($satisfied_clients) . '</div>';
                $output .= '<div><b>Technology </b>' . esc_html($technology) . '</div>';
                $output .= '</div>';
                $output .= '<div class="post-dec">';
                $output .= esc_html(get_the_content());
                $output .= '</div>';
                $output .= '</div>';
                $output .= '</div>';
                $output .= '</div>';
            }
            


          
            $output .= '</div>';
            $output .= '<button class="load-more-posts" data-category="' . esc_attr($atts['category_id']) . '" data-page="2" data-identifier="' . esc_attr($container_id) . '">Load More</button>'; // Change data-identifier
        
        } else {
            $output .= '<p>No posts found</p>';
        }
        
        wp_reset_postdata();
        
        return $output;

    }
    add_shortcode('posts_by_category', 'display_posts_by_category');


function load_more_posts_script() {
    ?>
    <script>
        jQuery(document).ready(function($) {
            $('.load-more-posts').click(function() {
                var categoryID = $(this).data('category');
                var page = $(this).data('page');
                var identifier = $(this).data('identifier'); // Add this line
                var button = $(this);
                $.ajax({
                    url: '<?php echo admin_url('admin-ajax.php'); ?>',
                    type: 'post',
                    data: {
                        action: 'load_more_posts',
                        category_id: categoryID,
                        page: page,
                    },
                    success: function(response) {
                        $('#' + identifier).append(response); // Append response to the specific category ID div
                        button.data('page', page + 1);
                        // Check if response is empty, if so, hide the button and display 'No more posts' message
                        if (response.trim() === '') {
                            button.hide();
                            $('#' + identifier).append('<p>No more Projects</p>'); // Append message to the specific category ID div
                        }
                    }
                });
            });
        });



    </script>
    <?php
}
add_action('wp_footer', 'load_more_posts_script');


// AJAX handler for Load More functionality
function load_more_posts() {
    $category_id = $_POST['category_id'];
    $page = $_POST['page'];
    $args = array(
        'post_type' => 'projects',
        'posts_per_page' => 3,
        'paged' => $page,
        'tax_query' => array(
            array(
                'taxonomy' => 'projects-category',
                'field' => 'term_id',
                'terms' => $category_id,
            ),
        ),
    );
    $query = new WP_Query($args);
    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();
            $location = get_field('location');
                $img1 = get_field('img1');
                $img2 = get_field('img2');
                $img3 = get_field('img3');
                $days = get_field('days');
                $man_power = get_field('man_power');
                $satisfied_clients = get_field('satisfied_clients');
                $technology = get_field('technology');
            
                echo '<div class="post-list">';
                echo '<div class="post-data">';
                if ($img1) {
                    echo '<div class="post-img"><img src="' . $img1 . '" alt=""></div>';
                }
                if ($img2) {
                    echo '<div class="post-img"><img src="' . $img2 . '" alt=""></div>';
                }
                echo '<div class="post-desc">';
                echo '<h3>' . esc_html(get_the_title()) . '</h3>';
                if ($location) {
                    echo '<div class="location">' . $location . '</div>';
                }
                echo '<button class="readMore">Read More</button>';
                echo '</div>';
                echo '</div>';
                echo '<div class="content-more">';
                echo '<div class="img3"><img src="' . $img3 . '" alt=""></div>';
                echo '<div class="content-more-dec">';
                echo '<div class="post-filed">';
                echo '<div><b>Days </b>' . esc_html($days) . '</div>';
                echo '<div><b>Man Power </b>' . esc_html($man_power) . '</div>';
                echo '<div><b>Satisfied Clients </b>' . esc_html($satisfied_clients) . '</div>';
                echo '<div><b>Technology </b>' . esc_html($technology) . '</div>';
                echo '</div>';
                echo '<div class="post-dec">';
                echo esc_html(get_the_content());
                echo '</div>';
                echo '</div>';
                echo '</div>';
                echo '</div>';
      
        }
    }
    wp_reset_postdata();
    die();
}
add_action('wp_ajax_load_more_posts', 'load_more_posts');
add_action('wp_ajax_nopriv_load_more_posts', 'load_more_posts');

function custom_footer_script() {
    echo '<script>
        document.addEventListener("DOMContentLoaded", function() {
            var readMoreButtons = document.querySelectorAll(".readMore");
            readMoreButtons.forEach(function(button) {
                button.addEventListener("click", function() {
                    var postContent = this.closest(".post-list").querySelector(".content-more");
                    postContent.classList.toggle("active");
                });
            });
        });
    </script>';
}
add_action('wp_footer', 'custom_footer_script');

