<?php
include $_SERVER['DOCUMENT_ROOT'] . '/classes/autoload.php';
// var_dump($_POST);


$user_type_name = $_POST['name'];
$admin_access = isset($_POST['admin_access']) ? 1 : 2;
$app_access_array = $_POST['app_access'];
// echo "USER TYPE: " . $user_type_name . "<br>";
// echo "ADMIN ACCESS: " . $admin_access . "<br>";
// echo "ACCESS TO : <br>";
$db->query("user_types", "UPDATE `user_types` SET `name`='$user_type_name', `admin_access`='$admin_access' WHERE `record_id`='" . $_POST['record_id'] . "'");
$db->query("user_type_app_access", "DELETE FROM `user_type_app_access` WHERE `user_type_id`='" . $_POST['record_id'] . "'");
foreach ($app_access_array as $app_access) {
    // echo "UNDER $app_access USER TYPE CAN: <br>";
    $db->query("user_type_app_access", "INSERT INTO `user_type_app_access`(`user_type_id`, `file_name`) VALUES ('{$_POST['record_id']}','$app_access')");
    // if (isset($_POST[$app_access . '_add_access']) && $_POST[$app_access . '_add_access'] == 'on') {
    //     $add_access = 1;
    //     echo "ADD<br>";
    // }
    // if (isset($_POST[$app_access . '_edit_access']) && $_POST[$app_access . '_edit_access'] == 'on') {
    //     $edit_access = 1;
    //     echo "EDIT<br>";
    // }
    // if (isset($_POST[$app_access . '_view_access'])) {
    //     echo "COLUMNS<br>";
    //     foreach ($_POST[$app_access . '_view_access'] as $view_access) {
    //         echo $view_access;
    //     }
    // }
}



header("location: home.php");