{% extends "base_frontoffice.html" %}

{% block content %}



<div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center">
        <h4>TRANSFERT</h4>
        <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addArticleModal" onclick="clear_form()">Ajouter </button>
    </div>

    <!-- Article Table -->
    <div class="table-responsive mt-4">
        <table class="table table-sm align-items-center justify-content-center mb-0">
            <thead class="table-light">
                <tr>
                    <th>DATE</th>
                    <th>TYPE</th>
                    <th>LIBELLE</th>
                    <th>CAISSE</th>
                    <th>MONTANT</th>
                </tr>
            </thead>
            <tbody>
                {% for transfert in all_transfert %}
                <tr>
                    <td>{{ transfert.date_str }}</td>
                    <td >{{ transfert.t_type }}</td>
                    <td >{{ transfert.libelle }}</td>
                    <td >{{ transfert.caisse.name }}</td>
                    <td class="money ">{{ transfert.montant }}</td>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
</div>

<!-- Modal Form -->
<div class="modal fade" id="addArticleModal" tabindex="-1" aria-labelledby="addArticleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-md">
        <div class="modal-content">
            <form method="POST" action="/add/transfert">
                <div class="modal-header">
                    <h5 class="modal-title" id="addArticleModalLabel">TRANSFERT</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Fermer"></button>
                </div>
                <div class="modal-body">
                    <div class="input-group mb-1">
            <span class="input-group-text bg-info text-white" >OUT</span>
            <select class="form-select" aria-label="Default select example" name="caisse_out" >
              {% for caisse in all_caisse %}
              <option value="{{caisse.id}}">{{caisse.name}}</option>
              {% endfor %}
            </select>
          </div>
          <div class="input-group mb-1">
            <span class="input-group-text bg-warning text-white" >IN</span>
            <select class="form-select" aria-label="Default select example" name="caisse_in" >
              {% for caisse in all_caisse %}
              <option value="{{caisse.id}}">{{caisse.name}}</option>
              {% endfor %}
            </select>
          </div>
          <div class="input-group mb-1">
            <span class="input-group-text bg-secondary text-white" >DATE</span>
            <input type="date"  class="form-control px-2" name="date" required />
        </div>
          <div class="input-group mb-1">
            <span class="input-group-text bg-secondary text-white" >MONTANT</span>
            <input type="number" class="form-control " id="moneyInput" name="montant" min=0 step="0.01" required  autocomplete="off" />
          </div>
          <div id="moneyInWords" class="form-text mt-2"></div>
            </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-warning" >ENREGISTRER</button>
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Annuler</button>
                    
                </div>
            </form>
        </div>
    </div>
</div>
<script src="../static/js/chiffre2lettre.js"></script>
<script>
  const moneyInput = document.getElementById('moneyInput');
  const moneyInWords = document.getElementById('moneyInWords');

  moneyInput.addEventListener('input', () => {
    moneyInput.value = moneyInput.value.replace(/[^0-9+ ]/g, ''); 
    const value = parseInt(moneyInput.value);
    if (!isNaN(value) && value <= 1000000000) {
      moneyInWords.textContent = NumberToLetter(value) + ' Ariary';
    } else {
      moneyInWords.textContent = '';
    }
  });

  window.onload=function(){
    var all_number =$(".money");
    for (let number of all_number){

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

};
</script>
{% endblock %}