<?php
include("Database.php");
if (version_compare(phpversion(), '5.4.0', '<')) {
    if (session_id() == '') {
        session_start();
    }
} else {
    if (session_status() == PHP_SESSION_NONE) {
        session_start();
    }
}
$db = new Database();

function getCartTotal($db)
{
    $cart = $_SESSION['cart'] ?? '';
    $cart = json_decode($cart, true);
    $ids = array_keys($cart);
    if (
        !$ids
    ) {
        echo json_encode(['status' => 'error', 'cart' => []]);
        return;
    }
    $ids = '(' . implode(',', $ids) . ')';
    $products = $db->customSelect('SELECT * from product_post where id in ' . $ids);
    $sum_price = 0;
    foreach ($products as $key => $value) {
        $sum_price += $total_price = $value['price'] * $cart[$value['id']];
    }
    return $sum_price;
}

function getCartForCheckout($db)
{
    $cart = $_SESSION['cart'] ?? '';
    $cart = json_decode($cart, true);
    $ids = array_keys($cart);
    if (!$ids) {
        echo json_encode(['status' => 'error', 'cart' => []]);
        return;
    }
    $ids = '(' . implode(',', $ids) . ')';
    $products = $db->customSelect('SELECT * from product_post where id in ' . $ids);
    $store = "";
    $ii = 1;
    foreach ($products as $key => $value) {
        $total_price = $value['price'] * $cart[$value['id']];
        $store .= '
            <tr>
                <td><img src="' . $value['image'] . '" style="width: 45px;"></td>' .
            '<td class="text-left">
                    <b>' . $value['title'] . '</b><br>
                    <span>Qty: ' . $cart[$value['id']] . '</span><br>
                 <b>Price: ' . $total_price . '</b>
                </td>
            </tr>';
    }
    return $store;
}

if (isset($_POST['load_product']) && $_POST['load_product'] != '') {
    $cart = $_SESSION['cart'] ?? '';
    $cart = json_decode($cart, true);
    $ids = array_keys($cart);
    if (!$ids) {
        echo json_encode(['status' => 'error', 'cart' => []]);
        return;
    }
    $ids = '(' . implode(',', $ids) . ')';
    $products = $db->customSelect('SELECT * from product_post where id in ' . $ids);
    $store = "";
    $ii = 1;
    $sum_price = 0;
    foreach ($products as $key => $value) {
        $sum_price += $total_price = $value['price'] * $cart[$value['id']];
        $store .= '
            <tr>
                <td>' . ($ii++) . '</td>' .
            '<td>' . $value['title'] . '</td>' .
        '<td><img src="admin/product_upload_image/' . $value['image'] . '" style="width: 45px;"></td>' .
            '<td>' . $value['price'] . '</td>' .
            '<td class="text-right">' . ($total_price) . '</td>' .
            '<td><button data-id="' . $value['id'] . '" class="remove_cart_item btn btn-sm btn-danger">×</button></td>
            </tr>';
    }
    $store .= '
                <tr>
                    <th colspan="3"></th>' .
        '<th>Total=</th>' .
        '<th class="text-right">' . $sum_price . '</th>' .
        '<th></th>
                </tr>';

    echo json_encode(['status' => 'success', 'cart' =>$store, 'cart_count' => count($products)]);
}

if (isset($_POST['remove_cart_item']) && $_POST['remove_cart_item'] != '') {

    $product_id = $_POST['remove_product_id'];
    $cart = $_SESSION['cart'] ?? '';
    $cart = json_decode($cart, true);

    $exists = 0;
    if (is_array($cart)) {
        foreach ($cart as $key => $value) {
            if ($product_id == $key) {
                unset($cart[$key]);
            }
        }
    }
    $_SESSION['cart'] = json_encode($cart);
    echo 1;
}

if (isset($_POST['product_id']) && $_POST['product_id'] != '') {
    $product_id = $_POST['product_id'];
    $cart = $_SESSION['cart'] ?? '';
    $cart = json_decode($cart, true);

    $exists = 0;
    if (is_array($cart)) {
        foreach ($cart as $key => $value) {
            if ($product_id == $key) {
                $cart[$key] = $value + 1;
                $exists = 1;
            }
        }
    }
    if (!$exists) {
        $cart[$product_id] = 1;
    }
    $_SESSION['cart'] = json_encode($cart);

    echo json_encode(['status' =>'success', 'cart' =>$cart, 'cart_count' => count($cart)]);

    // session_unset();
    // session_destroy();

}
