<?php
/**
 * Template Name: 首页模板 (锋利直角版)
 * 文件: home-cases.php
 * 描述: 全新设计的首页，完全适配直角/强烈色彩主题
 */

// ========== 1. Banner 数据获取 ==========
$banner_title = get_post_meta(get_the_ID(), 'banner_title', true);
$banner_subtitle = get_post_meta(get_the_ID(), 'banner_subtitle', true);
$banner_btn_text = get_post_meta(get_the_ID(), 'banner_btn_text', true);
$banner_btn_url = get_post_meta(get_the_ID(), 'banner_btn_url', true);

if(empty($banner_title)) $banner_title = 'Cell and <span>Cell Line</span> Products';
if(empty($banner_subtitle)) $banner_subtitle = 'A Wide Range of Cell, Cell Line, Media, and Reagents From USD $750.00';
if(empty($banner_btn_text)) $banner_btn_text = 'Shop Products';
if(empty($banner_btn_url)) $banner_btn_url = home_url('/products');

// ========== 2. 获取案例文章 ==========
$cases_args = array(
    'post_type'      => 'post',
    'posts_per_page' => 4,
    'post_status'    => 'publish',
    'orderby'        => 'date',
    'order'          => 'DESC'
);
$case_category = get_category_by_slug('case-study');
if ($case_category) {
    $cases_args['cat'] = $case_category->term_id;
}
$cases_query = new WP_Query($cases_args);

// ========== 3. 获取产品分类 ==========
$product_categories = get_terms(array(
    'taxonomy' => 'product_cat',
    'hide_empty' => false,
    'parent' => 0,
    'exclude' => array(15, 16),
    'number' => 5
));
?>

<!-- ========== Section 1: Home Banner ========== -->
<section class="home-banner">
    <div class="container">
        <div class="home-banner__content">
            <h1 class="home-banner__title"><?php echo $banner_title; ?></h1>
            <p class="home-banner__subtitle"><?php echo esc_html($banner_subtitle); ?></p>
            <a href="<?php echo esc_url($banner_btn_url); ?>" class="btn btn-primary"><?php echo esc_html($banner_btn_text); ?> →</a>
        </div>
    </div>
</section>

<!-- ========== Section 2: Home Support (三大服务卡片) ========== -->
<section class="home-support">
    <div class="container">
        <div class="card-grid">
            <div class="support-card">
                <h3>Special Offers</h3>
                <p>Advanced Solutions for Academic Excellence. Get your special offer today to empower your research.</p>
                <a href="<?php echo home_url('/special-offers/'); ?>" class="btn-circle">Get Offers →</a>
            </div>
            <div class="support-card">
                <h3>Research Area</h3>
                <p>Discover a research area that you are interested in to see the applications of our cells.</p>
                <a href="<?php echo home_url('/applications/'); ?>" class="btn-circle">Learn More →</a>
            </div>
            <div class="support-card">
                <h3>Support</h3>
                <p>Get technical resources, practical tips, and guidance on how to place orders to help with your research.</p>
                <a href="<?php echo home_url('/support/'); ?>" class="btn-circle">Get Support →</a>
            </div>
        </div>
    </div>
</section>

