/******* Do not edit this file *******
Simple Custom CSS and JS - by Silkypress.com
Saved: Dec 29 2025 | 10:28:30 */
/*

<?php
/**
 * Hook để nạp file style.css của theme
 */
function my_theme_enqueue_styles() {
    // Nạp file style.css của theme hiện tại
    wp_enqueue_style( 'my-theme-style', get_stylesheet_uri(), array(), wp_get_theme()->get('Version') );
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );











function custom_breadcrumb_shortcode() {
    // Không hiển thị trên trang chủ
    if (is_home() || is_front_page()) {
        return;
    }

    $breadcrumb = '<nav class="custom-breadcrumb">';
    $breadcrumb .= '<a href="' . home_url() . '">Trang chủ</a>';

    if (is_category() || is_single()) {
        $breadcrumb .= ' <span class="sep">/</span> ';
        if (is_single()) {
            $categories = get_the_category();
            if (!empty($categories)) {
                $breadcrumb .= '<a href="' . esc_url(get_category_link($categories[0]->term_id)) . '">' . esc_html($categories[0]->name) . '</a>';
            }
            $breadcrumb .= ' <span class="sep">/</span> ' . get_the_title();
        } else {
            $breadcrumb .= single_cat_title('', false);
        }
    } elseif (is_page()) {
        $breadcrumb .= ' <span class="sep">/</span> ' . get_the_title();
    } elseif (is_search()) {
        $breadcrumb .= ' <span class="sep">/</span> Kết quả tìm kiếm cho: ' . get_search_query();
    }

    $breadcrumb .= '</nav>';

    return $breadcrumb;
}
add_shortcode('breadcrumb', 'custom_breadcrumb_shortcode');










// 1. Shortcode hiển thị danh mục (Taxonomy)
function shortcode_cac_loai_du_an() {
    $terms = get_terms( array(
        'taxonomy'   => 'cac-loai-du-an',
        'hide_empty' => false,
    ) );

    if ( is_wp_error( $terms ) ) return 'Lỗi danh mục.';

    $output = '<ul class="list-taxonomy-du-an">';
    // Mục "Tất cả" - Để slug trống để lấy hết bài
    $output .= '<li class="item-all-projects active"><a href="#" data-filter="">Tất cả dự án</a></li>';

    if ( ! empty( $terms ) ) {
        foreach ( $terms as $term ) {
            $output .= '<li>';
            $output .= '<a href="#" data-filter="' . esc_attr( $term->slug ) . '">' . esc_html( $term->name ) . '</a>';
            $output .= '</li>';
        }
    }
    $output .= '</ul>';
    return $output;
}
add_shortcode( 'taxonomy__du_an', 'shortcode_cac_loai_du_an' );

function filter_projects_ajax_handler() {
    $slug = $_POST['category'];
    $args = array(
        'post_type' => 'du-an',
        'posts_per_page' => -1,
        // ... các tham số khác giữ nguyên ...
    );

    if ( !empty($slug) ) {
        $args['tax_query'] = array(array('taxonomy' => 'cac-loai-du-an','field' => 'slug','terms' => $slug));
    }

    $query = new WP_Query( $args );
    
    if ( $query->have_posts() ) {
        $i = 0;
        while ( $query->have_posts() ) {
            $query->the_post();
            echo render_project_item_html($i);
            $i++;
        }
        wp_reset_postdata();
    } else {
        echo '<div class="swiper-slide">Không tìm thấy dự án nào.</div>';
    }
    die();
}
add_action('wp_ajax_filter_projects', 'filter_projects_ajax_handler');
add_action('wp_ajax_nopriv_filter_projects', 'filter_projects_ajax_handler');






function shortcode_post_type_du_an() {
    $args = array(
        'post_type'      => 'du-an',
        'posts_per_page' => -1,
        'post_status'    => 'publish',
    );

    $query = new WP_Query( $args );
    $output = '';

    if ( $query->have_posts() ) {
        $output .= '<div class="swiper mySwiperProject"><div class="swiper-wrapper">';
        
        $i = 0; // Biến đếm số thứ tự
        while ( $query->have_posts() ) {
            $query->the_post();
            $output .= render_project_item_html($i);
            $i++;
        }
        
        $output .= '</div>';
        $output .= '<div class="swiper-pagination"></div>';
        $output .= '</div>';
        wp_reset_postdata();
    }
    return $output;
}
add_shortcode( 'post_type__du_an', 'shortcode_post_type_du_an' );








