<?php
/**
 * Created by PhpStorm.
 * User: Sarada
 * Date: 2018-11-30
 * Time: 3:28 PM
 */

session_start();
include("db/db.php");

date_default_timezone_set('Asia/Colombo');
$datetime = date('Y-m-d H:i:s', time());

if ($_POST['read'] == 1) {
    // change read and unread
    if ($_POST['readehino']) {

        $ehino = $_POST['readehino'];

        $get_read_report = "SELECT
                          vehical.read
                        FROM
                            vehical
                        WHERE
                            vehical.Ehino = '" . $ehino . "'";

        $result1 = mysqli_query($db, $get_read_report);

        if ($result1->num_rows > 0) {

            while ($row1 = $result1->fetch_assoc()) {
                $read_report = $row1['read'];
            }

            if ($read_report == 1) {
                // die('yep');
                $sql1 = "UPDATE
                      vehical
                    SET
                      vehical.read = '0' , read_datetime = '" . $datetime . "'
                    WHERE
                      vehical.Ehino = '" . $ehino . "'";
            } else {
                // die('no');
                $sql1 = "UPDATE
                      vehical
                    SET
                      vehical.read = '1' , read_datetime = '" . $datetime . "'
                    WHERE
                      vehical.Ehino = '" . $ehino . "'";
            }
            mysqli_query($db, $sql1);


            //header("Refresh:0");

        } else {
            //$abc = "null";
            // die('gone');
        }


    }

    if ($_POST['arno']) {
        $arno = $_POST['arno'];
    }

} elseif ($_POST['pick'] == 1) {
    // change pick n unpick
    if ($_POST['readehino']) {
        $ehino = $_POST['readehino'];

        $get_pick_report = "SELECT
                          vehical.pick
                        FROM
                            vehical
                        WHERE
                            vehical.Ehino = '" . $ehino . "'";

        $result2 = mysqli_query($db, $get_pick_report);

        if ($result2->num_rows > 0) {

            while ($row2 = $result2->fetch_assoc()) {
                $pick_report = $row2['pick'];
            }

            if ($pick_report == 1) {
                $sql2 = "UPDATE
                      vehical
                    SET
                      vehical.pick = '0' , pick_datetime = '" . $datetime . "'
                    WHERE
                      vehical.Ehino = '" . $ehino . "'";
            } else {
                $sql2 = "UPDATE
                      vehical
                    SET
                      vehical.pick = '1' , pick_datetime = '" . $datetime . "'
                    WHERE
                      vehical.Ehino = '" . $ehino . "'";
            }
            mysqli_query($db, $sql2);

            //header("Refresh:0");

        } else {
            //$abc = "null";
            // die('gone');
        }


    }

    if ($_POST['arno']) {
        $arno = $_POST['arno'];
    }

} elseif ($_POST['drop'] == 1) {
// change drop n undrop
    if ($_POST['readehino']) {

        $ehino = $_POST['readehino'];

        $get_drop_report = "SELECT
                          vehical.drop
                        FROM
                            vehical
                        WHERE
                            vehical.Ehino = '" . $ehino . "'";

        $result3 = mysqli_query($db, $get_drop_report);

        if ($result3->num_rows > 0) {

            while ($row3 = $result3->fetch_assoc()) {
                $drop_report = $row3['drop'];
            }

            if ($drop_report == 1) {
                // die('yep');
                $sql3 = "UPDATE
                      vehical
                    SET
                      vehical.drop = '0' , drop_datetime = '" . $datetime . "'
                    WHERE
                      vehical.Ehino = '" . $ehino . "'";
            } else {
                // die('no');
                $sql3 = "UPDATE
                      vehical
                    SET
                      vehical.drop = '1' , drop_datetime = '" . $datetime . "'
                    WHERE
                      vehical.Ehino = '" . $ehino . "'";
            }
            mysqli_query($db, $sql3);

            //header("Refresh:0");

        } else {
            //$abc = "null";
            // die('gone');
        }

    }

    if ($_POST['arno']) {
        $arno = $_POST['arno'];
    }
}


echo json_encode($ehino);
