{% extends "base_frontoffice.html" %}

{% block content %}



<div class="container mt-4">
    <h4>Historique Dépense</h4>
    <div class="d-flex justify-content-between align-items-center">
        <div class="input-group mb-1">
          <span class="input-group-text bg-secondary text-light" id="basic-addon1">Caisse: </span>
              <select class="form-control "  name="client" onchange="selectCaisse(this)">
                <option value="{{caisse.id}}">{{caisse.name}}</option>
                {% for caisse in all_caisse %}
                  <option value="{{caisse.id}}" data-name="{{caisse.name}}" >{{caisse.name}}</option>
                {% endfor %}
              </select>
          </div>

          <div class="input-group mb-1">
            <ul class="list-group " >
            <li class="list-group-item d-flex justify-content-between">
              <span >SOLDE: </span>
              <strong class="money">{{caisse.solde_val_html}}</strong>
            </li>
            </ul>
        </div>
    </div>
        
    <div class="d-flex justify-content-between align-items-center no-print">

    <input type="text " id="tableSearch" class="form-control  w-50" placeholder="Recherche ..." >
    
    </div>
    <!-- Article Table -->
    <div class="table-responsive mt-4">
        <table class="table table-sm align-items-center justify-content-center mb-0" id="commandeTable">
            <thead class="table-light">
                <tr>
                    <th>Date</th>
                    <th>N° FICHE</th>
                    <th>LIBELLE</th>
                    <th>OBJET</th>
                    <th>BENEFICIAIRE</th>
                    <th>MONTANT</th>
                    <th>IMPRESSION</th>                    
                </tr>
            </thead>
            <tbody>
                {% for depense in all_depense %}
                <tr data-id="{{depense.id}}">
                    <td>{{ depense.date_str }}</td>
                    <td>{{ depense.numero_fiche }}</td>
                    <td>{{ depense.libelle }}</td>
                    <td>{{ depense.objet }}</td>
                    <td>{{ depense.beneficiaire }}</td>
                    <td class="money">{{ depense.montant }}</td>
                    <td>
                      <span class="d-flex">
                        <a class="btn btn-outline-primary btn-sm mx-1" href="#" onclick="ajax_print('/rawbt-caisse-out?id={{depense.id}}', this)">IMPRIMER</a>
                      </span>
                    </td>
                </tr>
              
                {% endfor %}
            </tbody>
        </table>
    </div>
</div>

<script>
document.getElementById("tableSearch").addEventListener("keyup", function () {
  const input = this.value.toLowerCase();
  const rows = document.querySelectorAll("#commandeTable tbody tr");

  rows.forEach(row => {
    const rowText = row.innerText.toLowerCase();
    row.style.display = rowText.includes(input) ? "" : "none";
  });
});
</script>
<script>
  window.onload=function(){
    var all_number =$(".money");
    for (let number of all_number){

      number.textContent=parseFloat(number.textContent).toLocaleString("fr-FR", {minimumFractionDigits:2});
    };

};


function selectCaisse(self){

    window.location.href="/depense/caisse?cid="+self.value;
}

</script>
<script>
function pc_print(data){
alert("ONLY ON A BLUETOOTH ANDROID PRINTER");
}       


function android_print(data){
            window.location.href = data; 
        }

// for php demo call
function ajax_print(url, btn) {
    console.log(btn);
    b = $(btn);
    b.attr('data-old', b.text());
    b.text('wait');
    $.get(url, function (data) {
    var ua = navigator.userAgent.toLowerCase();
    var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
    if(isAndroid) {
        android_print(data);
    }else{
        pc_print(data);
    }
        }).fail(function () {
            alert("ajax error");
        }).always(function () {
            b.text(b.attr('data-old'));
        })
    }


</script>
{% endblock %}