<?php
/**
 * MTC — Camera edit form
 * @package My Trail Camera
 * @since 8.1.0
 */

if (!defined('ABSPATH')) exit;

function mtcam_render_edit_camera_form($cam_id, $cam) {
    $is_my = mtcam_is_my_camera($cam);
    ?>
    
        <input type="hidden" name="edit_cam_id[]" value="<?php echo esc_attr($cam_id); ?>">
        
        <div class="mtcam-edit-grid-3col">
            <!-- Column 1: Basic info -->
            <div class="mtcam-edit-section">
                <h5>📋 <?php esc_html_e('Basic Info', 'my-trail-camera'); ?></h5>
                
                <div class="mtcam-edit-field">
                    <label for="mtcam-edit-device-id-<?php echo esc_attr($cam_id); ?>"><?php esc_html_e('Device ID', 'my-trail-camera'); ?></label>
                    <?php if ($is_my): ?>
                        <input type="text" id="mtcam-edit-device-id-<?php echo esc_attr($cam_id); ?>" value="<?php echo esc_attr($cam_id); ?>" disabled>
                    <?php else: ?>
                        <input type="text" id="mtcam-edit-device-id-<?php echo esc_attr($cam_id); ?>" value="●●●●●●" disabled class="mtcam-text-muted">
                    <?php endif; ?>
                </div>

                <div class="mtcam-edit-field">
                    <label for="mtcam-edit-location-<?php echo esc_attr($cam_id); ?>"><?php esc_html_e('Location *', 'my-trail-camera'); ?></label>
                    <input type="text" name="edit_cam_location_<?php echo esc_attr($cam_id); ?>" id="mtcam-edit-location-<?php echo esc_attr($cam_id); ?>" 
                           value="<?php echo esc_attr($cam['location']); ?>">
                </div>
                
                <?php if ($is_my): ?>
                <div class="mtcam-edit-field">
                    <label>📋 <?php esc_html_e('Model', 'my-trail-camera'); ?></label>
                    <?php
                    $cur_profile = $cam['profile'] ?? 'custom';
                    $cur_profile_data = function_exists('mtcam_get_camera_profile') ? mtcam_get_camera_profile($cur_profile) : array();
                    $cur_label = ($cur_profile === 'custom') ? __('Custom camera', 'my-trail-camera') : ($cur_profile_data['label'] ?? $cur_profile);
                    ?>
                    <strong><?php echo esc_html($cur_label); ?></strong>
                    <input type="hidden" name="edit_cam_profile_<?php echo esc_attr($cam_id); ?>" value="<?php echo esc_attr($cur_profile); ?>">
                </div>
                
                <?php
                $edit_profile = function_exists('mtcam_get_camera_profile') ? mtcam_get_camera_profile($cam['profile'] ?? 'custom') : array();
                $show_imei = !empty($edit_profile['has_sim']) || !empty($cam['imei']);
                ?>
                <div class="mtcam-edit-field">
                    <label for="mtcam-edit-serial-number-<?php echo esc_attr($cam_id); ?>"><?php esc_html_e('Serial Number', 'my-trail-camera'); ?></label>
                    <input type="text" name="edit_cam_serial_number_<?php echo esc_attr($cam_id); ?>" id="mtcam-edit-serial-number-<?php echo esc_attr($cam_id); ?>"
                           value="<?php echo esc_attr($cam['serial_number'] ?? ''); ?>">
                </div>

                <?php if ($show_imei): ?>
                <div class="mtcam-edit-field" data-mtcam-field="imei">
                    <label for="mtcam-edit-imei-<?php echo esc_attr($cam_id); ?>"><?php esc_html_e('IMEI', 'my-trail-camera'); ?></label>
                    <input type="text" name="edit_cam_imei_<?php echo esc_attr($cam_id); ?>" id="mtcam-edit-imei-<?php echo esc_attr($cam_id); ?>"
                           value="<?php echo esc_attr($cam['imei'] ?? ''); ?>">
                </div>
                <?php endif; ?>
                <?php else: ?>
                <div class="mtcam-edit-field">
                    <label for="mtcam-edit-owner-name-<?php echo esc_attr($cam_id); ?>"><?php esc_html_e('Owner Name', 'my-trail-camera'); ?></label>
                    <input type="text" id="mtcam-edit-owner-name-<?php echo esc_attr($cam_id); ?>" value="<?php echo esc_attr($cam['owner_name'] ?? ''); ?>" disabled>
                </div>
                <div class="mtcam-edit-field">
                    <label for="mtcam-edit-owner-email-<?php echo esc_attr($cam_id); ?>"><?php esc_html_e('Owner Email', 'my-trail-camera'); ?></label>
                    <input type="email" id="mtcam-edit-owner-email-<?php echo esc_attr($cam_id); ?>" value="<?php echo esc_attr($cam['owner_email'] ?? ''); ?>" disabled>
                </div>
                <?php endif; ?>
                
                <div class="mtcam-edit-field">
                    <label for="mtcam-edit-power-type-<?php echo esc_attr($cam_id); ?>">⚡ <?php esc_html_e('Power type', 'my-trail-camera'); ?></label>
                    <?php
                    $cam_profile_id = $cam['profile'] ?? 'custom';
                    $cam_profile = mtcam_get_camera_profile($cam_profile_id);
                    $allowed_power_types = $cam_profile['power_types'] ?? array_keys(mtcam_get_all_power_types());
                    ?>
                    <select name="edit_cam_power_type_<?php echo esc_attr($cam_id); ?>" id="mtcam-edit-power-type-<?php echo esc_attr($cam_id); ?>" class="mtcam-select-field mtcam-power-type-select">
                        <?php foreach (mtcam_get_all_power_types() as $val => $label): ?>
                            <?php if (!in_array($val, $allowed_power_types, true)) continue; ?>
                            <option value="<?php echo esc_attr($val); ?>" <?php selected(($cam['power_type'] ?? 'lithium'), $val); ?>>
                                <?php echo esc_html($label); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <?php if ( function_exists( 'mtcam_get_battery_presets_grouped' ) ) : ?>
                <div class="mtcam-edit-field">
                    <label for="mtcam-edit-battery-preset-<?php echo esc_attr($cam_id); ?>">🔋 <?php esc_html_e('Battery model', 'my-trail-camera'); ?></label>
                    <select name="edit_cam_battery_preset_<?php echo esc_attr($cam_id); ?>"
                            id="mtcam-edit-battery-preset-<?php echo esc_attr($cam_id); ?>"
                            class="mtcam-select-field mtcam-battery-preset-select"
                            data-cam-id="<?php echo esc_attr($cam_id); ?>">
                        <option value=""><?php esc_html_e('— Unspecified (profile default) —', 'my-trail-camera'); ?></option>
                        <?php
                        $selected_preset = $cam['battery_preset'] ?? '';
                        $format_labels   = mtcam_battery_format_labels();
                        foreach ( mtcam_get_battery_presets_grouped() as $fmt => $opts ) :
                            if ( empty( $opts ) ) continue; ?>
                            <optgroup label="<?php echo esc_attr( $format_labels[ $fmt ] ?? $fmt ); ?>">
                                <?php foreach ( $opts as $pid => $plabel ) : ?>
                                    <option value="<?php echo esc_attr( $pid ); ?>" <?php selected( $selected_preset, $pid ); ?>>
                                        <?php echo esc_html( $plabel ); ?>
                                    </option>
                                <?php endforeach; ?>
                            </optgroup>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div class="mtcam-edit-field">
                    <label for="mtcam-edit-cell-count-<?php echo esc_attr($cam_id); ?>">🔢 <?php esc_html_e('Number of cells', 'my-trail-camera'); ?></label>
                    <input type="number" min="0" max="64" step="1"
                           name="edit_cam_cell_count_<?php echo esc_attr($cam_id); ?>"
                           id="mtcam-edit-cell-count-<?php echo esc_attr($cam_id); ?>"
                           class="mtcam-cell-count-input"
                           data-cam-id="<?php echo esc_attr($cam_id); ?>"
                           value="<?php echo esc_attr( intval( $cam['cell_count'] ?? 0 ) ); ?>">
                    <small class="mtcam-capacity-preview" id="mtcam-capacity-preview-<?php echo esc_attr($cam_id); ?>"></small>
                </div>
                <?php endif; ?>

                <?php if (mtcam_is_active($cam) || mtcam_is_stolen($cam)): ?>
                <div class="mtcam-edit-field">
                    <label for="mtcam-edit-gps-lat-<?php echo esc_attr($cam_id); ?>"><?php esc_html_e('GPS Latitude', 'my-trail-camera'); ?></label>
                    <input type="text" name="edit_cam_gps_lat_<?php echo esc_attr($cam_id); ?>" id="mtcam-edit-gps-lat-<?php echo esc_attr($cam_id); ?>"
                           value="<?php echo esc_attr($cam['gps_lat'] ?? ''); ?>">
                </div>
                <div class="mtcam-edit-field">
                    <label for="mtcam-edit-gps-lon-<?php echo esc_attr($cam_id); ?>"><?php esc_html_e('GPS Longitude', 'my-trail-camera'); ?></label>
                    <input type="text" name="edit_cam_gps_lon_<?php echo esc_attr($cam_id); ?>" id="mtcam-edit-gps-lon-<?php echo esc_attr($cam_id); ?>"
                           value="<?php echo esc_attr($cam['gps_lon'] ?? ''); ?>">
                </div>
                <?php endif; ?>
            </div>
            
            <!-- Column 2: Dates & Status -->
            <div class="mtcam-edit-section">
                <h5>📅 <?php esc_html_e('Dates & Status', 'my-trail-camera'); ?></h5>
                
                <?php if ($is_my): ?>
                <div class="mtcam-edit-field">
                    <label for="mtcam-edit-purchase-date-<?php echo esc_attr($cam_id); ?>"><?php esc_html_e('Purchase Date', 'my-trail-camera'); ?></label>
                    <input type="date" name="edit_cam_purchase_date_<?php echo esc_attr($cam_id); ?>" id="mtcam-edit-purchase-date-<?php echo esc_attr($cam_id); ?>" 
                           value="<?php echo esc_attr($cam['purchase_date'] ?? ''); ?>">
                </div>
                
                <?php if (mtcam_is_active($cam)): ?>
                <div class="mtcam-edit-field">
                    <label for="mtcam-edit-deploy-date-<?php echo esc_attr($cam_id); ?>"><?php esc_html_e('Deploy Date', 'my-trail-camera'); ?></label>
                    <div style="display:flex; gap:6px;">
                        <input type="date" name="edit_cam_deploy_date_<?php echo esc_attr($cam_id); ?>" id="mtcam-edit-deploy-date-<?php echo esc_attr($cam_id); ?>"
                               value="<?php echo esc_attr($cam['deploy_date'] ?? ''); ?>" style="flex:1;">
                        <input type="time" name="edit_cam_deploy_time_<?php echo esc_attr($cam_id); ?>" id="mtcam-edit-deploy-time-<?php echo esc_attr($cam_id); ?>"
                               value="<?php echo esc_attr($cam['deploy_time'] ?? ''); ?>" style="width:100px;">
                    </div>
                </div>

                <div class="mtcam-edit-field">
                    <label for="mtcam-edit-planned-days-<?php echo esc_attr($cam_id); ?>">📅 <?php esc_html_e('Planned stay (days)', 'my-trail-camera'); ?></label>
                    <input type="number" name="edit_cam_planned_days_<?php echo esc_attr($cam_id); ?>" id="mtcam-edit-planned-days-<?php echo esc_attr($cam_id); ?>"
                           min="0" step="1" placeholder="0"
                           value="<?php echo esc_attr($cam['planned_days'] ?? 0); ?>">
                    <small>0 = <?php esc_html_e('no limit', 'my-trail-camera'); ?></small>
                </div>
                <?php endif; ?>
                <?php endif; ?>
                
                <?php
                    $cur_status = 'active';
                    if (mtcam_is_tested($cam)) $cur_status = 'tested';
                    elseif (mtcam_is_stolen($cam)) $cur_status = 'stolen';
                    elseif (($cam['active'] ?? 'yes') !== 'yes') $cur_status = 'home';
                ?>
                
                <div class="mtcam-edit-field">
                    <label><strong><?php esc_html_e('Status:', 'my-trail-camera'); ?></strong></label>
                    <div style="display:flex; flex-wrap:wrap; gap:8px; margin-top:4px;">
                        <label style="cursor:pointer; font-size:13px;">
                            <input type="radio" name="edit_cam_status_<?php echo esc_attr($cam_id); ?>" value="active"
                                   <?php checked($cur_status, 'active'); ?>
                                   data-mtcam-stolen-toggle="<?php echo esc_attr($cam_id); ?>">
                            ✅ <?php esc_html_e('In field', 'my-trail-camera'); ?>
                        </label>
                        <label style="cursor:pointer; font-size:13px;">
                            <input type="radio" name="edit_cam_status_<?php echo esc_attr($cam_id); ?>" value="tested"
                                   <?php checked($cur_status, 'tested'); ?>
                                   data-mtcam-stolen-toggle="<?php echo esc_attr($cam_id); ?>">
                            🧪 <?php esc_html_e('Test', 'my-trail-camera'); ?>
                        </label>
                        <label style="cursor:pointer; font-size:13px;">
                            <input type="radio" name="edit_cam_status_<?php echo esc_attr($cam_id); ?>" value="stolen"
                                   <?php checked($cur_status, 'stolen'); ?>
                                   data-mtcam-stolen-toggle="<?php echo esc_attr($cam_id); ?>">
                            ⚠️ <?php esc_html_e('Stolen', 'my-trail-camera'); ?>
                        </label>
                        <label style="cursor:pointer; font-size:13px;">
                            <input type="radio" name="edit_cam_status_<?php echo esc_attr($cam_id); ?>" value="home"
                                   <?php checked($cur_status, 'home'); ?>
                                   data-mtcam-stolen-toggle="<?php echo esc_attr($cam_id); ?>">
                            🏠 <?php esc_html_e('At home', 'my-trail-camera'); ?>
                        </label>
                    </div>
                </div>
                
                <div class="mtcam-edit-field" id="stolen_date_field_<?php echo esc_attr($cam_id); ?>" 
                     style="display: <?php echo esc_attr($cur_status === 'stolen' ? 'block' : 'none'); ?>;">
                    <label for="edit_cam_stolen_date_input_<?php echo esc_attr($cam_id); ?>"><?php esc_html_e('Stolen Date', 'my-trail-camera'); ?></label>
                    <input type="date"
                           name="edit_cam_stolen_date_<?php echo esc_attr($cam_id); ?>" 
                           id="edit_cam_stolen_date_input_<?php echo esc_attr($cam_id); ?>"
                           value="<?php echo esc_attr($cam['stolen_date'] ?? ''); ?>">
                </div>
                
                <div class="mtcam-edit-field" id="police_field_<?php echo esc_attr($cam_id); ?>"
                     style="display: <?php echo esc_attr($cur_status === 'stolen' ? 'block' : 'none'); ?>;">
                    <label>
                        <input type="checkbox" name="edit_cam_police_reported_<?php echo esc_attr($cam_id); ?>" value="yes"
                               <?php checked(($cam['police_reported'] ?? 'no'), 'yes'); ?>>
                        <strong>🚨 <?php esc_html_e('Reported to police', 'my-trail-camera'); ?></strong>
                    </label>
                </div>
            </div>
            
            <!-- Column 3: Price & Accessories (only for my cameras) -->
            <?php if ($is_my): ?>
            <div class="mtcam-edit-section">
                <h5>💰 <?php esc_html_e('Price & Accessories', 'my-trail-camera'); ?></h5>
                
                <div class="mtcam-edit-field">
                    <label for="mtcam-edit-price-<?php echo esc_attr($cam_id); ?>">📷 <?php esc_html_e('Camera price', 'my-trail-camera'); ?> (€)</label>
                    <input type="number" name="edit_cam_price_<?php echo esc_attr($cam_id); ?>" id="mtcam-edit-price-<?php echo esc_attr($cam_id); ?>" 
                           step="0.01" min="0"
                           value="<?php echo esc_attr($cam['price'] ?? 0); ?>">
                </div>
                
                <label class="mtcam-acc-row">
                    <input type="checkbox" name="edit_cam_acc_battery_<?php echo esc_attr($cam_id); ?>" 
                           value="yes" <?php checked(($cam['acc_battery'] ?? 'no'), 'yes'); ?>>
                    🔋 <?php esc_html_e('Batteries', 'my-trail-camera'); ?>
                    <input type="number" name="edit_cam_acc_battery_price_<?php echo esc_attr($cam_id); ?>" 
                           step="0.01" min="0" value="<?php echo esc_attr($cam['acc_battery_price'] ?? 0); ?>"
                           placeholder="0.00"> €
                </label>
                
                <label class="mtcam-acc-row">
                    <input type="checkbox" name="edit_cam_acc_sdcard_<?php echo esc_attr($cam_id); ?>" 
                           value="yes" <?php checked(($cam['acc_sdcard'] ?? 'no'), 'yes'); ?>>
                    💾 <?php esc_html_e('SD Card', 'my-trail-camera'); ?>
                    <input type="number" name="edit_cam_acc_sdcard_price_<?php echo esc_attr($cam_id); ?>" 
                           step="0.01" min="0" value="<?php echo esc_attr($cam['acc_sdcard_price'] ?? 0); ?>"
                           placeholder="0.00"> €
                </label>
                
                <?php
                $cam_profile = function_exists('mtcam_get_camera_profile') ? mtcam_get_camera_profile($cam['profile'] ?? 'custom') : array();
                $show_sim = !empty($cam_profile['has_sim']) || ($cam['acc_sim'] ?? 'no') === 'yes';
                ?>
                <?php if ($show_sim): ?>
                <label class="mtcam-acc-row" data-mtcam-acc="sim">
                    <input type="checkbox" name="edit_cam_acc_sim_<?php echo esc_attr($cam_id); ?>"
                           value="yes" <?php checked(($cam['acc_sim'] ?? 'no'), 'yes'); ?>>
                    📶 <?php esc_html_e('SIM Card', 'my-trail-camera'); ?>
                    <input type="number" name="edit_cam_acc_sim_price_<?php echo esc_attr($cam_id); ?>"
                           step="0.01" min="0" value="<?php echo esc_attr($cam['acc_sim_price'] ?? 0); ?>"
                           placeholder="0.00"> €
                </label>
                <?php endif; ?>

                <p class="mtcam-acc-subgroup">🔒 <?php esc_html_e('Security', 'my-trail-camera'); ?></p>

                <label class="mtcam-acc-row mtcam-acc-row-noprice">
                    <input type="checkbox" name="edit_cam_bait_html_<?php echo esc_attr($cam_id); ?>"
                           value="yes" <?php checked(($cam['bait_html'] ?? 'no'), 'yes'); ?>>
                    <?php echo esc_html(mtcam_label_bait_html()); ?>
                </label>

                <label class="mtcam-acc-row mtcam-acc-row-noprice">
                    <input type="checkbox" name="edit_cam_bait_tel_<?php echo esc_attr($cam_id); ?>"
                           value="yes" <?php checked(($cam['bait_tel'] ?? 'no'), 'yes'); ?>>
                    <?php echo esc_html(mtcam_label_bait_tel()); ?>
                </label>

                <label class="mtcam-acc-row">
                    <input type="checkbox" name="edit_cam_sec_box_<?php echo esc_attr($cam_id); ?>"
                           value="yes" <?php checked(($cam['sec_box'] ?? 'no'), 'yes'); ?>>
                    <?php echo esc_html(mtcam_label_sec_box()); ?>
                    <input type="number" name="edit_cam_sec_box_price_<?php echo esc_attr($cam_id); ?>"
                           step="0.01" min="0" value="<?php echo esc_attr($cam['sec_box_price'] ?? 0); ?>"
                           placeholder="0.00"> €
                </label>

                <label class="mtcam-acc-row">
                    <input type="checkbox" name="edit_cam_sec_cable_<?php echo esc_attr($cam_id); ?>"
                           value="yes" <?php checked(($cam['sec_cable'] ?? 'no'), 'yes'); ?>>
                    <?php echo esc_html(mtcam_label_sec_cable()); ?>
                    <input type="number" name="edit_cam_sec_cable_price_<?php echo esc_attr($cam_id); ?>"
                           step="0.01" min="0" value="<?php echo esc_attr($cam['sec_cable_price'] ?? 0); ?>"
                           placeholder="0.00"> €
                </label>

                <label class="mtcam-acc-row">
                    <input type="checkbox" name="edit_cam_sec_padlock_<?php echo esc_attr($cam_id); ?>"
                           value="yes" <?php checked(($cam['sec_padlock'] ?? 'no'), 'yes'); ?>>
                    <?php echo esc_html(mtcam_label_sec_padlock()); ?>
                    <input type="number" name="edit_cam_sec_padlock_price_<?php echo esc_attr($cam_id); ?>"
                           step="0.01" min="0" value="<?php echo esc_attr($cam['sec_padlock_price'] ?? 0); ?>"
                           placeholder="0.00"> €
                </label>

                <label class="mtcam-acc-row">
                    <input type="checkbox" name="edit_cam_sec_password_<?php echo esc_attr($cam_id); ?>"
                           value="yes" <?php checked(($cam['sec_password'] ?? 'no'), 'yes'); ?>>
                    <?php echo esc_html(mtcam_label_sec_password()); ?>
                    <input type="number" name="edit_cam_sec_password_price_<?php echo esc_attr($cam_id); ?>"
                           step="0.01" min="0" value="<?php echo esc_attr($cam['sec_password_price'] ?? 0); ?>"
                           placeholder="0.00"> €
                </label>

                <label class="mtcam-acc-row">
                    <input type="checkbox" name="edit_cam_sec_camera_<?php echo esc_attr($cam_id); ?>"
                           value="yes" <?php checked(($cam['sec_camera'] ?? 'no'), 'yes'); ?>>
                    <?php echo esc_html(mtcam_label_sec_camera()); ?>
                    <input type="number" name="edit_cam_sec_camera_price_<?php echo esc_attr($cam_id); ?>"
                           step="0.01" min="0" value="<?php echo esc_attr($cam['sec_camera_price'] ?? 0); ?>"
                           placeholder="0.00"> €
                </label>

                <p class="mtcam-acc-subgroup">📍 <?php esc_html_e('GPS Trackers', 'my-trail-camera'); ?></p>

                <label class="mtcam-acc-row">
                    <input type="checkbox" name="edit_cam_gps_airtag_<?php echo esc_attr($cam_id); ?>"
                           value="yes" <?php checked(($cam['gps_airtag'] ?? 'no'), 'yes'); ?>>
                    <?php echo esc_html(mtcam_label_gps_airtag()); ?>
                    <input type="number" name="edit_cam_gps_airtag_price_<?php echo esc_attr($cam_id); ?>"
                           step="0.01" min="0" value="<?php echo esc_attr($cam['gps_airtag_price'] ?? 0); ?>"
                           placeholder="0.00"> €
                </label>

                <label class="mtcam-acc-row">
                    <input type="checkbox" name="edit_cam_gps_tile_<?php echo esc_attr($cam_id); ?>"
                           value="yes" <?php checked(($cam['gps_tile'] ?? 'no'), 'yes'); ?>>
                    <?php echo esc_html(mtcam_label_gps_tile()); ?>
                    <input type="number" name="edit_cam_gps_tile_price_<?php echo esc_attr($cam_id); ?>"
                           step="0.01" min="0" value="<?php echo esc_attr($cam['gps_tile_price'] ?? 0); ?>"
                           placeholder="0.00"> €
                </label>

                <label class="mtcam-acc-row">
                    <input type="checkbox" name="edit_cam_gps_samsung_<?php echo esc_attr($cam_id); ?>"
                           value="yes" <?php checked(($cam['gps_samsung'] ?? 'no'), 'yes'); ?>>
                    <?php echo esc_html(mtcam_label_gps_smarttag()); ?>
                    <input type="number" name="edit_cam_gps_samsung_price_<?php echo esc_attr($cam_id); ?>"
                           step="0.01" min="0" value="<?php echo esc_attr($cam['gps_samsung_price'] ?? 0); ?>"
                           placeholder="0.00"> €
                </label>

                <label class="mtcam-acc-row">
                    <input type="checkbox" name="edit_cam_gps_other_<?php echo esc_attr($cam_id); ?>"
                           value="yes" <?php checked(($cam['gps_other'] ?? 'no'), 'yes'); ?>>
                    🔵 <?php esc_html_e('Other', 'my-trail-camera'); ?>
                    <input type="number" name="edit_cam_gps_other_price_<?php echo esc_attr($cam_id); ?>"
                           step="0.01" min="0" value="<?php echo esc_attr($cam['gps_other_price'] ?? 0); ?>"
                           placeholder="0.00"> €
                </label>

                <div class="mtcam-acc-total">
                    <strong>💰 <?php esc_html_e('Total:', 'my-trail-camera'); ?>
                        <?php echo esc_html(number_format(mtcam_get_camera_total_value($cam), 2, ',', ' ')); ?> €
                    </strong>
                </div>
            </div>
            <?php endif; ?>
        </div>
        
        <!-- Recording & Battery Settings (only for active cameras in the field) -->
        <?php if ($is_my && mtcam_is_active($cam) && empty($cam['stolen_date'])): ?>
        <div class="mtcam-edit-grid mtcam-mt-15">
            <div class="mtcam-edit-section">
                <h5>🎥 <?php esc_html_e('Recording Settings', 'my-trail-camera'); ?></h5>
                
                <div class="mtcam-edit-field">
                    <label for="mtcam-edit-rec-mode-<?php echo esc_attr($cam_id); ?>"><?php esc_html_e('Recording Mode', 'my-trail-camera'); ?></label>
                    <select name="edit_cam_rec_mode_<?php echo esc_attr($cam_id); ?>" id="mtcam-edit-rec-mode-<?php echo esc_attr($cam_id); ?>" class="mtcam-select-field">
                        <option value="video" <?php selected(($cam['rec_mode'] ?? 'video'), 'video'); ?>>🎥 <?php esc_html_e('Video', 'my-trail-camera'); ?></option>
                        <option value="photo" <?php selected(($cam['rec_mode'] ?? 'video'), 'photo'); ?>>📸 <?php esc_html_e('Photo', 'my-trail-camera'); ?></option>
                    </select>
                </div>

                <div class="mtcam-edit-field">
                    <label for="mtcam-edit-rec-resolution-<?php echo esc_attr($cam_id); ?>"><?php esc_html_e('Video Resolution', 'my-trail-camera'); ?></label>
                    <?php
                    $cur_res = $cam['rec_resolution'] ?? '1080p';
                    $profile = function_exists('mtcam_get_camera_profile') ? mtcam_get_camera_profile($cam['profile'] ?? 'custom') : null;
                    $resolutions = $profile['video_resolutions'] ?? array('4K', '2K', '1296p', '1080p', '720p', '480p');
                    $res_labels = array('4K' => '4K Ultra HD', '2K' => '2K (2560×1440)', '1296p' => '1296p', '1080p' => '1080p Full HD', '720p' => '720p', '480p' => '480p');
                    ?>
                    <select name="edit_cam_rec_resolution_<?php echo esc_attr($cam_id); ?>" id="mtcam-edit-rec-resolution-<?php echo esc_attr($cam_id); ?>" class="mtcam-select-field">
                        <?php foreach ($resolutions as $res): ?>
                        <option value="<?php echo esc_attr($res); ?>" <?php selected($cur_res, $res); ?>><?php echo esc_html($res_labels[$res] ?? $res); ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div class="mtcam-edit-field">
                    <label for="mtcam-edit-rec-length-<?php echo esc_attr($cam_id); ?>"><?php esc_html_e('Video Length (seconds)', 'my-trail-camera'); ?></label>
                    <input type="number" name="edit_cam_rec_length_<?php echo esc_attr($cam_id); ?>" id="mtcam-edit-rec-length-<?php echo esc_attr($cam_id); ?>"
                           value="<?php echo esc_attr($cam['rec_length'] ?? 110); ?>"
                           min="5" max="300" step="5">
                    <small><?php esc_html_e('Default: 110 s (Wachman Solar Pro)', 'my-trail-camera'); ?></small>
                </div>

                <div class="mtcam-edit-field">
                    <label for="mtcam-edit-rec-day-<?php echo esc_attr($cam_id); ?>">☀️ <?php esc_html_e('Day recordings', 'my-trail-camera'); ?></label>
                    <input type="number" name="edit_cam_rec_day_count_<?php echo esc_attr($cam_id); ?>" id="mtcam-edit-rec-day-<?php echo esc_attr($cam_id); ?>"
                           value="<?php echo esc_attr($cam['rec_day_count'] ?? 1); ?>"
                           min="0" max="50">
                </div>

                <div class="mtcam-edit-field">
                    <label for="mtcam-edit-rec-night-<?php echo esc_attr($cam_id); ?>">🌙 <?php esc_html_e('Night recordings (IR)', 'my-trail-camera'); ?></label>
                    <input type="number" name="edit_cam_rec_night_count_<?php echo esc_attr($cam_id); ?>" id="mtcam-edit-rec-night-<?php echo esc_attr($cam_id); ?>"
                           value="<?php echo esc_attr($cam['rec_night_count'] ?? 2); ?>"
                           min="0" max="50">
                </div>
            </div>
            
            <div class="mtcam-edit-section">
                <h5>🔋 <?php esc_html_e('Battery Status', 'my-trail-camera'); ?></h5>

                <div class="mtcam-edit-field">
                    <label for="mtcam-edit-battery-pct-<?php echo esc_attr($cam_id); ?>"><?php esc_html_e('Current battery (%)', 'my-trail-camera'); ?></label>
                    <input type="number" name="edit_cam_battery_pct_<?php echo esc_attr($cam_id); ?>" id="mtcam-edit-battery-pct-<?php echo esc_attr($cam_id); ?>"
                           value="<?php echo esc_attr($cam['battery_pct'] ?? 100); ?>"
                           min="0" max="100" step="1">
                    <small><?php
                        if ( mtcam_has_solar( $cam['power_type'] ?? 'lithium' ) ) {
                            esc_html_e( 'Value from camera display at last check — estimate starts from this date', 'my-trail-camera' );
                        } else {
                            esc_html_e( 'Value from camera display at deployment or last battery swap', 'my-trail-camera' );
                        }
                    ?></small>
                </div>

                
                <div class="mtcam-edit-field">
                    <label>
                <?php
                // Show battery estimate if GPS is available
                if (!empty($cam['gps_lat']) && !empty($cam['gps_lon'])):
                    $battery = mtcam_estimate_battery_simple($cam);
                    $pct = $battery['battery_pct'];
                    $bar_color = mtcam_battery_color($pct);
                ?>
                <div class="mtcam-battery-estimate">
                    <div class="mtcam-flex-center mtcam-gap-10 mtcam-mb-8">
                        <span><?php esc_html_e('Estimate today:', 'my-trail-camera'); ?></span>
                        <div class="mtcam-battery-bar-bg mtcam-battery-bar-sm">
                            <div class="mtcam-battery-bar-fill" style="width:<?php echo intval($pct); ?>%; background:<?php echo esc_attr($bar_color); ?>;"></div>
                        </div>
                        <strong style="color:<?php echo esc_attr($bar_color); ?>;"><?php echo intval($pct); ?> %</strong>
                    </div>
                    <small class="mtcam-text-gray">
                        <?php esc_html_e('Consumption:', 'my-trail-camera'); ?> −<?php echo intval($battery['consumed_mah']); ?> mAh |
                        <?php esc_html_e('Solar:', 'my-trail-camera'); ?> +<?php echo intval($battery['solar_gained_mah']); ?> mAh |
                        <?php esc_html_e('Weather:', 'my-trail-camera'); ?> <?php echo esc_html($battery['weather']); ?> <?php echo esc_html($battery['temperature']); ?>°C
                    </small>
                    <?php if ($battery['error']): ?>
                        <br><small class="mtcam-text-muted">⚠️ <?php echo esc_html($battery['error']); ?></small>
                    <?php endif; ?>
                </div>
                <?php endif; ?>
            </div>
        </div>
        <?php endif; ?>
        
        <p class="mtcam-edit-actions">
            <button type="submit" name="mtcam_settings_submit" class="button button-primary">💾 <?php esc_html_e('Save Changes', 'my-trail-camera'); ?></button>
            <a href="<?php echo esc_url(admin_url('admin.php?page=mtcam-dashboard')); ?>" class="button"><?php esc_html_e('Cancel', 'my-trail-camera'); ?></a>
        </p>
   
    <?php
}

