<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('Modification et Activation des villes'); ?>
        </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('Activer/Désactiver'); ?></h3>
                    </div>
                    <div class="box-body" style="overflow-x:auto;">

                        <?php

                        $sql2 = $list_villes;
                        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('Villes'); ?></th>
                                        <th><?= $this->lang->line('Pays'); ?></th>
                                        <th align='center'><?= $this->lang->line('Statut'); ?></th>
                                        <th align='center'><?= $this->lang->line('Activer'); ?></th>
                                        <th align='center'><?= $this->lang->line('Désactiver'); ?></th>
                                        <th align='center'><?= $this->lang->line('Modifier'); ?></th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                <?php
                                    foreach($sql2->result() as $row) {
                                        $num ++;
                                        //$member_code = randomString1(10);
                                        $libelle_status = $this->lang->line('Inactive');
                                        if($row->status==1){
                                            $libelle_status = $this->lang->line('Active');
                                        }
                                        echo "<tr>
                                                  <td align='center'>$num</td>
                                                  <td >$row->name</td>
                                                  <td>$row->country</td>
                                                  <td align='center'>$libelle_status</td>";
                                        if($row->status==0) {
                                            echo "<td align='center'><input type='checkbox' name='activer' class='chk' value='".$row->id."'></td>
                                                  <td align='center'><input type='checkbox' name='desactiver' value='".$row->id."' disabled></td>";
                                        }else{
                                            echo "<td align='center'><input type='checkbox' name='activer' value='".$row->id."' disabled></td>
                                                  <td align='center'><input type='checkbox' name='desactiver' value='".$row->id."' class='unchk'></td>";
                                        }
                                                echo  "<td id='edition' align='center'>
                                                           <button data-toggle='modal' data-id-town='$row->id' data-town='$row->name' data-country='$row->country' data-id-country='$row->country_id' class='open-modal modif btn btn-success' data-target='#editModal'><i class='fa fa-edit'></i></button>
                                                           </td>
                                              </tr>";
                                    }
                                ?>
                                    </tbody>
                                </table>
                                    <button type="button" class="btn btn-success" id="valider"><?= $this->lang->line('Activer/Désactiver'); ?></button>
                                <?php

                            }

                            else {
                                echo $this->lang->line('Aucune ville');
                            }
                        }else {
                            echo $this->lang->line('Aucune ville');
                        }
                        ?>
                    </div>
                </div>
            </div>
            <div class="col-xs-4">
                <div class="box">
                    <div class="box-header">
                        <h3 class="box-title"><?= $this->lang->line('Ajouter une ville'); ?></h3>
                    </div>
                    <div class="box-body">
                        <form action="<?= base_url('Gestion/ajout')?>" 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">
                                        <?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('Ville'); ?></label>
                                <input type="text" class="form-control input-lg" name="ville" required id="ville">
                            </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 la ville'); ?></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="modal-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="modal-ville"><?= $this->lang->line('Ville'); ?></label>
                                    <input type="text" class="form-control input-lg" name="ville" required id="modal-ville">
                                </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>
    $(function() {
        $('#unvalidated').DataTable();
        $('#validated').DataTable();
    })

</script>

<script>

    var town = null;
    var id_town = null;
    var country = null;
    var id_country = null;

    $(document).on("click", ".open-modal", function () {
        town = $(this).data('town');
        id_town = $(this).data('id-town');
        country = $(this).data('country');
        id_country = $(this).data('id-country');

        $(".modal-body #modal-pays").append("<option value='"+id_country+"' selected>"+country+"</option>");
        $(".modal-body #modal-ville").val( town );

    });

    $("#update-form").submit(function(){
        var new_id_country = $("#modal-pays").val();
        var new_town = $("#modal-ville").val();
        $.ajax({
            url:'<?= base_url('Gestion/update')?>',
            type: 'post',
            data: {id_country: new_id_country,id_town:id_town,town:new_town},
            dataType: 'json',
            success: function(data){
                if(data==='completed'){
                    alert('La ville a été mise à jour');
                    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(){

        var chkArray = [];
        $(".chk:checked").each(function() {
            chkArray.push($(this).val());
        });
        var selected;
        selected = chkArray.join(',') ;

        var unchkArray = [];
        $(".unchk:checked").each(function() {
            unchkArray.push($(this).val());
        });
        var selectedForDisable;
        selectedForDisable = unchkArray.join(',') ;

        if(selected.length > 0 && selectedForDisable.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 if (selectedForDisable.length > 0 && selected.length === 0){
            $.ajax({
                url:'<?= base_url('Gestion/desactiver')?>',
                type: 'post',
                data: {villes: unchkArray},
                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 if (selectedForDisable.length > 0 && selected.length > 0){
            $.ajax({
                url:'<?= base_url('Gestion/activer_desactiver')?>',
                type: 'post',
                data: {dVilles: unchkArray,aVilles: chkArray},
                dataType: 'json',
                success: function(data){
                    if(data==='completed'){
                        alert('Ville(s) (dé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(){
        var unchkArray = [];
        $(".unchk:checked").each(function() {
            unchkArray.push($(this).val());
        });
        var selectedForDisable;
        selectedForDisable = unchkArray.join(',') ;

        if(selectedForDisable.length > 0){
            $.ajax({
                url:'<?= base_url('Gestion/desactiver')?>',
                type: 'post',
                data: {villes: unchkArray},
                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>
