<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="description" content="Responsive HTML Admin Dashboard Template based on Bootstrap 5">
    <meta name="author" content="NobleUI">
    <meta name="keywords" content="nobleui, bootstrap, bootstrap 5, bootstrap5, admin, dashboard, template, responsive, css, sass, html, theme, front-end, ui kit, web">

    <title id="title"></title>

    <!-- Fonts -->
    <link rel="preconnect" href="https://fonts.googleapis.com/">
    <link rel="preconnect" href="https://fonts.gstatic.com/" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700;900&amp;display=swap" rel="stylesheet">
    <!-- End fonts -->

    <!-- core:css -->
    <link rel="stylesheet" href="<?= base_url(); ?>/public/assets/vendors/core/core.css">
    <!-- endinject -->

    <!-- Plugin css for this page -->
    <!-- End plugin css for this page -->

    <!-- inject:css -->
    <link rel="stylesheet" href="<?= base_url(); ?>/public/assets/fonts/feather-font/css/iconfont.css">
    <link rel="stylesheet" href="<?= base_url(); ?>/public/assets/vendors/flag-icon-css/css/flag-icon.min.css">
    <!-- endinject -->

    <!-- Layout styles -->
    <link rel="stylesheet" href="<?= base_url(); ?>/public/assets/css/demo1/style.min.css">
    <!-- End layout styles -->

    <link rel="shortcut icon" href="<?= base_url(); ?>/public/assets/images/favicon.png" />

</head>

<body>
    <div class="main-wrapper">
        <div class="page-wrapper full-page">
            <div class="page-content d-flex align-items-center justify-content-center">

                <div class="row w-100 mx-0 auth-page">
                    <div class="col-md-8 col-xl-6 mx-auto">
                        <div class="card">
                            <?php if (session()->get('success')) : ?>
                                <div class="alert alert-primary" role="alert">
                                </div>
                                <?= session()->get('success') ?>
                        </div>
                    <?php endif; ?>
                    <div class="row">
                        <div class="col-md-4 pe-md-0">
                            <div id="logoImage" class="auth-side-wrapper" style="background-image: url('<?= base_url('public/assets/images/faces/default.png') ?>'); background-size: cover; background-position: center; height: 90%;">
                            </div>
                        </div>
                        <div class="col-md-8 ps-md-0">
                            <div class="auth-form-wrapper px-4 py-5">
                                <?php if (session()->getFlashdata('logout_message')) : ?>
                                    <div class="alert alert-warning">
                                        <?= session()->getFlashdata('logout_message') ?>
                                    </div>
                                <?php endif; ?>

                                <a href="#" class="noble-ui-logo d-block mb-2"><span id="brandName"></span></a>
                                <h5 class="text-muted fw-normal mb-4">Welcome back! Log in to your account.</h5>
                                <form class="forms-sample" id="loginform" method="post" action="<?= base_url(); ?>/Users/index">
                                    <div class="mb-3">
                                        <label for="userEmail" class="form-label">Email address</label>
                                        <input type="email" class="form-control" id="email" name="email" required placeholder="Email">
                                    </div>
                                    <div class="mb-3">
                                        <label for="userPassword" class="form-label">Password</label>
                                        <input type="password" class="form-control password" name="password" id="password" autocomplete="current-password" placeholder="Password">
                                    </div>
                                    <div class="mb-3">
                                        <input type="checkbox" id="md_checkbox_21" class="filled-in chk-col-red lihat" />
                                        <label for="md_checkbox_21">Show Password</label>
                                    </div>
                                    <?php if (isset($validation)) : ?>
                                        <div class="form-group">
                                            <div class="col-xs-12">
                                                <div class="alert alert-danger" role="alert">
                                                    <?= $validation->listErrors() ?>
                                                </div>
                                            </div>
                                        </div>
                                    <?php endif; ?>
                                    <div>
                                        <button class="btn btn-primary" type="submit">Login</button>

                                    </div>

                                    <input type="hidden" id="levelAdmin" value="1"> <!-- Nilai level admin -->

                                </form>
                            </div>
                        </div>
                    </div>
                    </div>
                </div>
            </div>

        </div>
    </div>
    </div>

    <!-- <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> -->
    <!-- core:js -->
    <script src="<?= base_url(); ?>/public/assets/vendors/core/core.js"></script>
    <!-- endinject -->
    <script>
        $(document).ready(function() {
            loadSettingsLogin(); // Muat Data Pengaturan saat halaman dimuat
        });

        function loadSettingsLogin() {
            var level_admin = $('#levelAdmin').val();
            console.log("Level Admin: " + level_admin); // Debug: lihat nilai level_admin

            $.ajax({
                url: "<?= base_url('/settings/getSettings') ?>/" + level_admin,
                type: "GET",
                dataType: "json",
                success: function(response) {
                    console.log(response); // Debug: cek data yang diterima
                    if (response.success && response.settings && response.settings.nama_aplikasi) {
                        $('#brandName').text(response.settings.nama_aplikasi + ""); // Update HTML

                        // Update judul di elemen dengan id #title
                        if (response.settings.nama_aplikasi) {
                            $('#title').text(response.settings.nama_aplikasi);
                        } else {
                            $('#title').text("Aplikasi Default"); // Ganti dengan default jika tidak ada
                        }

                        // Misalnya, jika pengaturan juga memberikan gambar latar belakang baru
                        if (response.settings.logo) {
                            var logoUrl = "<?= base_url('/public/assets/images/faces/') ?>/" + response.settings.logo;
                            $('#logoImage').css('background-image', 'url(' + logoUrl + ')');

                            // Update favicon
                            updateFavicon(logoUrl);
                        } else {
                            var defaultFavicon = "<?= base_url('public/assets/images/photos/posyandu.png') ?>";
                            // Jika tidak ada logo baru, gunakan gambar default
                            $('#logoImage').css('background-image', 'url("<?= base_url('public/assets/images/photos/posyandu.png') ?>")');

                            // Update favicon dengan default
                            updateFavicon(defaultFavicon);
                        }


                    } else {
                        console.error("Pengaturan tidak ditemukan!");
                    }
                },
                error: function(xhr, status, error) {
                    console.error("AJAX Error: " + status + " - " + error);
                }
            });
        };

        // Fungsi untuk mengganti favicon
        function updateFavicon(faviconUrl) {
            var link = document.querySelector("link[rel~='icon']");
            if (!link) {
                link = document.createElement('link');
                link.rel = 'icon';
                document.getElementsByTagName('head')[0].appendChild(link);
            }
            link.href = faviconUrl;
        }
    </script>



    <!-- inject:js -->
    <script src="<?= base_url(); ?>/public/assets/vendors/feather-icons/feather.min.js"></script>
    <script src="<?= base_url(); ?>/public/assets/js/template.js"></script>
    <!-- endinject -->

    <!-- Custom js for this page -->
    <!-- End custom js for this page -->

    <script type="text/javascript">
        $(document).ready(function() {
            $('.lihat').click(function() {
                if ($(this).is(':checked')) {
                    $('.password').attr('type', 'text');
                } else {
                    $('.password').attr('type', 'password');
                }
            });
        });
    </script>

</body>

</html>