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


if (!isset($_POST['taluka_id']) == "") {
    session_start();
    $taluka_id = $_POST['taluka_id'];

    $query = 'SELECT MAX(tfs.id) as id FROM `tbl_freez_status` as tfs LEFT JOIN tbl_school_user as tsu ON tfs.user_id=tsu.id left join tbl_school as ts on tsu.school_id=ts.id LEFT JOIN tbl_village as tv on ts.village_id=tv.id WHERE tfs.flag=1 and tv.taluka_id='.$taluka_id.' GROUP BY tfs.user_id ORDER by tfs.id asc';
    mysqli_set_charset($con, 'utf8');
    $result = mysqli_query($con, $query);
    if (mysqli_num_rows($result) > 0) {
        $i = 1;
        while ($row = mysqli_fetch_assoc($result)) {

            $id = $row["id"];

            $query1 = 'SELECT twts.id,twts.user_id,twts.freez_temperature,twts.freez_humidity,twts.comment,DATE_FORMAT(twts.created_date,"%d-%m-%Y %H:%i:%s") AS created_date,DATE_FORMAT(twts.created_date,"%Y-%m-%d") AS days_date,tsu.full_name,tsu.mobile_no,ts.school_name,tv.village,tv.village_hindi,tv.village_guj FROM `tbl_freez_status` as twts LEFT join tbl_school_user as tsu on twts.user_id=tsu.id left join tbl_school as ts on tsu.school_id=ts.id left join tbl_village as tv on ts.village_id=tv.id where twts.flag=1 and twts.id=' . $id . ' order by twts.id DESC LIMIT 0,1';
            mysqli_set_charset($con, 'utf8');
            $result1 = mysqli_query($con, $query1);
            if (mysqli_num_rows($result) > 0) {
                while ($row1 = mysqli_fetch_assoc($result1)) {
                    ?>
                    <tr id="<?php echo $row1["user_id"]; ?>" class="view_school_tank_status">
                        <td><?php echo $i; ?></td>
                        <td>

                            <div class="d-inline-block align-middle">
                                <h6><?php echo $row1["school_name"]; ?></h6>
                                <p class="text-muted m-b-0"><?php echo $row1["village_guj"]; ?></p>
                            </div>
                        </td>
                        <td><?php echo $row1["freez_temperature"]; ?></td>
                        <td><?php echo $row1["freez_humidity"]; ?></td>
                        <td><?php echo $row1["created_date"]; ?></td>

                    <?php
                    $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));
                    ?>

                        <td class="text-c-blue">
                            <!--<div class="btn-group " role="group" data-toggle="tooltip" data-placement="top" data-original-title=".btn-xlg">-->
                    <?php
                    if ($Days > 3) {
                        ?>


                        <?php
                        if ($_SESSION['User_language'] == 'ENGLISH') {
                            ?>
                                    <label class="btn-danger btn-sm">Offline</label>
                                    <?php
                                }

                                if ($_SESSION['User_language'] == 'GUJARATI') {
                                    ?>
                                    <label class="btn-danger btn-sm">ઑફલાઇન</label>
                                    <?php
                                }
                                ?>

                                <?php
                            } else {
                                ?>


                        <?php
                        if ($_SESSION['User_language'] == 'ENGLISH') {
                            ?>
                                    <label class=" btn-success btn-sm">Online</label>
                                    <?php
                                }

                                if ($_SESSION['User_language'] == 'GUJARATI') {
                                    ?>
                                    <label class=" btn-success btn-sm">ઓનલાઈન</label>
                                    <?php
                                }
                                ?>
                                <?php
                            }
                            ?>



                            <!--</div>-->
                        </td>
                    </tr>
                    <?php
                }
            }
            $i = $i + 1;
        }
    }
} else {
    echo 'else';
}