<?php
/**
 * The template for displaying device category archive
 *
 * @package Tech_Repair_Service
 */

get_header(); 

// Get current term
$term = get_queried_object();

// Include the device category class if not already loaded
if (!class_exists('Tech_Repair_Device_Category')) {
    require_once plugin_dir_path(dirname(__FILE__)) . 'includes/taxonomies/class-tech-repair-device-category.php';
}

// Get category image
$category_image_url = Tech_Repair_Device_Category::get_category_image_url($term->term_id, 'large');

// Get plugin settings
$options = get_option('tech_repair_settings', array());
$default_category_text = isset($options['default_category_text']) ? $options['default_category_text'] : 'FIXAR';
?>

<div class="container">
    <div class="row">
        <div class="col-md-12">
            <div class="tech-repair-search-container">
                <div class="tech-repair-search-header">
                    <?php if (!empty($category_image_url)) : ?>
                    <div class="tech-repair-category-header-image">
                        <img src="<?php echo esc_url($category_image_url); ?>" alt="<?php echo esc_attr($term->name); ?>" class="tech-repair-category-image">
                    </div>
                    <?php else : ?>
                    
                    <?php endif; ?>
                    <h1 class="tech-repair-search-title"><?php echo esc_html($term->name); ?></h1>
                    <?php if (!empty($term->description)) : ?>
                        <div class="tech-repair-search-description">
                            <?php echo wpautop($term->description); ?>
                        </div>
                    <?php endif; ?>
                </div>
                
                <div class="tech-repair-search-box">
                    <div class="tech-repair-search-info">
                       
                        <div class="tech-repair-search-text">
                            <h3><?php _e('Find Your Device', 'tech-repair-service'); ?></h3>
                            <p><?php _e('Search for devices by name, model, or year', 'tech-repair-service'); ?></p>
                        </div>
                    </div>
                    <form role="search" method="get" class="tech-repair-search-form" action="<?php echo esc_url(home_url('/')); ?>">
                        <input type="hidden" name="post_type" value="tech_repair_device">
                        <?php if (is_tax('tech_repair_device_cat')) : ?>
                            <input type="hidden" name="tech_repair_device_cat" value="<?php echo esc_attr($term->slug); ?>">
                        <?php endif; ?>
                        <div class="tech-repair-search-input-wrap">
                            <input type="search" class="tech-repair-search-input" placeholder="<?php echo esc_attr_x('Search devices...', 'placeholder', 'tech-repair-service'); ?>" value="<?php echo get_search_query(); ?>" name="s" />
                            <button type="submit" class="tech-repair-search-submit"><?php _e('Search', 'tech-repair-service'); ?> <i class="fa fa-search"></i></button>
                        </div>
                        <div class="tech-repair-search-help">
                            <?php 
                            // Get count of devices in this category
                            $device_count = get_queried_object()->count;
                            printf(
                                _n(
                                    'Searching in %d device in this category', 
                                    'Searching in %d devices in this category', 
                                    $device_count, 
                                    'tech-repair-service'
                                ), 
                                $device_count
                            ); 
                            ?>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    
    <div class="row">
        <div class="col-md-8">
            <div class="tech-repair-devices">
                <h2><?php _e('Devices', 'tech-repair-service'); ?></h2>
                
                <?php if (have_posts()) : ?>
                    <div class="tech-repair-devices-grid">
                        <?php while (have_posts()) : the_post(); 
                            $model = get_post_meta(get_the_ID(), '_tech_repair_model', true);
                            $year = get_post_meta(get_the_ID(), '_tech_repair_year', true);
                        ?>
                            <div class="tech-repair-device-card">
                                <?php if (has_post_thumbnail()) : ?>
                                    <div class="tech-repair-device-thumbnail">
                                        <a href="<?php the_permalink(); ?>">
                                            <?php the_post_thumbnail('medium'); ?>
                                        </a>
                                    </div>
                                <?php else : ?>
                                    <div class="tech-repair-device-icon">
                                        <?php echo esc_html($default_category_text); ?>
                                    </div>
                                <?php endif; ?>
                                
                                <h3 class="tech-repair-device-title">
                                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                </h3>
                                
                                <div class="tech-repair-device-meta">
                                    <?php if (!empty($model)) : ?>
                                        <div class="tech-repair-model">
                                            <span class="tech-repair-label"><?php _e('Model:', 'tech-repair-service'); ?></span>
                                            <span class="tech-repair-value"><?php echo esc_html($model); ?></span>
                                        </div>
                                    <?php endif; ?>
                                    
                                    <?php if (!empty($year)) : ?>
                                        <div class="tech-repair-year">
                                            <span class="tech-repair-label"><?php _e('Year:', 'tech-repair-service'); ?></span>
                                            <span class="tech-repair-value"><?php echo esc_html($year); ?></span>
                                        </div>
                                    <?php endif; ?>
                                </div>
                                
                                <div class="tech-repair-device-excerpt">
                                    <?php the_excerpt(); ?>
                                </div>
                                
                                <a href="<?php the_permalink(); ?>" class="tech-repair-device-link"><?php _e('View Device', 'tech-repair-service'); ?></a>
                            </div>
                        <?php endwhile; ?>
                    </div>
                    
                    <?php the_posts_pagination(); ?>
                    
                <?php else : ?>
                    <p><?php _e('No devices found in this category.', 'tech-repair-service'); ?></p>
                <?php endif; ?>
            </div>
            
            <div class="tech-repair-subcategories">
                <?php
                // Get subcategories
                $subcategories = get_terms(array(
                    'taxonomy' => 'tech_repair_device_cat',
                    'parent' => $term->term_id,
                    'hide_empty' => false
                ));
                
                if (!empty($subcategories) && !is_wp_error($subcategories)) :
                ?>
                    <h2><?php _e('Subcategories', 'tech-repair-service'); ?></h2>
                    
                    <div class="tech-repair-subcategories-grid">
                        <?php foreach ($subcategories as $subcategory) : 
                            // Get subcategory image
                            $subcategory_image_url = Tech_Repair_Device_Category::get_category_image_url($subcategory->term_id, 'medium');
                        ?>
                            <div class="tech-repair-subcategory-card">
                                <?php if (!empty($subcategory_image_url)) : ?>
                                <div class="tech-repair-subcategory-image">
                                    <a href="<?php echo esc_url(get_term_link($subcategory)); ?>">
                                        <img src="<?php echo esc_url($subcategory_image_url); ?>" alt="<?php echo esc_attr($subcategory->name); ?>">
                                    </a>
                                </div>
                                <?php else : ?>
                                <div class="tech-repair-subcategory-noimage">
                                    <a href="<?php echo esc_url(get_term_link($subcategory)); ?>">
                                        <?php echo esc_html($default_category_text); ?>
                                    </a>
                                </div>
                                <?php endif; ?>
                                
                                <h3 class="tech-repair-subcategory-title">
                                    <a href="<?php echo esc_url(get_term_link($subcategory)); ?>"><?php echo esc_html($subcategory->name); ?></a>
                                </h3>
                                
                                <?php if (!empty($subcategory->description)) : ?>
                                    <div class="tech-repair-subcategory-description">
                                        <?php echo wpautop($subcategory->description); ?>
                                    </div>
                                <?php endif; ?>
                                
                                <a href="<?php echo esc_url(get_term_link($subcategory)); ?>" class="tech-repair-subcategory-link"><?php _e('View Category', 'tech-repair-service'); ?></a>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </div>
        </div>
        
        <div class="col-md-4 col-sticky">
            <div class="tech-repair-sidebar">
                <?php Tech_Repair_Templates::get_sidebar(); ?>
                
                <div class="tech-repair-categories">
                    <h3><?php _e('Device Categories', 'tech-repair-service'); ?></h3>
                    
                    <?php
                    // Get all categories
                    $categories = get_terms(array(
                        'taxonomy' => 'tech_repair_device_cat',
                        'hide_empty' => false
                    ));
                    
                    if (!empty($categories) && !is_wp_error($categories)) :
                    ?>
                        <ul class="tech-repair-categories-list">
                            <?php foreach ($categories as $category) : ?>
                                <li class="<?php echo ($category->term_id === $term->term_id) ? 'current-cat' : ''; ?>">
                                    <a href="<?php echo esc_url(get_term_link($category)); ?>"><?php echo esc_html($category->name); ?></a>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    <?php endif; ?>
                </div>
                
                <div class="tech-repair-issue-types">
                    <h3><?php _e('Issue Types', 'tech-repair-service'); ?></h3>
                    
                    <?php
                    // Get all issue types
                    $issue_types = get_terms(array(
                        'taxonomy' => 'tech_repair_issue_type',
                        'hide_empty' => false
                    ));
                    
                    if (!empty($issue_types) && !is_wp_error($issue_types)) :
                    ?>
                        <ul class="tech-repair-issue-types-list">
                            <?php foreach ($issue_types as $issue_type) : ?>
                                <li>
                                    <a href="<?php echo esc_url(get_term_link($issue_type)); ?>"><?php echo esc_html($issue_type->name); ?></a>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>

<?php get_footer(); ?>
