<?php
include "classes/class.loader.php";
$html = new html();
$html->bacground_emoji();
$calls = new functions();
$db = new db();
$html->header();
$wallet_amount = $calls->get_wallet_amount($_SESSION['user_id']);
$park_group_price = $calls->get_park_group_price() * 1.15;
$park_price = $calls->get_park_price() * 1.15;
if ($_POST['service'] != 0) {
    $services_res = $db->query("SELECT * FROM services WHERE record_id = {$_POST['service']}");
    $service = $services_res->fetch_assoc();
    $service_name = $service['name'];
    $service_price = $service['price'];
} else {
    $service_name = "N/A";
    $service_price = 0;
}

/**
 * @param array $data
 * @param null $passPhrase
 * @return string
 */
function generateSignature($data, $passPhrase = null)
{
    // Create parameter string
    $pfOutput = '';
    foreach ($data as $key => $val) {
        if ($val !== '') {
            $pfOutput .= $key . '=' . urlencode(trim($val)) . '&';
        }
    }
    // Remove last ampersand
    $getString = substr($pfOutput, 0, -1);
    if ($passPhrase !== null) {
        $getString .= '&passphrase=' . urlencode(trim($passPhrase));
    }
    return md5($getString);
}
// testing
// $merchant_id = '10000100';
// $mechhant_key = '46f0cd694581a';
// $passphrase = 'jt7NOE43FZPn';
// $link = "https://sandbox.payfast.co.za/eng/process";

// Live
$merchant_id = 23635991;
$mechhant_key = '5n8lir8uw1cun';
$passphrase = 'eWG123456789';
$link = "https://www.payfast.co.za/eng/process";

if ($wallet_amount > 0) {
    $wallet = 1;
} else {
    $wallet = 0;
}


if ($_POST['type'] == "PARK-GROUP") {
    $duration = " - 6 Hours";
    $cartTotal = $park_group_price - $wallet_amount; // This amount needs to be sourced from your application
    $cart = $park_group_price;


} else if ($_POST['type'] == "PARK") {
    $duration = " - 1 Hour";
    if ($_POST['paid'] == 1) {
        $cartTotal = ($service_price * 1.15) - $wallet_amount; // This amount needs to be sourced from your application
        $cart = 0;
    } else {
        $cartTotal = $park_price + ($service_price * 1.15) - $wallet_amount; // This amount needs to be sourced from your application
        $cart = $park_price;
    }

} else {
    $duration = "";

    if ($_POST['paid'] == 1) {
        $cartTotal = ($service_price * 1.15) - $wallet_amount;
        $cart = 0;
    } else {
        $cartTotal = 250 * 1.15 + ($service_price * 1.15) - $wallet_amount;
        $cart = 250 * 1.15;
    }

}
$data = array(
    // Merchant details
    'merchant_id' => $merchant_id,
    'merchant_key' => $mechhant_key,
    'return_url' => 'https://unleashed.elegantwork.co.za/successfull.php?booking_id=' . $_POST['booking_id'] . '&amount=' . $cartTotal . "&type=" . $_POST['type'] . "&wallet=$wallet",
    'cancel_url' => 'https://unleashed.elegantwork.co.za/unsuccessfull.php?booking_id=' . $_POST['booking_id'] . '&amount=' . $cartTotal . "&type=" . $_POST['type'] . "&wallet=$wallet",
    'notify_url' => 'https://unleashed.elegantwork.co.za/send_notification.php?booking_id=' . $_POST['booking_id'] . '&amount=' . $cartTotal . "&type=" . $_POST['type'] . "&wallet=$wallet",
    // Buyer details
    'name_first' => $_SESSION['username'],
    'email_address' => $_SESSION['email'],
    // Transaction details
    'amount' => number_format(sprintf('%.2f', $cartTotal), 2, '.', ''),
    'item_name' => @$_POST['type']
);

$signature = generateSignature($data, $passphrase);

?>
<style>
    body {
        background-color: #bebebee7;
        color: white;
    }
</style>

