<?php
session_start();
$usuario = array();
include('../../php/conn.php');
if(!isset($_SESSION["id"])){
  header('Location: index.php');
}
if(isset($_POST["accion"])){
echo '$_POST["accion"]: '.$_POST["accion"];
  if($_POST["accion"]=="registrar"){
    $q_insert = "INSERT INTO tb_usuario (cod_rol, usu_usuario, pas_usuario, nom_persona, ape_persona, cor_persona, tel_persona, bdOmar_Sucursal, bdOmar_Compania, bdOmar_Tienda, bdOmar_Terminal) VALUES ('".$_POST["rol"]."', '".$_POST["usuario"]."', '".md5($_POST["password"])."', '".$_POST["nombres"]."', '".$_POST["apellidos"]."', '".$_POST["correo"]."', '".$_POST["telefono"]."', ".$_POST["oficina"].", ".$_SESSION["bdOmar_Compania"].", ".$_SESSION["bdOmar_Tienda"].", ".$_SESSION["bdOmar_Terminal"].")";
echo '$q_insert: '.$q_insert;
    if (mysqli_query($enlace, $q_insert)) {
      header('Location: usuario-lista.php');
    }
  }
  else{
    if(is_numeric($_POST["accion"])){
      $q_update = "UPDATE tb_usuario SET cod_rol='".$_POST["rol"]."', usu_usuario='".$_POST["usuario"]."', nom_persona='".$_POST["nombres"]."', ape_persona='".$_POST["apellidos"]."', cor_persona='".$_POST["correo"]."', tel_persona='".$_POST["telefono"]."', bdOmar_Sucursal = ".$_POST["oficina"]." WHERE cod_usuario = ".$_POST["accion"];

      if (mysqli_query($enlace, $q_update)) {
        header('Location: usuario-lista.php');
      }
    }
  }
}
if(isset($_GET["editar"])){
  if(is_numeric($_GET["editar"])){
    if ($resultado = mysqli_query($enlace, "SELECT cod_usuario,cod_rol,usu_usuario,nom_persona,ape_persona,cor_persona,tel_persona, bdOmar_Sucursal FROM tb_usuario WHERE cod_usuario='".$_GET["editar"]."';")) {
      if (mysqli_num_rows($resultado) > 0) {
        while($row = mysqli_fetch_assoc($resultado)) {
          $usuario = $row;
        }
      }
      else{
        header('Location: usuario-nuevo.php');
      }
      mysqli_free_result($resultado);
    }
  }
}
?>
<!DOCTYPE html>
<html lang="es">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <!-- Meta, title, CSS, favicons, etc. -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="icon" href="images/favicon.ico" type="image/ico" />

    <title>Sistema Encuestas | </title>

    <!-- Bootstrap -->
    <link href="../vendors/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome -->
    <link href="../vendors/font-awesome/css/font-awesome.min.css" rel="stylesheet">
    <!-- NProgress -->
    <link href="../vendors/nprogress/nprogress.css" rel="stylesheet">
    <!-- iCheck -->
    <link href="../vendors/iCheck/skins/flat/green.css" rel="stylesheet">
	
    <!-- bootstrap-progressbar -->
    <link href="../vendors/bootstrap-progressbar/css/bootstrap-progressbar-3.3.4.min.css" rel="stylesheet">
    <!-- JQVMap -->
    <link href="../vendors/jqvmap/dist/jqvmap.min.css" rel="stylesheet"/>
    <!-- bootstrap-daterangepicker -->
    <link href="../vendors/bootstrap-daterangepicker/daterangepicker.css" rel="stylesheet">

    <!-- Custom Theme Style -->
    <link href="../build/css/custom.min.css" rel="stylesheet">
  </head>

  <body class="nav-md">
    <div class="container body">
      <div class="main_container">
        <?php include('partes/sidebar.php'); ?>  
        <!-- page content -->
        <div class="right_col" role="main">
          <!-- top tiles -->
          
          
          <div class="row">
            <div class="col-md-12 col-sm-12 col-xs-12">
              <div class="x_panel">
                <div class="x_title">
                  <h2>Registrar Agente</h2>
                  <div class="clearfix"></div>
                </div>
                <div class="x_content">
                  <div class="row">
                    <div class="col-md-12">
                      <form class="form-horizontal form-label-left input_mask" method="POST">
                        <div class="col-md-6 col-sm-6 col-xs-12 form-group has-feedback">
                          <input name="accion" value="<?php if(isset($_GET["editar"])){echo $_GET["editar"];}else{echo "registrar";} ?>" required hidden>
                          <input type="text" class="form-control has-feedback-left" id="usuario" placeholder="Usuario" name="usuario" value="