/**
 * Hàm render item dự án dùng chung
 */
function render_project_item_html($index = null) {
    $thumbnail = get_the_post_thumbnail_url(get_the_ID(), 'full');
    $title = get_the_title();
    $permalink = get_permalink();
    
    // 1. Lấy số thứ tự (định dạng 01, 02...)
    $number_html = '';
    if ($index !== null) {
        $formatted_number = str_pad($index + 1, 2, '0', STR_PAD_LEFT);
        $number_html = '<span class="project-number">' . $formatted_number . '.</span>';
    }

    // 2. Lấy đoạn trích (Expect/Excerpt)
    // Nếu bài viết có Excerpt riêng thì lấy, không thì cắt 20 từ từ Content
    $excerpt = has_excerpt() ? get_the_excerpt() : wp_trim_words(get_the_content(), 20, '...');

    $html = '<div class="swiper-slide">';
    $html .= '    <div class="project-item">';
    $html .= '        <div class="project-image">';
    $html .= '            <img src="' . esc_url($thumbnail) . '" alt="' . esc_attr($title) . '">';
    $html .= '        </div>';
    $html .= '        <div class="project-info">';
    $html .=              $number_html;
    $html .= '            <h3 class="project-title">' . esc_html($title) . '</h3>';
    $html .= '            <div class="project-excerpt">' . esc_html($excerpt) . '</div>';
    $html .= '            <a href="' . esc_url($permalink) . '" class="project-button">Chi tiết dự án</a>';
    $html .= '        </div>';
    $html .= '    </div>';
    $html .= '</div>';
    
    return $html;
}







add_action('wp_footer', 'my_custom_project_filter_script');
function my_custom_project_filter_script() {
    ?>
    <script>
    document.addEventListener("DOMContentLoaded", function () {
        const SPACE_BETWEEN = 20;
        let swiper;

        // Hàm xác định chiều rộng slide dựa trên Breakpoint
        function getStepSize() {
            const width = window.innerWidth;
            let slideWidth = 316; // Mặc định (Desktop)

            if (width < 768) {
                slideWidth = 200; // Mobile
            } else if (width < 1024) {
                slideWidth = 260; // Tablet
            }

            return slideWidth + SPACE_BETWEEN;
        }

        function initMySwiper() {
            if (document.querySelector(".mySwiperProject")) {
                swiper = new Swiper(".mySwiperProject", {
                    slidesPerView: "auto",
                    spaceBetween: SPACE_BETWEEN,
                    loop: false,
                    autoplay: {
                        delay: 3000,
                    },
                    speed: 500,
                    on: {
                        transitionStart(swiper) {
                            const index = swiper.activeIndex;
                            const STEP = getStepSize(); // Lấy giá trị STEP động tại thời điểm chạy
                            let newTranslate = -(index * STEP);
                            swiper.setTranslate(newTranslate);
                        }
                    }
                });
            }
        }

        initMySwiper();

        // Xử lý Click lọc Taxonomy (Giữ nguyên logic của bạn)
        const filterLinks = document.querySelectorAll('.list-taxonomy-du-an a');
        filterLinks.forEach(link => {
            link.addEventListener('click', function(e) {
                e.preventDefault();
                filterLinks.forEach(l => l.parentElement.classList.remove('active'));
                this.parentElement.classList.add('active');

                const category = this.getAttribute('data-filter');

                jQuery.ajax({
                    url: '<?php echo admin_url('admin-ajax.php'); ?>', 
                    type: 'POST',
                    data: {
                        action: 'filter_projects',
                        category: category
                    },
                    beforeSend: function() {
                        const wrapper = document.querySelector('.swiper-wrapper');
                        if(wrapper) wrapper.style.opacity = '0.5';
                    },
                    success: function(response) {
                        if(swiper) swiper.destroy(true, true);
                        const wrapper = document.querySelector('.swiper-wrapper');
                        if(wrapper) {
                            wrapper.innerHTML = response;
                            wrapper.style.opacity = '1';
                        }
                        initMySwiper();
                    }
                });
            });
        });
        
        // Cập nhật lại swiper khi resize màn hình để đảm bảo STEP luôn đúng
        window.addEventListener('resize', function() {
            if(swiper) {
                swiper.update();
            }
        });
    });
    </script>
    <?php
}
*/