<?php
require("includes/logincheck.php");
require("config.php");
require("includes/debug.php");
require("includes/functions.php");
require("includes/settings.php");
//require("includes/admincheck.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"];
    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");
}
?>
<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"/>
            <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
            <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
            <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
            <link rel="stylesheet" href="/resources/demos/style.css" />
            <script>
                $(function() {
                    $("#expiredate").datepicker({
                        dateFormat: "yy-mm-dd <?php echo date("H:i:s"); ?>"
                        /*showOn: "button",
                        buttonImage: "images/calendar/calendar.gif",
                        buttonImageOnly: true*/
                    });
                });
            </script>
            <!--<link href="css/calendar.css" type="text/css" media="screen" rel="stylesheet">
            <script type="text/javascript" src="js/mootools-core.js"></script>
            <script type="text/javascript" src="js/mootools-more.js"></script>
            <script type="text/javascript" src="js/mootools-locale.js"></script>
            <script type="text/javascript" src="js/calendar.js"></script>
            <script type="text/javascript">
            window.addEvent('domready', function() {
            //new CalendarEightysix('startdate', { prefill: false, startMonday: true, toggler: 'togglestart', format: '%Y-%m-%d', hiddenInputName: 'stdate' });
            new CalendarEightysix('expiredate', { prefill: false, startMonday: true, toggler: 'toggleexpire', format: '%Y-%m-%d', hiddenInputName: 'exdate' });
            });
            </script>-->
    </head>

    <body>
    <!--<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="980px" 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 colspan="3">
                    <form name="editadmin" action="admins.php" method="post">
                        
                        <fieldset>
                            <legend>Fomulario Editar Revendas</legend>
                            <?php
                            if (isset($_GET["error"]) and $_GET["error"] == "1") {
                                print("<div class='alert alert-danger'>As senhas não combinam.</div>");
                            }
                            if (isset($_GET["chngusername"]) and $_GET["chngusername"] == "true") {
                                print("<div class='alert alert-success'>Usuario alterado com sucesso!</div>");
                            }
                            $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);
                            $enab = "";
                            if ($id == $_SESSION["adminid"] && $_SESSION["adminlevel"] == 1) {
                                $enab = 'readOnly';
                            }
                            ?>
                            <input type="hidden" name="adminid" value="<?php print($line["id"]); ?>"/>
                        <input type="hidden" name="username" value="<?php print($line["username"]); ?>"/>
                            </td>
                            </tr>
                            <tr>
                                <td>Ativo:<br>
                                                                                                <select name="enabled" class="form-control" tabindex="4">
                                                                                                    <option <?php
                            if ($line["enabled"] == "true") {
                                print("selected=\"selected\"");
                            }
                            ?>>True</option>
                                                                                                        <?php if ($id != $_SESSION["adminid"] || $_SESSION["adminlevel"] != 1): ?>
                                                                                                        <option <?php
                                                                                                        if ($line["enabled"] == "false") {
                                                                                                            print("selected=\"selected\"");
                                                                                                        }
                                                                                                            ?>>False</option>
                                                                                                        <?php endif; ?>
                                                                                                </select></td><td>&nbsp;</td>
                                <td>Tipo:<br>
                                                                                                           <select name="level" class="form-control" tabindex="5">
                                                                                                                <?php if ($_SESSION["adminlevel"] != 1): ?>
                                                                                                                    <option <?php
                                                                                                                if ($line["level"] == "0") {
                                                                                                                    print("selected=\"selected\"");
                                                                                                                }
                                                                                                                    ?> value="0">Administrador</option>
                                                                                                                    <?php endif; ?>
                                                                                                                <option <?php
                                                                                                                    if ($line["level"] == "1") {
                                                                                                                        print("selected=\"selected\"");
                                                                                                                    }
                                                                                                                    ?> value="1">Revendedor</option>
                                                                                                            </select></td>
                            </tr>
                            <tr>
                                <td><br/>
                                    <div class="alert alert-info">
                                        <?php 
                                        $p = base64_encode("{\"id\":{$id},\"aid\":{$aid}}");
                                        ?>
                                        Login: 
                                        <strong><?php print($line["username"]); ?></strong>
                                        <sup><a href="editadminname.php?p=<?php echo $p;?>"><img src="images/edit.png" title="Editar" alt="Editar"/></a></sup>
                                    </div>
                                </td>
                                <td>&nbsp;</td>
                                <td>
                                    
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Nova senha:<br>
                                    <input type="text" name="padmin" size="20" class="form-control" tabindex="1">
                                </td>
                                <td>&nbsp;</td>
                                <td>
                                    Repita a senha:<br>
                                   <input type="text" name="padmin2" size="20" class="form-control" tabindex="2">
                                </td>
                            </tr>
            <tr>
                <td>
                    Nome de Exibição:<br>
                   <input type="text" name="radmin" size="20" tabindex="3" class="form-control" value="<?php print($line["realname"]); ?>">
                </td>
                <td>&nbsp;</td>
                <td>
                   Vencimento:<br>
                   <input id="<?php if ($id == $_SESSION["adminid"] && $_SESSION["adminlevel"] == 1){echo '';}else{echo 'expiredate';}?>" type="text" name="expiredate" size="20" class="form-control" tabindex=""  <?php echo $enab; ?> value="<?php print($line["expiredate"]); ?>">   
                </td>
            </tr>
            <tr>
                <td>&nbsp;</td>
            </tr>
            <tr><td>
                                                            
                                                                            
                                                                                                                                                                                                                                              
                                                                                                                            
                                                                                                            
                                                                                                                            <button name="baction" type="submit" value="Save" accesskey="s" alt="Salvar" title="Salvar" style="" class="btn btn-default">
                                                                                                                                <img src="images/ok.png"  width="25px" alt="Salvar" title="Salvar"/>
                                                                                                                            </button>
                                                                                                                            <button name="baction" type="submit" value="Change username" accesskey="s" alt="Modificar Login" title="Modificar Login" style="" class="btn btn-default">
                                                                                                                                <img src="images/eduser.png"  width="25px" alt="Modificar Login" title="Modificar Login"/>
                                                                                                                            </button>
                                                                                                                            <button name="baction" type="submit" value="Cancel" accesskey="s" alt="Cancelar" title="Cancelar" style="" class="btn btn-default">
                                                                                                                                <img src="images/cancel.png"  width="25px" alt="Cancelar" title="Cancelar"/>
                                                                                                                            </button>
                                                                                                                            </fieldset>
                                                                                                                            </form>
                                                                                                                            </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>
