<body>

    <?php if ($titulo == 'Login' || $titulo == 'Cadastro') { ?>
        <script>
            window.fbAsyncInit = function () {
                FB.init({
                    appId: <?= ID_FACEBOOK ?>,
                    cookie: true,
                    xfbml: true,
                    version: 'v4.0'
                });

                FB.AppEvents.logPageView();

            };

            (function (d, s, id) {
                var js, fjs = d.getElementsByTagName(s)[0];
                if (d.getElementById(id)) {
                    return;
                }
                js = d.createElement(s);
                js.id = id;
                js.src = "https://connect.facebook.net/en_US/sdk.js";
                fjs.parentNode.insertBefore(js, fjs);
            }(document, 'script', 'facebook-jssdk'));

            function logoutFacebook() {
                FB.getLoginStatus(function (response) {
                    if (response.status === 'connected') {
                        FB.logout(function (response) {
                            if (response.status === "connected") {
                                FB.Auth.setAuthResponse(null, 'unknown');
                            }
                        });
                    }
                });
            }
            $(document).ready(function () {
                $('.btn-logout').click(function () {
                    var auth2 = gapi.auth2.getAuthInstance();
                    auth2.signOut().then(function () {
                        console.log('Usuário deslogou');
                    });
                    auth2.disconnect();
                    window.location = 'logout';
                });
            });
            $(document).ready(function () {
                $('.btn-facebook').click(function (event) {
                    event.preventDefault();
                    $("#aguarde").css("display", "block");
                    $("#blanket").css("display", "block");
                    FB.getLoginStatus(function (response) {
                        if (response.status === 'connected') {
                            $("#aguarde").css("display", "none");
                            $("#blanket").css("display", "none");
                            FB.api('/me?fields=id,name,email', function (response) {
                                var email = response.email;

                                $(document).ajaxStart(function () {
                                    $("#aguarde").css("display", "block");
                                    $("#blanket").css("display", "block");
                                });

                                $(document).ajaxComplete(function () {
                                    $("#aguarde").css("display", "none");
                                    $("#blanket").css("display", "none");
                                });

                                $.ajax({
                                    data: {nome: response.name, email: email, 'tipo': 'facebook'},
                                    type: "POST",
                                    dataType: 'json',
                                    url: 'login_social',
                                    success: function (data) {
                                        login(data);
                                    }
                                });
                            });
                        } else {
                            if (response.status === 'unknown') {
                                $("#aguarde").css("display", "none");
                                $("#blanket").css("display", "none");
                            }
                            FB.login(function (response) {
                                if (response.status === 'connected') {
                                    $("#aguarde").css("display", "none");
                                    $("#blanket").css("display", "none");
                                    FB.api('/me?fields=id,name,email', function (response) {

                                        var email = response.email;

                                        $(document).ajaxStart(function () {
                                            $("#aguarde").css("display", "block");
                                            $("#blanket").css("display", "block");
                                        });

                                        $(document).ajaxComplete(function () {
                                            $("#aguarde").css("display", "none");
                                            $("#blanket").css("display", "none");
                                        });

                                        $.ajax({
                                            data: {nome: response.name, email: email, 'tipo': 'facebook'},
                                            type: "POST",
                                            dataType: 'json',
                                            url: 'login_social',
                                            success: function (data) {
                                                login(data);
                                            }
                                        });
                                    });
                                } else {
                                    $("#aguarde").css("display", "none");
                                    $("#blanket").css("display", "none");
                                    Materialize.toast('Ocorreu um erro ao realizar o seu login', 2500, 'black');
                                }
                            }, {scope: 'public_profile, email'});
                        }
                    });

                });
            });

        </script>
        <div id="fb-root"></div>
        <script async defer crossorigin="anonymous" src="https://connect.facebook.net/pt_BR/sdk.js#xfbml=1&version=v4.0&appId=962521494116283&autoLogAppEvents=1"></script>
    <?php } else { ?>
        <script>

        </script>
    <?php } ?>
    <div id="blanket"></div>
    <div id="aguarde">
        <div class="progress">
            <div class="indeterminate" style="width: 100%; background-color: #2BA447"></div>
        </div>
    </div>
    <div id="blanketS"></div>
    <div id="aguardeS">
        <div class="progress-bar progress-bar-striped active textos" role="progressbar" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100" style="width: 100%;" >

        </div>
    </div>
    <main>
        <nav class="green"></nav>

        <nav>
            <?php if (isset($_SESSION['aluno']) == FALSE) { ?>
                <?php $this->load->view('commons/menu-deslogado'); ?>
            <?php } else { ?>
                <?php $this->load->view('commons/menu-logado'); ?>
            <?php } ?>
        </nav>
        <?php
        if($_SESSION['aluno']['email'] != NULL){
          $url_notificacoes = "https://notificacoes.online/1.0/";
            $email_64 = base64_encode($_SESSION['aluno']['email']);
            // $iframe = "<iframe id='banner_email' style=\"border:0; border:none;\"
            // src='$url_notificacoes/cliente/verificar_banner/$email_64'></iframe>";
            // echo $iframe;
//            $banner = file_get_contents("$url_notificacoes/cliente/verificar_banner/$email_64");
//            echo $banner;
        }
        ?>
