{% extends "base_frontoffice.html" %}

{% block content %}



<div class="container mt-4">
    

        <h4>Liste des commandes</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">Client</span>
              <select class="form-control "  name="client" onchange="selectClient(this)">
                <option value="{{client.id}}">{{client.nom}}</option>
                {% for client in all_client %}
                  <option value="{{client.id}}" data-name="{{client.nom}}" data-phone="{{client.phone}}">{{client.nom}}</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 >TOTAL: </span>
              <strong class="money">{{total_achat}}</strong>
            </li>
            </ul>
        </div>
    </div>
    <div class="d-flex justify-content-between align-items-center">
    <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>Numéro</th>
                    <th>Date Comm</th>
                    <th>Client code</th>
                    <th>Client nom</th>
                    <th>Net à payer</th>
                    <th>Règlement</th>
                    <th>Livraison</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                {% for commande in all_commande %}
                <tr data-id="{{commande.id}}">
                    <td>{{ commande.numero }}</td>
                    <td>{{ commande.date_str }}</td>
                    <td>{{ commande.client.code }}</td>
                    <td>{{ commande.client.nom }}</td>
                    <td class="money">{{ commande.net_payer_str }}</td>
                    <td class="money">{{ commande.montant_paye[1] }}</td>
                    <td>{{ commande.taux_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" data-bs-toggle="modal" data-bs-target="#reglerModal" onclick="reglement('{{commande.id}}')" >Regler</button><button class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#livrerModal" onclick="livraison('{{commande.id}}')">Livrer</button>
                    </span>
                    </td>
                </tr>
              
                {% endfor %}
            </tbody>
        </table>
    </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" >NUMERO</span>
            <input type="text"  class="form-control px-2"  id="detail_numero"  disabled />
        </div>
        <div class="input-group mb-1">
            <span class="input-group-text bg-secondary text-white" >CLIENT</span>
            <input type="text"  class="form-control px-2 w-50"  id="detail_client_nom" disabled  />
            <input type="text"  class="form-control px-2"  id="detail_client_code" 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>PU</th>
                    <th>Qté</th>
                    <th>Rem</th>
                    <th>Montant</th>
                </tr>
            </thead>
            <tbody id="body_detail">
             </tbody>
        </table> 
        </div> 
        <div class="input-group mb-1">
            <span class="input-group-text bg-secondary text-white" >TOTAL REMISE</span>
            <input type="text"  class="form-control px-2"  id="total_remise" />
        </div>
        <div class="input-group mb-1">
            <span class="input-group-text bg-secondary text-white" >NET A PAYER</span>
            <input type="text"  class="form-control px-2"  id="net_payer" />
        </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 COMMANDE</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' class="form-control px-2" name="montant" required />
        </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="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>
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/add?id='+id;
}

function show_detail(id) {
    let detail_date=document.getElementById("detail_date");
    detail_date.value="";
    let detail_client_nom=document.getElementById("detail_client_nom");
    detail_client_nom.value="";
    let detail_client_code=document.getElementById("detail_client_code");
    detail_client_code.value="";
    let detail_numero=document.getElementById("detail_numero");
    detail_numero.value="";

    let body_detail=document.getElementById("body_detail");
    body_detail.innerHTML="";
    fetch("/json/commande?id="+id, {
      method: "GET",
    })
    .then(res => res.json())
    .then(data => {

      detail_client_code.value=data["client_code"];
      detail_client_nom.value=data["client_nom"];
      detail_numero.value=data["numero"];
      detail_date.value=data["date"];

      for(let detail of data["detail"]){

          let tr_row=document.createElement("tr");
          let ref_td=document.createElement("td");
          ref_td.textContent=detail["article_code"];

          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 rem_td=document.createElement("td");
          rem_td.textContent=detail["remise"];
          let montant_td=document.createElement("td");
          montant_td.textContent=parseFloat(detail["sous_total"]).toLocaleString("fr-FR", {minimumFractionDigits:2});
          tr_row.appendChild(ref_td);
 
          tr_row.appendChild(prix_td);
          tr_row.appendChild(qty_td);
          tr_row.appendChild(rem_td);
          tr_row.appendChild(montant_td);
          body_detail.appendChild(tr_row);
      }
     

      


    })
    .catch(err => {
      console.error(" failed:", err);
      
    });
}


function selectClient(self){

    window.location.href="/commande/client?id="+self.value;
}
</script>
{% endblock %}