<?php
    include "../../db/connection.php";

    $variant_id = isset($_GET['variant_id']) ? $_GET['variant_id'] : '';
    $back_url = 'variant_list.php';
    
    $row1 = [];
    $query1 = "SELECT id, variant, 
                        weight, dimensions, frame_material, take_off_Weight, payload_capacity, flight_time, 
                        max_speed, max_range, max_altitude, temp_range, wind_resistance_level, battery_type, 
                        battery_capacity, battery_voltage, charging_time, charger_type, GPS_GNSS, return_home, 
                        flight_modes, obstacle_avoidance, controller_type, mobile_app_support, camera_available, 
                        camera_resolution, video_resolution, gimbal_type, camera_sensors, FPV_support, 
                        transmission_frequency, live_video_range, signal_encryption, safety_features, 
                        AI_software_features, certifications, package_contents, warranty_support, area_spraying_rate, 
                        spray_tank_capacity, No_of_Nozzles, spray_width, spray_volume, GPS_accuracy, terrain_following 
                FROM drone_specification 
                WHERE variant = '$variant_id'";
    $result1 = mysqli_query($conn, $query1);
    if ($result1 && mysqli_num_rows($result1) > 0) {
        $row1 = mysqli_fetch_assoc($result1);
    } 
    $savetype = "edit_specification";
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SkyBots</title>

    <?php include "../support_header.php"; ?>
    <link rel="stylesheet" href="../../assets/custom_css/other_styles.css">
