@extends('layout')
@section('title','Facture globale')
@section('content')
		<br/>
          <div class="section-body">
            <div class="row">
              <div class="col-lg-10 offset-1 col-md-12 ">
				@if (session('status'))
            <div class="alert alert-warning" style="font-size: 15px; background-color: #328039; color: white">
                <i class="fa fa-check"></i> &nbsp; {{ session('status') }}
            </div>
        @endif

        @if (session('error'))
            <div class="alert alert-danger" style="font-size: 15px; background-color: #fb5757; color: white">
                <i class="ti-na"></i> {{ session('error') }}
            </div>
        @endif
        @if ($errors->any())
            <div class="alert alert-danger" style="font-size: 15px; background-color: #fb5757; color: white" >
                <ul>
                    @foreach ($errors->all() as $error)
                        <li> <i class="ti-na"></i> {{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif
		<br/>
                <div class="card">
                  <div class="card-header">
                    <h4>Enregistrer une nouvelle vente (<h7>Les champs en * sont obligatoires</h7>)</h4>
                  </div>
				  
                  <div class="card-body">
				<form  action="" method="POST">
                         {{csrf_field()}}
				  <div class="row" style="padding-top: 30px">
                     <div class="col-lg-2 col-md-12"> 
					  <div class="form-group">
                      <label>Type de vente <font color="red">*</font></label>
                      <select class="form-control select2" name="typevente">
                        <option value="ORDINAIRE">Ordinaire</option>
                        <option value="VIP">VIP</option>
                      </select>
                    </div>
					</div>
					<div class="col-lg-4 col-md-12"> 
					  <div class="form-group">
                      <label>Client <font color="red">*</font></label>
                      <select class="form-control select2" name="client">
                        <option value="">Sélectionner un client</option>
						
						   @foreach($cli as $clients)
							<option value="{{$clients->id}}">{{DB::table('client_externe')->where('id', $clients->id)->first()->noms}}</option>
							@endforeach
                      </select>
                    </div>
					</div>
					  <div class="col-lg-4 col-md-12"> 
					  <div class="form-group">
                      <label>Serveuse <font color="red">*</font></label>
                      <select class="form-control select2" name="serveuse">
                        <option value="">Sélectionner un(e) serveur(se)</option>
						
						   @foreach($serveuse as $serveuses)
							<option value="{{$serveuses->id}}">{{DB::table('employe')->where('id', $serveuses->id)->first()->nom}}
							{{DB::table('employe')->where('id', $serveuses->id)->first()->prenoms}}</option>
							@endforeach
                      </select>
                    </div>
					</div>
                      </div>
					  <div class="row">
                    <div class="col-lg-6 col-md-12"> 
					  <div class="form-group">
                      <label>Produit <font color="red">*</font></label>
                      <select title="sélectionner le produit" class="form-control select2" name="peps[0][produit][]">
                        <option>Sélectionner le produit</option>
						 @foreach($produit as $produits) 
						<option value="{{$produits->id}}">{{$produits->designation}} </option>
						@endforeach
                      </select>
                    </div>
					</div>
					 <div class="col-lg-4 col-md-12">
					<div class="form-group">
                      <label>Qte(Boules/Morceaux) <font color="red">*</font></label>
                      <input title="sélectionner la quantité" type="text" class="form-control" name="peps[0][qte][]">
                    </div>
					</div>
					 <div class="col-lg-1 col-md-1 col-sm-1 col-xs-12" style="padding: 30px"> 
				     
                     <button data-toggle="tooltip" data-placement="bottom" title="Ajouter produit" type="button" class="btn btn-icon btn-primary" id="addNewblock"><i class="fas fa-plus"></i></button>
                    
					</div>
				
                      </div>
					  <div class="addNouveau"></div>
					  
					 <hr>  	

                   <div class="pull-left">
                 
                    <button type="reset" class="btn btn-icon icon-left btn-danger"><i class="fas fa-times"></i> Annuler</button>
                    <button class="btn btn-icon icon-left btn-primary" id="addvente"><i class="fas fa-check"></i> Enregistrer</button>
                  </div>
            
			<br><br>
					  
				</form>
					  
					    </div>
						 
                    </div>
                  </div>
                </div>

			
           
          </div>
		  
		  
		   <div class="section-body">
            <div class="row">
              <div class="col-12">
                <div class="card">
                  <div class="card-header">
                    <h4>Dernières ventes</h4>
                  </div>
                  <div class="card-body">
                    <div class="table-responsive">
                      <table class="table table-striped" id="table-1">
                        <thead>
                          <tr>
                            <th class="text-center"> N° </th>
                            <th class="text-center">Type de vente</th>
                            <th class="text-center">Client</th>
							<th class="text-center">Date</th>
                            <th class="text-center">Montant</th>
                            <th>Action</th>
                          </tr>
                        </thead>
                        <tbody>
						@foreach($ventes as $vente)
                          <tr>
							<td style="text-align:center;">{{$vente->numero}}</td>
							<td style="text-align:center;">{{$vente->type_vente}}</td>
							
							@php
							
							$dd = implode('/', array_reverse(explode('-', $vente->date)));
							
								 $lh = DB::table('ligne_factureglobale')->where('vente_id', $vente->id)->get()->sum('montligne');	
							
							
							$lh1 =  number_format($lh, 0, ' ', ' ');
							\Illuminate\Support\Facades\DB::table('factureglobale')->where('id', $vente->id)->update(
							[
							'montant' => $lh,
							]);
							@endphp
							<td style="text-align:center;">{{DB::table('client_externe')->where('id', $vente->client_id)->first()->noms}}</td>
							<td style="text-align:center;">{{$dd}} {{$vente->heure}}</td>
							<td style="text-align:center;">{{$lh1}} FCFA</td>
							<td class="align-middle">
							<a type="button" href="#" data-toggle="tooltip" data-id="{{$vente->id}}" data-placement="bottom" title="Supprimer" class="btn btn-icon btn-danger data-supprimer"><i class="fas fa-trash"></i></a>
							<a type="button" target="_blank" href="{{route('factureglobalepdf',['id'=>$vente->id])}}" class="btn btn-icon btn-warning"><i class="fas fa-print"></i></a>	
							</td>
                          </tr>
						  @endforeach
                        </tbody>
                      </table>
                    </div>
                  </div>
                </div>
              </div>
            </div>
           
           
          </div>
		  
             
            </div>
          </div>
		
	
		
		<div class="modal fade" id="Supprimermodal" >
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Suppression</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
              </div>
              <div class="modal-body">
                Voulez-vous supprimer cette vente  ?
              </div>
              <div class="modal-footer bg-whitesmoke br" style="background-color:#ffffff" id="delete">
                
              </div>
            </div>
          </div>
        </div>
		  
	   @push('scripts') 
	      <script>
        $(".data-supprimer").on('click', function () {
                var id=$(this).data('id');
                $.get(location.origin+'/Ventecli/'+id, function (data) {
                  var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal">Non</button>
                      <form action="{{route('SupprimerventecliPost')}}" method="post" style="display: inline">
                        {{csrf_field()}}
                      <input type="hidden" value="`+data.cli.id+`" name="id" >
                      <button type="submit" class="btn btn-success">Oui</button>

            </form>`
            ;
            $('#delete').html('').append(html);
          })
          $('#Supprimermodal').modal();
        });
    </script>
	   
	   <script>

            function removeBlock() {
               $(".deleteblock").on("click", function () {
                           $(this).parent().parent().remove()
                       })
              }

              var i=0;
               $("#addNewblock").on("click", function ()
                {
                   i++;
                   var html = `
				   
				   <div class="row" >
                  
				   <div class="col-lg-6 col-md-12"> 
					
					  <div class="form-group">
                      <label>Produit <font color="red">*</font></label>
                      <select title="sélectionner le produit" class="form-control select2" name="peps[`+ i +`][produit][]">
                        <option value="">Sélectionner le produit</option>
						
						 @foreach($produit as $produits)
						<option value="{{$produits->id}}">{{$produits->designation}} </option>
						@endforeach
                      </select>
                    </div>
					</div>
					 <div class="col-lg-4 col-md-12"> 
					
					<div class="form-group">
                      <label>Qte(Boules/Morceaux) <font color="red">*</font></label>
                      <input title="sélectionner la quantité" type="text" class="form-control" name="peps[`+ i +`][qte][]">
                    </div>
					</div>
					  <div class="col-lg-1 col-md-1 col-sm-1 col-xs-12" style="padding: 30px"> 
				 
                     <button class="btn btn-icon btn-danger deleteblock"><i class="fas fa-times"></i></button>
                   
					</div>
					
		 </div>
				  
                       `;

                   $('.addNouveau').append(html);
                   removeBlock();

             });
    </script>
	
		<script>
		$( "#addvente").on('click', function( e ) {
			e.preventDefault();
			var data = {};
			
			data.typevente = $("select[name=typevente]").val();
			data.client = $("select[name=client]").val();
			data.serveuse = $("select[name=serveuse]").val();
            data.peps = [];

            $("[name*=peps]").each(function(index, element){
                if($(element).val() != ""){
                    data.peps.push($(element).val());
                }else{
                    toastr.error('Veuillez '+ $(element)[0].attributes[0].textContent, 'Error', {timeOut: 5000});
                   console.log($(element))
                    return false;
                }
            });
			
			
			if(!data.typevente){
				toastr.error('Veuillez sélectionner le type de vente ', 'Erreur !', {timeOut: 5000})
				return false;
			}
			
			
			if(!data.client){
				toastr.error('Veuillez sélectionner le client ', 'Erreur !', {timeOut: 5000})
				return false;
			}
			
			if(!data.serveuse){
				toastr.error('Veuillez sélectionner le nom de la serveuse ', 'Erreur !', {timeOut: 5000})
				return false;
			}


			data._token = "{{csrf_token()}}";

                	if( data.serveuse && data.client && data.peps ){
				$.post("{{route('fgPost')}}", data, function (result) {
					if(result.type == 'error'){
						toastr.error(result.response, result.title, {timeOut: 5000})
					}else{

					     toastr.success(result.response, result.title, {timeOut: 15000})
                         location.reload();
					}

				})
			}

		});
     
  </script>
	   
	   
@endpush

@endsection