<?php
include "../../classes/autoload.php";

$html = new html("");

$app = new inner_app();
$app->quick_bar();
$app->app_start();

?>
<form action="save_user_types.php" method="post">
    <div class="column width_80 padding background_1 border_radius">
        <div class="column width_100">
            <h1>ADD USER TYPE</h1>
            <input type="text" name="name" placeholder="NAME" required>


            <h2>APP ACCESS</h2>
            <table>
                <thead>
                    <tr>
                        <th>ACCESS</th>
                        <!-- <th>ADD</th>
                        <th>EDIT</th>
                        <th>VIEW</th> -->
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td><input type="checkbox" name="admin_access">ADMIN</td>
                        <!-- <td></td>
                        <td></td>
                        <td></td> -->
                    </tr>
                    <?php
                    $path = $_SERVER["DOCUMENT_ROOT"] . "/app/";
                    foreach (scandir($path) as $item) {
                        echo "<tr>";
                        if ($item === '.' || $item === '..')
                            continue;
                        if (is_dir($path . DIRECTORY_SEPARATOR . $item)) {
                            if ($item == basename(dirname($_SERVER['REQUEST_URI']))) {
                                continue;
                            }
                            // check if item has a table in database with same name
                            $table_access_res = $db->query("logs", "SHOW TABLES LIKE '%$item%'");
                            if ($table_access_res->num_rows != 0) {
                                echo "<td><input type=\"checkbox\" name=\"app_access[]\" value=\"" . $item . "\">" . strtoupper($item) . "</td>";
                                // echo "<td><input type=\"checkbox\" name=\"" . $item . "_add_access\" ></td>";
                                // echo "<td><input type=\"checkbox\" name=\"" . $item . "_edit_access\" ></td>";
                                // echo "<td>";
                                // $all_columns_on_table = $db->query("$item", "SHOW COLUMNS FROM $item");
                                // if ($all_columns_on_table->num_rows > 0) {
                                //     while ($column = $all_columns_on_table->fetch_assoc()) {
                                //         if ($column["Field"] == "record_id") {
                                //             continue;
                                //         }
                                //         echo "<input type=\"checkbox\" name=\"" . $item . "_view_access[]\" value=\"" . $item . "." . $column["Field"] . "\" >{$column["Field"]} ";
                                //     }
                                // }
                                // echo "</td>";
                            }
                        }
                        echo "</tr>";

                    }
                    ?>
                </tbody>

            </table>
            <button class="button" type="submit">SAVE</button>
        </div>
    </div>

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