@extends('layout')
@section('title','Utilisation ')
@section('content')


<div class="section-header">
            <h1>Utilisation</h1>
            <div class="section-header-breadcrumb">
              <div class="breadcrumb-item active"><a href="#">Utilisation des produits</a></div>
              <div class="breadcrumb-item"><a href="#">Nouveau</a></div>
            </div>
          </div>
          <div class="section-body">
		    @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
            <div class="row">
              <div class="col-lg-8 offset-2 col-md-12 ">
                <div class="card">
                  <div class="card-header">
                    <h4>Utilisation ingrédient</h4>
                  </div>
				  
                  <div class="card-body">
				  
				  <h7><strong>Les champs en * sont obligatoires</strong></h7>
				   </br></br>
				<form  action="" method="POST">
                         {{csrf_field()}}
					  <div class="row">
                   <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12"> 
					
					  <div class="form-group">
                      <label>Produit <font color="red">*</font></label>
                      <select title="sélectionner le produit" class="form-control select2" name="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-6 col-md-6 col-sm-6 col-xs-12"> 
					
					
					<div class="form-group">
                      <label>Quantité <font color="red">*</font></label>
                      <input title="saisir la quantité" type="text" class="form-control" name="qte">
                    </div>
					</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-success" id="addutilisation"><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 entrées</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>Référence</th>
							 <th>Produit</th>
							  <th>Quantité</th>
                           
                            <th>Etat</th>
                            <th>Action</th>
                          </tr>
                        </thead>
                        <tbody>
						 @foreach($utilisation as $utilisations)
                          <tr>
                            <td>{{$loop->iteration}}</td>
                            <td>{{$utilisations->numero}}</td>
                            <td> {{DB::table('produit_vendable')->where('id', $utilisations->produit_id)->first()->designation}} </td>
							<td> {{$utilisations->qte}} </td>
							
                          
                            <td class="align-middle">
							    @if( $utilisations->etat ==  "Proforma")
									
                                  <div class="badge badge-warning badge-shadow">{{$utilisations->etat}}</div>
							    @else
                                      <div class="badge badge-success badge-shadow">{{$utilisations->etat}}</div>
                                @endif
								  
                            </td>
							
                            <td>
							 @if( $utilisations->etat ==  "Proforma")
							<a type="button" href="#" data-toggle="tooltip" data-id="{{$utilisations->id}}" data-placement="bottom" title="Valider" class="btn btn-icon btn-success data-valider"><i class="fas fa-check"></i></a>
							
							
							<a type="button" href="#" data-id="{{$utilisations->id}}" data-toggle="tooltip" data-placement="bottom" title="Supprimer" class="btn btn-icon btn-danger data-supprimer"><i class="fas fa-trash"></i></a>
							@endif
							</td>
                          </tr>
                      @endforeach
                        </tbody>
                      </table>
                    </div>
                  </div>
                </div>
              </div>
            </div>
           
           
          </div>
		  
             
            </div>
          </div>
		  
  <div class="modal fade" id="Validermodal" >
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Validation</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 valider cette utilisation ?
              </div>
              <div class="modal-footer bg-whitesmoke br" style="background-color:#ffffff" id="valider">
                
              </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 vraiment supprimer cette utilisation ?
              </div>
              <div class="modal-footer bg-whitesmoke br" style="background-color:#ffffff" id="delete">
                
              </div>
            </div>
          </div>
        </div>
		  
	   @push('scripts') 
	   
	    <script>
        $(".data-valider").on('click', function () {
                var id=$(this).data('id');
                $.get(location.origin+'/validerutilisationingredient/'+id, function (data) {
                  var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal">Non</button>
                      <form action="{{route('validerutilisationingredientPost')}}" method="post" style="display: inline">
                        {{csrf_field()}}
                      <input type="hidden" value="`+data.ing.id+`" name="id" >
                      <button type="submit" class="btn btn-success">Oui</button>

            </form>`
            ;
            $('#valider').html('').append(html);
          })
          $('#Validermodal').modal();
        });
    </script>
	   
	    <script>
        $(".data-supprimer").on('click', function () {
                var id=$(this).data('id');
                $.get(location.origin+'/validerutilisationingredient/'+id, function (data) {
                  var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal">Non</button>
                      <form action="{{route('SupprimerutilisationingredientPost')}}" method="post" style="display: inline">
                        {{csrf_field()}}
                      <input type="hidden" value="`+data.ing.id+`" name="id" >
                      <button type="submit" class="btn btn-success">Oui</button>

            </form>`
            ;
            $('#delete').html('').append(html);
          })
          $('#Supprimermodal').modal();
        });
    </script>	
	
	 <script>
        $( "#addutilisation").on('click', function( e ) {
        e.preventDefault();
        var data = {};
       	    data.produit = $("select[name=produit]").val();
            data.qte = $("input[name=qte]").val();
			
         
			if(!data.produit){
				toastr.error('Veuillez sélectionner le produit ', 'Erreur !', {timeOut: 5000})
				return false;
			}

            if(!data.qte){
				toastr.error('Veuillez saisir la quantité ', 'Erreur !', {timeOut: 5000})
				return false;
			}
			
		

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

        if( data.produit  && data.qte  ){
            $.post("{{route('nouvelle_utilisationingredientPost')}}", 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