@extends('layout')
@section('title','Vente ')
@section('content')
		<br/>
          <div class="section-body">
            <div class="row">
              <div class="col-lg-10 offset-1 col-md-12 ">
			
		<br/>
                <div class="card">
                  <div class="card-header">
                    <h4>Valider un versement (<h7><strong>Les champs en * sont obligatoires</strong></h7>)</h4>
                  </div>
                  <div class="card-body">
				<form  action="" method="POST">
                         {{csrf_field()}}
				  <div class="row" style="padding-top: 30px">
				      @php
					       
						     $d = date('d/m/Y');
					  @endphp
                     <div class="col-lg-5 col-md-5"> 
					  <div class="form-group">
                      <label>Montant total à verser</label>
                      <input type="text" class="form-control" name="montavers" value="{{$cloture->montant}}" disabled>
                    </div>
					</div>
					
					 <div class="col-lg-5 col-md-5"> 
					 <div class="form-group">
                      <label>Date </label>
                      <input type="text" class="form-control datepicker" name="datevers" value="{{$d}}" id="champ_date">
					  <div id="calendarMain"></div>
                    </div>
					 </div>
					</div>
					
					  <div class="row">
					 <div class="col-lg-5 col-md-5"> 
					
					  <div class="form-group">
                      <label>Billets / pièces <font color="red">*</font></label>
                      <select title="sélectionner le billet" class="form-control select2" name="verse[0][billet][]">
                        <option>Sélectionner le billet ou pièce</option>
                        <option value="10000">10000</option>
                        <option value="5000"> 5000 </option>
                        <option value="2000">2000 </option>
                        <option value="1000">1000 </option>
                        <option value="500">500 </option>
                        <option value="250">250 </option>
                        <option value="200">200 </option>
                        <option value="100">100 </option>
                        <option value="50">50 </option>
                        <option value="25">25 </option>
                        <option value="10">10 </option>
                        <option value="5">5 </option>
                      </select>
                    </div>
					</div>
				<div class="col-lg-3 col-md-3"> 
					
					<div class="form-group">
                      <label>Nombre de billets ou pièces  <font color="red">*</font></label>
                      <input title="sélectionner le nombre de billets ou pièces " type="text" class="form-control" name="verse[0][nbre][]">
                    </div>
					</div>
					
					 <div class="col-lg-3 col-md-3" style="padding: 30px"> 
				     
                     <button  type="button" class="btn btn-icon btn-primary" id="addNewblock"><i class="fas fa-plus"> &nbsp;Ajouter&nbsp;&nbsp;&nbsp; </i></button>
                    
					</div>
					
					</div>
					  <div class="addNouveau"></div>
					
					
					 <div class="col-lg-12 col-md-12"> 
					 <div class="form-group">
                      <label>Notes </label>
                         <textarea class="form-control" name="notes" >Briève commentaire...</textarea>
                    </div>
					
					</div>
                      </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="addversement"><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" style="padding: 6px">
                  
				  <div class="col-lg-5 col-md-5"> 
					
					  <div class="form-group">
                      <label>Billets / pièces <font color="red">*</font></label>
                      <select title="sélectionner le billet" class="form-control select2" name="verse[`+ i +`][billet][]">
                        <option>Sélectionner le billet ou pièce</option>
                        <option value="10000">10000</option>
                        <option value="5000"> 5000 </option>
                        <option value="2000">2000 </option>
                        <option value="1000">1000 </option>
                        <option value="500">500 </option>
                        <option value="250">250 </option>
                        <option value="200">200 </option>
                        <option value="100">100 </option>
                        <option value="50">50 </option>
                        <option value="25">25 </option>
                        <option value="10">10 </option>
                        <option value="5">5 </option>
                      </select>
                    </div>
					</div>
				    <div class="col-lg-3 col-md-3"> 
					
					<div class="form-group">
                      <label>Nombre de billets ou pièces  <font color="red">*</font></label>
                      <input title="sélectionner le nombre de billets ou pièces " type="text" class="form-control" name="verse[`+ i +`][nbre][]">
                    </div>
					</div>
					
					  <div class="col-lg-3 col-md-3" style="padding: 30px"> 
				 
                     <button class="btn btn-icon btn-danger deleteblock"><i class="fas fa-times"></i> Supprimer</button>
                   
					</div>
					
		       </div>
				  
                       `;

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

             });
    </script>
	  
	   
		<script>
		$( "#addversement").on('click', function( e ) {
			e.preventDefault();
			var data = {};
			var isValide = true;
			data.montavers = $("input[name=montavers]").val();
			data.datevers = $("input[name=datevers]").val();
			data.notes = $("textarea[name=datetic]").val();	
			data.verse = [];
			
			    $("[name*=verse]").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.verse.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()}}";
			
			                 @php
                               $idclo = $cloture->id;
                             @endphp

			if(data.verse.length && isValide){
				$.post("{{route('nouveauversementPost', ['id'=>$idclo])}}", 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/liste-des-ventes-cloturees';
					}

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

@endsection