<?php
include "classes/class.loader.php";
$html = new html();
$html->bacground_emoji();
$calls = new functions();
$html->header();
$db = new db();
if(!isset($_POST['user_id'])){
    echo "<script>window.location.href='login.php';</script>";
}

/**
 * @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";

?>
<style>
    body {
        background-color: #bebebee7;
        color: white;
    }
</style>


<div class="home_container">
    <div class="container_square">
        <h2>UPCOMMING <br> BOOKINGS</h2>
        <div class="line"></div>
        <h3><?php echo $calls->get_latest_booking($_SESSION['user_id']); ?></h3>
        <button onclick="window.location.href='bookings.php'">BOOK NOW</button>
    </div>
    <div class="container_square">
        <h2>PARK <br> CREDITS</h2>
        <div class="line"></div>
        <h1><?php echo ($calls->user_park_credits($_SESSION['user_id']) == 0 || empty($calls->user_park_credits($_SESSION['user_id']))) ? 0 : $calls->user_park_credits($_SESSION['user_id']); ?>
        </h1>
        <br>
        <button onclick="window.location.href='buy.php'">BUY MORE</button>
    </div>
    <div class="container_square">
        <h2>WALLET</h2>
        <div class="line"></div>
        <h1>R<?php echo number_format($calls->get_wallet_amount($_SESSION['user_id']) == 0 || empty($calls->get_wallet_amount($_SESSION['user_id'])) ? 0 : $calls->get_wallet_amount($_SESSION['user_id']), 2); ?>
        </h1>
        <br>
        <button onclick="window.location.href='buy.php'">BUY MORE</button>
    </div>

    <?php
    $park_packages = $db->query("SELECT * FROM `park_prices` WHERE record_id = 3 ORDER BY price ASC");
    $package = $park_packages->fetch_assoc();

    $price = $package['price'] * 1.15;


    $data = array(
        // Merchant details
        'merchant_id' => $merchant_id,
        'merchant_key' => $mechhant_key,
        'return_url' => "https://unleashed.elegantwork.co.za/successfull.php?credits=$package[record_id]&zar=$price",
        'cancel_url' => 'https://unleashed.elegantwork.co.za/home.php',
        'notify_url' => "https://unleashed.elegantwork.co.za/send_notification.php?credits=$package[record_id]",
        // Buyer details
        'name_first' => $_SESSION['username'],
        'email_address' => $_SESSION['email'],
        // Transaction details
        'amount' => $price,
        'item_name' => "buy_credits"
    );
    $signature = generateSignature($data, $passphrase);

    ?>

    <script>
        window.onload = function () {
            setTimeout(function () {
                var popup = document.createElement('div');
                popup.style.position = 'fixed';
                popup.id = 'popup';
                popup.style.top = '0px';
                popup.style.left = '0px';
                popup.style.width = '100%';
                popup.style.height = '100%';
                popup.style.zIndex = '9999';
                popup.style.background = 'rgba(0,0,0,0.5)';
                popup.style.padding = '50px';
                popup.style.overflow = 'auto';
                popup.innerHTML = `

                    <div class="data_row">
                    <h1 style="font-size: 3em; text-align: center; animation: glow 1s ease-in-out infinite alternate;">
                        SPECIAL LIMNITED OFFER
                    </h1>
                    <div class="line"></div>
                    <style>
                        @keyframes glow {
                            from {
                                text-shadow: 0 0 10px #fff, 0 0 20px #fff, 0 0 30px #fff, 0 0 40px #ff007b, 0 0 70px #ff007b, 0 0 80px #ff007b, 0 0 100px #ff007b;
                            }
                            to {
                                text-shadow: 0 0 20px #fff, 0 0 30px #ff007b, 0 0 40px #ff007b, 0 0 50px #ff007b, 0 0 60px #ff007b, 0 0 70px #ff007b, 0 0 80px #ff007b, 0 0 100px #ff007b;
                            }
                        }
                    </style>
                    <h1><?php echo $package['name']; ?></h1>
                    <h2>Credits : <?php echo $package['credits']; ?></h2>
                    <h1> R <?php echo number_format($price, 2); ?> inc</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 $price; ?>">
                        <input type="hidden" name="item_name" value="<?php echo "buy_credits"; ?>">
                        <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?credits=<?php echo $package['record_id']; ?>&zar=<?php echo $price; ?>">
                        <input type="hidden" name="cancel_url" value="https://unleashed.elegantwork.co.za/home.php">
                        <input type="hidden" name="notify_url"
                            value="https://unleashed.elegantwork.co.za/send_notification.php?credits=<?php echo $package['record_id']; ?>">
                        <!-- <input type="submit"  class="button" value="BUY NOW">
                      -->
                        <button>BUY NOW</button>

                    </form>
                        <button onclick="closePopup()">CLOSE</button>
                        <br><br>

                </div>
                
                `;
                document.body.appendChild(popup);
            }, 1000);
        }

        function closePopup() {
            var popup = document.getElementById('popup');
            document.body.removeChild(popup);
        }
    </script>
    <div class="container_wide">
        <h1>LATEST NEWS</h1>
        <?php
        $db = new db();
        $news_res = $db->query("SELECT * FROM news WHERE 1 ORDER BY record_id DESC LIMIT 5");
        while ($news = $news_res->fetch_assoc()) {
            ?>
        <div class="news_container">
            <span class="time_stamp"><?php echo $news['date_time']; ?></span>
            <div class="news_heading">
                <h2><?php echo $news['heading']; ?></h2>
            </div>
            <div class="news_body">
                <div class="news_image">
                    <img src="news_upload/<?php echo $news['file_name']; ?>" class="news_img" />
                </div>
                <div class="news_description">
                    <textarea class="news_text" readonly><?php echo $news['description']; ?></textarea>
                </div>
            </div>
        </div>
        <?php } ?>
    </div>

</div>
<?php $html->html_end(); ?>