@extends('layout')
@section('title','Retour emballage ')
@section('description','Retour emballage client ')
@section('content')
<div class="section-header">
            <h1>Retour emballage client</h1>
            <div class="section-header-breadcrumb">
              <div class="breadcrumb-item active"><a href="#">Retour emballage client </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-12 col-md-12 ">
                <div class="card">
                  <div class="card-header">
                    <h4>Enregistrer retour d'emballage</h4>
                  </div>
				  
                  <div class="card-body">
				  
				  <h7><strong>Les champs en * sont obligatoires</strong></h7>
				  
				<form  action="" method="POST">
                         {{csrf_field()}}
				  <div class="row" style="padding-top: 30px">
					<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 le client</option>
						@foreach($client as $clients)
							<option value="{{$clients->id}}">{{$clients->noms}}</option>
						@endforeach
                      </select>
                    </div>
					</div>
					<div class="col-lg-4 col-md-12">
					<div class="form-group">
                      <label>Magasin </label>
                       <input  type="text" class="form-control" value="{{DB::table('magasin')->where('id', session('user')->magasin_id)->first()->nom}}" name="magasin" readOnly>
                    </div>
					</div>
					
					@php
						$d = date('d/m/Y');
					@endphp
					 <div class="col-lg-3 col-md-12"> 
					 <div class="form-group">
                      <label>Date du retour</label>
                      <input type="text" class="form-control datepicker" name="dateretour" value="{{$d}}">
                    </div>
					
					</div>
					</div>
					<div class="row">
                    <div class="col-lg-4 col-md-12">
					  <div class="form-group">
                      <label>Emballage <font color="red">*</font></label>
                      <select title="sélectionner le produit" class="form-control select2" name="transmag[0][produit][]">
                        <option>Sélectionner l'emballage</option>
						 @foreach($emballage as $emballages) 
						<option value="{{$emballages->id}}">{{$emballages->libelle}} </option>
						@endforeach
                      </select>
                    </div>
					</div>
					<div class="col-lg-4 col-md-12">
					<div class="form-group">
                      <label>Quantité <font color="red">*</font></label>
                      <input title="saisir la quantité " type="text" class="form-control" name="transmag[0][qte][]">
                    </div>
					</div>
					
				 <div class="col-lg-4 col-md-3 col-sm-3 col-xs-12" style="padding: 30px"> 
				     
                     <button data-toggle="tooltip" type="button" class="btn btn-icon btn-primary" id="addNewblock"><i class="fas fa-plus">&nbsp; Ajouter produit</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="addretour"><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>Derniers retour</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">Référence</th>
                            <th class="text-center">Client </th>
							 <th class="text-center">Date </th>
							  <th class="text-center">Etat </th>
                            <th>Action</th>
                          </tr>
                        </thead>
                        <tbody>
						@foreach($retour as $retours)
                          <tr>
							<td style="text-align:center;">{{$loop->iteration}}</td>
							
							<td style="text-align:center;">{{ $retours->numero}} </td>
							
							<td style="text-align:center;">{{DB::table('client_externe')->where('id', $retours->client_id)->first()->noms}}</td>
							
							@php
							
							$dd = implode('/', array_reverse(explode('-', $retours->date)));
							@endphp
							<td style="text-align:center;">{{$dd}} </td>
						
							 <td class="text-center">
							    @if( $retours->etat ==  "En cours")
									
                                  <div class="badge badge-warning badge-shadow">{{$retours->etat}}</div>
							    @else
                                      <div class="badge badge-success badge-shadow">{{$retours->etat}}</div>
                                @endif
								  
                            </td>
                            <td>
							 @if( $retours->etat ==  "En cours")
							<a type="button" href="#" data-toggle="tooltip" data-id="{{$retours->id}}" data-placement="bottom" title="Valider" class="btn btn-icon btn-success data-valider"><i class="fas fa-check"></i></a>
							@endif
							
							<a type="button" href="{{route('consultation-retour-emballage',['id'=>$retours->id])}}" data-toggle="tooltip" data-placement="bottom" title="Détails" class="btn btn-icon btn-info"><i class="fas fa-info"></i></a>
							 @if( $retours->etat ==  "En cours")
							<a type="button" href="#" data-id="{{$retours->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 du retour</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 ce retour ?
              </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 supprimer définitivement ce retour ?
              </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+'/es/validerretouremballage/'+id, function (data) {
                  var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal">Non</button>
                      <form action="{{route('validerretouremballagePost')}}" method="post" style="display: inline">
                        {{csrf_field()}}
                      <input type="hidden" value="`+data.retour.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+'/es/validerretouremballage/'+id, function (data) {
                  var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal">Non</button>
                      <form action="{{route('SupprimerretouremballagePost')}}" method="post" style="display: inline">
                        {{csrf_field()}}
                      <input type="hidden" value="`+data.retour.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-4 col-md-12"> 
					
					  <div class="form-group">
                      <label>Emballage <font color="red">*</font></label>
                      <select title="sélectionner l'emballage" class="form-control select2" name="transmag[`+ i +`][produit][]">
                        <option value="">Sélectionner l'emballage</option>
						
						 @foreach($emballage as $emballages)
						 
						<option value="{{$emballages->id}}">{{$emballages->libelle}} </option>
						@endforeach
                      </select>
                    </div>
					</div>
					 <div class="col-lg-4 col-md-12">
					<div class="form-group">
                      <label>Quantité <font color="red">*</font></label>
                      <input title="saisir la quantité réelle"  type="text" class="form-control" name="transmag[`+ i +`][qte][]">
                    </div>
					</div>
					
					  <div class="col-lg-4 col-md-3 col-sm-3 col-xs-12" style="padding: 30px"> 
				 
                     <button class="btn btn-icon btn-danger deleteblock"><i class="fas fa-times">&nbsp; Supprimer produit</i></button>
                   
					</div>
					
		 </div>
				  
                       `;

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

             });
    </script>
	
		<script>
		$( "#addretour").on('click', function( e ) {
			e.preventDefault();
			var data = {};
			var isValide = true;
			//data.magasinx = $("input[name=magasinx]").val();
			data.client = $("select[name=client]").val();
			data.dateretour = $("input[name=dateretour]").val();
			data.transmag = [];
			
			if(!data.client){
				toastr.error('Veuillez sélectionner le client ', 'Erreur !', {timeOut: 5000})
				return false;
			}
			
           $("[name*=transmag]").each(function(index, element){
                if($(element).val() != ""){
					if(!Number($(element).val())){
						isValide = false; 
						toastr.error('Veuillez '+ $(element)[0].attributes[0].textContent, 'Attention', {timeOut: 5000});
					}
					console.log($(element).val());
                    data.transmag.push($(element).val());
                }else{
                    toastr.error('Veuillez '+ $(element)[0].attributes[0].textContent, 'Attention', {timeOut: 5000});
                   console.log($(element));
				   isValide = false;
                    return false;
                }
             });



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

			if( data.client && data.transmag.length && isValide ){
				$.post("{{route('nouveauretouremballagePost')}}", 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