<?php 
  require_once("./session_job.php");
  if (@$_SESSION['tipo_user'] != 5) {
    echo "<script>window.location = 'index.php'</script>";
  }
  date_default_timezone_set('America/Bahia');
  require_once("./config/conexao.php");
  $conn = conn();
  $conn_s = conn_s();
  
  require_once("./checar_loja.php");

  if(!empty($_POST['loja_escolhida'])){
    $_SESSION['loja'] = $_POST['loja_escolhida'];
    header("Location: /sistema/dashboard.php");
    exit;
  } else if(@$_SESSION['loja'] == 'todas') {
    $_SESSION['loja'] = 1;
  }
?>
<!DOCTYPE html>
<html lang="pt-br">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Dashboard</title>

    <link rel="stylesheet" href="./utils/css/bootstrap.min.css" />
    <link rel="stylesheet" href="./utils/css/bootstrap-select.min.css" />
    <link rel="stylesheet" href="./utils/css/styles.css" />
    <link rel="stylesheet" href="./utils/css/dashboard.css" />
    <link
      rel="stylesheet"
      href="https://use.fontawesome.com/releases/v5.8.2/css/all.css"
      integrity="sha384-oS3vJWv+0UjzBfQzYUhtDYW+Pj2yciDJxpsK1OYPAYjqT085Qq/1cq5FLXAZQ7Ay"
      crossorigin="anonymous"
    />
  </head>
  <body>
    <nav class="navbar">
      <div class="container caixa_logo">
        <a class="" href="javascript:void(0);">
          <img src="./utils/img/logo.png" alt="" width="150" />
        </a>
        <div>
          <?php
            if (isset($_SESSION['id_user'])){
              if (@$_SESSION['id_user'] == '11' || @$_SESSION['id_user'] == '6' || @$_SESSION['id_user'] == '1') {
                ?>
                  <!-- <span><a href="fazer_backup.php" style="color: #fff;background-color:#00f; padding:5px;border-radius: 15px;">BACKUP</a></span> -->
                <?php
              }
            }
          ?>
          <?php if (isset($_SESSION['id_user']) || !empty($_SESSION['id_user'])) {?>
          <span><?= $_SESSION['nome_user'];?></span> &nbsp;

          <?php
            if(!empty($_SESSION['id_user'])){
              $SQL = "SELECT * FROM user WHERE id = " . @$_SESSION['id_user'];
              $dados_user = mysqli_fetch_assoc(mysqli_query($conn, $SQL));
              $geral = @$dados_user['user_geral'] == 'sim';

              if($geral){
                $SQL = "SELECT * FROM uni_lojas";
                $lojas = mysqli_fetch_all(mysqli_query($conn_s, $SQL), MYSQLI_ASSOC);
                ?>
                  <span>
                    <form method="post">
                      <select name="loja_escolhida" onchange="this.parentElement.submit()">
                        <?php
                          foreach($lojas as $loja){
                            $selected = $_SESSION['loja'] == $loja['id'];

                            $selected = $selected ? 'selected' : '';
                            ?>
                              <option value="<?= $loja['id'] ?>" <?= $selected ?>><?= $loja['nome'] ?></option>
                            <?php
                          }
                        ?>
                      </select>
                    </form>
                  </span> &nbsp;
                <?php
              }
            }
          ?>

          <?php if ($_SESSION['tipo_user'] == 6){?>
          <span><a href="index.php"><img src="./utils/img/icons/home.svg" width="19"></a></span> &nbsp;
          <?php if (in_array(4, $_SESSION['PRIVILEGIOS']) || $_SESSION['id_user'] == '1') {?>
          <span><a href="users.php"><img src="./utils/img/icons/user.svg" width="17"></a></span> &nbsp;
          <?php }?>
          <?php }?>
          <?php if ($_SESSION['tipo_user'] == 5){?>
          <span><a href="index.php"><img src="./utils/img/icons/archive.svg" width="19"></a></span> &nbsp;
          <span><a href="dashboard.php"><img src="./utils/img/icons/home.svg" width="19"></a></span> &nbsp;
          <span><a href="users.php"><img src="./utils/img/icons/user.svg" width="17"></a></span> &nbsp;
          <?php }?>
          <span><a href="exit.php"><img src="./utils/img/icons/exit.svg" width="17"></a></span>
          <?php }?>
        </div>
      </div>
    </nav>

    <!--Body-->
    <div class="container" style="margin-bottom: 100px;">
      <?php require_once("./components/cards_counter.php");?>
      <div class="row mt-4">
        <div class="col-12">
          <?php require_once("./components/performance.php");?>
        </div>
      </div>
      <div class="row mt-4">
        <div class="col-sm-8">
          <?php require_once("./components/content_c2.php");?>
        </div>
        <div class="col-sm-4">
          <?php require_once("./components/content_c1.php");?>
        </div>
      </div>

      
    </div>
    <!--/. Body-->

    <!--Footer-->
    <footer>
      <div class="container">
        <p>&copy; UNIDOS IMPORTADOS</p>
        <p>
          DESENVOLVIDO PELA
          <a href="https://agenciadp.com.br/" target="_blank">AGÊNCIA DP WEB</a>
        </p>
      </div>
    </footer>
    <!--/. Footer-->

    <?php require("./modal.php");?>

    <script src="./utils/js/jquery-1.12.4.min.js"></script>
    <script src="./utils/js/popper.min.js"></script>
    <script src="./utils/js/bootstrap.min.js"></script>
    <script src="./utils/js/bootstrap-select.min.js"></script>
    <script src="./utils/js/notify.min.js"></script>
    <script src="./utils/js/scripts.js"></script>
    <script src="./utils/js/dashboard.js"></script>
  </body>
</html>
