@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">
				<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>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>Date début <font color="red">*</font></label>
                      <input type="date" class="form-control" id="champ_date0" name="date_debut">
					  <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="date" class="form-control" id="champ_date0" name="date_fin">
					  <div id="calendarMain"></div>
                    </div>
					</div>
					<div class="col-lg-4 col-md-4"> 
					  <div class="form-group">
                      <label>Rémunération journalière <font color="red">*</font></label>
                      <input type="text" class="form-control" name="salaire" value="{{$employe->salaire_base}}" readOnly>
                    </div>
					</div>
					<div class="col-lg-4 col-md-4" hidden> 
					  <div class="form-group">
                      <label>Total absence en cours</label>
                      <input type="text" class="form-control" name="nbabsence" value="{{$nbreabsence}}" readOnly>
                    </div>
					</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 class="col-lg-10 offset-1 col-md-12">
			  
                <div class="card">
                  <div class="card-header">
                    <h4>Liste des salaires</h4>
                  </div>
                  <div class="card-body">
                    <div class="table-responsive">
                      <table class="table table-striped" id="table-1">
                        <thead>
                          <tr>
                            <th>N°</th>
							<th>Employé</th>
							<th>Date début</th>
							<th>Date fin</th>
							<th>Absen.</th> 
							<th>Jour effect.</th> 
							<th>A payer</th> 
							<th>ACTIONS</th>
                          </tr>
                        </thead>
                        <tbody>
                @foreach($salaire as $salaires)
                <tr>
                        @php
                        $dated = $salaires->date_debut;
                        $dated= explode('-', $dated);
                        $dated= array_reverse($dated);
                        $dated= implode('-', $dated);

                        $datef = $salaires->date_fin;
                        $datef= explode('-', $datef);
                        $datef= array_reverse($datef);
                        $datef= implode('-', $datef);
						$today = date('Y-m-d');

                    @endphp
					<td>{{$loop->iteration}}</td>
                    <td>
                        {{DB::table('employe')->where('id', $salaires->employe_id)->first()->nom}}
                        {{DB::table('employe')->where('id', $salaires->employe_id)->first()->prenoms}}
                    </td>
					<td style="text-align:center">{{$dated}}</td>
					<td style="text-align:center">{{$datef}}</td>
					<td style="text-align:center">{{$salaires->nbabsence}}</td>
					<td style="text-align:center">{{$salaires->nbjour}}</td>
                    <td style="text-align:center">{{number_format($salaires->apayer, 0, ' ', ' ')}}</td>
                    <td>
						@if ($today == $salaires->dateenreg)
						<a type="button" href="#" data-toggle="tooltip" data-id="{{$salaires->id}}"
                                            data-placement="bottom" title="Supprimer"
                                            class="btn btn-icon btn-danger data-supprimer"><i
                        class="fas fa-trash"></i>
						</a>
						@endif
						@if ($salaires->typeemploye == "Salarié")
						<a type="button" target="_blank" href="{{route('bulletin-paie-journalier', ['id'=> $salaires->id])}}" class="btn btn-icon btn-warning"><i class="fa fa-print"></i></a>
						@endif
					</td>
				</tr>
                @endforeach
                </tbody>
                      </table>
                    </div>
                  </div>
                </div>
              </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 cet enregistrement ?
            </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/Salaire/' + id, function (data) {
            var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal">Non</button>
                      <form action="{{route('SupprimerSalairePost')}}" method="post" style="display: inline">
                        {{csrf_field()}}
                      <input type="hidden" value="`+ data.salaire.id + `" name="id" >
                      <button type="submit" class="btn btn-success">Oui</button>

            </form>`
                ;
            $('#delete').html('').append(html);
        })
        $('#Supprimermodal').modal();
    });
</script>
	   <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.nbabsence = $("input[name=nbabsence]").val();
	  data.date_debut = $("input[name=date_debut]").val();
	  data.date_fin = $("input[name=date_fin]").val();
	  data.salaire = $("input[name=salaire]").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.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-journalier';
				}

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

@endsection