@extends('layout')
@section('title','Inventaire emballage')
@section('description','Inventaire emballage')
@section('content')
<div class="section-header">
            <h1>Inventaire emballage</h1>
            <div class="section-header-breadcrumb">
              <div class="breadcrumb-item active"><a href="#">Inventaire emballage</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>Nouvel inventaire</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>Emballage <font color="red">*</font></label>
                      <select title="sélectionner la matière" class="form-control select2" name="invmag[0][emballage][]">
                        <option>Sélectionner emballage</option>
						 @foreach($emballage as $emballages) 
						<option value="{{$emballages->id}}">{{$emballages->designation}} </option>
						@endforeach
                      </select>
                    </div>
					</div>
					<div class="col-lg-3 col-md-12">
					<div class="form-group">
                      <label>Quantité(En détail) <font color="red">*</font></label>
                      <input title="sélectionner la quantité réelle" type="text" class="form-control" name="invmag[0][qte][]">
                    </div>
					</div>
					<div class="col-lg-3 col-md-12" hidden>
					<div class="form-group">
                      <label>P.Achat <font color="red"></font></label>
                      <input title="Saisir le prix d'achat" type="text" class="form-control" name="invmag[0][pa][]" value="1">
                    </div>
					</div>
				 <div class="col-lg-2 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;</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="addinvmag"><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 inventaires</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">Date</th>
							<th class="text-center">Etat</th>
                            <th>Actions</th>
                          </tr>
                        </thead>
                        <tbody>
						@foreach($inventaire as $inventaires)
                          <tr>
							<td style="text-align:center;">{{$loop->iteration}}</td>
							<td style="text-align:center;">{{ $inventaires->reference}} </td>
							@php
							
							$dd = implode('/', array_reverse(explode('-', $inventaires->date)));
							@endphp
							<td style="text-align:center;">{{$dd}} </td>
							<td style="text-align:center;">
								@if( $inventaires->etat == "En cours")
								<div class="badge badge-warning badge-shadow">{{$inventaires->etat}}</div>
								@elseif( $inventaires->etat == "Validée")
								<div class="badge badge-success badge-shadow">{{$inventaires->etat}}</div>
								@else
								<div class="badge badge-danger badge-shadow">{{$inventaires->etat}}</div>
								@endif
							</td>
							<td class="align-middle">
							@if( $inventaires->etat == "En cours")
							<a type="button" href="#" data-id="{{$inventaires->id}}" data-toggle="tooltip" data-placement="bottom" title="Supprimer" class="btn btn-icon btn-danger data-supprimer"><i class="fas fa-trash"></i></a>
							<a type="button" href="#" data-id="{{$inventaires->id}}" data-toggle="tooltip" data-placement="bottom" title="Valider" class="btn btn-icon btn-success data-valider"><i class="fas fa-check"></i></a>
							<a type="button" href="{{route('Modifier-inventaire-emballage',['id'=>$inventaires->id])}}" data-toggle="tooltip" data-placement="bottom" title="Modifier" class="btn btn-icon btn-info"><i class="fas fa-edit"></i></a>
							@endif
							<a type="button" target="_blank" href="{{route('fichemagasingeneral',['id'=>$inventaires->id])}}" class="btn btn-icon btn-warning"><i class="fas fa-print"></i></a>	
							<a type="button" href="{{route('detail_emballage',['id'=>$inventaires->id])}}" data-toggle="tooltip" data-placement="bottom" title="Details" class="btn btn-icon btn-info"><i class="fas fa-info"></i></a>
							<!--<a type="button" href="#" data-id="{{$inventaires->id}}" data-toggle="tooltip" data-placement="bottom" title="Supprimer" class="btn btn-icon btn-danger data-supprimer"><i class="fas fa-trash"></i></a>-->
							
							</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">Valider l'inventaire</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 cet inventaire ? <font color="red">Veuillez bien vérifier les quantités saisies avant
				la validation car cette action est irréversible</font>
              </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 cet inventaire ?
              </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+'/es/Invemballage/'+id, function (data) {
                  var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal">Non</button>
                      <form action="{{route('SupprimerInventaireemballagePost')}}" method="post" style="display: inline">
                        {{csrf_field()}}
                      <input type="hidden" value="`+data.invent.id+`" name="id" >
                      <button type="submit" class="btn btn-success">Oui</button>

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

            </form>`
            ;
            $('#valider').html('').append(html);
          })
          $('#Validermodal').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" style="margin-top:-10px">
				   <div class="col-lg-4 col-md-12"> 
					  <div class="form-group">
                      <label>Emballage <font color="red">*</font></label>
                      <select title="sélectionner la emballage" class="form-control select2" name="invmag[`+ i +`][emballage][]">
                        <option value="">Sélectionner emballage</option>
						 @foreach($emballage as $emballages)
						<option value="{{$emballages->id}}">{{$emballages->designation}} </option>
						@endforeach
                      </select>
                    </div>
					</div>
					 <div class="col-lg-3 col-md-12">
					<div class="form-group">
                      <label>Quantité (En détail) <font color="red">*</font></label>
                      <input title="sélectionner la quantité réelle"  type="text" class="form-control" name="invmag[`+ i +`][qte][]">
                    </div>
					</div>
					 <div class="col-lg-3 col-md-12" hidden>
					<div class="form-group">
                      <label>P.Achat <font color="red">*</font></label>
                      <input title="Saisir le prix d'achat"  type="text" class="form-control" name="invmag[`+ i +`][pa][]" value="1">
                    </div>
					</div>
					  <div class="col-lg-2 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;</i></button>
                   
					</div>
					
		 </div>
				  
                       `;

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

             });
    </script>
	
		<script>
		$( "#addinvmag").on('click', function( e ) {
			e.preventDefault();
			var data = {};
			var isValide = true;
			data.invmag = [];
			
           $("[name*=invmag]").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.invmag.push($(element).val());
                }else{
                    toastr.error('Veuillez '+ $(element)[0].attributes[0].textContent, 'Error', {timeOut: 5000});
				   console.log($(element));
				   isValide = false;
                    return false;
                }
             });



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

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