<?php
global $Conf;
global $Route;
global $aModel;
$metacss = "";
$barre = "";
ob_start();
?>
  <div class="row">
    <div class="col-md-8">
      <div class="panel panel-primary">
        <div class="panel-heading">
          <h3 class="panel-title"><i class="fa fa-cubes"></i> Liste des utilisateurs
            <button class="btn btn-success btn-xs pull-right" id="btnCreateUser" title="Créer un nouvel utilisateur"><i class="fa fa-plus fa-lg"></i> Ajouter</button>
          </h3>
        </div>
        <div class="border cm-scrollbar cm-table-w-scroll table-responsive table-800">
          <table id="tblUsers" class="table table-bordered table-striped table-fixed">
            <thead>
            <tr>
              <th class="header text-center" scope="col" width="10%">Nom</th>
              <th class="header text-center" scope="col" width="10%">Prénom</th>
              <th class="header text-center" scope="col" width="10%">Rôle</th>
              <th class="header text-center" scope="col" width="10%">Mobile</th>
              <th class="header text-center" scope="col" width="20%">email</th>
              <th class="header text-center" scope="col" width="10%">Région</th>
              <th class="header text-center" scope="col" width="10%">Rattaché à</th>
              <th class="header text-center" scope="col" width="10%">Actions</th>
            </tr>
            </thead>
            <tbody>
            <?php
            foreach ($aModel["users"] as $user) {
              echo '<tr class="ligUser">';
              echo '<td class="clickable celUser" data-rowid="' . $user["rowid"] . '" data-libelle="' . $user["libelle"] . '">' . $user["libelle"] . '</td>';
              echo '<td class="clickable celUser" data-rowid="' . $user["rowid"] . '" data-libelle="' . $user["libelle"] . '">' . $user["prenom"] . '</td>';
              echo '<td class="text-center clickable celUser" data-rowid="' . $user["rowid"] . '" data-libelle="' . $user["libelle"] . '">' . $user["lib_role"] . '</td>';
              echo '<td class="text-center clickable celUser" data-rowid="' . $user["rowid"] . '" data-libelle="' . $user["libelle"] . '">' . $user["mobile"] . '</td>';
              echo '<td class="clickable celUser" data-rowid="' . $user["rowid"] . '" data-libelle="' . $user["libelle"] . '">' . $user["email"] . '</td>';
              echo '<td class="text-center clickable celUser" data-rowid="' . $user["rowid"] . '" data-libelle="' . $user["libelle"] . '">' . $user["lib_region"] . '</td>';
              $lib_parent = "-";
              foreach ($aModel["parents"] as $parent) {
                if ($parent["rowid"] == $user["fk_parent"]) {
                  $lib_parent = $parent["lib_parent"];
                  break;
                }
              }
              echo '<td class="text-center clickable celUser" data-rowid="' . $user["rowid"] . '" data-libelle="' . $user["libelle"] . '">' . $lib_parent . '</td>';
              echo '<td class="text-center">';
              echo '<a title="Modifier la fiche de cet utilisateur" class="btn btn-xs btn-primary btnModUser mr-1" data-act="E" data-rowid="' . $user["rowid"] . '" data-libelle="' . $user["libelle"] . '"><i class="fa fa-pencil fa-lg"></i></a>';
              echo '<a title="Supprimer cet utilisateur" class="btn btn-xs btn-danger btnSuppUser" data-act="D" data-rowid="' . $user["rowid"] . '" data-libelle="' . $user["libelle"] . '"><i class="fa fa-trash fa-lg"></i></a>';
              echo '</td>';
              echo '</tr>';
            }
            ?>
            </tbody>
          </table>
        </div>
      </div>
      <?php
      if ($Conf->_devIp) {
        echo '<button class="btn btn-warning btn-xs" id="btnMaintenance"><i class="fa fa-bug fa-lg"></i> Maintenance des utilisateurs</button>';
      }
      ?>
    </div>
  </div>
<?php
$content = ob_get_contents();
ob_clean();