</head>
<body>
    <div class="wrapper">
        <form id="saveForm" onsubmit="return add_form()" method="post" enctype="multipart/form-data">
            <div class="app-content-header">
                <div class="row">
                    <div class="col-6"><div class="heading">Save Specification</div></div>
                    <div class="col-6 text-right">
                        <button type="submit" class="btn btn-primary mr-2" name="submit">Save</button>
                        <a class="btn btn-secondary" href="<?php echo $back_url; ?>">Back</a>
                    </div>
                </div>
            </div>
            <div class="app-content">
                <div class="card">

                    <input type="hidden" id="save_type" name="save_type" class="form-control" value="<?php echo $savetype; ?>" />
                    <input type="hidden" id="variant_id" name="variant_id" class="form-control" value="<?php echo $row1['variant_id'] ?? ''; ?>" />
                    <div class="row">

                        <fieldset class="border p-3 mb-3" style="width: 46%;">
                            <legend class="w-auto px-2">Physical Specifications</legend>

                            <div class="row">
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Weight (gm / kg)</span>
                                            </div>
                                            <input type="text" 
                                                class="form-control" 
                                                name="weight" 
                                                id="weight"
                                                value="<?php echo $row1['weight'] ?? ''; ?>">
                                        </div>
                                        <div class="error" id="weight_error"></div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Dimensions (L×W×H)</span>
                                            </div>
                                            <input type="text" 
                                                class="form-control" 
                                                name="dimensions" 
                                                id="dimensions"
                                                value="<?php echo $row1['dimensions'] ?? ''; ?>">
                                        </div>
                                        <div class="error" id="dimensions_error"></div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Frame Material</span>
                                            </div>
                                            <input type="text" 
                                                class="form-control" 
                                                name="frame_material" 
                                                id="frame_material"
                                                value="<?php echo $row1['frame_material'] ?? ''; ?>">
                                        </div>
                                        <div class="error" id="frame_material_error"></div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Max Take-off Weight</span>
                                            </div>
                                            <input type="text" 
                                                class="form-control" 
                                                name="mtow" 
                                                id="mtow"
                                                value="<?php echo $row1['take_off_Weight'] ?? ''; ?>">
                                        </div>
                                        <div class="error" id="mtow_error"></div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Payload Capacity</span>
                                            </div>
                                            <input type="text" 
                                                class="form-control" 
                                                name="payload_capacity" 
                                                id="payload_capacity"
                                                value="<?php echo $row1['payload_capacity'] ?? ''; ?>">
                                        </div>
                                        <div class="error" id="payload_capacity_error"></div>
                                    </div>
                                </div>
                            </div>
                        </fieldset>

                        <fieldset class="border p-3 mb-3" style="width: 46%;">
                            <legend class="w-auto px-2">Flight Performance</legend>
                            
                            <div class="row">
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Max Flight Time</span>
                                            </div>
                                            <input type="text" 
                                                class="form-control" 
                                                name="max_flight_time" 
                                                id="max_flight_time"
                                                value="<?php echo $row1['flight_time'] ?? ''; ?>">
                                        </div>
                                        <div class="error" id="max_flight_time_error"></div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Max Speed</span>
                                            </div>
                                            <input type="text" 
                                                class="form-control" 
                                                name="max_speed" 
                                                id="max_speed"
                                                value="<?php echo $row1['max_speed'] ?? ''; ?>">
                                        </div>
                                        <div class="error" id="max_speed_error"></div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Max Range</span>
                                            </div>
                                            <input type="text" 
                                                class="form-control" 
                                                name="max_range" 
                                                id="max_range"
                                                value="<?php echo $row1['max_range'] ?? ''; ?>">
                                        </div>
                                        <div class="error" id="max_range_error"></div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Max Altitude</span>
                                            </div>
                                            <input type="text" 
                                                class="form-control" 
                                                name="max_altitude" 
                                                id="max_altitude"
                                                value="<?php echo $row1['max_altitude'] ?? ''; ?>">
                                        </div>
                                        <div class="error" id="max_altitude_error"></div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Operating Temp Range</span>
                                            </div>
                                            <input type="text" 
                                                class="form-control" 
                                                name="operating_temp" 
                                                id="operating_temp"
                                                value="<?php echo $row1['temp_range'] ?? ''; ?>">
                                        </div>
                                        <div class="error" id="operating_temp_error"></div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Wind Resistance Level</span>
                                            </div>
                                            <input type="text" 
                                                class="form-control" 
                                                name="wind_resistance" 
                                                id="wind_resistance"
                                                value="<?php echo $row1['wind_resistance_level'] ?? ''; ?>">
                                        </div>
                                        <div class="error" id="wind_resistance_error"></div>
                                    </div>
                                </div>

                                
                            </div>
                        </fieldset>

                        <fieldset class="border p-3 mb-3" style="width: 46%;">
                            <legend class="w-auto px-2">Power & Battery</legend>
                            
                            <div class="row">
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Battery Type</span>
                                            </div>
                                            <input type="text" 
                                                class="form-control" 
                                                name="battery_type" 
                                                id="battery_type"
                                                value="<?php echo $row1['battery_type'] ?? ''; ?>">
                                        </div>
                                        <div class="error" id="battery_type_error"></div>
                                    </div>
                                </div>

                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Battery Capacity</span>
                                            </div>
                                            <input type="text" 
                                                class="form-control" 
                                                name="battery_capacity" 
                                                id="battery_capacity"
                                                value="<?php echo $row1['battery_capacity'] ?? ''; ?>">
                                        </div>
                                        <div class="error" id="battery_capacity_error"></div>
                                    </div>
                                </div>

                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Battery Voltage</span>
                                            </div>
                                            <input type="text" 
                                                class="form-control" 
                                                name="battery_voltage" 
                                                id="battery_voltage"
                                                value="<?php echo $row1['battery_voltage'] ?? ''; ?>">
                                        </div>
                                        <div class="error" id="battery_voltage_error"></div>
                                    </div>
                                </div>

                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Charging Time</span>
                                            </div>
                                            <input type="text" 
                                                class="form-control" 
                                                name="charging_time" 
                                                id="charging_time"
                                                value="<?php echo $row1['charging_time'] ?? ''; ?>">
                                        </div>
                                        <div class="error" id="charging_time_error"></div>
                                    </div>
                                </div>

                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Charger Type</span>
                                            </div>
                                            <input type="text" 
                                                class="form-control" 
                                                name="charger_type" 
                                                id="charger_type"
                                                value="<?php echo $row1['charger_type'] ?? ''; ?>">
                                        </div>
                                        <div class="error" id="charger_type_error"></div>
                                    </div>
                                </div>
                            </div>
                        </fieldset>

                        <fieldset class="border p-3 mb-3" style="width: 46%;">
                            <legend class="w-auto px-2">Navigation & Control</legend>
                            
                            <div class="row">
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">GPS / GNSS</span>
                                            </div>
                                            <select name="gps_support" id="gps_support" class="form-control">
                                                <option value="">--Select--</option>
                                                <option value="Yes" <?php if(($row1['GPS_GNSS'] ?? '')=="Yes") echo 'selected'; ?>>Yes</option>
                                                <option value="No" <?php if(($row1['GPS_GNSS'] ?? '')=="No") echo 'selected'; ?>>No</option>
                                            </select>
                                        </div>
                                        <div class="error" id="gps_support_error"></div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Return To Home</span>
                                            </div>
                                            <select name="rth" id="rth" class="form-control">
                                                <option value="">--Select--</option>
                                                <option value="Yes" <?php if(($row1['return_home'] ?? '')=="Yes") echo 'selected'; ?>>Yes</option>
                                                <option value="No" <?php if(($row1['return_home'] ?? '')=="No") echo 'selected'; ?>>No</option>
                                            </select>
                                        </div>
                                        <div class="error" id="rth_error"></div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Flight Modes</span>
                                            </div>
                                            <input type="text" 
                                                class="form-control" 
                                                name="flight_modes" 
                                                id="flight_modes"
                                                value="<?php echo $row1['flight_modes'] ?? ''; ?>">
                                        </div>
                                        <div class="error" id="flight_modes_error"></div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Obstacle Avoidance</span>
                                            </div>
                                            <select name="obstacle_avoidance" id="obstacle_avoidance" class="form-control">
                                                <option value="">--Select--</option>
                                                <option value="Yes" <?php if(($row1['obstacle_avoidance'] ?? '')=="Yes") echo 'selected'; ?>>Yes</option>
                                                <option value="No" <?php if(($row1['obstacle_avoidance'] ?? '')=="No") echo 'selected'; ?>>No</option>
                                            </select>
                                        </div>
                                        <div class="error" id="obstacle_avoidance_error"></div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Controller Type</span>
                                            </div>
                                            <input type="text" 
                                                class="form-control" 
                                                name="controller_type" 
                                                id="controller_type"
                                                value="<?php echo $row1['controller_type'] ?? ''; ?>">
                                        </div>
                                        <div class="error" id="controller_type_error"></div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Mobile App Support</span>
                                            </div>
                                            <input type="text" 
                                                class="form-control" 
                                                name="mobile_app" 
                                                id="mobile_app"
                                                value="<?php echo $row1['mobile_app_support'] ?? ''; ?>">
                                        </div>
                                        <div class="error" id="mobile_app_error"></div>
                                    </div>
                                </div>
                            </div>
                        </fieldset>

                        <fieldset class="border p-3 mb-3" style="width: 46%;">
                            <legend class="w-auto px-2">Camera & Sensors</legend>
                            
                            <div class="row">
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Camera Available</span>
                                            </div>
                                            <select name="camera_available" id="camera_available" class="form-control">
                                                <option value="">--Select--</option>
                                                <option value="Yes" <?php if(($row1['camera_available'] ?? '')=="Yes") echo 'selected'; ?>>Yes</option>
                                                <option value="No" <?php if(($row1['camera_available'] ?? '')=="No") echo 'selected'; ?>>No</option>
                                            </select>
                                        </div>
                                        <div class="error" id="camera_available_error"></div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Camera Resolution</span>
                                            </div>
                                            <input type="text" 
                                                class="form-control" 
                                                name="camera_resolution" 
                                                id="camera_resolution"
                                                value="<?php echo $row1['camera_resolution'] ?? ''; ?>">
                                        </div>
                                        <div class="error" id="camera_resolution_error"></div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Video Resolution</span>
                                            </div>
                                            <input type="text" 
                                                class="form-control" 
                                                name="video_resolution" 
                                                id="video_resolution"
                                                value="<?php echo $row1['video_resolution'] ?? ''; ?>">
                                        </div>
                                        <div class="error" id="video_resolution_error"></div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Gimbal Type</span>
                                            </div>
                                            <select name="gimbal_type" id="gimbal_type" class="form-control">
                                                <option value="">--Select--</option>
                                                <option value="None" <?php if(($row1['gimbal_type'] ?? '')=="None") echo 'selected'; ?>>None</option>
                                                <option value="2-Axis" <?php if(($row1['gimbal_type'] ?? '')=="2-Axis") echo 'selected'; ?>>2-Axis</option>
                                                <option value="3-Axis" <?php if(($row1['gimbal_type'] ?? '')=="3-Axis") echo 'selected'; ?>>3-Axis</option>
                                            </select>
                                        </div>
                                        <div class="error" id="gimbal_type_error"></div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Camera Sensors</span>
                                            </div>
                                            <input type="text" 
                                                class="form-control" 
                                                name="camera_sensors" 
                                                id="camera_sensors"
                                                value="<?php echo $row1['camera_sensors'] ?? ''; ?>">
                                        </div>
                                        <div class="error" id="camera_sensors_error"></div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">FPV Support</span>
                                            </div>
                                            <select name="fpv_support" id="fpv_support" class="form-control">
                                                <option value="">--Select--</option>
                                                <option value="Yes" <?php if(($row1['FPV_support'] ?? '')=="Yes") echo 'selected'; ?>>Yes</option>
                                                <option value="No" <?php if(($row1['FPV_support'] ?? '')=="No") echo 'selected'; ?>>No</option>
                                            </select>
                                        </div>
                                        <div class="error" id="fpv_support_error"></div>
                                    </div>
                                </div>
                            </div>
                        </fieldset>

                        <fieldset class="border p-3 mb-3" style="width: 46%;">
                            <legend class="w-auto px-2">Communication</legend>
                            
                            <div class="row">
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Transmission Frequency</span>
                                            </div>
                                            <input type="text" 
                                                class="form-control" 
                                                name="frequency" 
                                                id="frequency"
                                                value="<?php echo $row1['transmission_frequency'] ?? ''; ?>">
                                        </div>
                                        <div class="error" id="frequency_error"></div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Live Video Range</span>
                                            </div>
                                            <input type="text" 
                                                class="form-control" 
                                                name="live_video_range" 
                                                id="live_video_range"
                                                value="<?php echo $row1['live_video_range'] ?? ''; ?>">
                                        </div>
                                        <div class="error" id="live_video_range_error"></div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Signal Encryption</span>
                                            </div>
                                            <input type="text" 
                                                class="form-control" 
                                                name="signal_encryption" 
                                                id="signal_encryption"
                                                value="<?php echo $row1['signal_encryption'] ?? ''; ?>">
                                        </div>
                                        <div class="error" id="signal_encryption_error"></div>
                                    </div>
                                </div>
                            </div>
                        </fieldset>

                        <fieldset class="border p-3 mb-3" style="width: 46%;">
                            <legend class="w-auto px-2">Safety, Software, Compliance, Package, Warranty</legend>
                            
                            <div class="row">
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Safety Features</span>
                                            </div>
                                            <input type="text" 
                                                class="form-control" 
                                                name="safety_features" 
                                                id="safety_features"
                                                value="<?php echo $row1['safety_features'] ?? ''; ?>">
                                        </div>
                                        <div class="error" id="safety_features_error"></div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">AI / Software Features</span>
                                            </div>
                                            <input type="text" 
                                                class="form-control" 
                                                name="software_features" 
                                                id="software_features"
                                                value="<?php echo $row1['AI_software_features'] ?? ''; ?>">
                                        </div>
                                        <div class="error" id="software_features_error"></div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Certifications</span>
                                            </div>
                                            <input type="text" 
                                                class="form-control" 
                                                name="certifications" 
                                                id="certifications"
                                                value="<?php echo $row1['certifications'] ?? ''; ?>">
                                        </div>
                                        <div class="error" id="certifications_error"></div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Package Contents</span>
                                            </div>
                                            <input type="text" 
                                                class="form-control" 
                                                name="package_contents" 
                                                id="package_contents"
                                                value="<?php echo $row1['package_contents'] ?? ''; ?>">
                                        </div>
                                        <div class="error" id="package_contents_error"></div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Warranty & Support</span>
                                            </div>
                                            <input type="text" 
                                                class="form-control" 
                                                name="warranty_support" 
                                                id="warranty_support"
                                                value="<?php echo $row1['warranty_support'] ?? ''; ?>">
                                        </div>
                                        <div class="error" id="warranty_support_error"></div>
                                    </div>
                                </div>
                            </div>
                        </fieldset>

                        <fieldset class="border p-3 mb-3" style="width: 46%;">
                            <legend class="w-auto px-2">Agricultural / Spraying Specifications</legend>
                            
                            <div class="row">
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Area Spraying Rate</span>
                                            </div>
                                            <input type="text"
                                                class="form-control"
                                                name="area_spraying_rate"
                                                id="area_spraying_rate"
                                                value="<?php echo $row1['area_spraying_rate'] ?? ''; ?>"
                                                placeholder="e.g. 10–15 acre/hour">
                                        </div>
                                        <div class="error" id="area_spraying_rate_error"></div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Spray Tank Capacity</span>
                                            </div>
                                            <input type="text"
                                                class="form-control"
                                                name="spray_tank_capacity"
                                                id="spray_tank_capacity"
                                                value="<?php echo $row1['spray_tank_capacity'] ?? ''; ?>"
                                                placeholder="e.g. 10 L / 20 L">
                                        </div>
                                        <div class="error" id="spray_tank_capacity_error"></div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">No. of Nozzles</span>
                                            </div>
                                            <input type="number"
                                                class="form-control"
                                                name="no_of_nozzles"
                                                id="no_of_nozzles"
                                                value="<?php echo $row1['No_of_Nozzles'] ?? ''; ?>">
                                        </div>
                                        <div class="error" id="no_of_nozzles_error"></div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Spray Width</span>
                                            </div>
                                            <input type="text"
                                                class="form-control"
                                                name="spray_width"
                                                id="spray_width"
                                                value="<?php echo $row1['spray_width'] ?? ''; ?>"
                                                placeholder="e.g. 3–7 meters">
                                        </div>
                                        <div class="error" id="spray_width_error"></div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Spray Volume</span>
                                            </div>
                                            <input type="text"
                                                class="form-control"
                                                name="spray_volume"
                                                id="spray_volume"
                                                value="<?php echo $row1['spray_volume'] ?? ''; ?>"
                                                placeholder="e.g. L/acre or L/hectare">
                                        </div>
                                        <div class="error" id="spray_volume_error"></div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">GPS Accuracy</span>
                                            </div>
                                            <input type="text"
                                                class="form-control"
                                                name="gps_accuracy"
                                                id="gps_accuracy"
                                                value="<?php echo $row1['GPS_accuracy'] ?? ''; ?>"
                                                placeholder="e.g. ±10 cm (RTK)">
                                        </div>
                                        <div class="error" id="gps_accuracy_error"></div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">Terrain Following</span>
                                            </div>
                                            <select name="terrain_following" id="terrain_following" class="form-control">
                                                <option value="">--Select--</option>
                                                <option value="Yes" <?php if(($row1['terrain_following'] ?? '')=="Yes") echo 'selected'; ?>>Yes</option>
                                                <option value="No" <?php if(($row1['terrain_following'] ?? '')=="No") echo 'selected'; ?>>No</option>
                                            </select>
                                        </div>
                                        <div class="error" id="terrain_following_error"></div>
                                    </div>
                                </div>
                            </div>
                        </fieldset>
                        
                    </div>
                </div>
            </div>
        </form>
    </div>

    <?php include "../support_footer.php"; ?>

    <script> 
        function add_form() {
            
            $('.error').html('');
            var url = "<?php echo $back_url;?>";
            var save_type = $("#save_type").val().trim();
            var variant_id = $("#variant_id").val().trim();

            let count = 0;
            let msg = [];

            function req(name, label) {
                let v = $('[name="'+name+'"]').val();
                if (!v || v.trim() === '') {
                    msg.push(label + " is required");
                    count++;
                }
                return v;
            }

            function optNumber(name, label) {
                let v = $('[name="'+name+'"]').val();
                if (v && isNaN(v)) {
                    msg.push(label + " must be numeric");
                    count++;
                }
            }

            /* PHYSICAL SPECIFICATIONS */
            req('weight', 'Weight');
            req('dimensions', 'Dimensions');
            req('frame_material', 'Frame Material');
            optNumber('mtow', 'Max Take-off Weight');
            optNumber('payload_capacity', 'Payload Capacity');

            /* FLIGHT PERFORMANCE */
            optNumber('max_flight_time', 'Max Flight Time');
            optNumber('max_speed', 'Max Speed');
            optNumber('max_range', 'Max Range');
            optNumber('max_altitude', 'Max Altitude');

            req('operating_temp', 'Operating Temperature Range');
            req('wind_resistance', 'Wind Resistance Level');

            /* POWER & BATTERY */
            req('battery_type', 'Battery Type');
            optNumber('battery_capacity', 'Battery Capacity');
            optNumber('battery_voltage', 'Battery Voltage');
            optNumber('charging_time', 'Charging Time');
            req('charger_type', 'Charger Type');

            /* NAVIGATION & CONTROL */
            req('gps_support', 'GPS / GNSS');
            req('rth', 'Return To Home');
            req('flight_modes', 'Flight Modes');
            req('obstacle_avoidance', 'Obstacle Avoidance');
            req('controller_type', 'Controller Type');
            req('mobile_app', 'Mobile App Support');

            /* CAMERA & SENSORS */
            let cameraAvailable = req('camera_available', 'Camera Available');

            if (cameraAvailable === 'Yes') {
                req('camera_resolution', 'Camera Resolution');
                req('video_resolution', 'Video Resolution');
                req('gimbal_type', 'Gimbal Type');
            }

            req('camera_sensors', 'Camera Sensors');
            req('fpv_support', 'FPV Support');

            /* COMMUNICATION */
            req('frequency', 'Transmission Frequency');
            optNumber('live_video_range', 'Live Video Range');
            req('signal_encryption', 'Signal Encryption');

            /* SAFETY / SOFTWARE / ETC */
            req('safety_features', 'Safety Features');
            req('software_features', 'Software / AI Features');
            req('certifications', 'Certifications');
            req('package_contents', 'Package Contents');
            req('warranty_support', 'Warranty & Support');

            /* AGRICULTURE (CONDITIONAL) */
            let agriUsed = false;

            [
                'area_spraying_rate',
                'spray_tank_capacity',
                'no_of_nozzles',
                'spray_width',
                'spray_volume',
                'gps_accuracy'
            ].forEach(f => {
                if ($('[name="'+f+'"]').val().trim() !== '') agriUsed = true;
            });

            if (agriUsed) {
                req('area_spraying_rate', 'Area Spraying Rate');
                req('spray_tank_capacity', 'Spray Tank Capacity');
                optNumber('no_of_nozzles', 'No. of Nozzles');
                req('spray_width', 'Spray Width');
                req('spray_volume', 'Spray Volume');
                req('gps_accuracy', 'GPS Accuracy');
                req('terrain_following', 'Terrain Following');
            }

            /* STOP ON ERROR */
            if (count > 0) {
                // alert(msg.join("\n"));
                window.parent.$("#warningModal .alertTitle").html(msg.join("<br>"));
                window.parent.showModal('warningModal');
                return false;
            }

            var formData = new FormData();
            formData.append("type", save_type); 
            formData.append("variant_id", variant_id); 
            $('form').find('input, select').each(function () {
                if ($(this).attr('name')) {
                    formData.append($(this).attr('name'), $(this).val());
                }
            }); 
            
            // for (var pair of formData.entries()) {
            //     console.log(pair[0] + ': ' + pair[1]);
            // }

            showLoader();
            
            $.ajax({
                type: "POST",
                url: "query_function.php",
                data: formData,
                processData: false, 
                contentType: false, 
                dataType: "json",
                success: function (response) {
                    hideLoader();
                    // console.log("Server response:", response);
                    // alert(JSON.stringify(response));

                    if (response.status === 'success' && response.code === 11) {
                        window.parent.$("#successModal .alertTitle").html("Successfully Saved!");
                        window.parent.showModal('successModal');
                        window.parent.$('#successModal').off('hidden.bs.modal').on('hidden.bs.modal', function () {
                            window.parent.location.reload();
                        });
                        window.parent.location.reload();
                    } else if (response.status === 'error') {
                        $('.error').html(''); 
                        if (response.errors && response.errors.length > 0) {
                            response.errors.forEach(function(field) {
                                $('#' + field + '_error').html('*Required');
                            });
                        }
                    } else {
                        window.parent.$("#warningModal .alertTitle").html(response.message || "Not Added!");
                        window.parent.showModal('warningModal');
                    }
                },
                error: function (xhr, status, error) {
                    hideLoader();
                    console.error("AJAX Error:", status, error);
                    alert("Server Error: " + error);
                }
            });

            return false;
        }
    </script>

</body>
</html>