<?php
include('../db/connection.php');

$ids = $_GET['ids'];
$id_array = explode(",", $ids);

echo '<div class="comparisonTable">';
echo '<table class="table table-bordered">';
echo '<thead>';
echo '<tr><th>Specification</th>';

foreach($id_array as $id){
    $q = mysqli_query($conn,"SELECT variant FROM variant WHERE id='$id'");
    $d = mysqli_fetch_assoc($q);
    echo "<th>".$d['variant']."</th>";
}
echo "</tr>";
echo '</thead>';
echo '<tbody>';

/* ---------------------------
   SPECIFICATION STRUCTURE
----------------------------*/

$specifications = [

"Flight Performance" => [
    "flight_time" => ["Max Flight Time"," mins"],
    "max_speed" => ["Max Speed"," m/sec"],
    "max_range" => ["Max Range"," km"],
    "max_altitude" => ["Max Altitude"," m"],
    "temp_range" => ["Operating Temp Range"," C"],
    "wind_resistance_level" => ["Wind Resistance Level"," m/sec"]
],

"Power & Battery" => [
    "battery_type" => ["Battery Type",""],
    "battery_capacity" => ["Battery Capacity"," mAh"],
    "battery_voltage" => ["Battery Voltage"," V"],
    "charging_time" => ["Charging Time"," Hr"],
    "charger_type" => ["Charger Type",""]
],

"Navigation & Control" => [
    "GPS_GNSS" => ["GPS/GNSS",""],
    "return_home" => ["Return to Home",""],
    "flight_modes" => ["Flight Modes",""],
    "obstacle_avoidance" => ["Obstacle Avoidance",""],
    "controller_type" => ["Controller Type",""],
    "mobile_app_support" => ["Mobile App Support",""]
],

"Camera & Sensors" => [
    "camera_available" => ["Camera Available",""],
    "camera_resolution" => ["Camera Resolution"," MP"],
    "video_resolution" => ["Video Resolution"," px"],
    "gimbal_type" => ["Gimbal Type",""],
    "camera_sensors" => ["Camera Sensors",""],
    "FPV_support" => ["FPV Support",""]
],

"Communication" => [
    "transmission_frequency" => ["Transmission Frequency"," GHz"],
    "live_video_range" => ["Live Video Range"," km"],
    "signal_encryption" => ["Signal Encryption",""]
],

"Safety, Software, Compliance, Package, Warranty" => [
    "safety_features" => ["Safety Features",""],
    "AI_software_features" => ["AI/Software Features",""],
    "certifications" => ["Certifications",""],
    "package_contents" => ["Package Contents",""],
    "warranty_support" => ["Warranty",""]
],

"Agricultural / Spraying Specifications" => [
    "area_spraying_rate" => ["Area Spraying Rate"," Acres/Hr"],
    "spray_tank_capacity" => ["Spray Tank Capacity"," L"],
    "No_of_Nozzles" => ["No. of Nozzles",""],
    "spray_width" => ["Spray Width"," m"],
    "spray_volume" => ["Spray Volume"," ml/min"],
    "GPS_accuracy" => ["GPS Accuracy"," m"],
    "terrain_following" => ["Terrain Following"," m"]
]

];

/* ---------------------------
   LOOP GROUPS
----------------------------*/

foreach($specifications as $group => $fields){

    $group_id = md5($group); // unique id for each group

    // Group Heading Row (Clickable)
    echo "<tr class='group-header' data-group='$group_id'>
            <td colspan='".(count($id_array)+1)."'style='font-weight:bold; cursor:pointer; color:#e06767; font-size:1.75rem;'>
                $group <i class='fas fa-caret-down' style='float:right;'></i>
            </td>
          </tr>";

    foreach($fields as $field => $details){

        echo "<tr class='group-content group-$group_id' style='display:none;'>";

        echo "<td>".$details[0]."</td>";

        foreach($id_array as $id){

            $q = mysqli_query($conn,"SELECT $field FROM drone_specification WHERE variant='$id' AND deletion=0");
            $d = mysqli_fetch_assoc($q);

            $value = $d[$field];

            if($value == "" || $value == null){
                $value = "NA";
            } else {
                $value .= $details[1];
            }

            echo "<td>$value</td>";
        }

        echo "</tr>";
    }
}


echo "</tbody>";
echo "</table>";
echo "</div>";
?>