ob_start();
?>
  <div class="modal draggable fade" id="modalEditUser" tabindex="-1" role="dialog" aria-labelledby="modUserTitre" data-backdrop="static">
    <div class="modal-dialog modal-xl">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title" id="modUserTitre"></h4>
        </div>
        <div class="modal-body">
          <div class="row">
            <div class="col-md-12">
              <form class="form-horizontal" id="frmUser" role="form" enctype="multipart/form-data">
                <input type="hidden" name="act" id="userAct" value=""/>
                <input type="hidden" name="rowid" id="userRowid" value=""/>

                <div class="form-group">
                  <label for="userLibelle" class="control-label col-md-1">Nom :</label>
                  <div class="col-md-4"><input type="text" class="form-control" name="libelle" id="userLibelle" data-default="" required="required"/></div>
                  <label for="userPrenom" class="control-label col-md-1">Prénom :</label>
                  <div class="col-md-4"><input type="text" class="form-control" name="prenom" id="userPrenom" data-default="" required="required"/></div>
                </div>
                <div class="form-group">
                  <label for="userMobile" class="control-label col-md-1">Mobile :</label>
                  <div class="col-md-4"><input type="text" class="form-control" name="mobile" id="userMobile" data-default=""/></div>
                  <label for="userEmail" class="control-label col-md-1">Email :</label>
                  <div class="col-md-4"><input type="email" class="form-control" name="email" id="userEmail" data-default="" required="required"/></div>
                </div>
                <div class="form-group">
                  <label for="userLogin" class="control-label col-md-1">Login :</label>
                  <div class="col-md-4"><input type="text" class="form-control" name="username" id="userName" data-default="" required="required"/></div>
                  <label for="userRole" class="control-label col-md-1">Rôle :</label>
                  <div class="col-md-4">
                    <select class="form-control" name="fk_role" id="userRole" data-default="" required="required">
                      <option value="0">Sélectionner un rôle</option>
                      <?php
                      foreach ($aModel["roles"] as $role) {
                        echo '<option value="' . $role["rowid"] . '">' . $role["libelle"] . '</option>';
                      }
                      ?>
                    </select>
                  </div>
                </div>
                <div class="form-group">
                  <label for="userRole" class="control-label col-md-1">Région :</label>
                  <div class="col-md-4">
                    <select class="form-control" name="fk_region" id="userRegion" data-default="">
                      <option value="0">Aucune région</option>
                      <?php
                      foreach ($aModel["regions"] as $region) {
                        echo '<option value="' . $region["rowid"] . '">' . $region["libelle"] . '</option>';
                      }
                      ?>
                    </select>
                  </div>
                  <label for="userParent" class="control-label col-md-1">Responsable :</label>
                  <div class="col-md-4">
                    <select class="form-control" name="fk_parent" id="userParent" data-default="">
                      <option value="0">-</option>
                      <?php
                      foreach ($aModel["parents"] as $parent) {
                        echo '<option value="' . $parent["rowid"] . '">' . $parent["lib_parent"] . '</option>';
                      }
                      ?>
                    </select>
                  </div>
                </div>
                <div class="form-group">
                  <label for="userRole" class="control-label col-md-1">Grands Comptes :</label>
                  <div class="col-md-4">
                    <input type="checkbox" name="chk_grands_comptes" id="userChk_grands_comptes" value="0"/>
                  </div>
                  <label for="userDepts" class="control-label col-md-1">Départements :</label>
                  <div class="col-md-4">
                    <input type="text" class="form-control" name="lst_depts" id="userLst_depts" data-default=""/>
                    <p class="help-block">Lister les départements, dont cet utilisateur a la responsabilité, en séparant chaque numéro par une virgule<br/>
                      par exemple : 09,11,16,17,24,33
                    </p>
                  </div>
                </div>
                <div class="form-group">
                  <label for="userActive" class="control-label col-md-2">Utilisateur actif :</label>
                  <div class="col-md-4">
                    <input type="checkbox" name="active" id="userActive" data-default="0"/>
                  </div>
                </div>
              </form>
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <div class="alert col-md-4" id="frmMsgErrorModUser"></div>
          <button type="button" class="btn btn-default" id="btnCancelModUser" title="Annuler la modification">Annuler</button>
          <button type="button" class="btn btn-success" id="btnSubmitModUser">Enregistrer</button>
        </div>
      </div>
    </div>
  </div>
<?php
$modal = ob_get_contents();
ob_clean();

ob_start();
?>
  <script src="/pub/res/js/jusers.js"></script>
<?php
$jscript = ob_get_contents();
ob_end_clean();
require_once $Route->_layout;
