<?php
/**
 * Template Name: Giao diện Sản phẩm (Catalog)
 * Template Post Type: page
 *
 * @package Theme_An_Phat
 */

get_header(); ?>

<div class="page-hero text-center py-5">
    <div class="container">
        <h1 class="page-title font-heading fw-bold text-white mb-2"><?php the_title(); ?></h1>
        <div class="accent-line mx-auto bg-accent"></div>
        <div class="breadcrumbs mt-3 text-sm text-white-70">
            <a href="<?php echo esc_url( home_url( '/' ) ); ?>" class="text-white-70 hover-white text-decoration-none"><?php esc_html_e( 'Trang chủ', 'theme-anphat' ); ?></a>
            <span class="mx-2">/</span>
            <span class="text-white"><?php esc_html_e( 'Sản phẩm', 'theme-anphat' ); ?></span>
        </div>
    </div>
</div>

<main id="primary" class="site-main py-5 bg-light text-dark">
    <div class="container">
        <!-- Search & Filter Controls -->
        <div class="row align-items-center mb-5 g-3 scroll-reveal">
            <!-- Category Filters -->
            <div class="col-lg-8">
                <div class="d-flex flex-wrap gap-2">
                    <button class="btn btn-primary product-filter-btn px-4 rounded-pill shadow-sm" data-category=""><?php esc_html_e( 'Tất cả', 'theme-anphat' ); ?></button>
                    <?php
                    $categories = get_terms( array(
                        'taxonomy'   => 'product_cat',
                        'hide_empty' => false,
                    ) );

                    if ( ! is_wp_error( $categories ) && ! empty( $categories ) ) {
                        foreach ( $categories as $cat ) {
                            echo '<button class="btn btn-outline-primary product-filter-btn px-4 rounded-pill shadow-sm" data-category="' . esc_attr( $cat->slug ) . '">' . esc_html( $cat->name ) . '</button>';
                        }
                    }
                    ?>
                </div>
            </div>
            <!-- Search Box -->
            <div class="col-lg-4">
                <div class="position-relative">
                    <input type="text" id="product-search" class="form-control rounded-pill ps-4 pe-5 shadow-sm border" placeholder="<?php esc_attr_e( 'Tìm kiếm sản phẩm...', 'theme-anphat' ); ?>">
                    <span class="position-absolute end-0 top-50 translate-middle-y me-4 text-muted"><i class="fa-solid fa-magnifying-glass"></i></span>
                </div>
            </div>
        </div>

        <!-- Products Listing Grid -->
        <div class="row g-4 scroll-reveal-right" id="product-archive-grid">
            <?php
            $args = array(
                'post_type'      => 'product',
                'post_status'    => 'publish',
                'posts_per_page' => -1,
                'orderby'        => 'title',
                'order'          => 'ASC'
            );
            $query = new WP_Query( $args );

            if ( $query->have_posts() ) :
                while ( $query->have_posts() ) : $query->the_post();
                    theme_anphat_render_woocommerce_product_card();
                    continue;
                    $terms = get_the_terms( get_the_ID(), 'danh_muc_san_pham' );
                    $term_name = ( $terms && ! is_wp_error( $terms ) ) ? $terms[0]->name : __( 'Dược Phẩm', 'theme-anphat' );
                    ?>
                    <div class="col-lg-3 col-md-6 d-flex">
                        <div class="product-card bg-white border border-light rounded-4 shadow-sm p-3 w-100 d-flex flex-column hover-shadow">
                            <div class="product-image-box position-relative rounded-3 bg-light p-3 mb-3 d-flex align-items-center justify-content-center" style="height: 200px;">
                                <?php if ( has_post_thumbnail() ) : ?>
                                    <?php the_post_thumbnail( 'medium', array( 'class' => 'img-fluid object-contain max-h-100' ) ); ?>
                                <?php else : ?>
                                    <span class="fs-1 text-primary-light"><i class="fa-solid fa-capsules text-primary"></i></span>
                                <?php endif; ?>
                                <span class="badge bg-primary text-white position-absolute top-2 start-2 text-xs"><?php echo esc_html( $term_name ); ?></span>
                            </div>
                            <h3 class="product-card-title font-heading fw-bold text-dark fs-6 mb-2 line-clamp-2"><?php the_title(); ?></h3>
                            <p class="product-card-desc text-muted text-xs mb-3 line-clamp-3"><?php echo wp_strip_all_tags( get_the_excerpt() ); ?></p>
                            <div class="mt-auto d-flex justify-content-between align-items-center pt-2 border-top">
                                <span class="text-xs text-muted"><i class="fa-solid fa-circle-check text-success me-1"></i><?php esc_html_e( 'Đạt chuẩn GSP', 'theme-anphat' ); ?></span>
                                <a href="<?php the_permalink(); ?>" class="btn btn-outline-primary btn-sm px-3 rounded-pill"><i class="fa-solid fa-magnifying-glass me-1"></i><?php esc_html_e( 'Xem Chi Tiết', 'theme-anphat' ); ?></a>
                            </div>
                        </div>
                    </div>
                <?php
                endwhile;
                wp_reset_postdata();
            else :
                ?>
                <!-- Empty State -->
                <div class="col-12 text-center py-5 text-muted">
                    <span class="fs-1"><i class="fa-solid fa-prescription-bottle"></i></span>
                    <h3 class="font-heading text-dark mt-3"><?php esc_html_e( 'Đang cập nhật danh mục sản phẩm', 'theme-anphat' ); ?></h3>
                    <p class="text-muted text-sm"><?php esc_html_e( 'Vui lòng quay lại sau để cập nhật danh mục sản phẩm chính thức từ An Phát Pharma.', 'theme-anphat' ); ?></p>
                </div>
            <?php endif; ?>
        </div>
    </div>
</main>

<?php get_footer(); ?>
