@extends('layout')
@section('title','Inventaire magasin général')
@section('description','Inventaire magasin général')
@section('content')
<div class="section-header">
            <h1>Inventaire magasin</h1>
            <div class="section-header-breadcrumb">
              <div class="breadcrumb-item active"><a href="#">Inventaire magasin général</a></div>
              <div class="breadcrumb-item"><a href="#">Modification</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-10 offset-1 col-md-12 ">
                <div class="card">
                  <div class="card-header">
                    <h4>Modifier 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">
					 @foreach($ligne as $lignes)
                    <div class="col-lg-6 col-md-6">
					  <div class="form-group">
                      <label>Produit <font color="red">*</font></label>
                      <select title="sélectionner le produit" class="form-control select2" name="invmag[0][produit][]">
                        <option value="{{$lignes->produit_id}}">{{DB::table('produit_vendable')->where('id', $lignes->produit_id)->first()->designation}}</option>
						 @foreach($produit as $produits) 
						<option value="{{$produits->id}}">{{$produits->designation}} </option>
						@endforeach
                      </select>
                    </div>
					</div>
					 <div class="col-lg-2 col-md-2">
					<div class="form-group">
                      <label>Quantité réélle <font color="red">*</font></label>
                      <input title="sélectionner la quantité réelle" type="text" value="{{$lignes->qte}}" class="form-control" name="invmag[0][qte][]">
                    </div>
					</div>
					<div class="col-lg-2 col-md-2" 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>
					 @endforeach
					
				    <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2" style="padding: 30px"> 
				     
                     <button data-toggle="tooltip" 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="editinvmag"><i class="fas fa-check"></i> Enregistrer</button>
                  </div>
            
			<br><br>
					  
				</form>
					  
					    </div>
						 
                    </div>
                  </div>
                </div>

			
           
          </div>
	
		  
             
            </div>
          </div>
		  
         
		  
	   @push('scripts') 
	   
     <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-6"> 
					
					  <div class="form-group">
                      <label>Produit <font color="red">*</font></label>
                      <select title="sélectionner le produit" class="form-control select2" name="invmag[`+ 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-2 col-md-2">
					<div class="form-group">
                      <label>Quantité réélle <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-2 col-md-2" 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-2 col-sm-2 col-xs-2" style="padding: 30px"> 
				 
                     <button class="btn btn-icon btn-danger deleteblock"><i class="fas fa-times"></i></button>
                   
					</div>
					
		 </div>
				  
                       `;

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

             });
    </script>
	
		<script>
		$( "#editinvmag").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()}}";

      @php
         $idinv = $invent->id;
      @endphp

			if(data.invmag.length && isValide ){
        $.post("{{route('modifier_inventaire_generalPost',['id'=>$idinv])}}", data, function (result) {
					if(result.type == 'error'){
						toastr.error(result.response, result.title, {timeOut: 5000})
					}else{

					     toastr.success(result.response, result.title, {timeOut: 15000})
               window.location.href = '/es/inventaire/inventaire-general';
					}

				})
			}
		});
     
  </script>
	   
	   
@endpush

@endsection