<?php 
    /*-------------------------
    Autor: Amner Saucedo Sosa
    Web: www.abisoftgt.net
    E-Mail: waptoing7@gmail.com
    ---------------------------*/
    $active11="active";
    include "header.php";
    include "sidebar.php";

    $id=intval($_GET['id']);

    if (isset($_GET['id']) && !empty($_GET['id'])){
        $id=$_GET["id"];
    } else {
        header("Location: usuarios.php");  
    }

    if($is_admin!=1){
        //header("location: dashboard.php");
        print "<script>window.location='dashboard.php?error';</script>";
    }

    $sql=mysqli_query($con, "select * from user where id=$id");
    $rows=mysqli_fetch_array($sql);
        $id=$rows['id'];
        $name=$rows['name'];
        $lastname=$rows['lastname'];
        $email=$rows['email'];
        $username=$rows['username'];
        $is_admin=$rows['is_admin'];
    
?>

    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <h1>Usuarios</h1>
            <ol class="breadcrumb">
                <li><a href="dashboard.php"><i class="fa fa-dashboard"></i> Dashboard</a></li>
                <li><a href="usuarios.php"><i class="fa fa-th-child"></i> Usuarios</a></li>
                <li class="active">Editar Usuario</li>
            </ol>
        </section>

        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-md-3"></div>
                <div class="col-xs-12 col-md-6">
                <div id="result"></div>
                    <!-- general form elements -->
                    <div class="box box-success">
                        <div class="box-header with-border">
                            <h3 class="box-title">Editar Usuario</h3>
                        </div>
                    <!-- /.box-header -->
                    <!-- form start -->
                        <form role="form" method="post" name="upd" id="upd">
                            <div class="box-body">
                                <div class="form-group">
                                  <label for="usuario">Usuario:</label>
                                  <input type="text" name="usuario" class="form-control" id="usuario" placeholder="Usuario" value="<?php echo $username ?>">
                                </div>
                                <div class="form-group">
                                  <label for="nombre">Nombre:</label>
                                  <input type="text" name="nombre" class="form-control" id="nombre" placeholder="Nombre" value="<?php echo $name ?>">
                                </div>
                                <div class="form-group">
                                  <label for="apellidos">Apellido:</label>
                                  <input type="text" name="apellidos" class="form-control" id="apellidos" placeholder="Apellido" value="<?php echo $lastname ?>">
                                </div>
                                <div class="form-group">
                                  <label for="email">E-mail:</label>
                                  <input type="email" name="email" class="form-control" id="email" placeholder="Correo Electrónico" value="<?php echo $email ?>">
                                </div>
                                <div class="form-group">
                                  <label for="password">Contraseña</label>
                                  <input type="password" name="password" class="form-control" id="password" placeholder="Contraseña" value="">
                                  <p class="text-info">La contraseña solo se modifica si escribes algo, en caso contrario no.</p>
                                </div>
                                <div class="checkbox">
                                  <label>
                                    <input type="checkbox" <?php if($is_admin==1){echo "checked"; } ?> name="is_admin"> Administrador
                                  </label>
                                </div>
                                <input type="hidden" name="mod_id" value="<?php echo $id ?>">
                            </div>
                            <!-- /.box-body -->
                            <!-- .|. -->
                            <div class="box-footer">
                                <button type="submit" id="save_data" class="btn btn-success">Actualizar</button>
                            </div>
                        </form>
                    </div><!-- /.box -->
                </div><!-- /.col -->
            </div><!-- /.row -->
        </section><!-- /.content -->
    </div><!-- /.content-wrapper -->

  <?php include "footer.php" ?>

<script>
$( "#upd" ).submit(function( event ) {
  $('#upd_data').attr("disabled", true);
  
 var parametros = $(this).serialize();
     $.ajax({
            type: "POST",
            url: "action/upduser.php",
            data: parametros,
             beforeSend: function(objeto){
                $("#result").html("Mensaje: Cargando...");
              },
            success: function(datos){
            $("#result").html(datos);
            $('#upd_data').attr("disabled", false);
            load(1);
          }
    });
  event.preventDefault();
})

</script>