@extends('layout')
@section('title','Nouveau salaire ')
@section('content')
		<br/>
          <div class="section-body">
            <div class="row">
              <div class="col-lg-10 offset-1 col-md-12 ">
				@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
		<br/>
                <div class="card">
                  <div class="card-header">
                    <h4>Salaire de {{$employe->nom}} {{$employe->prenoms}} (<h7>Les champs en * sont obligatoires</h7>)</h4>
                  </div>
				  
                  <div class="card-body">
				  @php
				$date = new DateTime();
				$dateoff = $date -> format('Y-m-d');
				$dateDeb = $date -> format('01/m/Y');
				$dateFin = $date -> format('t/m/Y');
				$dateDeb2= implode('-', array_reverse(explode('/', $dateDeb)));
				$dd = date_parse($dateoff);
				$y1 = $dd['year'];
				$y2 = $dd['month'];
				//dd($date);
				if ($y2 == 1)
					{$y22 = "Janvier";}
					elseif ($y2 == 2){$y22 = "Février";} elseif ($y2 == 3){$y22 = "Mars";}
				elseif ($y2 == 4){$y22 = "Avril";} elseif ($y2 == 5){$y22 = "Mai";} elseif ($y2 == 6){$y22 = "Juin";}
				elseif ($y2 == 7){$y22 = "Juillet";} elseif ($y2 == 8){$y22 = "Août";} elseif ($y2 == 9){$y22 = "Septembre";}
				elseif ($y2 == 10){$y22 = "Octobre";} elseif ($y2 == 11){$y22 = "Novembre";} elseif ($y2 == 12){$y22 = "Décembre";}
			  @endphp
				<form  action="" method="POST">
                         {{csrf_field()}}
				  <div class="row" style="padding-top: 30px">
					<div class="col-lg-4 col-md-4"> 
					  <div class="form-group">
                      <label>Salaire du mois de <font color="red">*</font></label>
                      <input type="text" class="form-control" name="mois" value="{{$y22}}-{{$y1}}" readOnly>
                    </div>
					</div>
					<div class="col-lg-4 col-md-4"> 
					  <div class="form-group">
                      <label>Date début <font color="red">*</font></label>
                      <input type="text" class="form-control" id="champ_date1" name="date_debut" value="{{$dateDeb}}" readOnly>
					  <div id="calendarMain1"></div>
                    </div>
					</div>
					<div class="col-lg-4 col-md-4"> 
					  <div class="form-group">
                      <label>Date fin <font color="red">*</font></label>
                      <input type="text" class="form-control" id="champ_date2" name="date_fin" value="{{$dateFin}}" readOnly>
					  <div id="calendarMain2"></div>
                    </div>
					</div>
					<div class="col-lg-4 col-md-4"> 
					  <div class="form-group">
                      <label>Nom & Prénoms <font color="red">*</font></label>
					  <input type="hidden" value="{{$id}}">
                      <input type="text" class="form-control" value="{{$employe->nom}} {{$employe->prenoms}}" readOnly>
                    </div>
					</div>
					<div class="col-lg-4 col-md-4"> 
					  <div class="form-group">
                      <label>Salaire <font color="red">*</font></label>
                      <input type="text" class="form-control" value="{{$employe->salaire_base}}" name="salaire" readOnly>
                    </div>
					</div>
					<div class="col-lg-4 col-md-4"> 
					  <div class="form-group">
                      <label>Retenue sur salaire <font color="red">*</font></label>
                      <input type="text" class="form-control" value="0" name="retenue">
                    </div>
					</div>
					<div class="col-lg-12 col-md-4"> 
							<a class="btn btn-icon icon-left btn-primary" id="addNewblock"><i class="fas fa-sign-plus"></i> Ajouter prime</a>
						</div>
                      </div>
						<div class="addNouveau"></div>
                      </div> 
                   <div class="pull-left" style="margin-left:20px;">
                    <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="addsalaire"><i class="fas fa-check"></i> Enregistrer</button>
                  </div>
            
					<br><br>
					  
					</form>
					  
					    </div>
						 
                    </div>
                  </div>
                </div>
          </div>
            </div>
          </div>
		  
	   @push('scripts') 
	   <script type="text/javascript">
      //<![CDATA[
      calInit("calendarMain", "", "champ_date", "jsCalendar", "day", "selectedDay");
      calInit("calendarMain1", "", "champ_date1", "jsCalendar", "day", "selectedDay");
			calInit("calendarMain2", "", "champ_date2", "jsCalendar", "day", "selectedDay");
      //]]>
     </script>
	 <script>

        function removeBlock() {
               $(".deleteblock").on("click", function () {
                           $(this).parent().parent().parent().remove()
                       })
              }

              var i=0;
              var j=1;
              var z=1;
               $("#addNewblock").on("click", function ()
                {
                   i++;
                   var html = `
                   <div class="row">
               <div class="col-lg-5 col-md-5"> 
                <div class="form-group">
                  <label>Prime `+ j++ +`<font color="red">*</font></label>
                  <input title="saisir le type de prime" type="text" class="form-control" placeholder="Entrer le type de prime"  name="sal[`+ i +`][prime][]">
                </div>
              </div>
             <div class="col-lg-5 col-md-5"> 
                <div class="form-group">
                  <label>Montant prime `+ z++ +`<font color="red">*</font></label>
                  <input type="text" class="form-control" placeholder="Entrer le montant de la prime" name="sal[`+ i +`][mont][]">
                </div>
                </div>
				<div class="col-lg-2 col-md-2"> 
                <div class="form-group">
                  <label>&nbsp;</label>
                  <button type="button"  class="btn btn-danger form-control deleteblock" id="addNewblock"><i class="fas fa-times"></i></button>
                </div>
                <!-- /.form-group -->
              </div>
              </div>
                       `;

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

             });
    </script>
	<script>
	$( "#addsalaire").on('click', function( e ) {
	  e.preventDefault();
	  var data = {};
	  data.mois = $("input[name=mois]").val();
	  data.retenue = $("input[name=retenue]").val();
	  data.salaire = $("input[name=salaire]").val();
	  data.date_debut = $("input[name=date_debut]").val();
	  data.date_fin = $("input[name=date_fin]").val();
	  data.sal = [];

	  if(!data.date_debut){
		toastr.error('Veuillez renseigner la date de debut', 'Attention !', {timeOut: 5000})
		return false;
	  }
	  if(!data.date_fin){
		toastr.error('Veuillez renseigner la date de fin', 'Attention !', {timeOut: 5000})
		return false;
	  }

			$("[name*=sal]").each(function(index, element){
				if (index != 0) {
					if($(element).val() != ""){
						data.sal.push($(element).val());
					}else{
						toastr.error('Veuillez '+ $(element)[0].attributes[0].textContent, 'Error', {timeOut: 5000});
					   console.log($(element))
						return false;
					}
				}

			 });


		data._token = "{{csrf_token()}}";
		if(data.sal.length > 1){
			if(data.date_debut && data.date_fin){
			$.post("{{route('salaire2Post',['id'=> $id])}}", 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/salaires/liste-permanent';
				}

			})
		 }
		}
		if(data.sal == undefined) {
			if(data.date_debut && data.date_fin){
			$.post("{{route('salairePost',['id'=> $id])}}", 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/salaires/liste-permanent';
				}

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

@endsection