<link rel="stylesheet" href="<?= base_url('<link rel="stylesheet" 
	href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap.min.css">') ?>">
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">

        <h1>
            <?= $this->lang->line('Gestion des administrateurs'); ?>
        </h1>
        <?php
        $site_url = base_url();

        if($alert=="ok") {

            if(!$success=="ok"){
                ?>
                <div class='alert alert-danger alert-dismissible col-xs-6'>
                    <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button>
                    <h4><i class='icon fa fa-ban'></i> Erreur!</h4>
                    <?= $message; ?>
                </div>

                <?php
            } else {
                ?>
                <div class="alert alert-success alert-dismissible col-xs-6">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                    <h4><i class="icon fa fa-check"></i> Success!</h4>
                    <?= $message; ?>
                </div>

                <?php
            }
        }
        ?>
    </section>

    <section class="content">
        <div class="row">
            <?php
            if($this->session->userdata('category')==0){
            ?>
            <div class="col-xs-8">
            <?php }else{
                echo "<div class='col-xs-12'>";
            } ?>
                <div class="box">
                    <div class="box-header">
                        <h3 class="box-title"><?= $this->lang->line('Administrateurs'); ?></h3>
                    </div>
                    <div class="box-body" style="overflow-x:auto;">

                        <?php

                        $sql2 = $list_admin;
                        if($sql2!=false){
                            $numrows=$sql2->num_rows();
                            $num = 0;
                            if ($numrows > 0) {
                                ?>
                                <table id="validated" class="table table-bordered table-striped">
                                    <thead>
                                    <tr>
                                        <th align='center'>N°</th>
                                        <th><?= $this->lang->line('Nom'); ?></th>
                                        <th><?= $this->lang->line('Prénom'); ?></th>
                                        <th align='center'><?= $this->lang->line('Contact'); ?></th>
                                        <th align='center'><?= $this->lang->line('Email'); ?></th>
                                        <th align='center'><?= $this->lang->line('Adresse'); ?></th>
                                        <th align='center'><?= $this->lang->line('Pays'); ?></th>
                                        <th align='center'><?= $this->lang->line('Catégorie'); ?></th>
                                        <th align='center'><?= $this->lang->line('Statut'); ?></th>
                                <?php
                                if($this->session->userdata('category')==0){
                                    ?>
                                        <th><?= $this->lang->line('Modifier'); ?></th>
                                        <th><?= $this->lang->line('Supprimer'); ?></th>
                                    <?php } ?>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <?php

                                    foreach($sql2->result() as $row) {
                                        $num ++;
                                        $category = $this->lang->line('Administrateur');
                                        $statut = $this->lang->line('Non confirmé');
                                        if($row->category==0){
                                            $category = "Super Admin";
                                        }
                                        if($row->salt!=''){
                                            $statut = $this->lang->line("Confirmé");
                                        }

                                        echo "<tr>
                                                  <td align='center'>$num</td>
                                                  <td >$row->lastname</td>
                                                  <td>$row->firstname</td>
                                                  <td align='center'>$row->phone</td>
                                                  <td align='center'>$row->email</td>
                                                  <td align='center'>$row->adresse</td>
                                                  <td align='center'>$row->country</td>";
                                                  if($row->email==$this->session->userdata('email')){
                                                       echo "<td align='center'><button data-toggle='modal' data-category='$row->category' data-id-admin='$row->id_admin' data-lastname='$row->lastname' data-country-id='$row->country_id' data-country='$row->country' data-adresse='$row->adresse' data-email='$row->email' data-firstname='$row->firstname' data-phone='$row->phone' class='open-modal modif btn btn-success' data-target='#editModal'>$category </button></td>";
                                                   }else{
                                                       echo "<td align='center'>$category</td>";
                                                   }
                                           echo "<td align='center'>$statut</td>";
                                                if($this->session->userdata('category')==0){
                                                  echo "<td id='edition' align='center'>
                                                    <button data-toggle='modal' data-category='$row->category' data-id-admin='$row->id_admin' data-lastname='$row->lastname' data-country-id='$row->country_id' data-country='$row->country' data-adresse='$row->adresse' data-email='$row->email' data-firstname='$row->firstname' data-phone='$row->phone' class='open-modal modif btn btn-success' data-target='#editModal'><i class='fa fa-edit'></i></button>
                                                  </td>";

                                                  }
                                                  if ($row->category!=0){
                                                      echo "<td id='suppression' align='center'>
                                                                <button data-toggle='modal' data-id-admin='$row->id_admin' data-lastname='$row->lastname' data-firstname='$row->firstname' class='supre-modal delete btn btn-danger' data-target='#suppreModal'><i class='fa fa-trash'></i></button>
                                                            </td>";
                                                  }
                                              echo "</tr>";
                                    }
                                    ?>
                                    </tbody>
                                </table>
                                <?php

                            }

                            else {
                                echo $this->lang->line("Aucun utilisateur trouvé");
                            }
                        }else {
                            echo $this->lang->line("Aucun utilisateur trouvé");
                        }
                        ?>
                    </div>
                </div>
            </div>
            <?php
            if($this->session->userdata('category')==0) {
                ?>
                <div class="col-xs-4">
                    <div class="box">
                        <div class="box-header">
                            <h3 class="" style="text-align: center"><?= $this->lang->line('Créer un compte administrateur'); ?></h3>
                        </div>
                        <div class="box-body">
                            <form method='POST' id="create-admin-form" class="bottom-75 center-block">
                                <div class="form-group">
                                    <label for="pays"><?= $this->lang->line('Pays'); ?></label>
                                    <?php
                                    $result = $pays;
                                    $numrows = $result->num_rows();
                                    if ($numrows > 0) { ?>
                                        <select class="form-control input-lg" name="pays" required id="pays">
                                            <?php foreach ($result->result() as $row) {
                                                echo "<option value='" . $row->id . "' id='" . $row->code_country . "'>" . $row->name . "</option>";
                                            } ?>
                                        </select>
                                    <?php } else {
                                        echo "Aucun pays";
                                    }
                                    ?>
                                </div>
                                <div class="form-group">
                                    <label for="nom"><?= $this->lang->line('Nom'); ?></label>
                                    <input type="text" class="form-control input-lg" name="nom" required id="nom">
                                </div>
                                <div class="form-group">
                                    <label for="prenom"><?= $this->lang->line('Prénom'); ?></label>
                                    <input type="text" class="form-control input-lg" name="prenom" required id="prenom">
                                </div>
                                <div class="form-group">
                                    <label for="email"><?= $this->lang->line('Email'); ?></label>
                                    <input type="email" class="form-control input-lg" name="email" required id="email">
                                </div>
                                <div class="form-group">
                                    <label for="contact"><?= $this->lang->line('Contact'); ?></label>
                                    <input type="text" class="form-control input-lg" name="contact" required
                                           id="contact">
                                </div>
                                <div class="form-group">
                                    <label for="adresse"><?= $this->lang->line('Adresse'); ?></label>
                                    <input type="text" class="form-control input-lg" name="adresse" required
                                           id="adresse">
                                </div>
                                <div style="text-align: center" class="form-group">
                                    <input id="btn-add" type="submit" value="<?= $this->lang->line('Créer le compte'); ?>" class="btn btn-primary">
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
                <?php
            }
            ?>
        </div>
        <div class="modal fade" id="editModal">
            <div class="modal-dialog">
                <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"><?= $this->lang->line('Mettre à jour des informations'); ?></h4>
                    </div>
                    <div class="modal-body">
                        <form id="update-form" method="post" class="bottom-75 center-block">
                            <div class="form-group">
                                <label for="modal-pays"><?= $this->lang->line('Pays'); ?></label>
                                <?php
                                $result=$pays;
                                $numrows=$result->num_rows();
                                if ($numrows > 0) { ?>
                                    <select class="form-control input-lg" name="pays" required id="pays">
                                        <?php foreach($result->result() as $row) {
                                            echo "<option value='".$row->id."' id='".$row->code_country."'>".$row->name."</option>";
                                        } ?>
                                    </select>
                                <?php }else{
                                    echo $this->lang->line("Aucun pays");
                                }
                                ?>
                            </div>
                            <div class="form-group">
                                <label for="nom"><?= $this->lang->line('Nom'); ?></label>
                                <input type="text" class="form-control input-lg" name="nom" required id="nom">
                            </div>
                            <div class="form-group">
                                <label for="prenom"><?= $this->lang->line('Prénom'); ?></label>
                                <input type="text" class="form-control input-lg" name="prenom" required id="prenom">
                            </div>
                            <div class="form-group">
                                <label for="email"><?= $this->lang->line('Email'); ?></label>
                                <input type="email" class="form-control input-lg" name="email" required id="email">
                            </div>
                            <div class="form-group">
                                <label for="contact"><?= $this->lang->line('Contact'); ?></label>
                                <input type="text" class="form-control input-lg" name="contact" required id="contact">
                            </div>
                            <div class="form-group">
                                <label for="adresse"><?= $this->lang->line('Adresse'); ?></label>
                                <input type="text" class="form-control input-lg" name="adresse" required id="adresse">
                            </div>
                            <?php if($this->session->userdata('category')==0){ ?>
                                <div class="form-group">
                                    <label for="category"><?= $this->lang->line('Catégorie'); ?></label>
                                    <select class="form-control input-lg" name="category" required id="category">
                                        <option value='1'><?= $this->lang->line('Administrateur'); ?></option>
                                        <option value='0'>Super Admin</option>
                                    </select>
                                </div>
                            <?php } ?>
                            <div class="form-group">
                                <input id="btn-update" type="submit" value="<?= $this->lang->line('Confirmer'); ?>" class="btn btn-primary">
                                <button type="button" class="btn btn-default pull-right" data-dismiss="modal"><?= $this->lang->line('Fermer'); ?></button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
        <div class="modal fade" id="suppreModal">
            <div class="modal-dialog">
                <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"><?= $this->lang->line('Suppression de compte'); ?></h4>
                    </div>
                    <div class="modal-body">
                        <p id="message"></p>
                    </div>
                    <div class="modal-footer">
                        <button class="btn btn-danger pull-left" id="delete"><?= $this->lang->line('Confirmer'); ?></button>
                        <button type="button" class="btn btn-default pull-right" data-dismiss="modal"><?= $this->lang->line('Fermer'); ?></button>
                    </div>
                </div>
            </div>
        </div>
    </section>
