<?php


class html
{
    function __construct($title)
    {
        ?>
        <html>

        <head>
            <title><?php echo $title; ?></title>
            <link rel="stylesheet" type="text/css" href="/styles/styles.css?v=<?php echo time(); ?>" media="screen" />
            <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">

        </head>
        <?php
    }

    function header($current_page)
    {
        $db = new db();
        ?>
        <div class="index_page_container">
            <div class="header">
                <div class="logo_container">
                    <img src="../assets/logo.png" class="logo" style="cursor: pointer;" onclick="go_to('../app/home.php')">
                </div>
                <div style="display:flex;flex-direction:row;flex-wrap:nowrap;overflow-x:auto;overflow-y:hidden;flex:1;gap:0.8em;scrollbar-width:none;-ms-overflow-style:none;">
                <button onclick="window.location.href='logout.php'">LOG OUT</button>
                <!-- <button onclick="go_to('/app/home.php')">HOME</button> -->

                <?php
                $path = $_SERVER["DOCUMENT_ROOT"] . "/app";
                $folders = [];

                foreach (scandir($path) as $item) {
                    if ($item === '.' || $item === '..')
                        continue;
                    if (is_dir($path . DIRECTORY_SEPARATOR . $item)) {
                        if ($item == basename(dirname($_SERVER['REQUEST_URI']))) {
                            continue;
                        }
                        // check access to this file under user permissions
                        if (isset($_SESSION['user_id'])) {
                            $res = $db->query("users", "SELECT * FROM users WHERE record_id = '{$_SESSION['user_id']}'");
                            $user = $res->fetch_assoc();
                            $user_type = $user['type'];
                            $check_res = $db->query("user_type_app_access", "SELECT * FROM user_type_app_access WHERE user_type_id = $user_type AND file_name = '$item'");
                            if ($check_res->num_rows == 0) {
                                continue;
                            }

                            $url = "/app/{$item}/home.php";
                            ?> <button style="flex-shrink:0;"
                                onclick="go_to('<?php echo $url; ?>')"><?php echo strtoupper(str_replace('_', ' ', $item)); ?></button> <?php
                        }
                    }
                }
                ?>
                <?php if ($_SESSION['user_type'] == 1) { ?>
                    <button style="flex-shrink:0;" onclick="go_to('/default_app_data/home.php')">ADMIN</button>
                <?php } ?>
                <?php if ($_SESSION['user_id'] == 1) { ?>
                    <button style="flex-shrink:0;" onclick="document.getElementById('app_frame').contentWindow.location.reload()">REFRESH</button>
                <?php } ?>
                </div>
            </div>
            <div class="index_body_container">
                <iframe src="app/home.php" class="index_body" id="app_frame"></iframe>
            </div>
        </div>

        <script>
            function go_to(url) {
                iframe = document.getElementById("app_frame");
                iframe.src = url;
            }

        </script>


        <?php
    }


    function app_quick_bar()
    {
        ?>
        <div class="app_quick_bar">
            <button class="quick_bar_buttons" onclick="window.location.href = 'new_job_card.php'">NEW</button>
            <button class="quick_bar_buttons" onclick="search();">FIND</button>
        </div>

        <?php
    }

}


class app
{
    function __construct($title)
    {

    }

    function add_button()
    {

    }
}