<?php
/************************************************************************/
/*     PROGRAMMER     :  SMA                                            */
/*     SCRIPT NAME    :  adminusers.php                                 */
/*     CREATED ON     :  10/JULY/2006                                   */
/*     MODERNIZED     :  20/AUG/2025                                    */
/*     PURPOSE        :  Create/display admin users (except super admin)*/
/************************************************************************/

include_once '../includes/session.php';
include_once '../includes/constants.php';
include_once '../includes/functions.php';
include_once '../includes/function1.php';

$adminobj = new adminuser();

$note     = isset($_REQUEST['msg']) ? htmlspecialchars($_REQUEST['msg']) : '';
$username = isset($_REQUEST['txt_username']) ? htmlspecialchars($_REQUEST['txt_username']) : '';
$password = isset($_REQUEST['txt_password']) ? htmlspecialchars($_REQUEST['txt_password']) : '';
$email    = isset($_REQUEST['txt_email']) ? htmlspecialchars($_REQUEST['txt_email']) : '';
?>

<form name="frm_users" method="post" action="adminusers_manage.php" onsubmit="return Validate();">
    <table border="0" cellpadding="0" cellspacing="0" class="tablebdr" width="65%" align="center">
        <tr>
            <td align="center" colspan="2" class="tdhead"><b>Add New Admin User</b></td>
        </tr>
        <tr><td height="10"></td></tr>
        <?php if ($note): ?>
            <tr><td colspan="2" align="center" class="textred"><?= $note ?></td></tr>
        <?php endif; ?>
        <tr>
            <td height="30" align="right" width="50%">Username&nbsp;&nbsp;&nbsp;&nbsp;</td>
            <td height="30" align="left" width="50%">
                <input type="text" name="txt_username" maxlength="25" value="<?= $username ?>" />
            </td>
        </tr>
        <tr>
            <td height="30" align="right" width="50%">Password&nbsp;&nbsp;&nbsp;&nbsp;</td>
            <td height="30" align="left" width="50%">
                <input type="text" name="txt_password" maxlength="25" value="<?= $password ?>" />
            </td>
        </tr>
        <tr>
            <td height="30" align="right" width="50%">Email&nbsp;&nbsp;&nbsp;&nbsp;</td>
            <td height="30" align="left" width="50%">
                <input type="text" name="txt_email" value="<?= $email ?>" />
            </td>
        </tr>
        <tr>
            <td height="30" align="center" colspan="2">
                Send Mail to Admin User&nbsp;
                <input type="checkbox" name="chk_mail" />
            </td>
        </tr>
        <tr>
            <td height="30" align="center" colspan="2">
                <input type="submit" name="btn_add" value="Add" />
            </td>
        </tr>
    </table>
    <br />

    <table border="0" cellpadding="0" cellspacing="0" class="tablebdr" width="95%" align="center">
        <tr>
            <td align="center" class="tdhead" colspan="6"><b>Existing Admin Users</b></td>
        </tr>
        <tr><td colspan="6">&nbsp;</td></tr>
        <?php
        $result = $adminobj->getAllAdminUsers();
        if ($result && count($adminobj->username) > 0): ?>
            <tr>
                <td width="30%" align="left" height="25"><b>Username</b></td>
                <td width="16%" align="left" height="25"><b>Password</b></td>
                <td width="30%" align="left" height="25"><b>Email</b></td>
                <td colspan="3" height="25"></td>
            </tr>
            <?php for ($i = 0; $i < count($adminobj->username); $i++): ?>
                <tr>
                    <td width="30%" align="left" height="20"><?= htmlspecialchars($adminobj->username[$i]) ?></td>
                    <td width="16%" align="left" height="20"><?= htmlspecialchars($adminobj->password[$i]) ?></td>
                    <td width="30%" align="left" height="20"><?= htmlspecialchars($adminobj->email[$i]) ?></td>
                    <td width="7%" align="center" height="20">
                        <a href="index.php?Act=adminuser_edit&id=<?= intval($adminobj->userid[$i]) ?>&mode=edit">Edit</a>
                    </td>
                    <td width="10%" align="center" height="20">
                        <a href="index.php?Act=adminuser_privilege&id=<?= intval($adminobj->userid[$i]) ?>">Privileges</a>
                    </td>
                    <td width="7%" align="center" height="20">
                        <a href="#" onclick="deleteValidate('<?= intval($adminobj->userid[$i]) ?>')">Delete</a>
                    </td>
                </tr>
            <?php endfor; ?>
        <?php else: ?>
            <tr><td colspan="6" align="center" class="textred">No Admin Users exist</td></tr>
        <?php endif; ?>
        <tr><td colspan="6">&nbsp;</td></tr>
    </table>
</form>
<br />

<script>
function Validate() {
    if (document.frm_users.txt_username.value.trim() === '') {
        alert('Please enter the username');
        document.frm_users.txt_username.focus();
        return false;
    }
    if (document.frm_users.txt_password.value.trim() === '') {
        alert('Please enter the password');
        document.frm_users.txt_password.focus();
        return false;
    }
    if (document.frm_users.txt_email.value.trim() === '') {
        alert('Please enter the Email Address');
        document.frm_users.txt_email.focus();
        return false;
    }
    return true;
}

function deleteValidate(id) {
    if (confirm('Are you sure to delete the Admin user?')) {
        document.frm_users.action = 'adminusers_manage.php?id=' + id + '&mode=delete';
        document.frm_users.submit();
    }
}
</script>
