@extends('layout')
@section('title','Nouvelle avance ')
@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>Enregistrer une nouvelle avance sur salaire(<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">
					
                     <div class="col-lg-4 col-md-4"> 
					  <div class="form-group">
                      <label>Nom & Prénoms<font color="red">*</font></label>
                      <input type="hidden" class="form-control" name="idemp" value="{{$id}}">
                      <input type="text" class="form-control" name="nom" value="{{DB::table('employe')->where('id', $id)->first()->nom}} {{DB::table('employe')->where('id', $id)->first()->prenoms}}" readOnly>
                    </div>
					</div>
					<div class="col-lg-4 col-md-4"> 
					  <div class="form-group">
                      <label>Type d'employé<font color="red">*</font></label>
                      <input type="text" class="form-control" value="{{DB::table('employe')->where('id', $id)->first()->typeemploye}}" readOnly>
                    </div>
					</div>
					@php
					$dd = date('d/m/Y');
					@endphp
					<div class="col-lg-3 col-md-3"> 
					  <div class="form-group">
                      <label>Date<font color="red">*</font></label>
                      <input type="text" class="form-control" name="date_avance" value="{{$dd}}" id="champ_date2" required>
					  <div id="calendarMain2"></div>
                    </div>
					</div>
					<div class="col-lg-4 col-md-3"> 
					  <div class="form-group">
                      <label>Montant <font color="red">*</font></label>
                      <input type="text" class="form-control" name="montant">
                    </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-primary" id="addemp"><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>Liste des avances sur salaires</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">Date</th>
                            <th class="text-center">Montant</th>
                            <th>Action</th>
                          </tr>
                        </thead>
                        <tbody>
						@foreach($avance as $avances)
						@php
						 $date_avance=implode('/', array_reverse(explode('-', $avances->date_avance)));
						 $today = date('d/m/Y');
						@endphp
                          <tr>
							<td style="text-align:center;">{{$loop->iteration}}</td>
							<td style="text-align:center;">{{$date_avance}}</td>
							<td style="text-align:center;">{{number_format($avances->montant, 0, ' ', ' ')}}</td>
							<td style="text-align:center;">
							@if ($today == $date_avance)
							<a type="button" href="#" data-toggle="tooltip" data-id="{{$avances->id}}"
                                            data-placement="bottom" title="Supprimer"
                                            class="btn btn-icon btn-danger data-supprimer"><i
                                                class="fas fa-trash"></i></a>
							@endif
							</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">Validation</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 approvisionnement ?
              </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 cette avance ?
            </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/Avance/' + id, function (data) {
            var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal">Non</button>
                      <form action="{{route('supprimeravance')}}" method="post" style="display: inline">
                        {{csrf_field()}}
                      <input type="hidden" value="`+ data.avance.id + `" name="id" >
                      <button type="submit" class="btn btn-success">Oui</button>

            </form>`
                ;
            $('#delete').html('').append(html);
        })
        $('#Supprimermodal').modal();
    });
</script>
		<script>
		$( "#addemp").on('click', function( e ) {
			e.preventDefault();
			var data = {};
			
			data.idemp = $("input[name=idemp]").val();
			data.montant = $("input[name=montant]").val();
            data.date_avance = $("input[name=date_avance]").val();
			
			if(!data.date_avance){
				toastr.error('Veuillez sélectionner la date ', 'Attention !', {timeOut: 5000})
				return false;
			}
			if(!data.montant){
				toastr.error('Veuillez saisir le montant ', 'Attention !', {timeOut: 5000})
				return false;
			}
			
			data._token = "{{csrf_token()}}";

			if( data.date_avance &&   data.montant){
				$.post("{{route('avancePost', ['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})
                         location.reload();
					}

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

@endsection