<?php
$data = json_decode(file_get_contents('php://input'));
$email = $data->username;
$firstname = $data->firstname;
$lastname = $data->lastname;
$address = $data->address;
$cart = $data->cart;
$total = $data->total;
$payment = $data->paymentId;


addOrder($email, $total, $payment, $cart);

//addOrdertoDB($user_id, $total, $payment);
//$success = addOrderToDB($user_id, $total, $payment);

/*if ($success) {
    $success = addItemsToOrderItems($cart, $user_id);
    if ($success) {
        echo "success";
    } else {
        echo "failure";
    }
} else {
    $success = false;
    echo "failure";
}*/


function addOrder($email, $total, $payment, $cart) {
    require_once($_SERVER['DOCUMENT_ROOT'] . '/config/setupDBAccess.php');

    //get UserID
    $sql = "SELECT `user_id` FROM `user` WHERE `username` = ?";
    $stmt = $db->prepare($sql);
    $stmt->bind_param("s", $email);

    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($user_id);
    if ($stmt->num_rows == 1) {
        $stmt->fetch();
    } else {
        $user_id = 0;
    }

    //add order to DB
    $sql = "INSERT INTO `order_details` (`user_id`, `total`, `payment_id`) VALUES (?, ?, ?)";
    $stmt = $db->prepare($sql);
    $stmt->bind_param("idi", $user_id, $total, $payment);

    if ($stmt->execute()) {
        $successorderToDB = true;

    } else {
        $successorderToDB = false;
    }

    //getLastInsertID
    $sql = "SELECT LAST_INSERT_ID() FROM `order_details`";
    $stmt = $db->prepare($sql);
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($order_id);
    if ($stmt->num_rows == 1) {
        $stmt->fetch();
    } else {
        $order_id = 0;
    }

    //addItemsToOrderItems
    for ($i = 0; $i < count($cart); $i++) {
        $product = json_decode($cart[$i]);
        $product_id = $product->productid;
        $quantity = $product->count;
        $sql = "INSERT INTO `order_items` (`order_id`, `product_id`, `quantity`) VALUES (?, ?, ?)";
        $stmt = $db->prepare($sql);
        $stmt->bind_param("iii", $order_id, $product_id, $quantity);
        if ($stmt -> execute()) {
            $successaddItemsToOrderItems = true;
        } else {
            $successaddItemsToOrderItems = false;
        }
    }
    $stmt->close();
    $db -> close();
    if ($successorderToDB && $successaddItemsToOrderItems) {
        echo "success";
    } else {
        echo "failure";
    }
}


/*function getIdByEmail($email) {
    require_once($_SERVER['DOCUMENT_ROOT'] . '/config/setupDBAccess.php');
    $sql = "SELECT `user_id` FROM `user` WHERE `username` = ?";
    $stmt = $db->prepare($sql);
    $stmt->bind_param("s", $email);

    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($user_id);
    if ($stmt->num_rows == 1) {
        $stmt->fetch();
    } else {
        $user_id = 0;
    }
    $stmt->close();
    $db->close();

    return $user_id;
}*/

/*function addOrdertoDB($user_id, $total, $payment) {
    require_once($_SERVER['DOCUMENT_ROOT'] . '/config/setupDBAccess.php');

    $sql = "INSERT INTO `order_details` (`user_id`, `total`, `payment_id`) VALUES (?, ?, ?)";
    $stmt = $db->prepare($sql);
    $stmt->bind_param("iis", $user_id, $total, $payment);

    if ($stmt->execute()) {
        return true;
    } else {
        return false;
    }
    $stmt->close();
    $db->close();
}*/

/*function getLastInsertID() {
    require_once($_SERVER['DOCUMENT_ROOT'] . '/config/setupDBAccess.php');
    $sql = "SELECT LAST_INSERT_ID() FROM `order_details`";
    $stmt = $db->prepare($sql);
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($order_id);
    if ($stmt->num_rows == 1) {
        $stmt->fetch();
    } else {
        $order_id = 0;
    }
    $stmt->close();
    $db->close();

    return $order_id;
}*/

/*function addItemsToOrderItems($order_id, $cart) {
    require_once($_SERVER['DOCUMENT_ROOT'] . '/config/setupDBAccess.php');

    for ($i = 0; $i < count($cart); $i++) {
        $product = json_decode($cart[$i]);
        $product_id = $product->product_id;
        $quantity = $product->quantity;
        $sql = "INSERT INTO `order_items` (`order_id`, `product_id`, `quantity`) VALUES (?, ?, ?)";
        $stmt = $db->prepare($sql);
        $stmt->bind_param("iii", $order_id, $product_id, $quantity);

        if ($stmt->execute()) {
            return true;
        } else {
            return false;
        }
        $stmt->close();
        $db -> close();
    }
}*/