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

        <h1>
            <?= $this->lang->line('Gestion des réseaux'); ?>
            <input type="button" class="btn btn-primary pull-right" id="Bactiver" value="Activer/Désactiver le(s) réseau(x)" style="display: none;" />
        </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">
            <div class="col-xs-8">
                <div class="box">
                    <div class="box-header">
                        <h3 class="box-title"><?= $this->lang->line('Réseaux assignés'); ?></h3>
                    </div>
                    <div class="box-body" style="overflow-x:auto;">

                        <?php

                        $sql2 = $assigned_networks;
                        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('Reseau'); ?></th>
                                        <th><?= $this->lang->line('Pays'); ?></th>
                                        <th align='center'><?= $this->lang->line('Statut'); ?></th>
                                        <th align='center'><?= $this->lang->line('Activer/Désactiver'); ?></th>
                                        <th align='center'><?= $this->lang->line('Modifier'); ?></th>
                                        <th style="text-align: center"><?= $this->lang->line('Supprimer'); ?></th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <?php
                                    foreach($sql2->result() as $row) {
                                        $num ++;
                                        //$member_code = randomString1(10);
                                        $libelle_status = $this->lang->line('Actif');
                                        if($row->status==0){
                                            $libelle_status = $this->lang->line('Inactif');
                                        }
                                        echo "<tr>
                                                  <td align='center'>$num</td>
                                                  <td >$row->network</td>
                                                  <td>$row->country</td>
                                                  <td align='center'>$libelle_status</td>";
                                        if($row->status==0){
                                            echo "<td align='center'><label>
                                                  <input type='checkbox' name='activer' class='minimal' data-id='".$row->id."' >
                                                </label>
                                                <label>
                                                  <input type='checkbox' name='desactiver' class='minimal-red' disabled data-id='".$row->id."'>
                                                </label></td>";
                                        }else{
                                            echo "<td align='center'><label>
                                                  <input type='checkbox' name='activer' class='minimal' disabled data-id='".$row->id."'>
                                                </label>
                                                <label>
                                                  <input type='checkbox' name='desactiver' class='minimal-red' data-id='".$row->id."'>
                                                </label></td>";
                                        }
                                        echo  "<td id='edition' align='center'>
                                                           <button data-toggle='modal' data-id-network='$row->id' data-name-network='$row->network' data-country='$row->country' data-id-country='$row->country_id'  
                                                           data-open_wallet_at_mobile_login='$row->open_wallet_at_mobile_login'  class='open-modal modif btn btn-success' data-target='#editModal'><i class='fa fa-edit'></i></button>
                                                           </td>
                                               <td id='suppression' align='center'>
                                                    <button data-toggle='modal' data-id-network='$row->id' data-id-country='$row->country_id' class=' btn btn-danger' data-target='#'><i class='fa fa-trash'></i></button>
                                                </td>
                                              </tr>";
                                    }
                                    ?>
                                    </tbody>
                                </table>

                                <?php

                            }

                            else {
                                echo $this->lang->line('Aucun réseau');
                            }
                        }else {
                            echo $this->lang->line('Aucun réseau');
                        }
                        ?>
                    </div>
                </div>
            </div>
            <div class="col-xs-4">
                <div class="box">
                    <div class="box-header">
                        <h3 class="box-title"><?= $this->lang->line('Ajouter un réseau à la liste'); ?></h3>
                    </div>
                    <div class="box-body">
                        <form id="add-network" method="post" class="bottom-75 center-block">
                            <div class="form-group">
                                <label for="network"><?= $this->lang->line('Reseau'); ?></label>
                                <input type="text" class="form-control input-lg" name="network" required id="network">
                            </div>
                            <div class="form-group">
                                <input id="btn-add" type="submit" value="<?= $this->lang->line('Ajouter'); ?>" class="btn btn-primary">
                            </div>
                        </form>
                    </div>
                </div>
            </div>
            <div class="col-xs-4">
                <div class="box">
                    <div class="box-header">
                        <h3 class="box-title"><?= $this->lang->line('Assigner un réseau à un pays'); ?></h3>
                    </div>
                    <div class="box-body">
                        <form id="assign-form" method="post" 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" multiple="multiple" >
                                        <?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="ville"><?= $this->lang->line('Reseaux'); ?></label>
                                <?php
                                $result=$networks;
                                if($result!=false) {
                                    $numrows = $result->num_rows();
                                    if ($numrows > 0) { ?>
                                        <select class="form-control input-lg" name="network" required id="network">
                                            <?php foreach ($result->result() as $row) {
                                                echo "<option value='" . $row->name. "'>" . $row->name . "</option>";
                                            } ?>
                                        </select>
                                    <?php } else {
                                        echo $this->lang->line("Aucun réseau");
                                    }
                                }else{
                                    echo $this->lang->line("Aucun réseau");
                                }
                                ?>
                            </div>
                            <div class="form-group">
                                <input id="btn-add" type="submit" value="<?= $this->lang->line('Ajouter'); ?>" class="btn btn-primary">
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </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('Modifier le réseau'); ?></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>
                                <input type="text" class="form-control input-lg" name="pays" required id="modal-pays" readonly="readonly">
                            </div>
                            <div class="form-group">
                                <label for="modal-reseau"><?= $this->lang->line('Reseau'); ?></label>
                                <input type="text" class="form-control input-lg" name="reseau" required id="modal-reseau">
                            </div>
							<div class="form-group">
								<input type="checkbox" name="open_wallet_at_mobile_login"  id="modal-open_wallet_at_mobile_login">
								<label for="modal-open_wallet_at_mobile_login"><?php echo $this->lang->line('open_wallet_at_mobile_login'); ?></label>
							</div>
                            <div class="form-group">
                                <input id="btn-add" type="submit" value="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>

    </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 src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.15/js/bootstrap-multiselect.js"></script>

