<?php

require_once './connection.php';

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

    $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');
    $notification = 0;
    $result = mysqli_query($con, $query);
    if (mysqli_num_rows($result) > 0) {
        $i = 0;
        $open_canal = 0;
        $close_canal = 0;
        while ($row = mysqli_fetch_assoc($result)) {
            $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 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 {
                        if ($row1["water_level"] >= 0.20) {   //1 days
//                            $open_canal = $open_canal + 1;
//                            $notification = $notification + 1;
////                            $index['desc_eng'] = $row1['meter_name'] . " stop working last " . $Days . " Days.";
////                            $index['desc_guj'] = $row1['meter_name'] . " કેનાલ છેલ્લા " . $Days . " દિવસથી બંધ છે.";
//                            $index['desc_eng'] = $row1['meter_name'];
//                            $index['desc_guj'] = $row1['meter_name'];
//                            $index["notification"] = $notification;
                            $index['desc_eng'] = "";
                            $index['desc_guj'] = "";
                            $index["notification"] = "";
                        } else {
                            $close_canal = $close_canal + 1;
                        }
                    }
                }
            }
            $i = $i + 1;
        }
//        $index["total_canal"] = $i;
//        $index["open_canal"] = $open_canal;
//        $index["close_canal"] = $close_canal;
//        array_push($result_data, $index);
        array_push($result_data, $index);
    }

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