<?php if (!defined('ABSPATH')) exit;

$mtcam_stolen_css = '.mtcam-stolen-widget{font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif}'
    . '.mtcam-stolen-camera{background:#fff;border:2px solid #d32f2f;border-radius:12px;padding:20px;margin:15px 0;box-shadow:0 4px 12px rgba(211,47,47,0.15)}'
    . '.mtcam-stolen-header{display:flex;justify-content:space-between;align-items:center;gap:15px}'
    . '.mtcam-stolen-header-left{display:flex;align-items:center;gap:15px;flex:1}'
    . '.mtcam-stolen-badge{background:#d32f2f;color:#fff;padding:10px 20px;border-radius:8px;font-weight:bold;font-size:16px;white-space:nowrap}'
    . '.mtcam-stolen-title{flex:1}'
    . '.mtcam-stolen-title h3{margin:0 0 5px 0;color:#333;font-size:18px}'
    . '.mtcam-stolen-title p{margin:0;color:#666;font-size:14px}'
    . '.mtcam-stolen-alert{background:#ffebee;color:#c62828;padding:8px 15px;border-radius:6px;font-size:14px;font-weight:600;white-space:nowrap}'
    . '.mtcam-detail-toggle{background:#2196F3;color:#fff;border:none;padding:10px 20px;border-radius:6px;cursor:pointer;font-size:14px;font-weight:600;transition:all 0.2s;white-space:nowrap}'
    . '.mtcam-detail-toggle:hover{background:#1976D2;transform:translateY(-2px)}'
    . '.mtcam-detail-toggle.active{background:#666}'
    . '.mtcam-stolen-details{display:none;margin-top:20px;padding-top:20px;border-top:2px solid #eee}'
    . '.mtcam-stolen-details.active{display:block}'
    . '.mtcam-stolen-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(250px,1fr));gap:20px;margin:20px 0}'
    . '.mtcam-stolen-info-box{background:#f5f5f5;padding:15px;border-radius:8px;border-left:4px solid var(--cam-color,#666)}'
    . '.mtcam-stolen-info-box h4{margin:0 0 10px 0;color:var(--cam-color,#666);font-size:14px;text-transform:uppercase;letter-spacing:0.5px}'
    . '.mtcam-stolen-info-item{margin:8px 0;font-size:14px}'
    . '.mtcam-stolen-info-item strong{display:inline-block;width:120px;color:#666}'
    . '.mtcam-stolen-map{margin:15px 0;text-align:center}'
    . '.mtcam-stolen-map a{display:inline-block;background:#2196F3;color:#fff;padding:12px 24px;border-radius:6px;text-decoration:none;font-weight:600;transition:all 0.3s}'
    . '.mtcam-stolen-map a:hover{background:#1976D2;transform:translateY(-2px);box-shadow:0 4px 8px rgba(33,150,243,0.3)}'
    . '.mtcam-stolen-badges{display:flex;flex-wrap:wrap;gap:8px;margin-top:10px}'
    . '.mtcam-badge-item{background:#e3f2fd;color:#1976d2;padding:6px 12px;border-radius:4px;font-size:12px;font-weight:600}'
    . '.mtcam-stolen-photo{margin:20px 0;text-align:center}'
    . '.mtcam-stolen-photo img{max-width:100%;height:auto;border-radius:8px;box-shadow:0 4px 12px rgba(0,0,0,0.15)}'
    . '.mtcam-no-cameras{text-align:center;padding:40px;background:#f5f5f5;border-radius:12px;color:#666}'
    . '@media(max-width:768px){.mtcam-stolen-grid{grid-template-columns:1fr}.mtcam-stolen-header{flex-direction:column;gap:10px}.mtcam-stolen-header-left{flex-direction:column;text-align:center;width:100%}.mtcam-stolen-title{text-align:center}.mtcam-detail-toggle{width:100%}}';
wp_register_style( 'mtc-stolen-widget', false, array(), MTCAM_VERSION );
wp_enqueue_style( 'mtc-stolen-widget' );
wp_add_inline_style( 'mtc-stolen-widget', $mtcam_stolen_css );
?>