<script>
    $(document).ready(function() {
        var checkboxes = $("td input[type='checkbox']");
        checkboxes.click(function() {
            if ($("td input:checkbox:checked").length > 0)
            {
                $("#Bactiver").css("display", "block");
            }else{
                $("#Bactiver").css("display", "none");
            }

        });


        var Bactiver = $("#Bactiver");
        Bactiver.click(function(){
            var activation = [];
            var desactivation = [];

            $.each($("input[name='activer']:checked"), function(){
                activation.push($(this).data('id'));
            });
            $.each($("input[name='desactiver']:checked"), function(){
                desactivation.push($(this).data('id'));
            });

            $.ajax({
                url:'<?= base_url('Gestion/change_status_network')?>',
                type: 'post',
                data: {activations: activation,desactivations:desactivation},
                dataType: 'json',
                success: function(data){
                    if(data==='completed'){
                        alert('Réseau(x) activé(s)/désactivé(s)');
                        location.reload(true);
                    }else{
                        alert(data);
                        //alert("Une erreur s'est produite");
                        location.reload(true);
                    }
                }
            });

            console.log("Activation : " + activation.join(", ") + " Desactivation : " + desactivation.join(", "));
        });
    });
</script>
<script>
    $(function() {
        $('#unvalidated').DataTable();
        $('#validated').DataTable();
    })

</script>

<script>
    $(function() {
        $('#pays').multiselect({
            includeSelectAllOption: true
        });
    });

</script>

<script>

    var id_network = null;
    var name_network = null;
    var id_country = null;
    var country = null;

    $(document).on("click", ".open-modal", function () {
        id_network = $(this).data('id-network');
        name_network = $(this).data('name-network');
        id_country = $(this).data('id-country');
        country = $(this).data('country');
		const open_wallet_at_mobile_login = $(this).data('open_wallet_at_mobile_login');

        $(".modal-body #modal-pays").val(country);
        $(".modal-body #modal-reseau").val( name_network );
		$(".modal-body #modal-open_wallet_at_mobile_login").prop('checked', open_wallet_at_mobile_login);

    });

    $("#assign-form").submit(function(event){
        //var id_pays = $("#assign-form #pays").val();
        var reseau = $("#assign-form #network").val();
        var selectedValues = $('#assign-form #pays').val();

        $.ajax({
            url:'<?= base_url('Gestion/assignation')?>',
            type: 'post',
            data: {id_country: selectedValues,network:reseau},
            dataType: 'json',
            success: function(data){
                if(data==='completed'){
                    alert('Le réseau a été assigné');
                    location.reload(true);
                }else if(data==='existe'){
                    alert('Ce réseau a déjà été assigné à certains des pays sélectionnés');
                    location.reload(true);
                }else{
                    alert("Une erreur s'est produite");
                    location.reload(true);
                    alert(data);
                }
            }
        });

        event.preventDefault();
    });

    $("#add-network").submit(function(event){
        var new_network = $("#add-network #network").val();
        $.ajax({
            url:'<?= base_url('Gestion/creat_network')?>',
            type: 'post',
            data: {new_network: new_network},
            dataType: 'json',
            success: function(data){
                if(data==='completed'){
                    alert('Le réseau a été ajouté');
                    location.reload(true);
                }else{
                    alert("Une erreur s'est produite");
                    location.reload(true);
                }
            }
        });

        event.preventDefault();
    });

    $("#update-form").submit(function(){
        let up_network = $("#update-form #modal-reseau").val();
		let open_wallet_at_mobile_login = $("#update-form #modal-open_wallet_at_mobile_login").is(':checked') ? 1 : 0 ;
        $.ajax({
            url:'<?= base_url('Gestion/update_network')?>',
            type: 'post',
            data: {new_network: up_network,old_name:name_network ,  open_wallet_at_mobile_login : open_wallet_at_mobile_login, id_network : id_network},
            dataType: 'json',
            success: function(data){
                if(data==='completed'){
                    alert('Le réseau a été modifié');
                    location.reload(true);
                }else{
                    alert("Une erreur s'est produite");
                    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");
        }
    }

</script>
