<?php require_once("config.php"); ?>


<?php

//Makes sure user can't add more quantity to cart than is in product_quantity in db
if (isset($_GET['add'])) {

    $query = query("SELECT * FROM products WHERE product_id=" . escape_string($_GET['add']) . " ");
    confirm($query);

    while ($row = fetch_array($query)) {

        if ($row['product_quantity'] != $_SESSION['product_' . $_GET['add']]) {

            $_SESSION['product_' . $_GET['add']] += 1;
            redirect("../public/checkout.php");
        } else {

            set_message("We only have {$row['product_quantity']} " . $row['product_title'] . " available");
            redirect("../public/checkout.php");
        }
    }

    // $_SESSION['product_' . $_GET['add']] += 1;

    // redirect("index.php");
}

if (isset($_GET['remove'])) {

    $_SESSION['product_' . $_GET['remove']]--;

    if ($_SESSION['product_' . $_GET['remove']] < 1) {
        unset($_SESSION['item_total']);
        unset($_SESSION['item_quantity']);
        redirect("../public/checkout.php");
    } else {
        redirect("../public/checkout.php");
    }
}

if (isset($_GET['delete'])) {

    $_SESSION['product_' . $_GET['delete']] = "0";
    unset($_SESSION['item_total']);
    unset($_SESSION['item_quantity']);
    redirect("../public/checkout.php");
}

function cart()
{

    $total = 0;
    $item_quantity = 0;

    //variables for paypal form
    $item_name = 1;
    $item_number = 1;
    $amount = 1;
    $quantity = 1;

    foreach ($_SESSION as $name => $value) {

        if ($value > 0) {

            if (substr($name, 0, 8) == "product_") {

                $length = strlen($name) - 8;
                $id = substr($name, 8, $length);
                $query = query("SELECT * FROM products WHERE product_id = " . escape_string($id) . " ");
                confirm($query);

                while ($row = fetch_array($query)) {

                    //$value holds quantity of a product you want to buy in you cart. $value is stored in the session string
                    $sub = $row['product_price'] * $value;
                    $item_quantity += $value;

                    $product_image = display_image($row['product_image']);

                    $product = <<<DELIMETER
            
                    <tr>
                        <td>{$row['product_title']}<br>
                        <img width="100" src='../resources/{$product_image}'>
                        </td>
                        <td>&#36;{$row['product_price']}</td>
                        <td>{$value}</td>
                        <td>&#36;{$sub}</td>
                        <td><a class='btn btn-warning' href="../resources/cart.php?remove={$row['product_id']}"><span class='glyphicon glyphicon-minus'></span></a> 
                        <a class='btn btn-success' href="../resources/cart.php?add={$row['product_id']}"><span class='glyphicon glyphicon-plus'></span></a>
                        <a class='btn btn-danger' href="../resources/cart.php?delete={$row['product_id']}"><span class='glyphicon glyphicon-remove'></span></a>
            
                    </tr>

                    <input type="hidden" name="item_name_{$item_name}" value="{$row['product_title']}">
                    <input type="hidden" name="item_number_{$item_number}" value="{$row['product_id']}">
                    <input type="hidden" name="amount_{$amount}" value="{$row['product_price']}">
                    <input type="hidden" name="quantity_{$quantity}" value="{$value}">

            
                    DELIMETER;

                    echo $product;

                    $item_name++;
                    $item_number++;
                    $amount++;
                    $quantity++;
                }

                $_SESSION['item_total'] = $total += $sub;
                $_SESSION['item_quantity'] = $item_quantity;
                $_SESSION['item_name'] = $item_name;
                $_SESSION['checkout'] = "true";
            }
        }
    }
}

function show_paypal()
{
    if (isset($_SESSION['item_quantity']) && $_SESSION['item_quantity'] >= 1) {

        $paypal_button = <<<DELIMETER

        <input type="image" name="upload" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif" alt="PayPal - The safer, easier way to pay online">

        DELIMETER;

        return $paypal_button;
    }
}

function process_transaction()
{
    // if (isset($_GET['tx'])) {

    // if (isset($_POST['upload'])) {

    if (isset($_SESSION['checkout'])) {

        //Data we get from Paypal confirmation
        // $amount = $_GET['amt'];
        // $currency = $_GET['cc'];
        // $transaction = $_GET['tx'];
        // $status = $_GET['st'];

        //session login_id as user_id
        $user_id = $_SESSION['login_id'];


        $total = 0;
        $item_quantity = 0;


        foreach ($_SESSION as $name => $value) {

            if ($value > 0) {

                if (substr($name, 0, 8) == "product_") {

                    $length = strlen($name) - 8;
                    $id = substr($name, 8, $length);

                    //inserts confirmation info from Paypal into orders table
                    // $send_order = query("INSERT INTO orders (user_id, order_amount, order_transaction, order_currency, order_status ) VALUES('{$user_id}','{$amount}', '{$transaction}', '{$currency}', '{$status}')");

                    //Gives us last inserted id
                    $last_id = last_id();

                    // confirm($send_order);


                    $query = query("SELECT * FROM products WHERE product_id = " . escape_string($id) . " ");
                    confirm($query);

                    while ($row = fetch_array($query)) {

                        //$value holds quantity of a product you want to buy in you cart.
                        $sub = $row['product_price'] * $value;
                        $item_quantity += $value;
                        $product_price = $row['product_price'];
                        $product_title = $row['product_title'];

                        $currentDate = new DateTime();
                        $date = $currentDate->format("m/d/Y");

                        $insert_report = query("INSERT INTO reports (product_id, user_id, product_title, product_price, product_quantity, date) VALUES('{$id}', '{$user_id}', '{$product_title}','{$product_price}', '{$value}', '{$date}')");
                        confirm($insert_report);
                    }

                    $total += $sub;
                    echo $item_quantity;
                }
            }
        }


        $tmp = $_SESSION['login_id'];
        $tmp2 = $_SESSION['username'];
        session_unset();
        $_SESSION['login_id'] = $tmp;
        $_SESSION['username'] = $tmp2;
        // redirect("index.php");
    } else {
        // redirect("index.php"); thank_you.php doesn't show tx amt cc details in url
    }
}

?>