<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
  <!-- Content Header (Page header) -->
  <section class="content-header">
    <h1>
      Gérer les
      <small>Rôles</small>
    </h1>
    <ol class="breadcrumb">
      <li><a href="#"><i class="fa fa-dashboard"></i> Accueil</a></li>
      <li class="active">Rôles</li>
    </ol>
  </section>

  
  <!-- Main content -->
  <section class="content">
    <!-- Small boxes (Stat box) -->
    <div class="row">
      <div class="col-md-12 col-xs-12">

        <?php if(session()->getFlashdata('success')): ?>
          <div class="alert alert-success alert-dismissible" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <?php echo session()->getFlashdata('success'); ?>
          </div>
        <?php elseif(session()->getFlashdata('error')): ?>
          <div class="alert alert-error alert-dismissible" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <?php echo session()->getFlashdata('error'); ?>
          </div>
        <?php endif; ?>

        <?php if(in_array('createGroup', $user_permission)): ?>
          <a href="<?php echo base_url('groups/create') ?>" class="btn btn-primary">Ajouter un Rôle</a>
          <br /> <br />
        <?php endif; ?>

        <div class="box">
          <div class="box-header">
            <h3 class="box-title">Gérer les Rôles</h3>
          </div>
          <!-- /.box-header -->
          <div class="box-body">
            <table id="groupTable" class="table table-bordered table-striped">
              <thead>
              <tr>
                <th>Désignation</th>
                <?php if(in_array('updateGroup', $user_permission) || in_array('deleteGroup', $user_permission)): ?>
                  <th>Action</th>
                <?php endif; ?>
              </tr>
              </thead>
              
              <tbody>
                <?php if($groups_data): ?>                  
                  <?php foreach ($groups_data as $k => $v): ?>
                    <tr>
                      <td><?php echo $v['group_name']; ?></td>

                      <?php if(in_array('updateGroup', $user_permission) || in_array('deleteGroup', $user_permission)): ?>
                      <td>
                        <?php if(in_array('updateGroup', $user_permission)): ?>
                        <a href="<?php echo base_url('groups/edit/'.$v['id']) ?>" class="btn btn-default"><i class="fa fa-edit"></i></a>  
                        <?php endif; ?>
                        <?php if(in_array('deleteGroup', $user_permission)): ?>
                        <button type="button" class="btn btn-danger" onclick="confirmDelete(<?php echo $v['id']; ?>, '<?php echo addslashes($v['group_name']); ?>')">
                          <i class="fa fa-trash"></i>
                        </button>
                        <?php endif; ?>
                      </td>
                      <?php endif; ?>
                    </tr>
                  <?php endforeach ?>
                <?php endif; ?>
              </tbody>
            </table>
          </div>
          <!-- /.box-body -->
        </div>
        <!-- /.box -->
      </div>
      <!-- col-md-12 -->
    </div>
    <!-- /.row -->
  </section>
  <!-- /.content -->
</div>
<!-- /.content-wrapper -->

<!-- Modal de confirmation de suppression -->
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
        <h4 class="modal-title" id="deleteModalLabel">Confirmation de suppression</h4>
      </div>
      <div class="modal-body">
        <p>Voulez-vous vraiment supprimer le rôle <strong id="roleName"></strong> ?</p>
      </div>
      <div class="modal-footer">
        <form id="deleteForm" method="post" style="display: inline;">
          <button type="button" class="btn btn-warning" data-dismiss="modal">Annuler</button>
          <button type="submit" class="btn btn-danger" name="confirm" value="Oui">
            <i class="fa fa-trash"></i> Oui, supprimer
          </button>
        </form>
      </div>
    </div>
  </div>
</div>

<script type="text/javascript">
  $(document).ready(function() {
    // datatable-fr.js
    $.extend(true, $.fn.dataTable.defaults, {
      language: {
        sProcessing:   "Traitement en cours...",
        sSearch:       "Rechercher&nbsp;:",
        sLengthMenu:   "Afficher _MENU_ &eacute;l&eacute;ments",
        sInfo:         "Affichage de l'&eacute;lement _START_ &agrave; _END_ sur _TOTAL_ &eacute;l&eacute;ments",
        sInfoEmpty:    "Affichage de l'&eacute;lement 0 &agrave; 0 sur 0 &eacute;l&eacute;ment",
        sInfoFiltered: "(filtr&eacute; de _MAX_ &eacute;l&eacute;ments au total)",
        sLoadingRecords: "Chargement en cours...",
        sZeroRecords:  "Aucun &eacute;l&eacute;ment &agrave; afficher",
        sEmptyTable:   "Aucune donn&eacute;e disponible dans le tableau",
        oPaginate: {
          sFirst:    "Premier",
          sPrevious: "Pr&eacute;c&eacute;dent",
          sNext:     "Suivant",
          sLast:     "Dernier"
        },
        oAria: {
          sSortAscending:  ": activer pour trier la colonne par ordre croissant",
          sSortDescending: ": activer pour trier la colonne par ordre d&eacute;croissant"
        }
      }
    });

    $('#groupTable').DataTable();

    $("#mainUserNav").addClass('active');
    $("#manageGroupNav").addClass('active');
  });

  // Fonction pour ouvrir le modal de confirmation
  function confirmDelete(id, name) {
    $('#roleName').text(name);
    $('#deleteForm').attr('action', '<?php echo base_url('groups/delete/'); ?>' + id);
    $('#deleteModal').modal('show');
  }
</script>