<template>



    <div class="content-header">
        <div class="container-fluid">
          <div class="row mb-2">
            <div class="col-sm-6">
            
              <h3 class="font-weight-bold">Achat</h3>
  
              
            </div><!-- /.col -->
        
            <div class="col-sm-6">
              <a v-if="!success"  @click="loadf" class="float-right  btn btn-info">Nouveau</a>   
  
       
          <!-- Button trigger modal -->
          <a v-if="success" class="float-right  btn btn-info" data-toggle="modal" data-target="#modal-xl">Categorie</a>
  
  
  
  
  
       
   
    </div></div></div>
      
               <div v-if="bloc" class="row">
                 <div class="col-12 ">
                   <div v-if="success" class="card">
                           <div class="card-header">
                            <form @submit.prevent="add">
                             <div class="card-body">
                               <div class="row">
          
  
  <div class="col-md-12">
    <div class="row">
       <div class="col-md-6">
        <div class="col-md-10">
                                    <div class="form-group row">
                                      <label class="col-sm-3 col-form-label">Fourniseur</label>
                                      <div class="col-sm-8">
              <select name="idfournisseur" class="form-control" v-model="vente.fournisseur_id">
                <option v-for="four in fournisseurs" :value="four.id">{{ four.nom }}</option>
            
              </select> 
            </div>
        <!-- Button trigger modal -->
  <button type="button" class="btn btn-info" data-toggle="modal" data-target="#f">
   <i class="fas fa-solid fa-user"></i>
  </button>
  
  
  <!-- Modal -->
  <div class="modal fade" id="f" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLabel">Ajouter fournisseur</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          <form @submit.prevent="addfournisseur">
  
  
  <div class="form-group">
  <label for="exampleInputEmail1">Ajouter nom</label>
  <input v-model="nomf" type="text" class="form-control" placeholder="Enter nom">
  </div>  
  
  <div class="form-group">
  <label for="exampleInputEmail1">Ajouter tele</label>
  <input v-model="telef" type="text" class="form-control" placeholder="Enter tele">
  </div> 
  
  
  
  <button type="button" class="btn btn-secondary" data-dismiss="modal">quitter</button>
    <button type="submit" class="btn btn-warning float-right">enregistrer</button> 
  </form>
        </div>
  
      </div>
    </div>
  </div>
            </div>
          </div> 
          <div class="col-md-10">
                                    <div class="form-group row">
                                      <label class="col-sm-3 col-form-label">Dépot</label>
                                      <div class="col-sm-8">
              <select name="idfournisseur" class="form-control" v-model="vente.lieustock_id" @change="gettopcat">
                <option v-for="lieu in lieustocks2" :value="lieu.id">{{ lieu.lieu }}</option>
            
              </select> 
            </div>
            </div>
          </div> 
          <div class="col-md-10">
                                    <div class="form-group row">
                                      <label class="col-sm-3 col-form-label">Categorie</label>
                                      <div class="col-sm-8">
              <select name="idfournisseur" class="form-control" v-model="vente.topcat_id" required >
                <option v-for="top in topcat12" :value="top.id"> {{ top.topcat }}</option>
            
              </select> 
            
            </div> 
  <!-- Button trigger modal -->
  <button type="button" class="btn btn-info" data-toggle="modal" data-target="#b">
   <i class="fas fa-solid fa-plus"></i>
  </button>
  
  <!-- Modal -->
  <div class="modal fade" id="b" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLabel">Ajouter une categorie</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          <form >
  
  
  <div class="form-group">
  <label for="exampleInputEmail1">Ajouter une categorie</label>
  <input v-model="catadd" type="text" class="form-control" placeholder="Enter categorie">
  </div>  
  <div class="form-group">
  <label for="exampleFormControlSelect1">Choisi stock </label>
  <select v-model="topcatadd" name="lieu" class="form-control" id="exampleFormControlSelect1">
    <option v-for="lieu in lieustocks2" :value="lieu.id">{{ lieu.lieu }}</option>
  </select>
  </div> 
  <div class="form-group">
  <label for="exampleFormControlFile1">Ajouter image</label>
  <input type="file" name="image" class="form-control-file"  @change="onfilselected2" accept="image/*" required>
  </div>
  
  
  
  
  <button type="button" class="btn btn-secondary" data-dismiss="modal">quitter</button>
    <button type="submit"  @click.prevent="addcategorie"    class="btn btn-warning float-right">enregistrer</button> 
  </form>
        </div>
  
      </div>
    </div>
  </div>
  
  
  
  
  
  
  
  
  
  
  
  
  
  
            </div>
          </div> 
         <p style=" font-size:16px;font-weight: bolder;">Ajouter image</p> 
          <input style="display: none;" type="file" name="image[]" id="image" ref="imageInput" @change="onfilselected" accept="image/*" required>
        <label style="
      height: 146px;
      border-radius: 10px;
      width: 80%;
      text-align: center;
      text-transform: uppercase;
      font-size: 49px;
      padding: 28px 40px;
      border-style: dotted;
      color: #17a2b8;
        
        
        
        " for="image">
      <i class="fa-solid fas-cloud-arrow-up"></i>
          
      <i class="fas fa-solid fa-upload"></i> </label>
       </div>   
      <div class="col-md-6"> 
        <div class="form-group row">
      <label class="col-sm-2 col-form-label">Code Artiicle</label>
      <div class="col-sm-10">
        <input name="ref"  v-model="vente.id"  type="text" class="form-control" id="exampleFormControlInput1" placeholder="changer prix " required>
      </div>
    </div>
    <div class="form-group row">
      <label class="col-sm-2 col-form-label">Nom Artiicle</label>
      <div class="col-sm-10">
        <input name="nom" v-model="vente.nom"  type="text" class="form-control" id="exampleFormControlInput1" placeholder="changer prix " required>
      </div>
    </div>
          <div class="form-group row">
      <label class="col-sm-2 col-form-label">Prix Achat</label>
      <div class="col-sm-10">
        <input name="prixun"  v-model="vente.prixun" type="text" class="form-control" id="exampleFormControlInput1" placeholder="changer prix " required>
      </div>
    </div>
          <div class="form-group row">
      <label class="col-sm-2 col-form-label">Prix Vente</label>
      <div class="col-sm-10">
        <input name="prixvente"  v-model="vente.prixvents"  type="text" class="form-control" id="exampleFormControlInput1" placeholder="changer prix " required>
      </div>
    </div>
    <div class="form-group row">
      <label  class="col-sm-2 col-form-label">Quantité</label>
      <div class="col-sm-10">
        <input name="qty"  v-model="vente.quantite" type="text" class="form-control" id="exampleFormControlInput1" placeholder="changer prix " required>
      </div>
    </div>
  
      </div>   
    </div>
  
  </div>
      
                     
  
                               </div>
     
     
      
    </div><button style="float: right;" class="brn btn-info" type="submit">Ajouter</button>     
     
                     </form>      </div>
     
    
    </div>
  
  
                
  
               
  
  
  
                               
  
  
                           
                                
  
   </div>
     
  
     
      
  
     
                 <div class="modal fade" id="modal-xl">
         <div class="modal-dialog modal-xl">    
             <div class="modal-content">
           <div class="modal-header">
             <h5 class="modal-title">
     
         
             </h5>
    
     <div v-if="showprod"  class="form-controller">
                  <label>cherher</label>
    <input v-model="q" type="text">
    </div>
     
      <div v-if="shoowliu"   v-for="lieu in lieustocks" :value="lieu.id">
     <button class="btn btn-info mr-2" @click="getcatebaylieux2(lieu.id)">{{ lieu.lieu }}</button>
     </div>
             <button @click="shlieu" type="button" class="btn btn-success">
            liste stoock
             </button>
           </div>
     
     
     
           <div  class="modal-body  ">
             <div class="row">
               <div v-if="showfirst" class="col-md-2"  v-for="tcat in topcat1" :value="tcat.id">
      
                 <div class="card">
       <div class="card-body" style=" padding: 2px !important;" >
         <img @click="showprduits(tcat.id)" style="width: 100%;height: 80px;padding: 0;"  :src="'img/' + tcat.img"/>
         <p style="font-weight: bold; color: green;text-align: center;">{{ tcat.topcat }}</p>
     </div>
     </div>
     </div></div>
     
     
     
     
     
     
           </div>
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
          
           <div v-if="showprod"  class="modal-body  ">
           
             <div class="row">
        
       <div class="col-md-2"  v-for="prod in filterpost" :value="prod.id">
      
     <div class="card" >
    
       <div class="card-body" @click="addcart(prod)"  style=" padding: 2px !important;" >
         <img  style="width: 100%;height: 80px;padding: 0;" :src=" prod.img"/>
     <p style="font-weight: bold; color: green;text-align: center;" > {{ prod.nom }}</p>
     <p style="font-weight: bold; color: red;text-align: center;">prix de vente: {{ prod.prixvents }}  DH</p>
     <p style="font-weight: bold; color:blueviolet;text-align: center;"> prix d'achat:{{ prod.prixun }}  DH</p>
     </div>
     </div> 
     
     
     
     
     
     
       </div>
     </div>
             </div>
    
         
           <div v-if="showtopcat"  class="modal-body  ">
             <div class="row">
       <div class="col-md-2"  v-for="top in topcats" :value="top.id">
      
     <div class="card">
       <div class="card-body" style=" padding: 2px !important;" >
         <img @click="showcategorie(top.id)" style="width: 100%;height: 80px;padding: 0;"  :src="'img/' + top.img"/>
         <p style="font-weight: bold; color: green;text-align: center;">{{ top.topcat }}</p>
     </div>
     </div>
     
     
     
     
     
     
       </div>
     </div>
           </div>
           
           <div class="modal-footer">
         
             <button type="button" class="btn btn-secondary" data-dismiss="modal">quitter</button>
           </div>
         </div>
         </div>
         <!-- /.modal-dialog -->
       </div>
       <!-- /.modal -->
           
              
    
     
     
     
        </div>
  
     
     
     
      <div class="content">
          <div class="card">
                      <!-- /.card-header -->
                      <div class="card-body">
                       
                        <table v-if="table" id="example2" class="table table-bordered table-hover">
                          <thead>
                          <tr class=" bg-info ">
                           <th style="width: 50px;">supp</th>
                           <th >PHOTO</th>
                            <th >CODE ARTICLE</th>
                            <th>DÉSIGNATION</th>
                            <th width="40">QAUNTITÉ</th>
                            <th>PRIX DE D'ACHAT</th>
                            <th>MONTANTACHAT</th>
                              <th>PRIX DE VENTE</th>
                          <th>DÉPOT</th>
                         
                            
                          </tr>
                          </thead>
                          <tbody>
                          
                            <tr   v-for="cart in carts " >
                             <td><button class="btn btn-danger"  @click="deleteRow(index, cart)"><i class="fas fa-solid fa-trash"></i></button></td>
                             <td  style="width:150px !important;"  >  <img :src="cart.img" width="50"></td>
                             <td  style="width:150px !important;"  class="text-primary font-bold">{{ cart.id }}</td>
                <td  class="text-primary font-bold">{{ cart.nom }}</td>
                   
                    <td> <input    style="width: 40px !important;"  v-model="cart.quantite"   @keyup="calculeqty(cart,)" >
                      
                      </td>
                    <td ><input    style="width: 40px !important;"  v-model="cart.prixun"   @keyup="calculeprix(cart)"></td> 
               <td>{{cart.x}} dh</td>
               <td ><input    style="width: 40px !important;"  v-model="cart.prixvents" ></td> 
                  <td>depot</td>
               
           
        
             
  
              
     
                  </tr>
     
                
                
      
               
  
                  
                          </tbody>
                        
                        </table>
                        <button  type="submit" @click="addpay" class="float-right  btn btn-info m-4">enregistrer</button>  
                      </div>
     
    
      
                      <!-- /.card-body -->
        </div>
        </div>
        <div class="content">
          <div v-if="bloc2" class="card">
            <div class="card-body">
            <div class="row"> 
     
  
          
     
     
     
            
        
        <div class="col-md-3"> 
  
        
       <p> total:{{ vente.total}} DH</p>
          </div>
     
      
          <div class="col-md-9">
            <p style="
              float: right;
      font-size: 30px;
      background: #17a2b8;
      padding: 10px;
      width: 230px;
      text-align: center;
      border-radius: 10px;
      color: white;
          
          
          
          " class="font-weight-bold"> total:{{vente.total}} DH</p>
  
          </div>
    
      
        
     
        
        </div> </div>
        </div>
        </div>
  
    </div>
     </template>
     
     <script>
           export default {
            props:['idachat'], 
               data() {
                 return{
                  success:true,
                     imgcat:null,
                  catadd:'',
                  topcatadd:'',
                    
        nomf:'',
        telef:'',         
                 
                  shoowliu:false,
                  q:'',
                  bloc:true,
                  bloc2:false,
                  csrf: document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
                   qty:1,
                   showfirst:true,
                   showprod:false,
                   showtopcat:false,
                   showcat:false,
                   table:true,
                   
                
                   showcat:false,
                   showmode:false,
                   showproduit:false,
                   showdate:false,
               
                   prods:[],
                   produit:[],
                
                   lieustocks:[],
                   topcats:[],
                   lieustocks2:[],
                   topcat1:[],
                   topcat12:[],
                   
                   fournisseurs:[],
  
    carts:[],
           
             totale:0,  
                   vente:{
                    nom:'',
                    image:null,
             fournisseur_id:1,
             lieustock_id:1,
             topcat_id:1,
             quantite:1,
             prixun:'',
             prixvents:'',
             x:0,
             date:Date.now() ,
                     subtotal:0 ,
                   total:0,
                   }
                 }
               },
      created(){
      
        this.showdet();
        this. getlieustock();
        this. getlieustock2();
        this.  getbonenattentsituation();
        this.getlastidvente();
        this.getcatebaylieuxfirst();
        this.getfournisseurs();
        this.gettopcat();
               },        
            
     
             
               methods:{

                showdet(){
                axios.get('./api/modife_achat/'+this.idachat)
.then(response =>{
 this.base = response.data.achats;
  //this.carts = response.data.detaille_vente;

  
let prs = Object.values(response.data.detaille_achat);

for(let pro of prs){
  this.carts.push({
    id: pro.produit_id,
    nom:pro.produit.nom,
    quantite:pro.qty,
    prixun:pro.prixachat,
    prixvents:pro.produit.prixvents,
    x:pro.total,
  
  })
}

 
  this.vente.fournisseur_id=this.base.fournisseur_id;
this.vente.lieustock_id=this.base.lieustock_id;
 this.vente.total=this.base.prixtotal;
 this.table=true;
  
  this.bloc2=true;
 
})
.catch(error =>{
console.log(error);
})
  },
                addfournisseur(){
                  let formdata = new FormData();
        formdata.append('nom',this.nomf)
        formdata.append('tele',this.telef)
     
        
       
  
       
  
       axios.post('./addfournisseur',formdata).then(function (response) { 
        $("#f .close").click();
  
  
      
   
   
  
  
     
        })
        .catch(function (error) {
      
        
   
        });
           this.fournisseurs.push(response.data.fournisseur);
      
        this.nomf='';
        this.telef='';
    
                },
                addcategorie(){
                  let formdata = new FormData();
        formdata.append('lieu',this.topcatadd)
        formdata.append('topcat',this.catadd)
        formdata.append('image',this.imgcat)
        
       
       let config = {
        headers :{"content-type":'multipart/form-data'}
    
        
       }
       
  
       axios.post('./addtopcat',formdata,config,).then(function (response) {
  
    
  $("#b .close").click();
  this.gettopcat();
        })
        .catch(function (error) {
      
        
   
        });
      
      
  
                },
                onfilselected2(event) {
              this.imgcat = event.target.files[0];
                },
                onfilselected() {
          const file = this.$refs.imageInput.files[0];
          if (file) {
            const reader = new FileReader();
            reader.onload = (e) => {
              this.vente.image = e.target.result;
            };
            reader.readAsDataURL(file);
          }
        },
                shlieu(){
                  this.shoowliu=true;
                },
  gettopcat(){ 
    axios.get('./api/topcatbyid/'+this.vente.lieustock_id)
         .then(response =>{
         this.topcat12 = response.data.topcats;
        
         
         })
         .catch(error =>{
         console.log(error);
         })
  },
  
                calculeqty(cart){
                  let  name = this.carts.findIndex(item=>item.id===cart.id);  
    
    if(name !=-1)  {
  
    this.carts[name].x= this.carts[name].prixun* this.carts[name].quantite;
    
    }
     
    
    var subtotal;
                   subtotal = this.carts.reduce(function (sum, product) {
                       var lineTotal = parseFloat(product.x);
                       if (!isNaN(lineTotal)) {
                           return sum + lineTotal;
                       }
                   }, 0);
            
       this.vente.subtotal=subtotal;
       this.vente.total=subtotal;
       this.vente.remise=0;
       this.vente.avance=0;
       this.vente.idcrediturgent=1;
      
    
    
    }
    ,
    calculeprix(cart){
       let  name = this.carts.findIndex(item=>item.id===cart.id);  
     
                 
    
    if(name !=-1)  {
    
    this.carts[name].x= this.carts[name].prixun* this.carts[name].quantite;
    
    }
     
    
    var subtotal;
                   subtotal = this.carts.reduce(function (sum, product) {
                       var lineTotal = parseFloat(product.x);
                       if (!isNaN(lineTotal)) {
                           return sum + lineTotal;
                       }
                   }, 0);
            
       this.vente.subtotal=subtotal;
       this.vente.total=subtotal;
       this.vente.remise=0;
       this.vente.avance=0;
       this.vente.idcrediturgent=1;
      
    
    
    }
    ,
     
    getfournisseurs(){
           
           axios.get('./api/fournisseurs')
         .then(response =>{
         this.fournisseurs = response.data.fournisseurs;
         
         
         })
         .catch(error =>{
         console.log(error);
         })
         },
        
    
  
          
                 getlieustock(){
            
            axios.get('./api/lieustock')
          .then(response =>{
          this.lieustocks = response.data.lieustock;
         
          
          })
          .catch(error =>{
          console.log(error);
          })
          },   
          getlieustock2(){
            
            axios.get('./api/lieustock')
          .then(response =>{
          this.lieustocks2 = response.data.lieustock;
         
          
          })
          .catch(error =>{
          console.log(error);
          })
          },  
          showcategorie(id){
            this.showcat=false;
      this.showtopcat=false;
        this.showprod=true;
        this.showfirst=false;
        axios.get('./getproduitbycat/'+id)
        .then(response =>{
        this.prods = response.data.produits;
       
        })
        .catch(error =>{
        console.log(error);
        })
          },
          getcatebaylieuxfirst(){
           this.showfirst=true,
          this.showtopcat=false;
          this.showcat=false;
        this.showprod=false;
            axios.get('./api/topcatbyid/1')
          .then(response =>{
          this.topcat1 = response.data.topcats;
          this.vente.idlieutoock=1;
          this.showtopcat=false;
          this.showcat=false;
        this.showprod=false;
        this.showfirst=true;
          
          })
          .catch(error =>{
          console.log(error);
          })
          },   
          getcatebaylieux(){
          this.showtopcat=true;
          this.showcat=false;
        this.showprod=false;
        this.showfirst=false;
            axios.get('./api/topcatbyid/'+this.vente.idlieutoock)
          .then(response =>{
          this.topcats = response.data.topcats;
          this.showtopcat=true;
          this.showcat=false;
        this.showprod=false;
        this.showfirst=false;
          
          })
          .catch(error =>{
          console.log(error);
          })
          },   
          getcatebaylieux2(id){
           this.vente.idlieutoock=id;
          this.showtopcat=true;
          this.showcat=false;
        this.showprod=false;
        this.showfirst=false,
            axios.get('./api/topcatbyid/'+id)
          .then(response =>{
          this.topcats = response.data.topcats;
          this.showtopcat=true;
          this.showcat=false;
        this.showprod=false;
        this.showfirst=false;
          
          })
          .catch(error =>{
          console.log(error);
          })
          }, 
    
     showprduits(id){
       this.showcat=false;
      this.showtopcat=false;
        this.showprod=true;
        this.showfirst=false;
        axios.get('./getproduitbycat/'+id)
        .then(response =>{
        this.prods = response.data.produits;
       
        })
        .catch(error =>{
        console.log(error);
        })
     },
     
    
  
       getbonenattentsituation(){
          
          axios.get('./api/bonenattentsituations')
       .then(response =>{
       this.bonenattentsituations = response.data.bonenattentsituations;
       
       })
       .catch(error =>{
       console.log(error);
       })
       },
  
        getproduitbycat(){
       
             axios.get('./getproduitbycat/'+this.vente.idcategorie)
        .then(response =>{
        this.prods = response.data.produits;
       
        })
        .catch(error =>{
        console.log(error);
        })
        },
        getproduitbyid(){
       
       axios.get('./produitbyid/'+this.idproduit)
       .then(response =>{
       this.produit = response.data.produit;
       this.prixdevente=this.produit.prixvents
     
       })
       .catch(error =>{
       console.log(error);
       })
       },  
       
  
        getclienttbyid(){
       
       axios.get('./clientbyid/'+this.vente.idclient)
       .then(response =>{
       this.client = response.data.client
       
       })
       .catch(error =>{
       console.log(error);
       })
       }, 
       loadf(){
      location.reload();
      buttonpdf1=true;
    
     },
       add(){
   let idp =Math.floor(Math.random() * 1000) + 1000;
   this.carts.push({
   
    id:idp,
    nom:this.vente.nom,
    quantite:this.vente.quantite,
    prixun:this.vente.prixun,
    x:this.vente.prixun*this.vente.quantite,
    prixvents:this.vente.prixvents,
    fournisseur_id:this.vente.fournisseur_id,
    lieustock_id:this.vente.lieustock_id,
    topcat_id:this.vente.topcat_id,
    img:this.vente.image,
   
  
  /*
     
        "ref" => "1222"
        "img" => "1695647062.jfif"
     
        */
   });
  
   
  
   var subtotal;
                   subtotal = this.carts.reduce(function (sum, product) {
                       var lineTotal = parseFloat(product.x);
                       if (!isNaN(lineTotal)) {
                           return sum + lineTotal;
                       }
                   }, 0);
            
       this.vente.subtotal=subtotal;
       this.vente.total=subtotal;
       this.vente.remise=0;
       this.vente.avance=0;
       this.vente.idcrediturgent=1;
      
     this.table=true;
    
     this.bloc2=true;
    
   
   
  
  
   
   
    },
       
       addcart(produit){
     
      let  name = this.carts.findIndex(item=>item.id===produit.id);  
    
    if(name !=-1)  {
    this.carts[name].quantite++;
    this.carts[name].x= this.carts[name].prixun* this.carts[name].quantite;
    
    }else{
    this.carts.push(produit);
    produit.quantite=1;
    produit.comment='rien';
    produit.x=produit.prixun*produit.quantite;
    produit.img=produit.img;
    
    }
     
    
    
      
        var subtotal;
                   subtotal = this.carts.reduce(function (sum, product) {
                       var lineTotal = parseFloat(product.x);
                       if (!isNaN(lineTotal)) {
                           return sum + lineTotal;
                       }
                   }, 0);
            
       this.vente.subtotal=subtotal;
       this.vente.total=subtotal;
       this.vente.remise=0;
       this.vente.avance=0;
       this.vente.idcrediturgent=1;
    
     this.table=true;
    
     this.bloc2=true;
    
    
     },
      
      
       getlastidvente(){
             axios.get('./api/lastid')
        .then(response =>{
       
       this.lastidvente = response.data.lasid
       
        })
        .catch(error =>{
        console.log(error);
        })
        },
       
       //post
       deleteRow(index, cart){
       
              axios.delete('./deletligneachat/'+cart.id)
     .then(response => {
      var idx = this.carts.indexOf(cart);
         
         if (idx > -1) {
             this.carts.splice(idx, 1);
         }
         var subtotal, total;
             subtotal = this.carts.reduce(function (sum, product) {
                 var lineTotal = parseFloat(product.x);
                 if (!isNaN(lineTotal)) {
                     return sum + lineTotal;
                 }
             }, 0);
 this.vente.subtotal=subtotal;
 this.vente.total=subtotal;
     });
  },
    
       deletroow2(index, cart) {
               var idx = this.carts.indexOf(cart);
         
               if (idx > -1) {
                   this.carts.splice(idx, 1);
               }
               var subtotal, total;
                   subtotal = this.carts.reduce(function (sum, product) {
                       var lineTotal = parseFloat(product.x);
                       if (!isNaN(lineTotal)) {
                           return sum + lineTotal;
                       }
                   }, 0);
       this.vente.subtotal=subtotal;
       this.vente.total=subtotal;
       //cart.prix='';
       //this.vente.qty[index]='';
           },
    
      
           addpay(){
            let formdata = new FormData();
        formdata.append('fournisseur_id',this.vente.fournisseur_id)
        formdata.append('lieustock_id',this.vente.lieustock_id)
        formdata.append('prixtotal',this.vente.total)
        formdata.append('cart',JSON.stringify(this.carts))
       
       let config = {
        headers :{"content-type":'multipart/form-data'}
    
        
       }
    
        axios.post('./modifier_achat/'+this.idachat,formdata,config,).then(function (response) {
           Swal.fire(
      'parfait!',
      'vente bien enregistrer!',
      'success'
    );
   
        })
        .catch(function (error) {
      
        
   
   Swal.fire({
     icon: 'error',
     title: 'Oops...',
     text: 'tous les donner sont obligatoire',
     footer: '<a href="">revenire au facture?</a>'
   });
   
        });
   
        this.carts='';
        this.vente.total=0;
        this.vente.subtotal=0;
        this.success=false;
   
      },
    
    
    
             },
     
     
             computed:{
    
    
     
    
  
  
    
    filterpost(){
      return this.prods.filter(prod=>{
        return prod.nom.toLowerCase().includes(this.q.toLowerCase());
      })
    }
      
         },
             
           }
       </script>
     