<?php
error_reporting(0);
if (isset($_POST['CotCode'])) {
    if($_POST['CotCode'] != "") {
    require $_SERVER['DOCUMENT_ROOT']."/personal-access/functions/connect.php";
    $CotCode = $_POST['CotCode'];
    $transactionID =  $_POST['transactionID'];
    $Level = $_POST['level'];

    // GET TRANSACTION ID 

    $getTransID = mysqli_query($connect, "SELECT * FROM transactions ORDER BY ID DESC LIMIT 0,1 ");
    $fetchID = mysqli_fetch_assoc($getTransID);
    $transactionID = $fetchID['ID'];


    // level 1 

    if($Level == 1) {
        

        $checkCOTcode = mysqli_query($connect, "SELECT * FROM tracktrans WHERE TransID = '$transactionID' AND Code = '$CotCode'");
        $countRows = mysqli_num_rows($checkCOTcode);

        if($countRows > 0) {
            echo "true";
            mysqli_query($connect, "UPDATE tracktrans SET Level = 2, Code = '' WHERE TransID = '$transactionID' AND Code = '$CotCode'");
        } else {
            echo "false";
        }
    }
    // level 2
    elseif($Level == 2) {
  
        $checkCOTcode = mysqli_query($connect, "SELECT * FROM tracktrans WHERE TransID = '$transactionID' AND Code = '$CotCode'");
        $countRows = mysqli_num_rows($checkCOTcode);

        if($countRows > 0) {
            echo "true";
            mysqli_query($connect, "UPDATE tracktrans SET Level = 3, Code = '' WHERE TransID = '$transactionID' AND Code = '$CotCode'");
        } else {
            echo "false";
        }
    }
    // level 3

    elseif($Level == 3) {

        $checkCOTcode = mysqli_query($connect, "SELECT * FROM tracktrans WHERE TransID = '$transactionID' AND Code = '$CotCode'");
        $countRows = mysqli_num_rows($checkCOTcode);

        if($countRows > 0) {
            echo "true";
            mysqli_query($connect, "UPDATE tracktrans SET Level = 4, Code = '' WHERE TransID = '$transactionID' AND Code = '$CotCode'");
            mysqli_query($connect, "UPDATE transactions SET Status = 1 WHERE ID = '$transactionID'");

           
        } else {
            echo "false";
        }
    }




    }else {
        echo "empty";
    }
	

}else {
	header("Location: /personal/");
}