<?php if(isset($_GET["editar"])){echo $usuario["usu_usuario"];} ?>" required>
                          <span class="fa fa-user form-control-feedback left" aria-hidden="true"></span>
                        </div>

                        <div class="col-md-6 col-sm-6 col-xs-12 form-group has-feedback">
                          <input type="password" class="form-control has-feedback-left" id="password" placeholder="Contraseña" name="password" value="" <?php if(isset($_GET["editar"])){echo "readonly";}else{echo "required";} ?>>
                          <span class="fa fa-user form-control-feedback left" aria-hidden="true"></span>
                        </div>

                        <div class="col-md-6 col-sm-6 col-xs-12 form-group has-feedback">
                          <input type="text" class="form-control has-feedback-left" id="nombre" placeholder="Nombres" name="nombres" value="
<?php if(isset($_GET["editar"])){echo $usuario["nom_persona"];} ?>" required>
                          <span class="fa fa-user form-control-feedback left" aria-hidden="true"></span>
                        </div>

                        <div class="col-md-6 col-sm-6 col-xs-12 form-group has-feedback">
                          <input type="text" class="form-control has-feedback-left" id="apellidos" placeholder="Apellidos" name="apellidos" value="
<?php if(isset($_GET["editar"])){echo $usuario["ape_persona"];} ?>" required>
                          <span class="fa fa-user form-control-feedback left" aria-hidden="true"></span>
                        </div>

                        <div class="col-md-6 col-sm-6 col-xs-12 form-group has-feedback">
                          <input type="text" class="form-control has-feedback-left" id="correo" placeholder="Correo" name="correo" value="
<?php if(isset($_GET["editar"])){echo $usuario["cor_persona"];} ?>" required>
                          <span class="fa fa-user form-control-feedback left" aria-hidden="true"></span>
                        </div>

                        <div class="col-md-6 col-sm-6 col-xs-12 form-group has-feedback">
                          <input type="text" class="form-control has-feedback-left" id="telefono" placeholder="Telefono" name="telefono" value="
