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


<?php
if(isset($_GET['add'])){
    
//    $_SESSION['product_' . $_GET['add']] +=1;
//    
//    redirect("index.php"); +
    
    $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("../main_files/checkout.php");
            
        } else {
            
            set_message("Količina {$row['product_title']}: " . $row['product_quantity'] . " " . "proizvoda dostupno. Molimo vas da nas kontaktirate" );
            redirect("../main_files/checkout.php");
            
        }
        
    }
    
}

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


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


function cart(){
    
    $total = 0;
    $item_quantity = 0;
    $item_name = 1;
    $item_number = 1;
    $amount = 1; 
    $quantity = 1;
    $sub = 0;
     
    foreach($_SESSION as $name => $value){
        
        if($value > 0){
            
        if(substr($name, 0, 8 ) == "product_"){
            
        //Warning: A non-numeric value encountered    
        @$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)){
            
        $sub = $row['product_price']*$value; 
        $item_quantity += $value;    
        $product_image = display_image($row['product_image']);   
        $categoryname = show_product_category_title($row['product_category_id']);    
        
$product = <<<DELIMETER

<tr class="product">    
    
    
    <td class="product-name">
        <img alt="img" width="150" src='../resources/{$product_image}'>
        <div>
            <h4>{$row['product_title']}</h4>
            <p>$categoryname</p>
        </div>
    </td>
    
    <td class="product-price">
        <span class="woocommerce-Price-amount"><bdi>
        <span class="woocommerce-Price-currencySymbol">rsd 
        </span>{$row['product_price']}</bdi>
        </span>
    </td>
    
    <td class="product-quantity">
     <input type="button" class="input-text" value="{$value}" min="1" >
    </td>
    
    <td class="product-subtotal">
        <span class="woocommerce-Price-amount"><bdi>
        <span class="woocommerce-Price-currencySymbol">rsd
        </span>{$sub}</bdi></span>
    </td>
    
    <td>
        
        <td class="product-remove">
        <a href="../resources/cart.php?remove={$row['product_id']}"><i class="fa-sharp fa-solid fa-minus"></i></a>
        </td>
        
        <td class="product-remove">
        <a href="../resources/cart.php?add={$row['product_id']}"><i class="fa-solid fa-plus"></i>
        </td>
        
        <td class="product-remove">
        <a href="../resources/cart.php?delete={$row['product_id']}"><i class="fa-regular fa-circle-xmark"></i></a>
        </td>
</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;    
        
        }    
            
        }
        
    }
        
}

function show_pay(){
    
    if(isset($_SESSION['item_quantity']) && ($_SESSION['item_quantity']) >=1 ){
    
$pay_buton = <<<DELIMETER

<input type="image" name="upload"
    src="assets/img/button.png"
    alt="Nastavi sa kupovinom">

DELIMETER;
    
    return $pay_buton;
    
}

        
}

/* U tabeli Orders imamo narudzbine ukupno, a u tabeli reports imamo posebne stavke u narudzbini */

function process_transaction() {



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

        $amount = $_GET['amt'];
        $currency = $_GET['cc'];
        $transaction = $_GET['tx'];
        $status = $_GET['st'];
        $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);


                    $send_order = query("INSERT INTO orders (order_amount, order_transaction, order_currency, order_status ) VALUES('{$amount}', '{$transaction}','{$currency}','{$status}')");
                    $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)) {
                        $product_price = $row['product_price'];
                        $product_title = $row['product_title'];
                        $sub = $row['product_price']*$value;
                        $item_quantity +=$value;


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





                    }


                    $total += $sub;
                    $item_quantity;


                }

            }

        }

        session_destroy();
    } else {


        redirect("index.php");


    }



}

?>