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

$userexists = "false";
$nousername = "false";

/* if (isset($_GET["id"]) <> "") {
  $adminid = $_GET["id"];
  } else {
  header( "Location: editadmin.php" );
  } */
if (isset($_GET["p"]) and $_GET["p"] <> "") {
    //exit("IF 1");
    //$adminid = $_GET["id"];
    $params = json_decode(base64_decode($_GET["p"]), true);
    $id = $params["id"];
    $aid = $params["aid"];
    //var_dump(base64_decode($_GET["p"]));
    if ($_SESSION["adminlevel"] == 1 || $id == $_SESSION["adminid"]) {
        //exit("IF 2 - {$_SESSION["adminlevel"]}");
        if ($params["aid"] != $_SESSION["adminid"]) {
            if ($id != $_SESSION["adminid"]) {
                header("Location: admins.php");
            }
        }
    }
} else {
    //exit("ELSE 2");
    header("Location: admins.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_admins WHERE username='" . $_POST["newusername"] . "'");
        $rowcheck = mysql_num_rows($sql);
        if ($rowcheck == 1) {
            $userexists = "true";
            mysql_close($conn);
        } else {
            $userexists = "false";
            mysql_query("UPDATE cmum_admins SET username='" . $_POST["newusername"] . "' WHERE id='" . $_POST["adminid"] . "'") or sqlerror();
            mysql_close($conn);
            header("Location: editadmin.php?p=" . $_GET["p"] . "&chngusername=true");
        }
    }
}

if (isset($_POST["baction"]) and $_POST["baction"] == "Cancel") {
    header("Location: editadmin.php?id=" . $_POST["adminid"]);
}
?>
<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>
                <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_admins WHERE id='" . $id . "'");
                    $line = mysql_fetch_array($sql);
                    mysql_close($conn);
                    ?>
                    <form name="editadminname" action="editadminname.php?p=<?php echo $_GET['p']; ?>" method="post">
                        <fieldset>         
                            <legend>Alterar Login Revenda:</legend>
                            <table>
                                <tr>
                                    <td>Login Atual: <strong><?php print($line["username"]); ?></strong></td></tr>
                                <input type="hidden" name="adminid" value="<?php print($line["id"]); ?>">
                                    <input type="hidden" name="username" value="<?php print($line["name"]); ?>">
                                        <?php
                                        if ($userexists == "true") {
                                            print("<div class='alert alert-danger'>Nome de usuario ja existe</div>");
                                        }
                                        if ($nousername == "true") {
                                            print("<div class='alert alert-danger'>Voce deve digitar um nome de usuario.</div>");
                                        }
                                        ?>
                                        <tr>
                                            <td>Novo Login:</td>
                                            <td><input type="text" name="newusername" size="20" tabindex="1" class="form-control" value="<?php if ($userexists == "true") {
                                            print($_POST["newusername"]);
                                        } ?>"/></td>
                                            <!--<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)">-->
                                            <td>
                                                <button name="baction" type="submit" value="Save" accesskey="s" alt="Salvar" title="Salvar" class="btn btn-default" style="">
                                                    <img src="images/ok.png"  width="25px" alt="Adicionar" title="Salvar"/>
                                                </button>
                                            </td>
                                            <td>
                                                <button name="baction" type="submit" value="Cancel" accesskey="s" alt="Cancelar" title="Cancelar" class="btn btn-default" style="">
                                                    <img src="images/cancel.png"  width="25px" alt="Cancelar" title="Cancelar"/>
                                                </button> 
                                            </td> 
                                        </tr>
                                        </table>
                                        </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>
                                                <td height="20" width="780">&nbsp;</td>
                                            </tr>
                                        </table>
<?php
require("includes/footer.php");
?>
                                        </body>
                                        </html>