<div class="home_container">

    <div class="container_wide" style="">
        <h1>Payment Summary</h1>
        <div class="line"></div>
        <div class="dogs_data_row_number">
            <h2><?php echo $_POST['type'] . " ". $duration; ?></h2>
        </div>
        <div class="dogs_data_row_number">
            <h2>R <?php echo number_format(sprintf('%.2f', $cart), 2, '.', ''); ?></h2>
        </div>
        <?php if ($service_name != 'N/A') { ?>
            <div class="line"></div>
            <div class="dogs_data_row_number">
                <h2><?php echo $service_name; ?> - Training</h2>
            </div>

            <div class="dogs_data_row_number">
                <h2>R <?php echo number_format(sprintf('%.2f', $service_price * 1.15), 2, '.', ''); ?></h2>
            </div>
        <?php }
        if ($wallet_amount > 0) {
            $wallet = 1;
            ?>
            <div class="line"></div>
            <div class="dogs_data_row_number">
                <h2>Wallet</h2>
            </div>
            <div class="dogs_data_row_number">
                <h2>- R <?php echo number_format(sprintf('%.2f', $wallet_amount), 2, '.', ''); ?></h2>
            </div>
        <?php } else {
            $wallet = 0;
        }

        ?>


        <div class="line"></div>

        <h1>R <?php echo number_format(sprintf('%.2f', $cartTotal), 2, '.', ''); ?></h1>


        <form action=" <?php echo $link; ?>" method="post">
            <input type="hidden" name="signature" value="<?php echo $signature; ?>">
            <input type="hidden" name="merchant_id" value="<?php echo $merchant_id; ?>">
            <input type="hidden" name="merchant_key" value="<?php echo $mechhant_key; ?>">
            <input type="hidden" name="amount"
                value="<?php echo number_format(sprintf('%.2f', $cartTotal), 2, '.', ''); ?>">
            <input type="hidden" name="item_name" value="<?php echo @$_POST['type']; ?>">
            <input type="hidden" name="name_first" value="<?php echo $_SESSION['username']; ?>">
            <input type="hidden" name="email_address" value="<?php echo $_SESSION['email']; ?>">
            <input type="hidden" name="return_url"
                value="https://unleashed.elegantwork.co.za/successfull.php?booking_id=<?php echo $_POST['booking_id'] . '&amount=' . $cartTotal . "&type=" . $_POST['type']; ?>&wallet=<?php echo $wallet; ?>">
            <input type="hidden" name="cancel_url"
                value="https://unleashed.elegantwork.co.za/unsuccessfull.php?booking_id=<?php echo $_POST['booking_id'] . '&amount=' . $cartTotal . "&type=" . $_POST['type']; ?>&wallet=<?php echo $wallet; ?>">
            <input type="hidden" name="notify_url"
                value="https://unleashed.elegantwork.co.za/send_notification.php?booking_id=<?php echo $_POST['booking_id'] . '&amount=' . $cartTotal . "&type=" . $_POST['type']; ?>&wallet=<?php echo $wallet; ?>">
            <button onclick="document.getElementById('overlay').style.display='block'" style="width:80vw;"> PAY NOW
            </button>
            <div id="overlay"
                style="display:none;position:fixed;top:0;left:0;width:100vw;height:100vh;background-color:rgba(0,0,0,0.5);z-index:9999;">
                <div style="position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);text-align:center;">
                    <!-- <img src="images/loader.gif" alt="loading..." style="width:50px;height:50px;" /> -->
                    <p style="color:white;font-size:24px;">Loading...</p>
                </div>
            </div>
        </form>
        <form action="cancellation.php" method="post">
            <input type="hidden" name="booking_id" value="<?php echo $_POST['booking_id']; ?>">
            <input type="hidden" name="type" value="<?php echo $_POST['type']; ?>">
            <button onclick="document.getElementById('overlay').style.display='block'"> CANCEL </button>
            <div id="overlay"
                style="display:none;position:fixed;top:0;left:0;width:100vw;height:100vh;background-color:rgba(0,0,0,0.5);z-index:9999;">
                <div style="position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);text-align:center;">
                    <!-- <img src="images/loader.gif" alt="loading..." style="width:50px;height:50px;" /> -->
                    <p style="color:white;font-size:24px;">Loading...</p>
                </div>
            </div>
        </form>
    </div>





</div>