<?php
    require_once('database.php');
    $conn = connect_database();
    $num = 0;
    $fail = 0;
    $success = 0;
    $id = $_POST['id'];
    for ($i=0; $i < count($_POST['partyValuesStatus']); $i++) { 
        $inc = (float)$_POST['partyValues'][$num];
        $bjp = (float)$_POST['partyValues'][$num + 1];
        $aap = (float)$_POST['partyValues'][$num + 2];
        $others = (float)$_POST['partyValues'][$num + 3];
        $status = $_POST['partyValuesStatus'][$i];
        $caste = $_POST['allData'][$i]['caste'];
        $num = $num + 4;
        $sum = $inc + $bjp + $aap + $others;
        
       if($inc < 0 || $bjp < 0 || $aap < 0 || $others < 0 || $inc > 100 || $bjp > 100 || $aap > 100 || $others > 100) {
            $fail++;
        }
        elseif(!(abs($sum - 100.0)/100.0 < 0.00001)){
            $fail++;
            // echo $caste." - ".$sum."  ";
        } 
        else {
            $sql = "UPDATE `trends` SET `adjusted_INC`= " . $inc . ",`adjusted_BJP`= " . $bjp . ",`adjusted_AAP`= " . $aap . ",`adjusted_Others`= " . $others . ",`active_status`= " . $status . " WHERE caste = '" . $caste . "' AND report_id=" . $id;
            $conn->query($sql);
            $success++;
        } 
    }
     echo $fail == 0 ? "Updated successfully" : $fail." of the field fail(s) to get updated, please make ensure to have values between 0 to 100 and total should be 100. " ;
?>