<?php
require("config.php");
require("includes/debug.php");
require("includes/functions.php");
require("includes/logincheck.php");
require("includes/settings.php");

$userexists = "false";
$nousername = "false";
$opc_log = unserialize($_SESSION["_opcoes_log"]);
if (is_null($opc_log)) {
    $opc_log = array();
}
if (array_search('1', $opc_log) === false) {
    exit('Voce nao tem permissao para alterar login');
}

/*if (isset($_GET["id"]) <> "") {
    $userid = $_GET["id"];
} else {
    header("Location: manusers.php");
}*/
if (isset($_GET["p"]) and $_GET["p"] <> "") {
    //exit("IF 1");
    //$adminid = $_GET["id"];
    //exit(var_dump(base64_decode($_GET["p"])));
    $params = json_decode(base64_decode($_GET["p"]), true);
    //var_dump($params);
    $uid = $params["id"];
    if ($_SESSION["adminlevel"] == 1) {
        //exit("IF 2 - {$_SESSION["adminlevel"]}");
        if ($params["aid"] != $_SESSION["adminid"]) {
            //exit("IF 3 {$params["aid"]} - {$_SESSION["adminid"]}");
            //exit(var_dump($params));
            //exit(var_dump($_SESSION["adminid"]));
            header("Location: manusers.php");
        }
    }
} else {
    //exit("ELSE 2");
    header("Location: manusers.php");
}

if (isset($_POST["baction"]) and $_POST["baction"] == "Save") {
    if ($_POST["newusername"] == "") {
        $nousername = "true";
    } else {
        $conn = @mysql_connect($dbhost, $dbuser, $dbpass) or sqlerror();
        mysql_select_db($dbname, $conn);
        $sql = mysql_query("SELECT * FROM cmum_udb WHERE name='" . $_POST["newusername"] . "'");
        $rowcheck = mysql_num_rows($sql);
        if ($rowcheck == 1) {
            $userexists = "true";
            mysql_close($conn);
        } else {
            $userexists = "false";
            $login = $_SESSION['_indt_rev'] . $_POST["newusername"]; // Cadastrar login com prefixo
            mysql_query("UPDATE cmum_udb SET name='" . $login . "' WHERE id='" . $_POST["userid"] . "'") or sqlerror();
            mysql_close($conn);
            $p = base64_encode("{\"id\":{$_POST["userid"]},\"aid\":{$_SESSION['adminid']}}");
            header("Location: edituser.php?p=" . $p . "&chngusername=true");
        }
    }
}

if (isset($_POST["baction"]) and $_POST["baction"] == "Cancel") {
    $p = base64_encode("{\"id\":{$_POST["userid"]},\"aid\":{$_SESSION['adminid']}}");
    header("Location: edituser.php?p=" . $p);
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta http-equiv="Content-Type" content="text/html; charset=<?php print($charset); ?>" />
            <title><?php print($longtitle); ?></title>
            <link rel="shortcut icon" href="favicon.ico">
                <link rel="apple-touch-icon" href="favicon.png"> 
                  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
                        <link href="css/main.css" rel="stylesheet" type="text/css">
                            </head>

                            <body onLoad="focus();
                                    editusername.newusername.focus()">
                            <!--<table width="980" border="0" align="center" valign="top" cellpadding="0" cellspacing="0" style="border-left: 1px solid #DDDDDD; border-right: 1px solid #DDDDDD; text-align: left;">
                              <tr bgcolor="#006699">
                                <td height="22" colspan="6">&nbsp;</td>
                              </tr>
                              <tr bgcolor="#006699">
                                <td width="15" bgcolor="#006699">&nbsp;</td>
                                <td width="751" height="120" bgcolor="#006699"><?php //require("includes/header.php");   ?></td>
                                <td width="15" bgcolor="#006699">&nbsp;</td>
                              </tr>
                              <tr bgcolor="#006699">
                                <td height="18">&nbsp;</td>
                                <td height="18"><center><?php //require("includes/menu.php");   ?></center></td>
                                <td height="18">&nbsp;</td>
                              </tr>
                                <tr bgcolor="#006699">
                                <td height="18">&nbsp;</td>
                                <td height="18">&nbsp;</td>
                                <td height="18">&nbsp;</td>
                              </tr>
                            </table>-->
                                <?php require_once 'topo.php'; ?>
                                <table width="980" height="20" border="0" align="center" valign="top" cellpadding="0" cellspacing="0" style="border-left: 0px solid #DDDDDD; border-right: 0px solid #DDDDDD; text-align: left;">
                                    <tr bgcolor="#FFFFFF">
                                        <td width="15" height="20">&nbsp;</td>
                                        <td height="20" colspan="4">
                                            <?php
                                            $conn = @mysql_connect($dbhost, $dbuser, $dbpass) or sqlerror();
                                            mysql_select_db($dbname, $conn);
                                            $sql = mysql_query("SELECT * FROM cmum_udb WHERE id='" . $uid . "'");
                                            $line = mysql_fetch_array($sql);
                                            mysql_close($conn);
                                            ?>
                                            <form name="editusername" action="editusername.php?id=<?php print($line["id"]); ?>" method="post">
                                                <fieldset> 
                                                    <legend>Alterar Login Usuario:</legend>
                                                    Login Usuario Atual: <strong><?php print($line["name"]); ?></strong><br><br>
                                                            <input type="hidden" name="userid" value="<?php print($line["id"]); ?>">
                                                                <input type="hidden" name="username" value="<?php print($line["name"]); ?>">
                                                                    <?php
                                                                    if ($userexists == "true") {
                                                                        print("<font color=\"#FF0000\">Nome de usu�rio ja existe!</font><br><br>");
                                                                    }
                                                                    if ($nousername == "true") {
                                                                        print("<font color=\"#FF0000\">Voce deve digitar um nome de usuario!</font><br><br>");
                                                                    }
                                                                    ?>
                                                                    Novo Login Usuario:<br>
                                                                        <div class="" style="width: 25%">
                                                                            <input type="text" name="newusername" size="20" class="form-control" tabindex="1" value="<?php
                                                                            if ($userexists == "true") {
                                                                                print($_POST["newusername"]);
                                                                            }
                                                                            ?>">
                                                                        </div>
                                                                        <br><br>
                                                                    <!--<input type="submit" name="baction" value="Save" id="submit" tabindex="2" accesskey="s" title="(ALT+S)">
                                                                    <input type="submit" name="baction" value="Cancel" id="submit" tabindex="3" accesskey="q" title="(ALT+Q)">-->
                                                                                <button name="baction" type="submit" value="Save" accesskey="s" alt="Salvar" title="Salvar" class="btn btn-success">
                                                                                    Salvar
                                                                                </button>
                                                                                <button name="baction" type="submit" value="Cancel" accesskey="s" alt="Cancelar" title="Cancelar" class="btn btn-danger">
                                                                                    Cancelar
                                                                                </button>
                                                                                </fieldset>
                                                                                </form>
                                                                                </td>
                                                                                <td width="15" height="20">&nbsp;</td>
                                                                                </tr>
                                                                                </table>
                                                                                <table width="980" height="20" border="0" align="center" valign="top" cellpadding="0" cellspacing="0" style="border-left: 0px solid #DDDDDD; border-right: 0px solid #DDDDDD; border-bottom: 0px solid #DDDDDD; text-align: left;">
                                                                                    <tr bgcolor="#FFFFFF">
                                                                                        <td height="20" width="780">&nbsp;</td>
                                                                                    </tr>
                                                                                </table>
                                                                                <?php
                                                                                require("includes/footer.php");
                                                                                ?>
                                                                                </body>
                                                                                </html>
