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

$html = new html("");

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

$user_type_data = $db->query("user_types", "SELECT * FROM user_types WHERE record_id = '" . $_GET["record_id"] . "'");
$user_type_data = $user_type_data->fetch_assoc();
if ($user_type_data['admin_access'] == 1) {
    $admin_access = "CHECKED";
} else {
    $admin_access = "";

}

?>
<form action="update_user_types.php" method="post">
    <div class="column width_80 padding background_1 border_radius">
        <div class="column width_100">
            <h1>EDIT USER TYPE</h1>
            <input type="text" name="name" placeholder="NAME" value="<?php echo $user_type_data['name']; ?>" required>
            <input type="text" name="record_id" hidden placeholder="record_id"
                value="<?php echo $user_type_data['record_id']; ?>" 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" <?php echo $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) {
                            // check if file_name in db for this type and check if so
                            $check_app_access_res = $db->query("user_type_app_access", "SELECT * FROM user_type_app_access WHERE user_type_id = '" . $_GET["record_id"] . "' AND file_name = '" . $item . "'");
                            if ($check_app_access_res->num_rows != 0) {
                                echo "<td><input type=\"checkbox\" name=\"app_access[]\" value=\"" . $item . "\" CHECKED>" . strtoupper($item) . "</td>";
                            } else {
                                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>