<?php
require_once ($_SERVER['DOCUMENT_ROOT']."/inc/auth.php");
require_once ($_SERVER['DOCUMENT_ROOT']."/inc/languages/" . HTML_LANG . ".php");

// Удаление OU
if (getPOST("remove") !== null) {
    $fid = getPOST("f_id", null, []);
    if (!empty($fid) && is_array($fid)) {
        foreach ($fid as $val) {
            $val = (int)$val;
            delete_group($db_link,$val);
        }
    }
    header("Location: " . $_SERVER["REQUEST_URI"]);
    exit;
}

// Создание нового OU
if (getPOST("create") !== null) {
    $ou_name = trim(getPOST("new_ou", null, ''));
    if ($ou_name !== '') {
        insert_record($db_link, "ou", ['ou_name' => $ou_name]);
    }
    header("Location: " . $_SERVER["REQUEST_URI"]);
    exit;
}

unset($_POST);
require_once ($_SERVER['DOCUMENT_ROOT']."/inc/header.php");
?>
<div id="cont">
<b><?php echo WEB_list_ou; ?></b><br>
<form name="def" action="index.php" method="post">
<table class="data">
<tr align="center">
<td><input type="checkbox" onClick="checkAll(this.checked);"></td>
<td><b>Id</b></td>
<td><b><?php echo WEB_cell_flags; ?></b></td>
<td><b><?php echo WEB_cell_name; ?></b></td>
<td><b><?php echo WEB_cell_dynamic; ?></b></td>
<td>
<input type="submit" onclick="return confirm('<?php echo WEB_msg_delete; ?>?')" name="remove" value="<?php echo WEB_btn_delete; ?>">
</td>
</tr>
<?php
$t_ou = get_records_sql($db_link,'SELECT * FROM ou ORDER BY ou_name');
foreach ($t_ou as $row) {
    print "<tr align=center>\n";
    print "<td class=\"data\" style='padding:0'><input type=checkbox name=f_id[] value='{$row['id']}'></td>\n";
    print "<td class=\"data\"><input type=\"hidden\" name='r_id[]' value='{$row['id']}'>{$row['id']}</td>\n";
    $flag='';
    if ($row['default_users'] == 1) { $flag='D'; }
    if ($row['default_hotspot'] == 1) { $flag='H'; }
    $rules_count = get_record_sql($db_link, "SELECT COUNT(id) as cnt FROM auth_rules WHERE ou_id=?", [$row['id']]);
    if (!empty($rules_count) && $rules_count['cnt']>0) { $flag .='['.$rules_count['cnt'].']'; }
    print "<td class=\"data\">$flag</td>\n";
    print "<td class=\"data\">"; print_url($row['ou_name'],"/admin/groups/edit_group.php?id=".$row['id']); print "</td>\n";
    print_yes_no($row['dynamic'],'custom','data');
    print "<td class=\"data\"></td>\n";
    print "</tr>\n";
}
?>
</table>
<div>
    <input type=text name=new_ou value="Unknown"></td>
    <input type="submit" name="create" value="<?php echo WEB_btn_add; ?>">
</div>
</form>
<?php
require_once ($_SERVER['DOCUMENT_ROOT']."/inc/footer.php");
?>
