<?php
session_start();

// Pastikan key admin sudah ada
if (!isset($_SESSION["admin"])) {
    $_SESSION["admin"] = [
        "haslogin" => false
    ];
}

if ($_SESSION["admin"]["haslogin"] === true) {
    header("Location: ./");
    exit;
}

$setting = json_decode(file_get_contents("assets/plugins/config.json"), true);

if (isset($_POST["submit"])) {
    $username = md5($_POST["username"]);
    $password = md5($_POST["password"]);

    if ($username . $password == $setting["username"] . $setting["password"]) {
        $_SESSION["admin"]["haslogin"] = true;
        file_put_contents("last.tmp", get_client_ip());
        header("Location: ./");
        exit;
    } else {
        echo '<script>alert("Wrong username or password")</script>';
    }
}

function get_client_ip() {
    if (getenv('HTTP_CLIENT_IP'))
        return getenv('HTTP_CLIENT_IP');
    else if(getenv('HTTP_X_FORWARDED_FOR'))
        return getenv('HTTP_X_FORWARDED_FOR');
    else if(getenv('HTTP_X_FORWARDED'))
        return getenv('HTTP_X_FORWARDED');
    else if(getenv('HTTP_FORWARDED_FOR'))
        return getenv('HTTP_FORWARDED_FOR');
    else if(getenv('HTTP_FORWARDED'))
        return getenv('HTTP_FORWARDED');
    else if(getenv('REMOTE_ADDR'))
        return getenv('REMOTE_ADDR');
    else
        return 'UNKNOWN';
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta content="IE=edge" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1, shrink-to-fit=no" name="viewport">
    <meta content="" name="description">
    <meta content="" name="author">
    <title>Panel Login</title><!-- loader-->
    <link href="assets/css/pace.min.css" rel="stylesheet">
    <script src="assets/js/pace.min.js">
    </script><!--favicon-->
    <link
        href="https://www.pinclipart.com/picdir/big/550-5508982_golden-crown-png-download-transparent-background-black-crown.png"
        rel="icon" type="image/x-icon">
    <link href="assets/css/bootstrap.min.css" rel="stylesheet"><!-- animate CSS-->
    <link href="assets/css/animate.css" rel="stylesheet" type="text/css"><!-- Icons CSS-->
    <link href="assets/css/icons.css" rel="stylesheet" type="text/css"><!-- Custom Style-->
    <link href="assets/css/app-style.css" rel="stylesheet">
</head>

<body class="bg-theme bg-theme2">
    <!-- start loader -->
    <div class="visible incoming" id="pageloader-overlay">
        <div class="loader-wrapper-outer">
            <div class="loader-wrapper-inner">
                <div class="loader"></div>
            </div>
        </div>
    </div><!-- end loader -->
    <!-- Start wrapper-->
    <div id="wrapper">
        <div class="loader-wrapper">
            <div class="lds-ring">
                <div></div>
                <div></div>
                <div></div>
                <div></div>
            </div>
        </div>
        <div class="card card-authentication1 mx-auto my-5">
            <div class="card-body">
                <div class="card-content p-2">
                    <div class="text-center"><img alt="logo icon"
                            src="https://www.pinclipart.com/picdir/big/550-5508982_golden-crown-png-download-transparent-background-black-crown.png"
                            width="100"></div>
                    <div class="card-title text-uppercase text-center py-3">
                        510 NOT FOUND Login Panel
                    </div>
                    <form action="" method="post">
                        <div class="form-group">
                            <label class="sr-only" for="exampleInputUsername">Username</label>
                            <div class="position-relative has-icon-right">
                                <input class="form-control input-shadow" id="exampleInputUsername"
                                    placeholder="Enter Username" type="text" name="username" required>
                                <div class="form-control-position">
                                    <i class="icon-user"></i>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="sr-only" for="exampleInputPassword">Password</label>
                            <div class="position-relative has-icon-right">
                                <input class="form-control input-shadow" id="exampleInputPassword"
                                    placeholder="Enter Password" type="password" name="password" required>
                                <div class="form-control-position">
                                    <i class="icon-lock"></i>
                                </div>
                            </div>
                        </div>
                        <button class="btn btn-light btn-block" type="submit" name="submit">Sign In</button>
                    </form>
                </div>
            </div>
            <div class="card-footer text-center py-3">
                <p class="text-normal mb-0" id="liveClock"></p>
            </div>
        </div><!--Start Back To Top Button-->
        <a class="back-to-top" href="javaScript:void();"><i class="fa fa-angle-double-up"></i></a>
        <!--End Back To Top Button-->
        <!--start color switcher-->
        <div class="right-sidebar">
            <div class="switcher-icon">
                <i class="zmdi zmdi-settings zmdi-hc-spin"></i>
            </div>
            <div class="right-sidebar-content">
                <p class="mb-0">Gaussion Texture</p>
                <hr>
                <ul class="switcher">
                    <li id="theme1"></li>
                    <li id="theme2"></li>
                    <li id="theme3"></li>
                    <li id="theme4"></li>
                    <li id="theme5"></li>
                    <li id="theme6"></li>
                </ul>
                <p class="mb-0">Gradient Background</p>
                <hr>
                <ul class="switcher">
                    <li id="theme7"></li>
                    <li id="theme8"></li>
                    <li id="theme9"></li>
                    <li id="theme10"></li>
                    <li id="theme11"></li>
                    <li id="theme12"></li>
                    <li id="theme13"></li>
                    <li id="theme14"></li>
                    <li id="theme15"></li>
                </ul>
            </div>
        </div><!--end color switcher-->
    </div><!--wrapper-->
    <!-- Bootstrap core JavaScript-->
    <script src="assets/js/jquery.min.js"></script>
    <script src="assets/js/popper.min.js"></script>
    <script src="assets/js/bootstrap.min.js"></script>

    <!-- sidebar-menu js -->
    <script src="assets/js/sidebar-menu.js"></script>

    <!-- Custom scripts -->
    <script src="assets/js/app-script.js"></script>

    <!-- Clock Script -->
    <script>
        function updateClock() {
            const now = new Date();

            const options = {
                hour: '2-digit',
                minute: '2-digit',
                second: '2-digit'
            };

            document.getElementById("liveClock").textContent =
                now.toLocaleTimeString('en-US', options);
        }

        setInterval(updateClock, 1000);
        updateClock();
    </script>
</body>

</html>