<div class="mtcam-stolen-widget">
    <?php
    $mtcam_has_cameras = false;
    
    // ⚡ OPTIMIZATION: Use centralized function for damage calculation
    $mtcam_damage_data = ($widget_type === 'stolen') ? mtcam_calculate_stolen_damage($cameras, 'my') : array('total_damage' => 0, 'stolen_count' => 0);
    $mtcam_total_damage = $mtcam_damage_data['total_damage'];
    $mtcam_stolen_count = $mtcam_damage_data['stolen_count'];
    
    // Show total damage if widget shows stolen cameras (ADMIN ONLY)
    if (is_admin() && $widget_type === 'stolen' && $mtcam_stolen_count > 0 && $mtcam_total_damage > 0):
    ?>
    <div class="mtcam-total-damage" style="background:#d32f2f;color:#fff;padding:20px;border-radius:12px;margin-bottom:20px;text-align:center;">
        <h3 style="margin:0 0 10px 0;font-size:24px;">💰 <?php esc_html_e('Total Damage', 'my-trail-camera'); ?></h3>
        <p style="margin:0;font-size:36px;font-weight:bold;"><?php echo esc_html(number_format($mtcam_total_damage, 2, ',', ' ')); ?> €</p>
        <p style="margin:10px 0 0 0;opacity:0.9;">📷 <?php echo esc_html( $mtcam_stolen_count ); ?> <?php esc_html_e( 'stolen cameras', 'my-trail-camera' ); ?></p>
    </div>
    <?php endif; ?>
    
    <?php
    foreach ($cameras as $mtcam_cam_id => $mtcam_cam):
        // FILTER 1: Show only MY cameras (not other people's cameras)
        if (!mtcam_is_my_camera($mtcam_cam)) continue;

        // FILTER: Skip tested cameras in public widget
        if (!is_admin() && mtcam_is_tested($mtcam_cam)) continue;

        // ⚡ OPTIMIZATION: Use helper functions
        $mtcam_is_stolen = mtcam_is_stolen($mtcam_cam);
        $mtcam_is_active = mtcam_is_active($mtcam_cam);
        
        // FILTER 2: Filter based on widget type
        if ($widget_type === 'stolen') {
            // Show only stolen cameras
            if (!$mtcam_is_stolen) continue;
        } elseif ($widget_type === 'active') {
            // Show only active cameras (in field, not stolen)
            if (!$mtcam_is_active || $mtcam_is_stolen) continue;
        }
        
        $mtcam_has_cameras = true;
        $mtcam_cam_color = MTCAM_DEFAULT_COLOR; // Fixed purple color
        
        // ⚡ OPTIMIZATION: Use batch-loaded detection counts
        $mtcam_detection_count = $detection_counts[$mtcam_cam_id] ?? 0;
    ?>
    
	<article class="mtcam-stolen-camera" style="--cam-color:<?php echo esc_attr( $mtcam_cam_color ); ?>; <?php if ( ! $mtcam_is_stolen ) { echo 'border-color:#4CAF50;'; } ?>">
        
        <!-- Compact Header -->
        <header class="mtcam-stolen-header">
            <div class="mtcam-stolen-header-left">
				<div class="mtcam-stolen-badge" style="background: <?php echo esc_attr( $mtcam_cam_color ); ?>;">
                    📷 <?php echo esc_html($mtcam_cam_id); ?>
                </div>
                
                <div class="mtcam-stolen-title">
                    <?php if ($show['name'] === 'yes' && !empty($mtcam_cam['name'])): ?>
                        <h3><?php echo esc_html(mtcam_get_camera_display_name($mtcam_cam_id, $mtcam_cam)); ?></h3>
                    <?php endif; ?>
                    <?php if ($show['location'] === 'yes' && !empty($mtcam_cam['location'])): ?>
                        <p>📍 <?php echo esc_html($mtcam_cam['location']); ?></p>
                    <?php endif; ?>
                    <?php if (!empty($mtcam_cam['stolen_date'])): ?>
						<p style="color:#d32f2f; font-weight:600;"><?php esc_html_e('Stolen:', 'my-trail-camera'); ?> <?php mtcam_echo_date_html( $mtcam_cam['stolen_date'] ); ?></p>
                    <?php endif; ?>
                </div>
            </div>
            
            <?php if ($mtcam_is_stolen): ?>
            <div class="mtcam-stolen-alert">
                <?php echo esc_html(mtcam_label_stolen()); ?>
            </div>
            <?php elseif ($mtcam_is_active): ?>
            <div class="mtcam-stolen-alert" style="background:#e8f5e9; color:#2e7d32;">
                <?php echo esc_html(mtcam_label_active()); ?>
            </div>
            <?php endif; ?>
            
            <button class="mtcam-detail-toggle" data-mtcam-detail-toggle data-mtcam-hide="<?php esc_attr_e('Hide', 'my-trail-camera'); ?>" data-mtcam-show="<?php esc_attr_e('Detail', 'my-trail-camera'); ?>" aria-expanded="false" aria-label="<?php esc_attr_e('Toggle camera details', 'my-trail-camera'); ?>">
                ▼ <?php esc_html_e('Detail', 'my-trail-camera'); ?>
            </button>
        </header>

        <!-- Expandable Details -->
        <section class="mtcam-stolen-details">
        
        <!-- Info Grid -->
        <div class="mtcam-stolen-grid">
            
            <!-- Basic Info -->
            <?php if ($show['manufacturer'] === 'yes' || $show['imei'] === 'yes'): ?>
            <div class="mtcam-stolen-info-box">
                <h4>📋 <?php esc_html_e('Information', 'my-trail-camera'); ?></h4>
                
                <?php if ($show['manufacturer'] === 'yes' && !empty($mtcam_cam['manufacturer'])): ?>
                <div class="mtcam-stolen-info-item">
                    <strong><?php esc_html_e('Manufacturer:', 'my-trail-camera'); ?></strong> <?php echo esc_html($mtcam_cam['manufacturer']); ?>
                </div>
                <?php endif; ?>

                <?php if ($show['imei'] === 'yes' && !empty($mtcam_cam['imei'])): ?>
                <div class="mtcam-stolen-info-item">
                    <strong><?php esc_html_e('IMEI:', 'my-trail-camera'); ?></strong> <?php echo esc_html($mtcam_cam['imei']); ?>
                </div>
                <?php endif; ?>

                <?php if ($show['location'] === 'yes' && !empty($mtcam_cam['location'])): ?>
                <div class="mtcam-stolen-info-item">
                    <strong><?php esc_html_e('Location:', 'my-trail-camera'); ?></strong> <?php echo esc_html($mtcam_cam['location']); ?>
                </div>
                <?php endif; ?>

                <?php if (is_admin() && !empty($mtcam_cam['price']) && $mtcam_cam['price'] > 0): ?>
                <div class="mtcam-stolen-info-item">
                    <strong>💰 <?php esc_html_e('Value:', 'my-trail-camera'); ?></strong> <span style="color:#d32f2f;font-weight:bold;"><?php echo esc_html(number_format($mtcam_cam['price'], 2, ',', ' ')); ?> €</span>
                </div>
                <?php endif; ?>
            </div>
            <?php endif; ?>
            
            <!-- Dates -->
            <?php if ($show['dates'] === 'yes'): ?>
            <div class="mtcam-stolen-info-box">
                <h4>📅 <?php esc_html_e('Dates', 'my-trail-camera'); ?></h4>
                
                <?php if (!empty($mtcam_cam['deploy_date'])): ?>
                <div class="mtcam-stolen-info-item">
					<strong><?php esc_html_e( 'Deployed:', 'my-trail-camera' ); ?></strong> <?php mtcam_echo_date_html( $mtcam_cam['deploy_date'] ); ?>
                </div>
                <?php endif; ?>

                <?php if (!empty($mtcam_cam['stolen_date'])): ?>
                <div class="mtcam-stolen-info-item" style="color:#d32f2f; font-weight:bold;">
					<strong><?php esc_html_e( 'Stolen:', 'my-trail-camera' ); ?></strong> <?php mtcam_echo_date_html( $mtcam_cam['stolen_date'] ); ?>
                </div>
                <?php endif; ?>
                
                <?php if ($show['police'] === 'yes' && ($mtcam_cam['police_reported'] ?? 'no') === 'yes'): ?>
                <div class="mtcam-stolen-info-item">
                    <span style="display:inline-block; background:#1976d2; color:#fff; padding:4px 10px; border-radius:4px; font-weight:600; font-size:12px;">
                        🚨 <?php esc_html_e('Police informed', 'my-trail-camera'); ?>
                    </span>
                </div>
                <?php endif; ?>
            </div>
            <?php endif; ?>
            
            <!-- Security & Files -->
            <?php if ($show['baits'] === 'yes' || $show['security'] === 'yes'): ?>
            <div class="mtcam-stolen-info-box">
                <h4>🔒 <?php esc_html_e('Protection', 'my-trail-camera'); ?></h4>
                
                <?php if ($show['baits'] === 'yes'): ?>
                <div class="mtcam-stolen-info-item">
                    <strong><?php echo esc_html(mtcam_label_bait_files()); ?>:</strong>
                    <div class="mtcam-stolen-badges">
                        <?php if (($mtcam_cam['bait_html'] ?? 'no') === 'yes'): ?><span class="mtcam-badge-item"><?php echo esc_html(mtcam_label_bait_html()); ?></span><?php endif; ?>
                        <?php if (($mtcam_cam['bait_tel'] ?? 'no') === 'yes'): ?><span class="mtcam-badge-item"><?php echo esc_html(mtcam_label_bait_tel()); ?></span><?php endif; ?>
                    </div>
                </div>
                <?php endif; ?>
                
                <?php if ($show['security'] === 'yes'): ?>
                <div class="mtcam-stolen-info-item">
                    <strong><?php echo esc_html(mtcam_label_security()); ?>:</strong>
                    <?php
                    $mtcam_security = array();
                    if (($mtcam_cam['sec_box'] ?? 'no') === 'yes') $mtcam_security[] = mtcam_label_sec_box();
                    if (($mtcam_cam['sec_cable'] ?? 'no') === 'yes') $mtcam_security[] = mtcam_label_sec_cable();
                    if (($mtcam_cam['sec_padlock'] ?? 'no') === 'yes') $mtcam_security[] = mtcam_label_sec_padlock();
                    if (($mtcam_cam['sec_password'] ?? 'no') === 'yes') $mtcam_security[] = mtcam_label_sec_password();
                    if (($mtcam_cam['sec_camera'] ?? 'no') === 'yes') $mtcam_security[] = mtcam_label_sec_camera();
                    echo !empty($mtcam_security) ? esc_html(implode(', ', $mtcam_security)) : esc_html__('None', 'my-trail-camera');
                    ?>
                </div>
                <?php endif; ?>
            </div>
            <?php endif; ?>
            
            <!-- GPS Trackers -->
            <?php if ($show['gps_trackers'] === 'yes'): ?>
            <div class="mtcam-stolen-info-box">
                <h4>📍 <?php esc_html_e('GPS Tracking', 'my-trail-camera'); ?></h4>
                
                <div class="mtcam-stolen-info-item">
                    <?php
                    $mtcam_trackers = array();
                    if (($mtcam_cam['gps_airtag'] ?? 'no') === 'yes') $mtcam_trackers[] = mtcam_label_gps_airtag();
                    if (($mtcam_cam['gps_tile'] ?? 'no') === 'yes') $mtcam_trackers[] = mtcam_label_gps_tile();
                    if (($mtcam_cam['gps_samsung'] ?? 'no') === 'yes') $mtcam_trackers[] = mtcam_label_gps_smarttag();
                    if (($mtcam_cam['gps_other'] ?? 'no') === 'yes') $mtcam_trackers[] = mtcam_label_gps_other();

                    if (!empty($mtcam_trackers)):
                    ?>
                        <div class="mtcam-stolen-badges">
                            <?php foreach ($mtcam_trackers as $mtcam_tracker_item): ?>
                            <span class="mtcam-badge-item" style="background:#fff3cd; color:#856404;"><?php echo esc_html($mtcam_tracker_item); ?></span>
                            <?php endforeach; ?>
                        </div>
                    <?php else: ?>
                        <span style="color:#999;"><?php esc_html_e('No GPS tracker', 'my-trail-camera'); ?></span>
                    <?php endif; ?>
                </div>
            </div>
            <?php endif; ?>
            
        </div>
        
        <!-- GPS Map Link -->
        <?php if ($show['gps'] === 'yes' && !empty($mtcam_cam['gps_lat']) && !empty($mtcam_cam['gps_lon'])): ?>
        <div class="mtcam-stolen-map">
			<a href="<?php echo esc_url( 'https://www.google.com/maps?q=' . $mtcam_cam['gps_lat'] . ',' . $mtcam_cam['gps_lon'] ); ?>"
               target="_blank" 
               rel="noopener noreferrer">
                📍 <?php esc_html_e('Show on Map', 'my-trail-camera'); ?> (GPS: <?php echo esc_html(number_format($mtcam_cam['gps_lat'], 4)); ?>°, <?php echo esc_html(number_format($mtcam_cam['gps_lon'], 4)); ?>°)
            </a>
        </div>
        <?php endif; ?>
        
        <!-- Photo -->
        <?php if ($show['photo'] === 'yes' && !empty($mtcam_cam['photo_url'])): ?>
        <div class="mtcam-stolen-photo">
            <?php
            /* translators: %s: camera ID */
            $mtcam_photo_alt = sprintf( __( 'Photo of camera %s', 'my-trail-camera' ), $mtcam_cam_id );
            ?>
            <img src="<?php echo esc_url($mtcam_cam['photo_url']); ?>" alt="<?php echo esc_attr( $mtcam_photo_alt ); ?>">
        </div>
        <?php endif; ?>
        
        </section><!-- /mtcam-stolen-details -->

    </article><!-- /mtcam-stolen-camera -->
    
    <?php endforeach; ?>
    
    <?php if (!$mtcam_has_cameras): ?>
    <div class="mtcam-no-cameras">
        <?php if ($widget_type === 'stolen'): ?>
            <h3>✅ <?php esc_html_e('No stolen cameras', 'my-trail-camera'); ?></h3>
            <p><?php esc_html_e('Currently there are no stolen cameras on record.', 'my-trail-camera'); ?></p>
        <?php else: ?>
            <h3>📷 <?php esc_html_e('No active cameras', 'my-trail-camera'); ?></h3>
            <p><?php esc_html_e('Currently there are no cameras in the field.', 'my-trail-camera'); ?></p>
        <?php endif; ?>
    </div>
    <?php endif; ?>
</div>