<!-- ========== Section 3: Home Products (产品分类 + 服务卡片) ========== -->
<section class="home-products">
    <div class="container">
        <div class="section-header">
            <h2>Products & Services For All Your Research Needs</h2>
        </div>
        
        <div class="cat-grid">
            <?php 
            if(!empty($product_categories) && !is_wp_error($product_categories)) :
                foreach($product_categories as $cat) : 
                    if($cat->name == 'Uncategorized' || $cat->slug == 'uncategorized') continue;
                    
                    $cat_slug = $cat->slug;
                    $svg_icon = '';
                    
                    if(strpos($cat_slug, 'primary') !== false) {
                        $svg_icon = '<svg viewBox="0 0 24 24" fill="none"><circle cx="12" cy="12" r="10" stroke="currentColor" stroke-width="1.5"/><path d="M12 8v8M8 12h8" stroke="currentColor" stroke-width="1.5"/></svg>';
                    } elseif(strpos($cat_slug, 'cancer') !== false || strpos($cat_slug, 'tumor') !== false) {
                        $svg_icon = '<svg viewBox="0 0 24 24" fill="none"><rect x="4" y="4" width="16" height="16" rx="2" stroke="currentColor" stroke-width="1.5"/><path d="M8 12h8M12 8v8" stroke="currentColor" stroke-width="1.5"/></svg>';
                    } elseif(strpos($cat_slug, 'immortal') !== false) {
                        $svg_icon = '<svg viewBox="0 0 24 24" fill="none"><polygon points="12,4 20,9 20,15 12,20 4,15 4,9" stroke="currentColor" stroke-width="1.5"/><circle cx="12" cy="12" r="3" stroke="currentColor" stroke-width="1.5"/></svg>';
                    } elseif(strpos($cat_slug, 'genome') !== false || strpos($cat_slug, 'edited') !== false) {
                        $svg_icon = '<svg viewBox="0 0 24 24" fill="none"><path d="M12 2L2 7l10 5 10-5-10-5zM2 17l10 5 10-5M2 12l10 5 10-5" stroke="currentColor" stroke-width="1.5"/><path d="M12 12v5" stroke="currentColor" stroke-width="1.5"/></svg>';
                    } elseif(strpos($cat_slug, 'media') !== false || strpos($cat_slug, 'reagent') !== false) {
                        $svg_icon = '<svg viewBox="0 0 24 24" fill="none"><path d="M4 4h16v16H4z" stroke="currentColor" stroke-width="1.5"/><path d="M8 8h8M8 12h6M8 16h4" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/></svg>';
                    } else {
                        $svg_icon = '<svg viewBox="0 0 24 24" fill="none"><circle cx="12" cy="12" r="10" stroke="currentColor" stroke-width="1.5"/><path d="M12 8v8M8 12h8" stroke="currentColor" stroke-width="1.5"/></svg>';
                    }
                    ?>
                    <a href="<?php echo esc_url(get_term_link($cat)); ?>" class="cat-item">
                        <div class="cat-icon"><?php echo $svg_icon; ?></div>
                        <strong><?php echo esc_html($cat->name); ?></strong>
                    </a>
                <?php endforeach;
            endif; ?>
        </div>
        
        <div class="services-grid">
            <div class="service-card">
                <h3>Cell Line Development</h3>
                <p>Fast, efficient creation of cell lines as your expectation, gene modification or reporter gene system, using CRISPR-Cas9, Tet-On/Off, or any tools you need for your research.</p>
                <a href="<?php echo home_url('/services/cell-line-development/'); ?>" class="btn-circle">Learn more →</a>
            </div>
            <div class="service-card">
                <h3>Cell Immortalization</h3>
                <p>Extend lifespan of primary cells through immortalization for a stable, repeatable, and reliable experiment data.</p>
                <a href="<?php echo home_url('/services/immortalization/'); ?>" class="btn-circle">Learn more →</a>
            </div>
            <div class="service-card">
                <h3>Cell Authentication</h3>
                <p>Ensure the reliability of your research data with cell authentication services. Using multiplex PCR and STR analysis, we deliver fast and accurate results you can trust.</p>
                <a href="<?php echo home_url('/services/cell-authentication/'); ?>" class="btn-circle">Learn more →</a>
            </div>
        </div>
    </div>
</section>

<!-- ========== Section 4: Home Cases (Featured Cases) ========== -->
<?php if ($cases_query->have_posts()) : ?>
<section class="home-cases">
    <div class="container">
        <div class="cases-flex">
            <div class="cases-header">
                <h2>Featured Cases from Our Researchers</h2>
                <a href="<?php echo esc_url(home_url('/resources')); ?>" class="btn btn-outline">Explore All →</a>
            </div>
            <ul class="case-list">
                <?php while ($cases_query->have_posts()) : $cases_query->the_post(); ?>
                    <li>
                        <a href="<?php echo esc_url(get_permalink()); ?>" class="case-link">
                            <span class="case-title"><?php echo esc_html(get_the_title()); ?></span>
                            <span class="case-arrow">→</span>
                        </a>
                    </li>
                <?php endwhile; wp_reset_postdata(); ?>
            </ul>
        </div>
    </div>
</section>
<?php endif; ?>

<!-- ========== Section 5: Home Solutions ========== -->
<section class="home-solutions">
    <div class="container">
        <div class="solution-row">
            <div class="solution-content">
                <h2>Driving Discovery with <br> Advanced Cell Solutions</h2>
                <p>Despite remarkable research advances, major health challenges such as cancer, autoimmune disorders, and genetic conditions remain.</p>
                <p>At Ascent Research, we are committed to driving medical progress through pioneering cellular innovations. Our quality guaranteed, precise and sustainable cellular solutions are transforming biotechnology and empowering scientists to create a healthier future.</p>
                <a href="<?php echo home_url('/about/'); ?>" class="btn btn-primary" style="margin-top: 16px;">More About Us →</a>
            </div>
            <div class="csr-card">
                <span>CSR</span>
                <p>Committed to Ethical, Sustainable and Socially Responsible Cell Services</p>
            </div>
        </div>
        
        <div class="banners-grid">
            <div class="banner-card">
                <span>Sustainability</span>
                <strong>Sustainable Science for a Healthier Future</strong>
                <p>Discover our green initiatives and eco-conscious production.</p>
            </div>
            <div class="banner-card">
                <span>DISTRIBUTOR</span>
                <strong>Global Market Supply and Collaboration</strong>
                <p>Join our worldwide network of partners and distributors.</p>
            </div>
        </div>
    </div>
</section>