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

get_header();

// Get all device categories
$device_categories = get_terms(array(
    'taxonomy' => 'tech_repair_device_cat',
    'hide_empty' => false,
));

// 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 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-breadcrumbs">
                <a href="<?php echo esc_url(home_url('/')); ?>"><?php _e('Home', 'tech-repair-service'); ?></a> &raquo;
                <span><?php _e('Repair Devices', 'tech-repair-service'); ?></span>
            </div>
            
            <h1 class="tech-entry-title"><?php _e('Repair Devices', 'tech-repair-service'); ?></h1>
            
            <div class="tech-repair-search-container">
                <div class="tech-repair-search-header">
                    <div class="tech-repair-search-description">
                        <p><?php _e('Browse all device categories or search for specific devices', 'tech-repair-service'); ?></p>
                    </div>
                </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, category, or model', '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">
                        <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 all devices
                            $device_count = wp_count_posts('tech_repair_device')->publish;
                            printf(
                                _n(
                                    'Searching in %d repair device', 
                                    'Searching in %d repair devices', 
                                    $device_count, 
                                    'tech-repair-service'
                                ), 
                                $device_count
                            ); 
                            ?>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    
    <!-- Device Categories Section -->
    <div class="row">
        <div class="col-md-12">
            <h2 class="tech-repair-section-title"><?php _e('Device Categories', 'tech-repair-service'); ?></h2>
            
            <?php if (!empty($device_categories)) : ?>
                <div class="tech-repair-device-categories-grid">
                    <?php foreach ($device_categories as $device_category) : 
                        // Get count of devices in this category
                        $device_count = $device_category->count;
                        
                        // Get category image using our new function
                        $image_url = Tech_Repair_Device_Category::get_category_image_url($device_category->term_id, 'medium');
                    ?>
                        <div class="tech-repair-device-category-card">
                            <?php if (!empty($image_url)) : ?>
                                <div class="tech-repair-device-category-image">
                                    <img src="<?php echo esc_url($image_url); ?>" alt="<?php echo esc_attr($device_category->name); ?>">
                                </div>
                            <?php else : ?>
                                <div class="tech-repair-device-category-icon">
                                    <?php echo esc_html($default_category_text); ?>
                                </div>
                            <?php endif; ?>
                            
                            <div class="tech-repair-device-category-content">
                                <h3 class="tech-repair-device-category-title">
                                    <a href="<?php echo esc_url(get_term_link($device_category)); ?>"><?php echo esc_html($device_category->name); ?></a>
                                </h3>
                                
                                <?php if (!empty($device_category->description)) : ?>
                                    <div class="tech-repair-device-category-description">
                                        <?php echo wp_kses_post($device_category->description); ?>
                                    </div>
                                <?php endif; ?>
                                
                                <div class="tech-repair-device-category-meta">
                                    <span class="tech-repair-device-category-count">
                                        <?php 
                                        printf(
                                            _n(
                                                '%d device', 
                                                '%d devices', 
                                                $device_count, 
                                                'tech-repair-service'
                                            ), 
                                            $device_count
                                        ); 
                                        ?>
                                    </span>
                                    
                                    <a href="<?php echo esc_url(get_term_link($device_category)); ?>" class="tech-repair-device-category-link">
                                        <?php _e('View Devices', 'tech-repair-service'); ?> 
                                    </a>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>
    </div>
    
    <!-- Popular Devices Section -->
    <div class="row">
        <div class="col-md-12">
            <h2 class="tech-repair-section-title"><?php _e('Popular Devices', 'tech-repair-service'); ?></h2>
            
            <?php
            // Get popular devices
            $popular_devices = new WP_Query(array(
                'post_type' => 'tech_repair_device',
                'posts_per_page' => 8,
                'orderby' => 'date',
                'order' => 'DESC'
            ));
            
            if ($popular_devices->have_posts()) : ?>
                <div class="tech-repair-devices-grid">
                    <?php while ($popular_devices->have_posts()) : $popular_devices->the_post(); 
                        // Get device details
                        $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-image">
                                    <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; ?>
                            
                            <div class="tech-repair-device-content">
                                <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-device-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-device-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 Repair Options', 'tech-repair-service'); ?> 
                                </a>
                            </div>
                        </div>
                    <?php endwhile; ?>
                </div>
                
                <?php wp_reset_postdata(); ?>
            <?php else : ?>
                <p><?php _e('No devices found.', 'tech-repair-service'); ?></p>
            <?php endif; ?>
        </div>
    </div>
</div>

<?php get_footer(); ?>
