<?php
    session_set_cookie_params ( 1*24*60*60, "/" );
    session_start ();
    $sections = [
        0 => 'Previous',
        1 => 'Home',
        2 => 'About myself',
        3 => 'What I\'ve done so far',
        4 => 'Who I would like to be',
        5 => 'Contact me via webform'
    ];
    if ( ! isset ( $_POST ['section'] ) || ! in_array ( $_POST ['section'], array_keys( $sections ) ) ) {
        $section_title = 'Home';
    }
    else {
        $section = $_POST ['section'];
        if ( $section == 0 ) {
            if ( isset ( $_SESSION [ 'section' ] ) ) {
                $section = $_SESSION [ 'section' ];
            }
            else {
                $section = 1;
            }
        }
        $_SESSION [ 'section' ] = $section;
        $section_title = $sections [ $section ];
        $sections_content = [
            1 => file_get_contents ( 'tmpl/home.phtml' ),
            2 => file_get_contents ( 'tmpl/description.phtml' ),
            3 => file_get_contents ( 'tmpl/projects.phtml' ),
            4 => file_get_contents ( 'tmpl/ideas.phtml' ),
            5 => file_get_contents ( 'tmpl/contact-form.phtml' ),
        ];
        $arr_res = [
            'status' => 200,
            'pageTitle' => $section_title,
            'html' => $sections_content [ $section ]
        ];
        $res = json_encode($arr_res);
        header('Content-Type: application/json');
        echo $res;
        exit;
    }
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <meta name="description" content="">
        <meta name="author" content="">
        <title id="page-title"><?=$section_title?> | Bryan Pedini</title>
        <link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
        <link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
        <link href='https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
        <link href='https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
        <link href="css/clean-blog.min.css" rel="stylesheet">
        <style>
            html,body {
                scrollbar-width: none;
            }
            ::-webkit-scrollbar {
                width: 0;
            }
        </style>
    </head>
    <body>
        <nav class="navbar navbar-expand-lg navbar-light fixed-top" id="mainNav">
            <div class="container">
                <a class="navbar-brand" href="#" id="navbar-home">Bryan Pedini</a>
                <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
                    Menu <i class="fas fa-bars"></i>
                </button>
                <div class="collapse navbar-collapse" id="navbarResponsive">
                    <ul class="navbar-nav ml-auto">
                        <li class="nav-item dropdown">
                            <a class="nav-link dropdown-toggle" href="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">About me</a>
                            <ul class="dropdown-menu">
                            <li class="dropdown-item"><a href="#" id="navbar-description">A brief description</a></li>
                            <li class="dropdown-divider"></li>
                            <li class="dropdown-item"><a href="#" id="navbar-work">My current projects</a></li>
                            <li class="dropdown-item"><a href="#" id="navbar-ideas">My future ideas</a></li>
                        </ul>
                        </li>
                        <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Contact me</a>
                        <div class="dropdown-menu">
                            <a href="#" class="dropdown-item" id="navbar-contact-form">Web form</a>
                            <a href="#" class="dropdown-item" id="navbar-contact-email">Email</a>
                        </div>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>
        <div id="user-content">

        </div>
        <script src="vendor/jquery/jquery.min.js"></script>
        <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
        <script src="js/clean-blog.min.js"></script>
        <script>
            var $ = jQuery;
            function getPageContent (section) {
                $.ajax({
                    url: 'index.php',
                    method: 'POST',
                    datatype: 'JSON',
                    data: {
                        section: section,
                    },
                    success: function (data) {
                        if ( data.status == 200 ) {
                            $('#page-title').html(data.pageTitle + " | Bryan Pedini");
                            $('#user-content').html(data.html);
                        }
                        else {
                            console.log(data.status + ": " + data.errorText);
                        }
                    },
                    error: function (jqXHR, exception) {
                        console.log(jqXHR.status + ": " +  jqXHR.responseText + " | - | " + exception);
                    },
                });
            }
            $(function() {
                $('#navbar-home').on('click', function (clickEvent) { getPageContent (1) } );
                $('#navbar-description').on('click', function (clickEvent) { getPageContent (2) });
                $('#navbar-work').on('click', function (clickEvent) { getPageContent (3) });
                $('#navbar-ideas').on('click', function (clickEvent) { getPageContent (4) });
                $('#navbar-contact-form').on('click', function (clickEvent) { getPageContent (5) });
                $('#navbar-contact-email').on('click', function (clickEvent) { window.location.href = "mailto:bryanpedini@bryanpedini.it"; });
                $('ul.nav li.dropdown').hover(function() {
                    $(this).find('.dropdown-menu').stop(true, true).fadeIn(500);
                }, function() {
                    $(this).find('.dropdown-menu').stop(true, true).fadeOut(500);
                });
                getPageContent(0);
            });
        </script>
    </body>
</html>