<?php

// include($_SERVER['DOCUMENT_ROOT'] . "/_ps2020/config/connect.php");

// $username = "";
// $email = "";

// session_start();

// if (isset($_SESSION['user']) && isset($_SESSION['email'])) {
//     $username = $_SESSION['user'];
//     $email = $_SESSION['email'];
// } else {
//     $username = "---";
//     $email = "---";
// }

// $feedbackType = $_GET['type'];
// $feedbackMessage = $_GET['message'];

// $id_set = false;

// $result = $conn->query("SELECT * FROM feedback");
// if ($result) {
//     $id = 1;
//     while ($row = $result->fetch_assoc()) {
//         if ($id == $row['feedback_id'] && $id_set == false) {  //if 'id' already exist, increment
//             $id++;
//         } else if ($id < $row['feedback_id']) {  //no more increment
//             $id_set = true;
//         }
//     }
// }


// if ($conn->query("INSERT INTO survey VALUES ('$id', '$username', '$email', '$feedbackType')") === TRUE) {
//     echo 'Your Feedback has Been Saved!!';
// }else{
//     echo 'Your Feedback cannot be Saved!!';
// }

// $conn->close();

?>


<?php
    include($_SERVER['DOCUMENT_ROOT'] . "/_ps2020/config/connect.php");

    $newFeedType = $_REQUEST["type"];
    $newFeedMessage = $_REQUEST["message"];
    $username = "----";
    $email = "----";

    //Check if session has been created
    session_start();
    if (isset($_SESSION['user'])) {
        $username = $_SESSION['user'];
        $email = $_SESSION['email'];
    }
    // $newOrder = $_GET["newOrder"] +1;
    // generate 'id'
    $id_set = false;

    $result = $conn->query("SELECT * FROM feedback");
    if ($result) {
        $id = 1;
        while ($row = $result->fetch_assoc()) {

            if ($id == $row['feedback_id'] && $id_set == false) {  //if 'id' already exist, increment
                $id++;
            } else if ($id < $row['feedback_id']) {  //no more increment
                $id_set = true;
            }

        }
    }

    if ($conn->query("INSERT INTO feedback VALUES ('$id','$username', '$email' ,'$newFeedType', '$newFeedMessage')") === TRUE) {
        echo '1';
    }else{
		echo '0';
	}

    $conn->close();
?>