<?php
/**
 * MTC — Add Camera Form
 * @package My Trail Camera
 * @since 8.1.0
 */

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

function mtcam_render_add_camera_form($owner_type = 'me') {
    $is_my = ($owner_type === 'me');
    ?>
    <div class="mtcam-edit-grid">
        
        <!-- Camera Profile Selector (autocomplete) -->
        <?php if (function_exists('mtcam_get_profiles_grouped')): ?>
        <div class="mtcam-edit-section" style="grid-column: 1 / -1;">
            <div class="mtcam-edit-field mtcam-profile-autocomplete-wrap">
                <label for="mtcam-profile-input"><strong>📋 <?php esc_html_e('Trail Camera model', 'my-trail-camera'); ?></strong></label>
                <input type="text" id="mtcam-profile-input" class="mtcam-profile-input"
                       autocomplete="off"
                       placeholder="<?php esc_attr_e('Start typing model name...', 'my-trail-camera'); ?>">
                <input type="hidden" id="mtcam-profile-select" name="new_cam_profile" value="custom">
                <div id="mtcam-profile-dropdown" class="mtcam-profile-dropdown" style="display:none;"></div>
                <div id="mtcam-profile-tip" class="mtcam-profile-tip" style="display:none;"></div>
            </div>
        </div>
        <?php endif; ?>
        
        <!-- Left Column -->
        <div class="mtcam-edit-section">
            <h5>📋 <?php esc_html_e('Basic Info', 'my-trail-camera'); ?></h5>
            
            <div class="mtcam-edit-field">
                <label for="mtcam-add-device-id"><?php esc_html_e('Device ID', 'my-trail-camera'); ?> *</label>
                <input type="text" name="new_cam_id" id="mtcam-add-device-id" required
                       placeholder="<?php echo esc_attr($is_my ? 'CAM-001' : 'JOZEF-001'); ?>">
            </div>
            
            <div class="mtcam-edit-field">
                <label for="mtcam-add-location"><?php esc_html_e('Location *', 'my-trail-camera'); ?></label>
                <input type="text" name="new_cam_location" id="mtcam-add-location" required
                       placeholder="<?php echo esc_attr($is_my ? __('Big Fatra', 'my-trail-camera') : __('Tatras', 'my-trail-camera')); ?>">
            </div>
            
            <div class="mtcam-edit-field">
                <label for="mtcam-add-price">💰 <?php esc_html_e('Price/Value (€)', 'my-trail-camera'); ?></label>
                <input type="number" name="new_cam_price" id="mtcam-add-price" step="0.01" min="0"
                       placeholder="149.99">
                <small>📝 <?php esc_html_e('Base camera price', 'my-trail-camera'); ?></small>
            </div>
            
            <?php if ($is_my): ?>
            <!-- Accessories Section for New Camera -->
            <div class="mtcam-edit-field mtcam-acc-section">
                <label><strong>🛒 <?php esc_html_e('Accessories & Add-ons', 'my-trail-camera'); ?></strong></label>
                <small ><?php esc_html_e('Select accessories and enter their price.', 'my-trail-camera'); ?></small>
                
                <label class="mtcam-acc-row">
                    <input type="checkbox" name="new_cam_acc_battery" value="yes" >
                    🔋 <?php esc_html_e('Batteries', 'my-trail-camera'); ?>
                    <input type="number" name="new_cam_acc_battery_price" step="0.01" min="0" 
                            placeholder="0.00"> €
                </label>
                
                <label class="mtcam-acc-row">
                    <input type="checkbox" name="new_cam_acc_sdcard" value="yes" >
                    💾 <?php esc_html_e('SD Card', 'my-trail-camera'); ?>
                    <input type="number" name="new_cam_acc_sdcard_price" step="0.01" min="0" 
                            placeholder="0.00"> €
                </label>
                
                <label class="mtcam-acc-row" data-mtcam-acc="sim">
                    <input type="checkbox" name="new_cam_acc_sim" value="yes" >
                    📶 <?php esc_html_e('SIM Card', 'my-trail-camera'); ?>
                    <input type="number" name="new_cam_acc_sim_price" step="0.01" min="0"
                            placeholder="0.00"> €
                </label>
            </div>
            <?php endif; ?>
            
            <?php if (!$is_my): ?>
            <div class="mtcam-edit-field">
                <label for="mtcam-add-owner-name"><?php esc_html_e('Owner Name', 'my-trail-camera'); ?> *</label>
                <input type="text" name="new_cam_owner_name" id="mtcam-add-owner-name" 
                       placeholder="<?php esc_attr_e('John Smith', 'my-trail-camera'); ?>">
            </div>
            
            <div class="mtcam-edit-field">
                <label for="mtcam-add-owner-email">📧 <?php esc_html_e('Owner Email', 'my-trail-camera'); ?> * (<?php esc_html_e('notifications will be sent here', 'my-trail-camera'); ?>!)</label>
                <input type="email" name="new_cam_owner_email" id="mtcam-add-owner-email" 
                       placeholder="jozef@email.com">
            </div>
            <?php endif; ?>
            
            <?php if ($is_my): ?>
            <div class="mtcam-edit-field">
                <label for="mtcam-add-serial-number"><?php esc_html_e('Serial Number', 'my-trail-camera'); ?></label>
                <input type="text" name="new_cam_serial_number" id="mtcam-add-serial-number">
            </div>
            <div class="mtcam-edit-field" data-mtcam-field="imei">
                <label for="mtcam-add-imei"><?php esc_html_e('IMEI', 'my-trail-camera'); ?></label>
                <input type="text" name="new_cam_imei" id="mtcam-add-imei"
                       placeholder="123456789012345">
            </div>
            <?php endif; ?>
        </div>
        
        <!-- Right Column -->
        <div class="mtcam-edit-section">
            <h5>📅 <?php esc_html_e('Dates & GPS', 'my-trail-camera'); ?></h5>
            
            <?php if ($is_my): ?>
            <div class="mtcam-edit-field">
                <label for="mtcam-add-purchase-date"><?php esc_html_e('Purchase Date', 'my-trail-camera'); ?></label>
                <input type="date" name="new_cam_purchase_date" id="mtcam-add-purchase-date">
            </div>
            
            <div class="mtcam-edit-field">
                <label for="mtcam-add-deploy-date"><?php esc_html_e('Deploy Date', 'my-trail-camera'); ?></label>
                <div style="display:flex; gap:6px;">
                    <input type="date" name="new_cam_deploy_date" id="mtcam-add-deploy-date" style="flex:1;">
                    <input type="time" name="new_cam_deploy_time" id="mtcam-add-deploy-time" style="width:100px;">
                </div>
            </div>
            
            <div class="mtcam-edit-field">
                <label for="mtcam-add-planned-days">📅 <?php esc_html_e('Planned stay in field (days)', 'my-trail-camera'); ?></label>
                <input type="number" name="new_cam_planned_days" id="mtcam-add-planned-days" min="0" step="1" placeholder="<?php esc_attr_e('e.g. 30', 'my-trail-camera'); ?>" value="0">
                <small>0 = <?php esc_html_e('no limit', 'my-trail-camera'); ?></small>
            </div>

            <?php if ( function_exists( 'mtcam_get_battery_presets_grouped' ) ) : ?>
            <div class="mtcam-edit-field">
                <label for="mtcam-add-battery-preset">🔋 <?php esc_html_e('Battery model', 'my-trail-camera'); ?></label>
                <select name="new_cam_battery_preset"
                        id="mtcam-add-battery-preset"
                        class="mtcam-select-field mtcam-battery-preset-select"
                        data-cam-id="add">
                    <option value=""><?php esc_html_e('— Unspecified (profile default) —', 'my-trail-camera'); ?></option>
                    <?php
                    $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 echo esc_html( $plabel ); ?></option>
                            <?php endforeach; ?>
                        </optgroup>
                    <?php endforeach; ?>
                </select>
            </div>

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

            <div class="mtcam-edit-field">
                <label for="mtcam-add-power-type">⚡ <?php esc_html_e('Power type', 'my-trail-camera'); ?></label>
                <select name="new_cam_power_type" id="mtcam-add-power-type" class="mtcam-select-field mtcam-power-type-select">
                    <?php foreach (mtcam_get_all_power_types() as $val => $label): ?>
                        <option value="<?php echo esc_attr($val); ?>" <?php selected('lithium', $val); ?>>
                            <?php echo esc_html($label); ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div class="mtcam-edit-field">
                <label for="mtcam-add-gps-lat"><?php esc_html_e('GPS Latitude', 'my-trail-camera'); ?></label>
                <input type="text" name="new_cam_gps_lat" id="mtcam-add-gps-lat"
                       placeholder="48.95739612">
            </div>

            <div class="mtcam-edit-field">
                <label for="mtcam-add-gps-lon"><?php esc_html_e('GPS Longitude', 'my-trail-camera'); ?></label>
                <input type="text" name="new_cam_gps_lon" id="mtcam-add-gps-lon"
                       placeholder="18.79786309">
            </div>
            
            <div class="mtcam-edit-field">
                <label>
                    <input type="radio" name="new_cam_active" value="no" checked>
                    <strong><?php echo esc_html(mtcam_label_at_home()); ?></strong>
                </label>
                <br>
                <label>
                    <input type="radio" name="new_cam_active" value="yes">
                    <strong>✅ <?php esc_html_e('In field (active)', 'my-trail-camera'); ?></strong>
                </label>
            </div>
        </div>
        
    </div>
    
    <!-- Security Options -->
    <div class="mtcam-edit-grid-3col">
        <div class="mtcam-edit-section">
            <h5>🔒 <?php esc_html_e('Security', 'my-trail-camera'); ?></h5>
            <label><input type="checkbox" name="new_cam_bait_html" value="yes"> <?php echo esc_html(mtcam_label_bait_html()); ?></label><br>
            <label><input type="checkbox" name="new_cam_bait_tel" value="yes"> <?php echo esc_html(mtcam_label_bait_tel()); ?></label>
            <?php if ($is_my): ?>
            <br>
            <label><input type="checkbox" name="new_cam_sec_box" value="yes"> <?php echo esc_html(mtcam_label_sec_box()); ?></label><br>
            <label><input type="checkbox" name="new_cam_sec_cable" value="yes"> <?php echo esc_html(mtcam_label_sec_cable()); ?></label><br>
            <label><input type="checkbox" name="new_cam_sec_padlock" value="yes"> <?php echo esc_html(mtcam_label_sec_padlock()); ?></label><br>
            <label><input type="checkbox" name="new_cam_sec_password" value="yes"> <?php echo esc_html(mtcam_label_sec_password()); ?></label><br>
            <label><input type="checkbox" name="new_cam_sec_camera" value="yes"> <?php echo esc_html(mtcam_label_sec_camera()); ?></label>
            <?php endif; ?>
        </div>

        <?php if ($is_my): ?>
        <div class="mtcam-edit-section">
            <h5>📍 <?php esc_html_e('GPS Trackers', 'my-trail-camera'); ?></h5>
            <label><input type="checkbox" name="new_cam_gps_airtag" value="yes"> <?php echo esc_html(mtcam_label_gps_airtag()); ?></label><br>
            <label><input type="checkbox" name="new_cam_gps_tile" value="yes"> <?php echo esc_html(mtcam_label_gps_tile()); ?></label><br>
            <label><input type="checkbox" name="new_cam_gps_samsung" value="yes"> <?php echo esc_html(mtcam_label_gps_smarttag()); ?></label><br>
            <label><input type="checkbox" name="new_cam_gps_other" value="yes"> 🔵 <?php esc_html_e('Other', 'my-trail-camera'); ?></label>
        </div>
        <?php endif; ?>
    </div>
    
    <button type="submit" name="add_camera" class="button button-primary">
        <?php echo esc_html(mtcam_btn_add_camera()); ?>
    </button>
    <?php
}

/**
 * Render edit camera form
 */

