<?php
require_once 'inc/usuario.php';
require_once 'inc/messages.php';

checkLogin('login.php');
$user = getLoggedUser();

$data = Date_Create()->format('d-m-Y');

?>
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
  <head>
    <title>Login parte 2</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="css/stylesheets.css" rel="stylesheet" type="text/css" />
    <link href="css/mystyles.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" href="css/login.css">
  </head>
  <body>
    <div class="container">
      <h1 class="text-center login-title">Bem vindo <?= $user['nome']; ?></h1>
      <?php
      //echo '<pre>' . var_export($user, true) . '</pre>';
      displayMessagesAndErrors();
      ?>
      <div class="account-wall">
        <img class="profile-img" src="<?= $user['imagem']; ?>"
             alt="">
        <form class="form-signin" action="login_ws.php" method="post">
          <h2>Database</h2>

          <div class="input-group">
            <div class="input-group-addon"><span class="fa fa-calendar"></span></div> <input type="text" name="data-base" class="datepicker form-control" placeholder="Data base" id="dp1475186359137" title="Data base" value="<?=$data?>"> </div>
          <h2>Unidade</h2>

          <!--
          <select name="filial" class="form-control " title="Filial">
            <option value="1">Filial 1</option>
            <option value="2">Filial 2</option>
            <option value="3">Filial 3</option>
            <option value="4">Filial 4</option>
          </select>
        -->

          <select id="filial" name="filial" class="form-control " title="Filial">
          <?
          $id_usuario = $_SESSION[SESSION_USER_NAME]['id'];

          $consulta = "select distinct uni.id, uni.ds_fantasia from erpc_unidades uni join erpc_usuarios_unidades usu on uni.id=usu.id_unidade where usu.id_usuario=$id_usuario and uni.st_ativo=1 order by 2";

          $resultado = mysqli_query($conexao, $consulta) or die ($erro = mysqli_error($conexao));
    			if (mysqli_num_rows($resultado)>0) {
                while ($registro = mysqli_fetch_assoc($resultado)){

                  $id_filial= $registro['id'];
                  $ds_razao = $registro['ds_fantasia'];

                  echo "<option value='$id_filial'>$ds_razao</option> ";
                }
              }
          ?>
        </select>

          <h2>Modulo</h2>
          <select name="modulo" class="form-control " title="Módulo Inicial" required>
            <option>Desktop</option>
            <?php
            $sql = 'select m.id, m.ds_modulo
                     from erpc_modulos m
                     join erpc_modulos_funcionalidades mf on mf.id_modulo = m.id
                     join erpc_funcionalidades f on mf.id_funcionalidade = f.id
                     join erpc_grupo_funcionalidade gf on gf.id_funcionalidade = f.id
                     join erpc_usuarios_grupo ug on ug.id = gf.id_grupo
                     where ug.id = ' . $user['grupo'] . '
                     group by m.id, m.ds_modulo order by m.ds_modulo';

            $result = mysqli_query($conexao, $sql) or die($sql . PHP_EOL . mysqli_error($conexao));
            while($row = $result->fetch_assoc()){
              echo "<option value='$row[id]'>$row[ds_modulo]</option>" . PHP_EOL;
            }
            
            ?>
          </select>
          <button class="btn btn-lg btn-primary btn-block" type="submit" name="btn-login2">
            Entrar</button>
        </form>
      </div>
    </div>
<script type="text/javascript" src="js/plugins/jquery/jquery.min.js"></script>
<script type="text/javascript" src="js/plugins/jquery/jquery-ui.min.js"></script>
<script type="text/javascript" src="js/plugins/jquery/i18n/datepicker-pt-BR.js"></script>
<script>
$.datepicker.setDefaults( $.datepicker.regional[ "pt-BR" ] );
</script>
<script type="text/javascript" src="js/plugins/jquery/jquery-migrate.min.js"></script>
<script type="text/javascript" src="js/plugins/jquery/globalize.js"></script>
<script type="text/javascript" src="js/plugins/bootstrap/bootstrap.min.js"></script>
<script type="text/javascript" src="js/plugins/bootstrap-modal/bootstrap-modal.js"></script>
<script type="text/javascript" src="js/plugins/mcustomscrollbar/jquery.mCustomScrollbar.min.js"></script>
<script type="text/javascript" src="js/plugins/uniform/jquery.uniform.min.js"></script>
<script type="text/javascript" src="js/plugins/datatables/datatables.min.js"></script>
<script type="text/javascript" src="js/plugins/animsition/animsition.min.js"></script>

<script type="text/javascript" src="js/plugins/select2/select2.min.js"></script>
<script type="text/javascript" src="js/plugins/tagsinput/jquery.tagsinput.min.js"></script>
<script type="text/javascript" src="js/plugins/jquery/jquery-ui-timepicker-addon.js"></script>
<script type="text/javascript" src="js/plugins/ibutton/jquery.ibutton.js"></script>
<script type="text/javascript" src="js/plugins/validationengine/languages/jquery.validationEngine-pt_BR.js"></script>
<script type="text/javascript" src="js/plugins/validationengine/jquery.validationEngine.js"></script>
<script type="text/javascript" src="js/plugins/maskedinput/jquery.maskedinput.min.js"></script>
<script type="text/javascript" src="js/plugins/stepy/jquery.stepy.min.js"></script>

<?php // plugin jquery noty ?>
<script src="js/plugins/noty/jquery.noty.js" type="text/javascript"></script>
<script src="js/plugins/noty/layouts/topCenter.js" type="text/javascript"></script>
<script src="js/plugins/noty/layouts/topLeft.js" type="text/javascript"></script>
<script src="js/plugins/noty/layouts/topRight.js" type="text/javascript"></script>
<script src="js/plugins/noty/themes/default.js" type="text/javascript"></script>


<script type="text/javascript" src="js/js.js"></script>
<script type="text/javascript" src="js/settings.js"></script>
    <script>
    $(".mask").each(function (i, item) {
      $(item).mask($(item).data('mask'));
    });
    </script>
  </body>
</html>
