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

get_header(); 

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

<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;
                <?php
                // Получаем категории устройств для текущего устройства
                $device_categories = get_the_terms(get_the_ID(), 'tech_repair_device_cat');
                if (!empty($device_categories) && !is_wp_error($device_categories)) {
                    $device_category = reset($device_categories); // Берем первую категорию устройства
                    echo '<a href="' . esc_url(get_term_link($device_category)) . '">' . esc_html($device_category->name) . '</a> &raquo; ';
                }
                ?>
                <span><?php the_title(); ?></span>
            </div>
            
            <h1 class="tech-entry-title"><?php the_title(); ?></h1>
        </div>
    </div>
    
    <div class="row">
        <div class="col-md-8">
            <?php while ( have_posts() ) : 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);
            ?>
                <article id="post-<?php the_ID(); ?>" <?php post_class('tech-repair-single-device'); ?>>
                    <header class="tech-entry-header">
                        <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('Release Year:', 'tech-repair-service'); ?></span>
                                    <span class="tech-repair-value"><?php echo esc_html($year); ?></span>
                                </div>
                            <?php endif; ?>
                            
                            <?php
                            // Display device categories
                            $device_categories = get_the_terms(get_the_ID(), 'tech_repair_device_cat');
                            if (!empty($device_categories) && !is_wp_error($device_categories)) :
                            ?>
                                <div class="tech-repair-device-categories">
                                    <span class="tech-repair-label"><?php _e('Categories:', 'tech-repair-service'); ?></span>
                                    <span class="tech-repair-value">
                                        <?php 
                                        $categories = array();
                                        foreach ($device_categories as $category) {
                                            $categories[] = '<a href="' . esc_url(get_term_link($category)) . '">' . esc_html($category->name) . '</a>';
                                        }
                                        echo implode(', ', $categories);
                                        ?>
                                    </span>
                                </div>
                            <?php endif; ?>
                        </div>
                    </header>
                    
                    <div class="tech-entry-content">
                        <?php the_content(); ?>
                    </div>
                    
                    <div class="tech-repair-device-issues">
                        <h3><?php _e('Common Issues', 'tech-repair-service'); ?></h3>
                        <?php
                        // Get issues compatible with this device
                        $args = array(
                            'post_type' => 'tech_repair_issue',
                            'posts_per_page' => -1,
                            'meta_query' => array(
                                array(
                                    'key' => '_tech_repair_compatible_devices',
                                    'value' => get_the_ID(),
                                    'compare' => 'LIKE'
                                )
                            )
                        );
                        
                        $issues = new WP_Query($args);
                        
                        if ($issues->have_posts()) :
                        ?>
                            <div class="tech-repair-issues-grid">
                                <?php while ($issues->have_posts()) : $issues->the_post(); 
                                    $difficulty = get_post_meta(get_the_ID(), '_tech_repair_difficulty', true);
                                    $repair_time = get_post_meta(get_the_ID(), '_tech_repair_time', true);
                                    $pricing_options = get_post_meta(get_the_ID(), '_tech_repair_pricing_options', true);
                                    
                                    if (!is_array($pricing_options) || empty($pricing_options)) {
                                        $pricing_options = array();
                                        $min_price = 0;
                                    } else {
                                        $prices = array_column($pricing_options, 'price');
                                        $min_price = !empty($prices) ? min($prices) : 0;
                                    }
                                ?>
                                    <div class="tech-repair-issue-card">
                                        <h4 class="tech-repair-issue-title">
                                            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                        </h4>
                                        
                                        <?php if (!empty($difficulty)) : ?>
                                            <div class="tech-repair-difficulty">
                                                <?php 
                                                // Convert string difficulty to numeric value
                                                $difficulty_map = array(
                                                    'easy' => 1,
                                                    'medium' => 2,
                                                    'hard' => 3,
                                                    'expert' => 5
                                                );
                                                
                                                $difficulty_level = isset($difficulty_map[$difficulty]) ? $difficulty_map[$difficulty] : 0;
                                                
                                                // Display difficulty as wrench icons
                                                echo '<span class="tech-repair-difficulty-icons">';
                                                for ($i = 1; $i <= 5; $i++) {
                                                    if ($i <= $difficulty_level) {
                                                        echo '<i class="fa fa-wrench tech-repair-difficulty-active"></i>';
                                                    } else {
                                                        echo '<i class="fa fa-wrench tech-repair-difficulty-inactive"></i>';
                                                    }
                                                }
                                                echo '</span>';
                                                
                                                // Display text label
                                                echo ' <span class="tech-repair-difficulty-text">';
                                                switch ($difficulty) {
                                                    case 'easy':
                                                        _e('Easy', 'tech-repair-service');
                                                        break;
                                                    case 'medium':
                                                        _e('Medium', 'tech-repair-service');
                                                        break;
                                                    case 'hard':
                                                        _e('Hard', 'tech-repair-service');
                                                        break;
                                                    case 'expert':
                                                        _e('Expert Only', 'tech-repair-service');
                                                        break;
                                                    default:
                                                        _e('Unknown', 'tech-repair-service');
                                                }
                                                echo '</span>';
                                                ?>
                                            </div>
                                        <?php endif; ?>
                                        
                                        <div class="tech-repair-issue-excerpt">
                                            <?php the_excerpt(); ?>
                                        </div>
                                        
                                        <div class="tech-repair-issue-meta">
                                            <?php if (!empty($repair_time)) : ?>
                                                <div class="tech-repair-time">
                                                    <i class="fa fa-clock-o"></i>
                                                    <?php echo esc_html($repair_time); ?> 
                                                    <?php echo ($repair_time == 1) ? __('hour', 'tech-repair-service') : __('hours', 'tech-repair-service'); ?>
                                                </div>
                                            <?php endif; ?>
                                            
                                            <?php if ($min_price > 0) : ?>
                                                <div class="tech-repair-price">
                                                    <i class="fa fa-tag"></i>
                                                    <?php 
                                                    $currency_symbol = get_woocommerce_currency_symbol();
                                                    echo esc_html(sprintf(__('From %s%s', 'tech-repair-service'), $currency_symbol, $min_price)); 
                                                    ?>
                                                </div>
                                            <?php endif; ?>
                                        </div>
                                        
                                        <a href="<?php the_permalink(); ?>" class="tech-repair-issue-link"><?php _e('View Details', 'tech-repair-service'); ?></a>
                                    </div>
                                <?php endwhile; ?>
                            </div>
                        <?php 
                        else :
                            echo '<p>' . __('No repair issues found for this device.', 'tech-repair-service') . '</p>';
                        endif;
                        
                        wp_reset_postdata();
                        ?>
                    </div>
                    
                    <?php
                    // If comments are enabled in settings and open or we have at least one comment, load up the comment template.
                    if ($enable_comments && (comments_open() || get_comments_number())) :
                        comments_template();
                    endif;
                    ?>
                </article>
            <?php endwhile; ?>
        </div>
        
        <div class="col-md-4 col-sticky">
            <div class="tech-repair-sidebar">
                <?php Tech_Repair_Templates::get_sidebar(); ?>
                
                <div class="tech-repair-related-devices">
                    <h3><?php _e('Related Devices', 'tech-repair-service'); ?></h3>
                    <?php
                    // Get related devices based on device category
                    $device_categories = get_the_terms(get_the_ID(), 'tech_repair_device_cat');
                    $category_ids = array();
                    
                    if (!empty($device_categories) && !is_wp_error($device_categories)) {
                        foreach ($device_categories as $category) {
                            $category_ids[] = $category->term_id;
                        }
                    }
                    
                    $related_args = array(
                        'post_type' => 'tech_repair_device',
                        'posts_per_page' => 5,
                        'post__not_in' => array(get_the_ID()),
                        'tax_query' => array(
                            array(
                                'taxonomy' => 'tech_repair_device_cat',
                                'field' => 'term_id',
                                'terms' => $category_ids
                            )
                        )
                    );
                    
                    $related_devices = new WP_Query($related_args);
                    
                    if ($related_devices->have_posts()) :
                    ?>
                        <ul class="tech-repair-related-list">
                            <?php while ($related_devices->have_posts()) : $related_devices->the_post(); ?>
                                <li>
                                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                </li>
                            <?php endwhile; ?>
                        </ul>
                    <?php 
                    else :
                        echo '<p>' . __('No related devices found.', 'tech-repair-service') . '</p>';
                    endif;
                    
                    wp_reset_postdata();
                    ?>
                </div>
            </div>
        </div>
    </div>
</div>

<?php get_footer(); ?>
