<?php

require_once './connection.php';

if (!isset($_POST['user_id']) == "") {
    $user_id = $_POST['user_id'];
//    echo '$user_id ' . $user_id;
    $result_data = array();

    function metersToFeet($meters) {
        $feet = $meters * 3.28084; // Convert meters to feet
        return $feet;
    }

    $query = 'SELECT MAX(id) as id FROM `tbl_flow_meter_data` WHERE flag=1 and user_id=' . $user_id . ' GROUP BY flow_meter_id ORDER by id asc';
//    echo '$$query ' . $query . ' ';
    mysqli_set_charset($con, 'utf8');
    $result = mysqli_query($con, $query);
    if (mysqli_num_rows($result) > 0) {
        $i = 1;
        while ($row = mysqli_fetch_assoc($result)) {

            $meter_name = "";
            $address = "";
            $water_level = 0;
            $Q_Flow = 0;
            $Velocity = 0;
            $cusec = 0;
            $created_date = "";

            $id = $row["id"];
//            echo '<br>$id ' . $id;
//            print_r($result);

            $query1 = 'SELECT tfmd.*,DATE_FORMAT(tfmd.created_date,"%d-%m-%Y %H:%i:%s") AS created_date_new,DATE_FORMAT(tfmd.created_date,"%Y-%m-%d") AS days_date,tfm.meter_name,tfm.address,tfm.canal_height,tfm.side_slop,tfm.channel_slop,tfm.m_r_coefficient,tfm.canal_bottom_width,tfm.canal_top_width,tfm.latitude,tfm.longitude FROM `tbl_flow_meter_data` as tfmd LEFT join tbl_flow_meter as tfm on tfm.id=tfmd.flow_meter_id WHERE tfmd.id=' . $id . ' and tfmd.flag=1 ORDER by tfmd.id DESC LIMIT 0,1';
//            echo '$query1 ' . $query1;
            mysqli_set_charset($con, 'utf8');
            $result1 = mysqli_query($con, $query1);
            if (mysqli_num_rows($result1) > 0) {
                while ($row1 = mysqli_fetch_assoc($result1)) {
                    $now = time(); // or your date as well
                    $your_date = strtotime($row1["days_date"]);
                    $datediff = $now - $your_date;

                    $Days = round($datediff / (60 * 60 * 24));
                    $check_on_off = $row1["canal_height"] - $row1["water_level"];

//                    if ($Days > 2) {
//                        
//                    } else {
////                                                                                            echo '<br>water_level ' . $row1["water_level"].' 0.20';
//                        if ($row1["water_level"] >= 0.20) {   //1 days
                            $bottom_width = $row1["canal_bottom_width"];
                            $side_slop = $row1["side_slop"];
                            $channel_slop = $row1["channel_slop"];
                            $m_r_coefficient = $row1["m_r_coefficient"];
                            $canal_top_width = $row1["canal_top_width"];
                            $water_level = $row1["water_level"];

                            $a1 = $water_level * $bottom_width;
                            $flow_level_squre = $water_level * $water_level;
                            $a2 = $side_slop * $flow_level_squre;
                            $Area = $a1 + $a2;

                            $z12 = $water_level * $side_slop;
                            $z3 = $bottom_width + $z12;
                            $Area = $z3 * $water_level;

                            $pw0 = $bottom_width;
                            $pw1 = 2 * $water_level;
                            $pw_side_slop = $side_slop * $side_slop;

                            $pw2 = 1 + $pw_side_slop;

                            $pw2_squre = number_format(sqrt($pw2), 4);
//                                                                                            
                            $pw1_squre = number_format($pw1 * $pw2_squre, 4);
//                                                                                            
                            $wetted_perimeter = $pw0 + $pw1_squre;

                            $hydraulic_radius = $Area / $wetted_perimeter;
                            $R = number_format($hydraulic_radius, 3);
//                                      
                            $S = $channel_slop; // slope
                            $V = (1 / $m_r_coefficient) * pow($R, 2 / 3) * sqrt($S);
                            $Q = $Area * $V;

                            $Q_Flow = number_format($Q, 4);
////                                                                                            
                            $Velocity = number_format($V, 2);

                            $Cusec_1_unit = 35.3147;
                            $liter_1_unit = 1000;

                            $meters = $water_level; // Example value in meters
                            $feet = metersToFeet($meters);

                            $water_level_check = $feet;

                            $cusec = 0;

                            $location_id = $row1["flow_meter_id"];
//                                                                                print_r($result);

                            $query21 = 'SELECT * FROM `tbl_canal_measure_data` WHERE location=' . $location_id . ' and flag=1 ORDER by id DESC LIMIT 0,1';
                                                                                // echo '$query21 ' . $query21;
                            mysqli_set_charset($con, 'utf8');
                            $result21 = mysqli_query($con, $query21);
                            if (mysqli_num_rows($result21) > 0) {
                                while ($row21 = mysqli_fetch_assoc($result21)) {

                                    if ($water_level_check >= 0.15 && $water_level_check <= 0.25) {
                                        $cusec = $row21["0_1"];
                                    } else if ($water_level_check >= 0.25 && $water_level_check <= 0.35) {
                                        $cusec = $row21["0_2"];
                                    } else if ($water_level_check >= 0.35 && $water_level_check <= 0.45) {
                                        $cusec = $row21["0_3"];
                                    } else if ($water_level_check >= 0.45 && $water_level_check <= 0.55) {
                                        $cusec = $row21["0_4"];
                                    } else if ($water_level_check >= 0.55 && $water_level_check <= 0.65) {
                                        $cusec = $row21["0_5"];
                                    } else if ($water_level_check >= 0.65 && $water_level_check <= 0.75) {
                                        $cusec = $row21["0_6"];
                                    } else if ($water_level_check >= 0.75 && $water_level_check <= 0.85) {
                                        $cusec = $row21["0_7"];
                                    } else if ($water_level_check >= 0.85 && $water_level_check <= 0.95) {
                                        $cusec = $row21["0_8"];
                                    } else if ($water_level_check >= 0.95 && $water_level_check <= 1.05) {
                                        $cusec = $row21["0_9"];
                                    } else if ($water_level_check >= 1.05 && $water_level_check <= 1.15) {
                                        $cusec = $row21["1_0"];
                                    } else if ($water_level_check >= 1.15 && $water_level_check <= 1.25) {
                                        $cusec = $row21["1_1"];
                                    } else if ($water_level_check >= 1.25 && $water_level_check <= 1.35) {
                                        $cusec = $row21["1_2"];
                                    } else if ($water_level_check > 1.35 && $water_level_check <= 1.45) {
                                        $cusec = $row21["1_3"] + 3;
                                    } else if ($water_level_check > 1.45 && $water_level_check <= 1.55) {
                                        $cusec = $row21["1_4"] + 3;
                                    } else if ($water_level_check > 1.55 && $water_level_check <= 1.65) {
                                        $cusec = $row21["1_5"] + 3;
                                    } else if ($water_level_check > 1.65 && $water_level_check <= 1.75) {
                                        $cusec = $row21["1_6"] + 3;
                                    } else if ($water_level_check > 1.75 && $water_level_check <= 1.85) {
                                        $cusec = $row21["1_7"] + 3;
                                    } else if ($water_level_check > 1.85 && $water_level_check <= 1.95) {
                                        $cusec = $row21["1_8"] + 3;
                                    } else if ($water_level_check > 1.95 && $water_level_check <= 2.05) {
                                        $cusec = $row21["1_9"] + 3;
                                    } else if ($water_level_check > 2.05 && $water_level_check <= 2.15) {
                                        $cusec = $row21["2_0"] + 3;
                                    } else if ($water_level_check > 2.15 && $water_level_check <= 2.25) {
                                        $cusec = $row21["2_1"] + 3;
                                    } else if ($water_level_check > 2.25 && $water_level_check <= 2.35) {
                                        $cusec = $row21["2_2"] + 3;
                                    } else if ($water_level_check > 2.35 && $water_level_check <= 2.45) {
                                        $cusec = $row21["2_3"] + 3;
                                    } else if ($water_level_check > 2.45 && $water_level_check <= 2.55) {
                                        $cusec = $row21["2_4"] + 3;
                                    } else if ($water_level_check > 2.55 && $water_level_check <= 2.65) {
                                        $cusec = $row21["2_5"] + 3;
                                    } else if ($water_level_check > 2.65 && $water_level_check <= 2.75) {
                                        $cusec = $row21["2_6"] + 3;
                                    } else if ($water_level_check > 2.75 && $water_level_check <= 2.85) {
                                        $cusec = $row21["2_7"] + 3;
                                    } else if ($water_level_check > 2.85 && $water_level_check <= 2.95) {
                                        $cusec = $row21["2_8"] + 3;
                                    } else if ($water_level_check > 2.95 && $water_level_check <= 3.05) {
                                        $cusec = $row21["2_9"] + 3;
                                    } else if ($water_level_check > 3.05 && $water_level_check <= 3.15) {
                                        $cusec = $row21["3_0"] + 3;
                                    } else if ($water_level_check > 3.15 && $water_level_check <= 3.25) {
                                        $cusec = $row21["3_1"] + 3;
                                    } else if ($water_level_check > 3.25 && $water_level_check <= 3.35) {
                                        $cusec = $row21["3_2"] + 3;
                                    } else if ($water_level_check > 3.35 && $water_level_check <= 3.45) {
                                        $cusec = $row21["3_3"] + 3;
                                    } else if ($water_level_check > 3.45 && $water_level_check <= 3.55) {
                                        $cusec = $row21["3_4"] + 3;
                                    } else if ($water_level_check > 3.55 && $water_level_check <= 3.65) {
                                        $cusec = $row21["3_5"] + 3;
                                    } else if ($water_level_check > 3.65 && $water_level_check <= 3.75) {
                                        $cusec = $row21["3_6"] + 3;
                                    } else if ($water_level_check > 3.75 && $water_level_check <= 3.85) {
                                        $cusec = $row21["3_7"] + 3;
                                    } else if ($water_level_check > 3.85 && $water_level_check <= 3.95) {
                                        $cusec = $row21["3_8"] + 3;
                                    } else if ($water_level_check > 3.95 && $water_level_check <= 4.05) {
                                        $cusec = $row21["3_9"] + 3;
                                    } else if ($water_level_check > 4.05 && $water_level_check <= 4.15) {
                                        $cusec = $row21["4_0"] + 3;
                                    } else if ($water_level_check > 4.15 && $water_level_check <= 4.25) {
                                        $cusec = $row21["4_1"] + 3;
                                    } else if ($water_level_check > 4.25 && $water_level_check <= 4.35) {
                                        $cusec = $row21["4_2"] + 3;
                                    } else if ($water_level_check > 4.35 && $water_level_check <= 4.45) {
                                        $cusec = $row21["4_3"] + 3;
                                    } else if ($water_level_check > 4.45 && $water_level_check <= 4.55) {
                                        $cusec = $row21["4_4"] + 3;
                                    } else if ($water_level_check > 4.55 && $water_level_check <= 4.65) {
                                        $cusec = $row21["4_5"] + 3;
                                    } else if ($water_level_check > 4.65 && $water_level_check <= 4.75) {
                                        $cusec = $row21["4_6"] + 3;
                                    } else if ($water_level_check > 4.72 && $water_level_check <= 4.85) {
                                        $cusec = $row21["4_7"] + 3;
                                    } else if ($water_level_check > 4.82 && $water_level_check <= 4.95) {
                                        $cusec = $row21["4_8"] + 3;
                                    } else if ($water_level_check > 4.92 && $water_level_check <= 5.05) {
                                        $cusec = $row21["4_9"] + 3;
                                    } else if ($water_level_check > 5.02 && $water_level_check <= 5.15) {
                                        $cusec = $row21["5_0"] + 3;
                                    } else if ($water_level_check > 5.15 && $water_level_check <= 5.25) {
                                        $cusec = $row21["5_1"] + 3;
                                    } else if ($water_level_check > 5.25 && $water_level_check <= 5.35) {
                                        $cusec = $row21["5_2"] + 3;
                                    } else if ($water_level_check > 5.35 && $water_level_check <= 5.45) {
                                        $cusec = $row21["5_3"] + 3;
                                    } else if ($water_level_check > 5.45 && $water_level_check <= 5.55) {
                                        $cusec = $row21["5_4"] + 3;
                                    } else if ($water_level_check > 5.55 && $water_level_check <= 5.65) {
                                        $cusec = $row21["5_5"] + 3;
                                    } else if ($water_level_check > 5.65 && $water_level_check <= 5.75) {
                                        $cusec = $row21["5_6"] + 3;
                                    } else if ($water_level_check > 5.75 && $water_level_check <= 5.85) {
                                        $cusec = $row21["5_7"] + 3;
                                    } else if ($water_level_check > 5.85 && $water_level_check <= 5.95) {
                                        $cusec = $row21["5_8"] + 3;
                                    } else if ($water_level_check > 5.95 && $water_level_check <= 6.05) {
                                        $cusec = $row21["5_9"] + 3;
                                    } else if ($water_level_check > 6.05 && $water_level_check <= 6.15) {
                                        $cusec = $row21["6_0"] + 3;
                                    } else if ($water_level_check > 6.15 && $water_level_check <= 6.25) {
                                        $cusec = $row21["6_1"] + 3;
                                    } else if ($water_level_check > 6.25 && $water_level_check <= 6.35) {
                                        $cusec = $row21["6_2"] + 3;
                                    } else if ($water_level_check > 6.35 && $water_level_check <= 6.45) {
                                        $cusec = $row21["6_3"] + 3;
                                    } else if ($water_level_check > 6.45 && $water_level_check <= 6.55) {
                                        $cusec = $row21["6_4"] + 3;
                                    } else if ($water_level_check > 6.55 && $water_level_check <= 6.65) {
                                        $cusec = $row21["6_5"] + 3;
                                    } else if ($water_level_check > 6.65 && $water_level_check <= 6.75) {
                                        $cusec = $row21["6_6"] + 3;
                                    } else if ($water_level_check > 6.75 && $water_level_check <= 6.85) {
                                        $cusec = $row21["6_7"] + 3;
                                    } else if ($water_level_check > 6.85) {
                                        $cusec = $row21["6_8"] + 3;
                                    }
                                }
                            } else {
//                                echo '<br>$Q_Flow '.$Q_Flow;
                                $cusec = number_format($Q_Flow * $Cusec_1_unit, 2);
//                                echo '<br>$cusec '.$cusec;
                            }

                            $water_level = $row1["water_level"];

                            $meter_name = $row1["meter_name"];
                            $address = $row1["address"];
                            $water_level = $water_level;
//                            $Q_Flow = $Q_Flow;
//                            $Velocity = $Velocity;
//                            $cusec = $cusec;
                            
                            $created_date = $row1["created_date_new"];

                            $index["id"] = $row["id"];
                            $index["flow_meter_id"]=$row1["flow_meter_id"];
                            $index["user_id"] = $user_id;
                            $index["meter_name"] = $meter_name;
                            $index["address"] = $address;
                            $index["water_level"] = $water_level;
                            $index["Q_Flow"] = $Q_Flow;
                            $index["Velocity"] = $Velocity;
                            $index["cusec"] = $cusec;
                            $index["latitude"] = $row1["latitude"];
                            $index["longitude"] = $row1["longitude"];
                            $index["created_date"] = $created_date;

                            array_push($result_data, $index);
//                        }
//                    }
                }
            }
        }
    }

    echo json_encode($result_data);
} else {
    echo 'Username is Null';
}
?>
