{% extends "base_frontoffice.html" %}

{% block content %}



<div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center">
        <h4 class="responsive-heading">Listes Commande fournisseur</h4>
    </div>
<div class="row">
    <div class="col-sm-12 col-md-8 col-lg-8 text-white">
    <div class="d-flex justify-content-between align-items-center">
    <input type="text " id="tableSearch" class="form-control  w-50" >
    <a class="btn btn-primary" href="/panier/commande">NOUVEAU</a>
    
    </div>
    <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>Fournisseur</th>
                    <th>Numéro</th>
                    <th>Date</th>
                    <th>Montant</th>
                    <th>Livraison</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                {% for commande in all_commande %}
                <tr data-id="{{commande.id}}">
                    <td>{{ commande.fournisseur.nom }}</td>
                    <td>{{ commande.numero }}</td>
                    <td>{{ commande.date_str }}</td>
                    <td class="money">{{ commande.montant_commande }}</td>
                    <td>{{ commande.statut_livraison() }}</td>
                    <td><span class="d-flex">
                        <button class="btn btn-secondary btn-sm" data-bs-toggle="modal" data-bs-target="#detailModal" onclick="show_detail('{{commande.id}}')">Detail</button>
                        <!-- <button class="btn btn-warning btn-sm mx-1" data-bs-toggle="modal" data-bs-target="#reglerModal" onclick="reglement('{{commande.id}}')">Régler</button> -->
                        <a class="btn btn-info btn-sm mx-1" href="/panier/fournisseur?cid={{commande.id}}">Livrer</a>
                        <!-- <a class="btn btn-success btn-sm mx-1" href="/bc?cid={{commande.id}}" target="_blanc">BC</a> -->
                    </span>
                    </td>
                </tr>
              
                {% endfor %}
            </tbody>
        </table>
    </div>
    </div>
    </div>
</div>
<!-- Modal Form -->
<div class="modal fade" id="detailModal" tabindex="-1" aria-labelledby="cancelLabel" >
  <div class="modal-dialog modal-md">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="cancelLabel">DETAIL COMMANDE</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-secondary text-white" >DATE COMMANDE</span>
            <input type="date"  class="form-control px-2"  id="detail_date"  disabled />
        </div>
        <div class="input-group mb-1">
            <span class="input-group-text bg-secondary text-white" >REF</span>
            <input type="text"  class="form-control px-2"  id="ref"  disabled />
        </div>
        
        
        <div class="table-responsive mt-1">
        <table class="table table-sm align-items-center justify-content-center mb-0">
            <thead class="table-light">
                <tr>
                    <th>Ref</th>
                    <th>P Achat</th>
                    <th>Qté</th>
                    <th>Montant</th>
                    <th>Qté liv</th>
                </tr>
            </thead>
            <tbody id="body_detail">
             </tbody>
        </table> 
        </div>
        
       
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">FERMER</button>
      </div>
    </div>
  </div>
</div>
</div>

<div class="modal fade" id="reglerModal" tabindex="-1" aria-labelledby="cancelLabel" >
  <div class="modal-dialog modal-md">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="cancelLabel">REGLEMENT LIVRAISON</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Fermer"></button>
      </div>
      <form method="POST" action="#" id='regForm'>
      <div class="modal-body" >
        <div class="input-group mb-1">
            <span class="input-group-text bg-secondary text-white" >DATE REGLEMENT</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" min="0" step='0.01' id="moneyInput" class="form-control px-2" name="montant" required />
        </div>
        <div id="moneyInWords" class="form-text mt-2"></div>
        <div class="input-group mb-1">
            <span class="input-group-text bg-secondary text-white" >MODE DE PAIEMENT</span>
            <input type="text"  class="form-control px-2" name="mode"  required/>
        </div>
        <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="caisse">
                {% 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" >REF</span>
            <input type="text" class="form-control px-2" name="ref" required />
        </div>
        
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">FERMER</button>
        <button type="submit" class="btn btn-danger">ENREGISTRER</button>
      </div>
    </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 = '';
    }
  });

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 reglement(id){
    let regForm=document.getElementById("regForm");
    regForm.action='/reglement/livraison?id='+id;
}

function show_detail(id) {
    let detail_date=document.getElementById("detail_date");
    detail_date.value="";
    let ref=document.getElementById("ref");
    ref.value="";
    let body_detail=document.getElementById("body_detail");
    body_detail.innerHTML="";


    fetch("/json/commande/fournisseur?id="+id, {
      method: "GET",
    })
    .then(res => res.json())
    .then(data => {
      console.log(data);
      ref.value=data["numero"];
      detail_date.value=data["date"];
      for(let detail of data["detail"]){
          let code =detail["article_code"];
          let tr_row=document.createElement("tr");
          let ref_td=document.createElement("td");
          ref_td.textContent=code;
          let livraison=data["livraison"];
          console.log(typeof(livraison));
          let prix_td=document.createElement("td");
          prix_td.textContent=parseFloat(detail["pu"]).toLocaleString("fr-FR", {minimumFractionDigits:2});
          let qty_td=document.createElement("td");
          qty_td.textContent=detail["quantite"];
          let montant_td=document.createElement("td");
          montant_td.textContent=parseFloat(detail["sous_total"]).toLocaleString("fr-FR", {minimumFractionDigits:2});
          let liv_td=document.createElement("td");
          liv_td.textContent=data["livraison"][code];
          tr_row.appendChild(ref_td);
 
          tr_row.appendChild(prix_td);
          tr_row.appendChild(qty_td);
          
          tr_row.appendChild(montant_td);
          tr_row.appendChild(liv_td);
          body_detail.appendChild(tr_row);
      }
    

      


    })
    .catch(err => {
      console.error(" failed:", err);
      
    });
}
</script>
{% endblock %}