</div>

<!-- jQuery 2.2.4 -->
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>

<!-- Bootstrap 3.3.7 -->
<script src="<?= base_url('bower_components/bootstrap/dist/js/bootstrap.min.js') ?>"></script>
<!-- DataTables -->
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap.min.js"></script>
<!-- Slimscroll -->
<script src="<?= base_url('bower_components/jquery-slimscroll/jquery.slimscroll.min.js') ?>"></script>
<!-- FastClick -->
<script src="<?= base_url('bower_components/fastclick/lib/fastclick.js') ?>"></script>
<!-- AdminLTE App -->
<script src="<?= base_url('dist/js/adminlte.min.js') ?>"></script>
<!-- AdminLTE for demo purposes -->
<script src="<?= base_url('dist/js/demo.js') ?>"></script>
<script>
    $(function() {
        $('#unvalidated').DataTable();
        $('#validated').DataTable();
    })

</script>

<script>

    var id_admin = null;

    $(document).on("click", ".open-modal", function () {

        var id_country = $(this).data('country-id');
        id_admin = $(this).data('id-admin');
        var nom = $(this).data('lastname');
        var prenom = $(this).data('firstname');
        var email = $(this).data('email');
        var contact = $(this).data('phone');
        var adresse = $(this).data('adresse');
        var category = $(this).data('category');

        $(".modal-body #pays").val(id_country);
        $(".modal-body #nom").val( nom );
        $(".modal-body #prenom").val( prenom );
        $(".modal-body #email").val( email );
        $(".modal-body #contact").val( contact );
        $(".modal-body #adresse").val( adresse );
        $(".modal-body #category").val( category );

    });

    $("#update-form").submit(function(){

        var m_id_country = $('#update-form #pays option:selected').val();
        //var id = $(this).val('pays');
        var m_nom = $('#update-form #nom').val();
        var m_prenom = $('#update-form #prenom').val();
        var m_email = $('#update-form #email').val();
        var m_contact = $('#update-form #contact').val();
        var m_adresse = $('#update-form #adresse').val();
        var m_category = $('#update-form #category').val();

        $.ajax({
            url:'<?= base_url('Gestion/modif_admin')?>',
            type: 'post',
            data: {id: id_admin,nom:m_nom,prenom:m_prenom,email:m_email,contact:m_contact,adresse:m_adresse,country:m_id_country,category:m_category},
            dataType: 'json',
            success: function(data){
                if(data==='completed'){
                    alert("Les informations ont été mises à jours");
                    location.reload(true);
                }else{
                    alert(data);
                    location.reload(true);
                }
            }
        });

        event.preventDefault();
    });

    $("#create-admin-form").submit(function(){
        var country = $("#create-admin-form #pays").val();
        var nom = $("#create-admin-form #nom").val();
        var prenom = $("#create-admin-form #prenom").val();
        var email = $("#create-admin-form #email").val();
        var contact = $("#create-admin-form #contact").val();
        var adresse = $("#create-admin-form #adresse").val();

        $.ajax({
            url:'<?= base_url('Gestion/create_admin')?>',
            type: 'post',
            data: {country: country,nom:nom,prenom:prenom,email:email,contact:contact,adresse:adresse},
            dataType: 'json',
            success: function(data){
                if(data==='completed'){
                    alert("L'administrateur a été créé");
                    location.reload(true);
                }else{
                    alert(data);
                    location.reload(true);
                }
            }
        });

        event.preventDefault();
    });

    $(document).ready(function(){

        var id_ville = 0;

        /* Get the checkboxes values based on the class attached to each check box */
        $("#valider").click(function() {
            activeVille();
        });

        $("#disable").click(function() {
            desactiveVille();
        });

        $(".modif").click(function(){
            id_ville = $(this).val();
        });


    });

    function activeVille(){
        /* declare an checkbox array */
        var chkArray = [];

        /* look for all checkboes that have a class 'chk' attached to it and check if it was checked */
        $(".chk:checked").each(function() {
            chkArray.push($(this).val());
        });

        /* we join the array separated by the comma */
        var selected;
        selected = chkArray.join(',') ;

        /* check if there is selected checkboxes, by default the length is 1 as it contains one single comma */
        if(selected.length > 0){
            $.ajax({
                url:'<?= base_url('Gestion/activer')?>',
                type: 'post',
                data: {villes: chkArray},
                dataType: 'json',
                success: function(data){
                    if(data==='completed'){
                        alert('Ville(s) activée(s)');
                        location.reload(true);
                    }else{
                        alert("Une erreur s'est produite");
                        location.reload(true);
                    }
                }
            });
        }else{
            alert("Veuillez cocher des villes");
        }
    }

    function desactiveVille(){
        /* declare an checkbox array */
        var chkArray = [];

        /* look for all checkboes that have a class 'chk' attached to it and check if it was checked */
        $(".unchk:checked").each(function() {
            chkArray.push($(this).val());
        });

        /* we join the array separated by the comma */
        var selected;
        selected = chkArray.join(',') ;

        /* check if there is selected checkboxes, by default the length is 1 as it contains one single comma */
        if(selected.length > 0){
            $.ajax({
                url:'<?= base_url('Gestion/desactiver')?>',
                type: 'post',
                data: {villes: chkArray},
                dataType: 'json',
                success: function(data){
                    if(data==='completed'){
                        alert('Ville(s) désactivée(s)');
                        location.reload(true);
                    }else{
                        alert("Une erreur s'est produite");
                        location.reload(true);
                    }
                }
            });
        }else{
            alert("Veuillez cocher des villes");
        }
    }

    $(document).on("click", ".supre-modal", function () {

        id_admin = $(this).data('id-admin');
        var nom = $(this).data('lastname');
        var prenom = $(this).data('firstname');

        var message = "Voulez-vous vraiment retirer "+prenom+" "+nom+"?";

        $(".modal-body #message").text(message);

    });

    $("#delete").click(function(){

        $.ajax({
            url:'<?= base_url('Gestion/delete_admin')?>',
            type: 'post',
            data: {id: id_admin},
            dataType: 'json',
            success: function(data){
                if(data==='completed'){
                    alert("L'administrateur a été retiré");
                    location.reload(true);
                }else{
                    alert(data);
                    location.reload(true);
                }
            }
        });

        event.preventDefault();
    });

</script>
