<?php
require_once './connection.php';

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

if (!isset($_POST['canal_id']) == "") {
    session_start();
    $canal_id = $_POST['canal_id'];
    $start_date123 = $_POST['start_date'];
    $to_date = $_POST['to_date'];

    $date = date_create($start_date123);
    $date_start_date = date_format($date, "d/m/Y");
    $en_date1 = date_create($to_date);
    $date_end_date = date_format($en_date1, "d/m/Y");
//    echo '$canal_id ' . $canal_id;
    ?>

    <table class="table table-bordered" id="device_wise_export_report">


        <?php
        $i = 1;

        $st_dt = $start_date123;
        $en_dt = $to_date;
//            echo '$st_dt ' . $st_dt;
//            echo '$en_dt ' . $en_dt;
//                          
        $startDate = new DateTime($st_dt); // Start date                                    
//            $startDate = $start_date; // Start date
        $endDate = new DateTime($en_dt);   // End date
//            $endDate = $to_date;   // End date
// Create an array to store the dates between start and end date
        $dateRange = [];

//            echo '$startDate ' . $startDate;
//            echo '$endDate ' . $endDate;
// Loop to add each date between the start and end date
        while ($startDate <= $endDate) {
            // Add the current date to the array
            $dateRange[] = $startDate->format('Y-m-d'); // Format the date as 'YYYY-MM-DD'
            // Move to the next date
            $startDate->modify('+1 day');
        }

        $arr_cnt = count($dateRange);
        if ($arr_cnt != 1) {
            ?>

            <thead>
                <tr>
                    <th colspan="8" class="text-center text-uppercase" style="font-size: 22px;"> Canal  monitoring system</th>
                </tr>

                <tr>
                    <th colspan="8" class="text-center text-uppercase" style="font-size: 15px;font-weight: bolder;">Canal Wise Log Report <span id="show_from_date"><?php echo $date_start_date; ?></span> <span id="to">To</span> <span id="show_to_date"><?php echo $date_end_date; ?></span></th>
                </tr>

                <tr class="text-center">
                    <th style="width:10%;">Sr No.</th>
                    <th>Canal Name</th>
                    <th>Date</th>
                    <!--<th>Water Level </th>-->
                    <!--<th>Gate Open </th>-->
                    <!--<th>Water Flow </th>-->
                    <!--<th>Discharge Water</th>-->
                    <th>Discharge Water</th>


                </tr> 
            </thead>
            <tbody id="table_data" class="text-center">
                <?php
// Display the dates
//                                                                echo "<h2>Dates between the selected range:</h2>";
                $total_cusec = 0;
                $total_time = 0;
                $cnt_array = count($dateRange);
                foreach ($dateRange as $date) {
//                                                                    echo $date . "<br>";
                    $start_date = $date . " 00:00:02";
                    $end_date = $date . " 23:59:59";
//                                                                    $start_date = $date;
//                                                                    $end_date = $date;
//                echo "<br>" . $start_date . "<br>" . $end_date;

                    $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 FROM `tbl_flow_meter_data` as tfmd LEFT join tbl_flow_meter as tfm on tfm.id=tfmd.flow_meter_id WHERE tfmd.created_date BETWEEN "' . $start_date . '" AND "' . $end_date . '" and tfmd.flow_meter_id=' . $canal_id . ' and tfmd.flag=1 ORDER by tfmd.created_date asc';
//                                                                    $query1='SELECT * FROM `tbl_flow_meter_data` WHERE created_date BETWEEN "'.$start_date.'" AND "'.$end_date.'"';
//                    echo '$query1 ' . $query1;
                    mysqli_set_charset($con, 'utf8');
                    $result1 = mysqli_query($con, $query1);
                    if (mysqli_num_rows($result1) > 0) {
                        $total_water_flow = 0;
                        $cnt_flow = 0;
                        $cnt_low_level = 0;
                        $cnt_up_level = 0;
                        $total_cusec_1 = 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"];

                            $bottom_width = $row1["canal_bottom_width"];
//                                                                                            echo '<br>$bottom_width ' . $bottom_width;
                            $side_slop = $row1["side_slop"];
//                            $side_slop = 1.5; //for canal messurement wise
//                                                                                            echo '<br>side_slop ' . $side_slop;
                            $channel_slop = $row1["channel_slop"];
//                            $channel_slop = 0.000055; //for canal messurement wise
//                                                                                            echo '<br>channel_slop ' . $channel_slop;
                            $m_r_coefficient = $row1["m_r_coefficient"];
//                            $m_r_coefficient = 0.015; //for canal messurement wise
//                                                                                            echo '<br>m_r_coefficient ' . $m_r_coefficient;
                            $canal_top_width = $row1["canal_top_width"];
//                                                                                            echo '<br>canal_top_width  ' . $canal_top_width;
//                            $water_level = $row1["canal_height"] - $row1["water_level"];
                            $water_level = $row1["water_level"];
//                                                                                            $water_level = 1.30;
//                                                                                            echo '<br>$water_level  ' . $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;
//                                                                                            echo '<br><br>$Area  ' . $Area;

                            $pw0 = $bottom_width;
                            $pw1 = 2 * $water_level;
                            $pw_side_slop = $side_slop * $side_slop;
//                                                                                            echo '<br>$pw_side_slop  ' . $pw_side_slop;
                            $pw2 = 1 + $pw_side_slop;
//                                                                                            echo '<br>$pw2  ' . $pw2;
//                                                                                            $pw2_squre= sqrt($pw2);
                            $pw2_squre = number_format(sqrt($pw2), 4);
//                                                                                            echo '<br>$pw2_squre  ' . $pw2_squre;
                            $pw1_squre = number_format($pw1 * $pw2_squre, 4);
//                                                                                            echo '<br>$pw1_squre  ' . $pw1_squre;
                            $wetted_perimeter = $pw0 + $pw1_squre;
//                                                                                            echo '<br>$wetted_perimeter  ' . $wetted_perimeter;
//                                                                                            echo '<br>$wetted_perimeter  ' .number_format($wetted_perimeter, 4); // 2.50

                            $hydraulic_radius = $Area / $wetted_perimeter;
                            $R = number_format($hydraulic_radius, 3);
//                                                                                            echo '<br>hydraulic_radius $R  ' . $R;
//                            $S12 = number_format(sqrt($channel_slop), 3);
////                                                                                            echo '<br>$S12  ' . $S12;
//
//                            $R23_squre = number_format($R * $R, 4);
////                                                                                            echo '<br>$R23_squre  ' . $R23_squre;
//
//                            $R23_cube_root = number_format(pow($R23_squre, 1 / 3), 4);
////                                                                                            echo '<br>$R23_cube_root  ' . $R23_cube_root;
//
//                            $Q_0 = number_format(1 / $m_r_coefficient, 3);
//                            $Q_1 = number_format($Q_0 * $Area, 3);
////                                                                                            echo '<br>$Q_0  ' . $Q_0;
////                                                                                            echo '<br>$Q_1  ' . $Q_1;
//                            $Q_2 = number_format($R * $S12, 4);
////                                                                                            echo '<br>$Q_2  ' . $Q_2;
//                            $Q_Flow = number_format($Q_1 * $Q_2, 4);
//                                                                                            echo '<br>$Q_Flow  ' . $Q_Flow;
//                                                                                            $Velocity=number_format($Q_Flow/$Area, 4);
//                                                                                            echo '<br>$Velocity  ' . $Velocity;

                            $S = $channel_slop; // slope
// Manning's formula for velocity
                            $V = (1 / $m_r_coefficient) * pow($R, 2 / 3) * sqrt($S);

// Discharge formula Q = A * V
                            $Q = $Area * $V;

//                                                                                                    echo "<br>Velocity (V): " . $V . " m/s\n";
//                                                                                                    echo "<br>Discharge (Q): " . $Q . " m^3/s\n";

                            $Q_Flow = number_format($Q, 4);
////                                                                                            echo '<br>$Q_Flow  ' . $Q_Flow;
                            $Velocity = number_format($V, 2);

                            $Cusec_1_unit = 35.3147;
                            $liter_1_unit = 1000;

                            $meters = $water_level; // Example value in meters
                            $feet = metersToFeet($meters);
                            // echo "<br>$meters meters is equal to " . $feet . " feet.";
//// Output the result
                            // echo "<br>$meters meters is equal to " . number_format($feet, 2) . " feet.";
                            $water_level_check = $feet;

                            $cusec = 0;

                            $location_id = $row1["flow_meter_id"];

                            $query21 = 'SELECT * FROM `tbl_canal_measure_data` WHERE location=' . $location_id . ' and flag=1 ORDER by id DESC LIMIT 0,1';
//                    echo '$query1 ' . $query1;
                            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;
                            }


                            $now = time(); // or your date as well
                            $your_date = strtotime($row1["days_date"]);
                            $datediff = $now - $your_date;

//                                                                                            echo round($datediff / (60 * 60 * 24));
                            $Days = round($datediff / (60 * 60 * 24));

//                        $total_water_flow = $total_water_flow + $Q_Flow;
                            $cnt_flow = $cnt_flow + 1;

                            if ($check_on_off > 0.20) {
                                $cnt_up_level = $cnt_up_level + 1;
                                $total_water_flow = $total_water_flow + $Q_Flow;
                                $water_level_up = $row1["water_level"];
                                $canal_height_up = $row1["canal_height"];
                            } else {
                                $cnt_low_level = $cnt_low_level + 1;
                            }
//                                                                            echo '<br>' . $Q_Flow . ' ' . $date;
//                                                                            echo '<br>' . $total_water_flow;
//                                                                            echo '<br>'.$total_water_flow;
//                        $total_water_flow = $total_water_flow + $Q_Flow;
//                        $cnt_flow = $cnt_flow + 1;

                            $meter_name = $row1["meter_name"];
                            $address = $row1["address"];
                            $water_level = $row1["water_level"];
                            $canal_height = $row1["canal_height"];

//                        echo '<br>'.$canal_height - $water_level . ' Meter '.$start_date;

                            $cusec = (float) str_replace(',', '', $cusec);

                            $minit5_cusec = $cusec * 300; //5 minit 60 seconds=5*60=300
//                                $total_cusec = $total_cusec + number_format($cusec, 2);
                            $total_cusec_1 = $total_cusec_1 + $minit5_cusec;
//                            echo '<br>$minit5_cusec ' . $minit5_cusec;
//                            echo '<br>$water_level ' . $water_level;
//                            echo '<br>$cusec ' . $cusec;
//                            echo '<br>$total_cusec_1 ' . $total_cusec_1;
                        }
//                        echo '<br>$minit5_cusec ' . $minit5_cusec;
//                        echo '<br>$total_cusec_1 ' . $total_cusec_1;
                        $total_cusec = $total_cusec + $total_cusec_1;

//                        echo '<br>$total_cusec ' . $total_cusec;
//                        echo '<br>';

                        $date = new DateTime($date); // YYYY-MM-DD format
// Format the date in 'dd-mm-yyyy'
                        $formattedDate = $date->format('d-m-Y');

//                    echo '<br>$total_water_flow ' . $total_water_flow;
//                    echo '<br>$cnt_flow ' . $cnt_flow;
//                    echo '<br>$Q_Flow ' . $Q_Flow;
//                    echo '<br>$cnt_low_level ' . $cnt_low_level;
//                    echo '<br>$cnt_up_level ' . $cnt_up_level;
//                    echo '<br>$canal_height ' . $canal_height . ' $water_level ' . $water_level;
//                    echo '<br>$canal_height ' . $canal_height - $water_level;

                        if ($cnt_up_level != 0) {

                            if ($total_water_flow == 0) {
                                $one_second_water_flow = 0;
                            } else {

//                                                                                $tf = $total_water_flow / $cnt_flow;
//                                                                                $one_second_water_flow = number_format($tf, 4);
//                                                                                $Q_Flow_up;


                                $tf = $total_water_flow / $cnt_up_level;
//                            echo '<br>$average tf ' . $tf;
                                $one_second_water_flow = number_format($tf, 4);
//                            echo '<br>$one_second_water_flow ' . $one_second_water_flow;

                                $total_Second = 5 * $cnt_up_level * 60;   // 5 fot 5 minit interval   , 60 for second
                                $total_minit = $total_Second / 3600;
//                                echo '<br>Day Cusec ' . $one_second_water_flow . ' ' . $Cusec_1_unit . ' ' . $total_Second;
//                                $total_cusec = $total_cusec + $one_second_water_flow * $Cusec_1_unit * $total_Second;
//                                $total_cusec = $total_cusec + $cusec * $total_Second;
                                $total_time = $total_time + $total_minit;
                            }
                            ?>
                            <tr>
                                <td><?php echo $i; ?></td>
                                <td>

                                    <div class="d-inline-block align-middle">
                                        <h6><?php echo $meter_name; ?></h6>
                                        <p class="text-muted m-b-0"><?php echo $address; ?></p>
                                    </div>
                                </td>                            
                                <td><?php echo $formattedDate; ?></td>
                                <!--<td><?php // echo number_format($canal_height_up - $water_level_up, 2) . ' Meter';       ?></td>-->
                                <!--<td><?php // echo $total_minit . ' Hour';               ?></td>-->
                                <!--<td><?php // echo $cusec . ' Cusec/Second';       ?></td>-->
                                <!--<td><?php // echo number_format($one_second_water_flow * $Cusec_1_unit, 2) . ' Cusec/Second';        ?></td>-->
                                <td><?php echo number_format($total_cusec_1, 2) . ' Cusec/Day'; ?></td>
                            </tr>
                            <?php
                        } else {
                            ?>
                            <tr>
                                <td><?php echo $i; ?></td>
                                <td>

                                    <div class="d-inline-block align-middle">
                                        <h6><?php echo $meter_name; ?></h6>
                                        <p class="text-muted m-b-0"><?php echo $address; ?></p>
                                    </div>
                                </td>                            
                                <td><?php echo $formattedDate; ?></td>
                    <!--                                <td><?php echo '0 Meter'; ?></td>
                                <td><?php echo '0 Hour'; ?></td>
                                <td><?php echo '0 m3/s'; ?></td>
                                <td><?php echo '0 Cusec/Second'; ?></td>-->
                                <td><?php echo '0 Cusec/Day'; ?></td>
                            </tr>
                            <?php
                        }
                        $i = $i + 1;
                    }
                }

                if ($total_time <= 24) {
                    $total_time_data = $total_time . " Hour";
                } else {
                    $tt = $total_time / 24;
                    $total_time_data = number_format($tt, 2) . " Days";
                }
                ?>
                <tr>
                    <td></td>
                    <td>

                        <div class="d-inline-block align-middle">
                            <h6></h6>
                            <p class="text-muted m-b-0"></p>
                        </div>
                    </td>                            
                    <td><b><b>Total Discharge</b></td>
                    <!--<td></td>-->
                    <!--<td></td>-->
                    <!--<td><b><?php // echo $total_time_data;         ?></b></td>-->
                    <!--<td></td>-->
                    <!--<td></td>-->
                    <td><b><?php echo number_format($total_cusec, 2) . ' Cusec/Day'; ?></b></td>
                </tr>                       
                <?php
            } else {
                ?>
            <thead>
                <tr>
                    <th colspan="7" class="text-center text-uppercase" style="font-size: 22px;"> Canal  monitoring system</th>
                </tr>

                <tr>
                    <th colspan="7" class="text-center text-uppercase" style="font-size: 15px;font-weight: bolder;">Canal Wise Log Report <span id="show_from_date"><?php echo $date_start_date; ?></span> <span id="to">To</span> <span id="show_to_date"><?php echo $date_end_date; ?></span></th>
                </tr>

                <tr class="text-center">
                    <th style="width:10%;">Sr No.</th>
                    <th>Canal Name</th>
                    <th>Date</th>
                    <th>Water Level </th>
                    <th>Water Flow </th>
                    <th>Discharge Water</th>


                </tr> 
            </thead>
            <tbody id="table_data" class="text-center">
                <?php
                $total_cusec = 0;
                foreach ($dateRange as $date) {
//                                                                    echo $date . "<br>";
                    $start_date = $date . " 00:00:02";
                    $end_date = $date . " 23:59:59";
//                                                                    $start_date = $date;
//                                                                    $end_date = $date;
//                echo "<br>" . $start_date . "<br>" . $end_date;

                    $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 FROM `tbl_flow_meter_data` as tfmd LEFT join tbl_flow_meter as tfm on tfm.id=tfmd.flow_meter_id WHERE tfmd.created_date BETWEEN "' . $start_date . '" AND "' . $end_date . '" and tfmd.flow_meter_id=' . $canal_id . ' and tfmd.flag=1 ORDER by tfmd.created_date asc';
//                                                                    $query1='SELECT * FROM `tbl_flow_meter_data` WHERE created_date BETWEEN "'.$start_date.'" AND "'.$end_date.'"';
//                                                                    echo '$query1 ' . $query1;
                    mysqli_set_charset($con, 'utf8');
                    $result1 = mysqli_query($con, $query1);
                    if (mysqli_num_rows($result1) > 0) {
                        $total_water_flow = 0;
                        $cnt_flow = 0;
                        $cnt_low_level = 0;
                        $cnt_up_level = 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"];

                            $bottom_width = $row1["canal_bottom_width"];
//                                                                                            echo '<br>$bottom_width ' . $bottom_width;
                            $side_slop = $row1["side_slop"];
//                            $side_slop = 1.5; //for canal messurement wise
//                                                                                            echo '<br>side_slop ' . $side_slop;
                            $channel_slop = $row1["channel_slop"];
//                            $channel_slop = 0.000055; //for canal messurement wise
//                                                                                            echo '<br>channel_slop ' . $channel_slop;
                            $m_r_coefficient = $row1["m_r_coefficient"];
//                            $m_r_coefficient = 0.015; //for canal messurement wise
//                                                                                            echo '<br>m_r_coefficient ' . $m_r_coefficient;
                            $canal_top_width = $row1["canal_top_width"];
//                                                                                            echo '<br>canal_top_width  ' . $canal_top_width;
//                            $water_level = $row1["canal_height"] - $row1["water_level"];
                            $water_level = $row1["water_level"];

//                                                                                            $water_level = 1.30;
//                                                                                            echo '<br>$water_level  ' . $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;
//                                                                                            echo '<br><br>$Area  ' . $Area;

                            $pw0 = $bottom_width;
                            $pw1 = 2 * $water_level;
                            $pw_side_slop = $side_slop * $side_slop;
//                                                                                            echo '<br>$pw_side_slop  ' . $pw_side_slop;
                            $pw2 = 1 + $pw_side_slop;
//                                                                                            echo '<br>$pw2  ' . $pw2;
//                                                                                            $pw2_squre= sqrt($pw2);
                            $pw2_squre = number_format(sqrt($pw2), 4);
//                                                                                            echo '<br>$pw2_squre  ' . $pw2_squre;
                            $pw1_squre = number_format($pw1 * $pw2_squre, 4);
//                                                                                            echo '<br>$pw1_squre  ' . $pw1_squre;
                            $wetted_perimeter = $pw0 + $pw1_squre;
//                                                                                            echo '<br>$wetted_perimeter  ' . $wetted_perimeter;
//                                                                                            echo '<br>$wetted_perimeter  ' .number_format($wetted_perimeter, 4); // 2.50

                            $hydraulic_radius = $Area / $wetted_perimeter;
                            $R = number_format($hydraulic_radius, 3);
//                                                                                            echo '<br>hydraulic_radius $R  ' . $R;
//                            $S12 = number_format(sqrt($channel_slop), 3);
////                                                                                            echo '<br>$S12  ' . $S12;
//
//                            $R23_squre = number_format($R * $R, 4);
////                                                                                            echo '<br>$R23_squre  ' . $R23_squre;
//
//                            $R23_cube_root = number_format(pow($R23_squre, 1 / 3), 4);
////                                                                                            echo '<br>$R23_cube_root  ' . $R23_cube_root;
//
//                            $Q_0 = number_format(1 / $m_r_coefficient, 3);
//                            $Q_1 = number_format($Q_0 * $Area, 3);
////                                                                                            echo '<br>$Q_0  ' . $Q_0;
////                                                                                            echo '<br>$Q_1  ' . $Q_1;
//                            $Q_2 = number_format($R * $S12, 4);
////                                                                                            echo '<br>$Q_2  ' . $Q_2;
//                            $Q_Flow = number_format($Q_1 * $Q_2, 4);
//                                                                                            echo '<br>$Q_Flow  ' . $Q_Flow;
//                                                                                            $Velocity=number_format($Q_Flow/$Area, 4);
//                                                                                            echo '<br>$Velocity  ' . $Velocity;

                            $S = $channel_slop; // slope
// Manning's formula for velocity
                            $V = (1 / $m_r_coefficient) * pow($R, 2 / 3) * sqrt($S);

// Discharge formula Q = A * V
                            $Q = $Area * $V;

//                                                                                                    echo "<br>Velocity (V): " . $V . " m/s\n";
//                                                                                                    echo "<br>Discharge (Q): " . $Q . " m^3/s\n";

                            $Q_Flow = number_format($Q, 4);
////                                                                                            echo '<br>$Q_Flow  ' . $Q_Flow;
                            $Velocity = number_format($V, 2);

                            $Cusec_1_unit = 35.3147;
                            $liter_1_unit = 1000;

                            $now = time(); // or your date as well
                            $your_date = strtotime($row1["days_date"]);
                            $datediff = $now - $your_date;

//                                                                                            echo round($datediff / (60 * 60 * 24));
                            $Days = round($datediff / (60 * 60 * 24));

//                        $total_water_flow = $total_water_flow + $Q_Flow;
                            $cnt_flow = $cnt_flow + 1;

//                                                                            echo '<br>' . $Q_Flow . ' ' . $date;
//                                                                            echo '<br>' . $total_water_flow;
//                                                                            echo '<br>'.$total_water_flow;
//                        $total_water_flow = $total_water_flow + $Q_Flow;
//                        $cnt_flow = $cnt_flow + 1;

                            $meter_name = $row1["meter_name"];
                            $address = $row1["address"];
                            $water_level = $row1["water_level"];
                            $canal_height = $row1["canal_height"];

//                            $date = new DateTime($date); // YYYY-MM-DD format
//// Format the date in 'dd-mm-yyyy'
//                            $formattedDate = $date->format('d-m-Y');
                            // if ($check_on_off > 0.20) {

                            $water_level_up = $row1["water_level"];
                            $canal_height_up = $row1["canal_height"];

//                                $meters = $canal_height_up - $water_level_up; // Example value in meters
                            $meters = $water_level_up; // Example value in meters
                            $feet = metersToFeet($meters);
//                             echo "<br>$meters meters is equal to " . $feet . " feet.";
//// Output the result
                            // echo "<br>$meters meters is equal to " . number_format($feet, 2) . " feet.";
                            $water_level_check = $feet;

                            $cusec = 0;

                            $location_id = $row1["flow_meter_id"];

                            $query21 = 'SELECT * FROM `tbl_canal_measure_data` WHERE location=' . $location_id . ' and flag=1 ORDER by id DESC LIMIT 0,1';
//                    echo '$query1 ' . $query1;
                            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;
                            }
// Remove commas and convert to float
                            $cusec = (float) str_replace(',', '', $cusec);
//                                $total_cusec = $total_cusec + number_format($Q_Flow * $Cusec_1_unit, 4);
                            $minit5_cusec = $cusec * 300; //5 minit 60 seconds=5*60=300
//                                $total_cusec = $total_cusec + number_format($cusec, 2);
                            $total_cusec = $total_cusec + $minit5_cusec;
//                                echo '<br><br>$minit5_cusec '.$minit5_cusec;
//                                echo '<br>$total_cusec '.$total_cusec;
                            ?>
                            <tr>
                                <td><?php echo $i; ?></td>
                                <td>

                                    <div class="d-inline-block align-middle">
                                        <h6><?php echo $meter_name; ?></h6>
                                        <p class="text-muted m-b-0"><?php echo $address; ?></p>
                                    </div>
                                </td>                            
                                <td><?php echo $row1["created_date_new"]; ?></td>
                                <!--<td><?php // echo number_format($canal_height_up - $water_level_up, 2) . ' Meter';       ?></td>-->
                                <td><?php echo number_format($water_level_up, 2) . ' Meter'; ?></td>
                                <td><?php echo number_format($Q_Flow, 2) . ' m3/s'; ?></td>
                                <td><?php echo number_format($cusec, 2) . ' Cusec/Second'; ?></td>
                            </tr>
                            <?php
                            // } else {
                            $cnt_low_level = $cnt_low_level + 1;
                            ?>
                            <!--<tr>-->
                            <!--    <td><?php echo $i; ?></td>-->
                            <!--    <td>-->

                            <!--        <div class="d-inline-block align-middle">-->
                            <!--            <h6><?php echo $meter_name; ?></h6>-->
                            <!--            <p class="text-muted m-b-0"><?php echo $address; ?></p>-->
                            <!--        </div>-->
                            <!--    </td>                            -->
                            <!--    <td><?php echo $row1["created_date_new"]; ?></td>-->
                            <!--    <td><?php echo '0 Meter'; ?></td>-->
                            <!--    <td><?php echo '0 m3/s'; ?></td>-->
                            <!--    <td><?php echo '0 Cusec/Second'; ?></td>-->
                            <!--</tr>-->
                            <?php
                            // }
//                            echo '<br>' . $canal_height - $water_level . ' Meter ' . $start_date;
                            $i = $i + 1;
                        }
                        ?>
                        <tr>
                            <td></td>
                            <td>

                                <div class="d-inline-block align-middle">
                                    <h6></h6>
                                    <p class="text-muted m-b-0"></p>
                                </div>
                            </td>                            
                            <td><b><?php echo $date_start_date; ?></b></td>
                            <!--<td></td>-->
                            <td colspan="2"><b>Total Discharge Water</b></td>
                            <td><b><?php echo number_format($total_cusec, 2) . ' Cusec/Day'; ?></b></td>
                        </tr>                       
                        <?php
                    }
                }
            }
//            echo '<br>$total_cusec '.$total_cusec;
            ?>

                                                                                <!--<tr><td class="text-danger" style="text-align:center;" colspan="7"><?php echo $total_cusec; ?></td></tr></tbody>-->

    </table>


    <?php
}
?>