<?php
/**
 * /subs/parts/userarea/admin.php
 * @version 1.0
 * @desc Userarea: admin area
 * @author Fándly Gergő Zoltán (gergo@systemtest.tk, systemtest.tk)
 * @copy 2018 Fándly Gergő Zoltán
 * License:
    Systemtest.tk website's.
    Copyright (C) 2018  Fándly Gergő Zoltán

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 **/
?>

<div id="userlist">
    <h2><?php echo $lang['userlist'] ?></h2>
    <table class="footable">
        <thead>
            <tr>
                <th><?php echo $lang['id'] ?></td>
                <th><?php echo $lang['username'] ?></td>
                <th data-breakpoints="xs sm"><?php echo $lang['fullname'] ?></td>
                <th data-breakpoints="xs sm"><?php echo $lang['email'] ?></td>
                <th data-breakpoints="xs sm"><?php echo $lang['accesslevel'] ?></td>
                <th data-breakpoints="xs sm"><?php echo $lang['quota'] ?></td>
                <th data-breakpoints="xs sm"><?php echo $lang['operations'] ?></td>
            </tr>
        </thead>
        <tbody>
            <?php
            $sql=$db->prepare("SELECT id, username, fullname, email, accesslevel, quota FROM users WHERE id<>1");
            $sql->execute();
            while($row=$sql->fetch(PDO::FETCH_ASSOC)){
                echo "
                <tr>
                    <td>".$row['id']."</td>
                    <td>".$row['username']."</td>
                    <td>".$row['fullname']."</td>
                    <td>".$row['email']."</td>
                    <td>".$row['accesslevel']."</td>
                    <td>".$row['quota']."</td>
                    <td>
                        <button type=\"button\" onclick=\"adminNewPassword(".$row['id'].")\">".$lang['ch_passwd']."</button>
                        <button type=\"button\" onclick=\"adminChangeLevel(".$row['id'].")\">".$lang['ch_accesslevel']."</button>
                        <button type=\"button\" onclick=\"adminChangeQuota(".$row['id'].")\">".$lang['ch_quota']."</button>
                    </td>
                </tr>
                ";
            }
            ?>
        </tbody>
    </table>
</div>
<hr class="placeholder">
<div id="requestlist">
    <h2><?php echo $lang['requestlist'] ?></h2>
    <table class="footable">
        <thead>
            <tr>
                <th><?php echo $lang['id'] ?></td>
                <th><?php echo $lang['date'] ?></td>
                <th data-breakpoints="xs sm"><?php echo $lang['username'] ?></td>
                <th data-breakpoints="all"><?php echo $lang['pgp_public'] ?></th>
                <th data-breakpoints="xs sqm"><?php echo $lang['operations'] ?></th>
            </tr>
        </thead>
        <tbody>
            <?php
            $sql=$db->prepare("SELECT dr.id, dr.date, u.username, dr.pgp FROM data_requests AS dr INNER JOIN users AS u ON (u.id=dr.user) WHERE finished=0 ORDER BY date DESC");
            $sql->execute();
            while($row=$sql->fetch(PDO::FETCH_ASSOC)){
                echo "
                <tr>
                    <td>".$row['id']."</td>
                    <td>".$row['date']."</td>
                    <td>".$row['username']."</td>
                    <td>".str_replace("\n", "<br>", $row['pgp'])."</td>
                    <td>
                        <button type=\"button\" onclick=\"adminFinishRequest(".$row['id'].", this)\">".$lang['finish'] ."</button>
                    </td>
                </tr>
                ";
            }
            ?>
        </tbody>
    </table>
</div>
<hr class="placeholder">
<div id="newuser">
    <form method="POST" class="ajaxform" onsubmit="adminNewUser()" id="usernewForm">
        <fieldset style="margin: auto">
            <legend><?php echo $lang['new_user'] ?></legend>
            <table>
                <tr>
                    <td><?php echo $lang['username'].": " ?></td>
                    <td><input type="text" name="usernew_username" placeholder="<?php echo $lang['username']."..." ?>" required></td>
                </tr>
                <tr>
                    <td><?php echo $lang['fullname'].": " ?></td>
                    <td><input type="text" name="usernew_fullname" placeholder="<?php echo $lang['fullname']."..." ?>" required></td>
                </tr>
                <tr>
                    <td><?php echo $lang['email'].": " ?></td>
                    <td><input type="email" name="usernew_email" placeholder="<?php echo $lang['email']."..." ?>"></td>
                </tr>
                <tr>
                    <td><?php echo $lang['accesslevel'].": " ?></td>
                    <td><input type="number" min="0" max="3" name="usernew_accesslevel" placeholder="<?php echo $lang['accesslevel']."..." ?>" required></td>
                </tr>
                <tr>
                    <td><?php echo $lang['quota'].": " ?></td>
                    <td><input type="number" min="-1" value="100" name="usernew_quota" placeholder="<?php echo $lang['quota']."..." ?>" required></td>
                </tr>
                <tr>
                    <td><?php echo $lang['password'].": " ?></td>
                    <td><input type="password" name="usernew_password" placeholder="<?php echo $lang['password']."..." ?>" required></td>
                </tr>
                <tr>
                    <td><?php echo $lang['password_confirm'].": " ?></td>
                    <td><input type="password" name="usernew_password_confirm" placeholder="<?php echo $lang['password_confirm']."..." ?>" required></td>
                </tr>
            </table>
            <br>
            <br>
            <button type="submit" form="usernewForm"><?php echo $lang['ok'] ?></button>
            <button type="reset" form="usernewForm"><?php echo $lang['cancel'] ?></button>
        </fieldset>
    </form>
</div>
