<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
  <!-- Content Header (Page header) -->
  <section class="content-header">
    <h1>
      Gestions des
      <small>Remises</small>
    </h1>
    <ol class="breadcrumb">
      <li><a href="#"><i class="fa fa-dashboard"></i> Accueil</a></li>
      <li class="active">Remise</li>
    </ol>
  </section>

  <section class="content">
    <div class="row">
      <div class="col-md-12 col-xs-12">
        <div id="messages"></div>
        <?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 elseif (session()->getFlashdata('errors')): ?>
          <div class="alert alert-warning 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('errors'); ?>
          </div>
        <?php endif; ?>

        <div class="box">
          <div class="box-header">
            <h3 class="box-title">Gérer les remises</h3>
          </div>

          <div class="table-responsive">
            <table id="manageTable" class="table table-bordered table-striped ">
              <thead>
                <tr>
                  <th>#</th>
                  <th>Produit</th>
                  <th>Prix total</th>
                  <th>Demande</th>
                  <th>Statut</th>
                  <?php if (in_array('validateRemise', $user_permission) || in_array('refusedRemise', $user_permission)): ?>
                    <th>Action</th>
                  <?php endif; ?>
                </tr>
              </thead>
              

            </table>
            <!-- totaliter mvola = somme tranche 1 izay type mvola tranche 1 + somme tranche 2
                    totaliter mvola dia - recouvrement izay sortie ùmvola plus recouvremnt entrer mvola
          -->
          </div>
        </div>
      </div>
    </div>
  </section>
</div>

<script>
  $(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"
                    }
                }
            });

    $("#remise_menu").addClass('active');

    // Check if the URL contains the _ parameter
    if (window.location.search.startsWith("?_=")) {
      window.location.href = window.location.origin + window.location.pathname;
    }

    manageTable = $('#manageTable').DataTable({
      ajax: '<?= base_url('remise/fetchRemiseData') ?>',
      order:[],
      columns: [
      { data: 0 },  
      { data: 1 },  
      { data: 2 },  
      { data: 3 },  
      { data: 4 },  
      { data: 5 }
    ],

    });
    
  })


  function refuseFunc(id) {
    
    $.ajax({
            url: '<?= base_url('remise/updateRemise') ?>/' + id,
            type: 'POST',
            data: {
                'demande_status': 'rejete'
            }, 
            dataType: 'json',
            success: function(response) {

              manageTable.ajax.reload(null, false);

              if (response.success === true) {
                $("#messages").html('<div class="alert alert-warning alert-dismissible" role="alert">' +
                  '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>' +
                  '<strong> <span class="glyphicon glyphicon-ok-sign"></span> </strong>' + response.messages +
                  '</div>');

              } else {

                if (response.messages instanceof Object) {
                  $.each(response.messages, function(index, value) {
                    var id = $("#" + index);

                    id.closest('.form-group')
                      .removeClass('has-error')
                      .removeClass('has-success')
                      .addClass(value.length > 0 ? 'has-error' : 'has-success');

                    id.after(value);

                  });
                } else {
                  $("#messages").html('<div class="alert alert-danger alert-dismissible" role="alert">' +
                    '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>' +
                    '<strong> <span class="glyphicon glyphicon-exclamation-sign"></span> </strong>' + response.messages +
                    '</div>');
                }
              }
            }
          });
  }

  function valideFunc(id) {
    
    $.ajax({
            url: '<?= base_url('remise/updateRemise') ?>/' + id,
            type: 'POST',
            data: {
                'demande_status': 'accepte'
            }, 
            dataType: 'json',
            success: function(response) {

              manageTable.ajax.reload(null, false);

              if (response.success === true) {
                $("#messages").html('<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>' +
                  '<strong> <span class="glyphicon glyphicon-ok-sign"></span> </strong>' + response.messages +
                  '</div>');

              } else {

                if (response.messages instanceof Object) {
                  $.each(response.messages, function(index, value) {
                    var id = $("#" + index);

                    id.closest('.form-group')
                      .removeClass('has-error')
                      .removeClass('has-success')
                      .addClass(value.length > 0 ? 'has-error' : 'has-success');

                    id.after(value);

                  });
                } else {
                  $("#messages").html('<div class="alert alert-warning alert-dismissible" role="alert">' +
                    '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>' +
                    '<strong> <span class="glyphicon glyphicon-exclamation-sign"></span> </strong>' + response.messages +
                    '</div>');
                }
              }
            }
          });
  }

</script>