<?php if(isset($_GET["editar"])){echo $usuario["tel_persona"];} ?>" required>
                          <span class="fa fa-user form-control-feedback left" aria-hidden="true"></span>
                        </div>

                        <div class="col-md-6 col-sm-6 col-xs-12 form-group has-feedback">
                          <select class="form-control has-feedback-left" id="rol" placeholder="Rol" name="rol" required onchange="f_ShowOficina()">
                            <option value="">Seleccionar Rol</option>
                              <?php

                                $q_rol = "SELECT nom_rol, cod_rol
                                            FROM tb_rol
                                           WHERE est_rol = 1";

                                if ($res_rol = mysqli_query($enlace, $q_rol)){
                                  if (mysqli_num_rows($res_rol) > 0){
                                    while($row_rol = mysqli_fetch_assoc($res_rol)){
                                      echo "<option ".(($usuario["cod_rol"] == $row_rol["cod_rol"]) ? 'selected' : '')." value='".$row_rol["cod_rol"]."'>".$row_rol["nom_rol"]."</option>";
                                    }
                                  }
                                }

                              ?>
                          </select>
                          <span class="fa fa-envelope form-control-feedback left" aria-hidden="true"></span>
                        </div>

                        <div id="div_oficina" class="col-md-6 col-sm-6 col-xs-12 form-group has-feedback" <?php (($usuario["cod_rol"] != 2) ? 'style="display: none;"' : '') ?>>
                          <select class="form-control has-feedback-left" id="oficina" placeholder="Oficina" name="oficina" required>
                            <option value="">Seleccionar Oficina</option>
                              <?php

                                $q_sucursal = "SELECT Id, des_sucursal
                                                FROM tb_sucursal
                                               WHERE estado_sucursal = 'A'
                                              ORDER BY Id";

                                if ($res_sucursal = mysqli_query($enlace, $q_sucursal)){
                                  if (mysqli_num_rows($res_sucursal) > 0){
                                    while($row_sucursal = mysqli_fetch_assoc($res_sucursal)){
                                      // echo "<option ".(($usuario["bdOmar_Sucursal"] == $row_rol["cod_sucursal"]) ? 'selected' : '')." value='".$row_sucursal["cod_sucursal"]."'>".$row_sucursal["des_sucursal"]."</option>";
                                      echo "<option value='".$row_sucursal["Id"]."'>".$row_sucursal["des_sucursal"]."</option>";
                                    }
                                  }
                                }
                              ?>
                          </select>
                          <span class="fa fa-envelope form-control-feedback left" aria-hidden="true"></span>
                        </div>

                        <div class="ln_solid"></div>
                        <div class="form-group">
                          <div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-5">
                             <button class="btn btn-primary" type="reset">Reset</button>
                            <button type="submit" class="btn btn-success">Guardar</button>
                          </div>
                        </div>
                      </form>
                    </div>
                  </div>
                      
                </div>
              </div>
            </div>
          </div>
        </div>
        <!-- /page content -->
      </div>
    </div>

    <!-- jQuery -->
    <script src="../vendors/jquery/dist/jquery.min.js"></script>
    <!-- Bootstrap -->
    <script src="../vendors/bootstrap/dist/js/bootstrap.min.js"></script>
    <!-- FastClick -->
    <script src="../vendors/fastclick/lib/fastclick.js"></script>
    <!-- NProgress -->
    <script src="../vendors/nprogress/nprogress.js"></script>
    <!-- Chart.js -->
    <script src="../vendors/Chart.js/dist/Chart.min.js"></script>
    <!-- gauge.js -->
    <script src="../vendors/gauge.js/dist/gauge.min.js"></script>
    <!-- bootstrap-progressbar -->
    <script src="../vendors/bootstrap-progressbar/bootstrap-progressbar.min.js"></script>
    <!-- iCheck -->
    <script src="../vendors/iCheck/icheck.min.js"></script>
    <!-- Skycons -->
    <script src="../vendors/skycons/skycons.js"></script>
    <!-- Flot -->
    <script src="../vendors/Flot/jquery.flot.js"></script>
    <script src="../vendors/Flot/jquery.flot.pie.js"></script>
    <script src="../vendors/Flot/jquery.flot.time.js"></script>
    <script src="../vendors/Flot/jquery.flot.stack.js"></script>
    <script src="../vendors/Flot/jquery.flot.resize.js"></script>
    <!-- Flot plugins -->
    <script src="../vendors/flot.orderbars/js/jquery.flot.orderBars.js"></script>
    <script src="../vendors/flot-spline/js/jquery.flot.spline.min.js"></script>
    <script src="../vendors/flot.curvedlines/curvedLines.js"></script>
    <!-- DateJS -->
    <script src="../vendors/DateJS/build/date.js"></script>
    <!-- JQVMap -->
    <script src="../vendors/jqvmap/dist/jquery.vmap.js"></script>
    <script src="../vendors/jqvmap/dist/maps/jquery.vmap.world.js"></script>
    <script src="../vendors/jqvmap/examples/js/jquery.vmap.sampledata.js"></script>
    <!-- bootstrap-daterangepicker -->
    <script src="../vendors/moment/min/moment.min.js"></script>
    <script src="../vendors/bootstrap-daterangepicker/daterangepicker.js"></script>

    <!-- Custom Theme Scripts -->
    <script src="../build/js/custom.min.js"></script>
    <script type="text/javascript">
      var tip = "<?php if(isset($_GET["editar"])){echo $usuario["cod_rol"];} ?>";

      if (tip != ""){
        $("#rol").val(tip);
      }

      var oficina = "<?php if(isset($_GET["editar"])){echo $usuario["bdOmar_Sucursal"];} ?>";

      if (oficina != ""){
        $("#oficina").val(oficina);
      }

      function f_ShowOficina(){
        var _div = document.getElementById('div_oficina');

        if ($("#rol").val() == 2){
          _div.style.display = 'block';
        }
        else{
          _div.style.display = 'none';
        }
      }
    </script>
  </body>
</html>
<?php
cerrarSesion($enlace);
?>