{% extends "base_frontoffice.html" %}

{% block content %}



<div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center">
        <h4>Réservaion poussin du :<strong>{{commande_poussin.date_prevu_livraison_str}}</strong></h4>
    </div>
    <div class="d-flex justify-content-between align-items-center">
    <input type="text " id="tableSearch" class="form-control  w-50" >
    </div>
    <!-- Article Table -->
    <div class="table-responsive mt-4">
        <table class="table table-sm align-items-center justify-content-center mb-0" id='clientTable'>
            <thead class="table-light">
                <tr>
                    <th>Date rés</th>
                    <th>Client</th>
                    <th>Qté com</th>
                    <th>Qté livré</th>
                    <th>Prix</th>
                    <th>Date prevu</th>
                    <th>Status</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                {% for reserve in all_reservation %}
                <tr data-id="{{reserve.id}}">
                    <td>{{ reserve.date_reservation_str }}</td>
                    <td>{{ reserve.client.nom }}</td>
                    <td class="bg-info">{{ reserve.quantite_commande }}</td>
                    <td >{{ reserve.quantite_livre }}</td>
                    <td class="money">{{ reserve.prix_str }}</td>
                    <td >{{ reserve.date_prevu_livraison_str }}</td>
                    <td >{{ reserve.status }}</td>
                    <td><span class="d-flex">
                        <button class="btn btn-outline-secondary btn-sm" data-bs-toggle="modal" data-bs-target="#detailModal" onclick="show_detail('{{reserve.id}}')">LIVRAISON</button>
                        
                    </span>
                    </td>
                </tr>
              
                {% endfor %}
            </tbody>
        </table>
    </div>
</div>
<!-- Modal Form -->
<div class="modal fade" id="newCommande" 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">NOUVEAU COMMANDE POUSSIN</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Fermer"></button>
      </div>
      <form method="POST" action="/poussin/new_commande">
      <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"  name='date_commande'  required />
        </div>
        <div class="input-group mb-1">
            <span class="input-group-text bg-secondary text-white" >QUANTITE COMMANDE</span>
            <input type="number"  class="form-control px-2" name="quantite_commande" min="0"  required />
        </div>
        <div class="input-group mb-1">
            <span class="input-group-text bg-secondary text-white" >DATE PREVU LIVRAISON</span>
            <input type="date"  class="form-control px-2"  name='date_prevu_livraison'  required />
        </div>
        <div class="input-group mb-1">
            <span class="input-group-text bg-secondary text-white" >PRIX D'ACHAT</span>
            <input type="number"  class="form-control px-2"   name='prix_achat' min="0" step="0.01"  required />
        </div>
        <div class="input-group mb-1">
            <span class="input-group-text bg-secondary text-white" >PRIX DE VENTE</span>
            <input type="number"  class="form-control px-2"   name='prix_vente' min="0" step="0.01"  required />
        </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">FERMER</button>
      </div>
    </div>
    </form>
  </div>
</div>
</div>

<div class="modal fade" id="reserveationModal" 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">reserveATION POUSSIN POUR: <strong id="date_reservee"></strong> </h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Fermer"></button>
      </div>
      <form method="POST" action="#" id='reserveationForm'>
      <div class="modal-body" >
        <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="0">NOUVEAU CLIENT</option>
                {% for client in all_client %}
                  <option value="{{client.id}}">{{client.full_name}}</option>
                {% endfor %}
              </select>
          </div>
          <div class="input-group mb-1">
          <span class="input-group-text bg-secondary text-light" id="basic-addon1">Téléphone</span>
          <input type="text" class="form-control" id="phone" required name="phone">
          </div>
          <div class="input-group mb-1">
          <span class="input-group-text bg-secondary text-light" id="basic-addon1">Nom</span>
          <input type="text" class="form-control" id="nom" required name="nom">
          </div>
          
          <div class="input-group mb-1">
          <span class="input-group-text bg-secondary text-light" id="basic-addon1">Adresse</span>
          <input type="text" class="form-control" id="address"  name="address">
        </div>
        <div class="input-group mb-1">
            <span class="input-group-text bg-secondary text-white" >DATE reserveATION</span>
            <input type="date"  class="form-control px-2" name="date_reserveation" required />
        </div>
        <div class="input-group mb-1">
            <span class="input-group-text bg-secondary text-white" >QUANTITE reserveE</span>
            <input type="number" min="1"  class="form-control px-2" name="quantite_commande" required />
        </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">FERMER</button>
      </div>
    </div>
    </form>
  </div>
</div>
</div>
<script>
document.getElementById("tableSearch").addEventListener("keyup", function () {
  const input = this.value.toLowerCase();
  const rows = document.querySelectorAll("#clientTable 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 reserveation(id, date_prevu){
    let regForm=document.getElementById("reserveationForm");
    regForm.action='/poussin/add_reserveation?id='+id;
    document.getElementById("date_reservee").textContent=date_prevu;
}

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){

  let nom=document.getElementById("nom");
  let phone=document.getElementById("phone");
  let address=document.getElementById("address");
  if(self.value==="0"){

    nom.hasAttribute("disabled") ? nom.removeAttribute('disabled') :null;
    phone.hasAttribute("disabled") ? phone.removeAttribute('disabled') :null;
    address.hasAttribute("disabled") ? address.removeAttribute('disabled') :null;
  }else{
    nom.setAttribute("disabled",true);
    phone.setAttribute("disabled",true);
    address.setAttribute("disabled",true);
  }
}
</script>
{% endblock %}