@extends('layout')
@section('title','Paiement fournisseur ')
@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 un paiement fournisseur (<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-3 col-md-3" hidden> 
					  <div class="form-group">
                          <label>ID <font color="red">*</font></label>
                      <input type="text" class="form-control" name="appro_id" value="{{$appro->id}}">
                    </div>
					</div>
					<div class="col-lg-4 col-md-4"> 
					  <div class="form-group">
                      <label>Montant <font color="red">*</font></label>
                      <input type="text" class="form-control" name="montant">
                    </div>
					</div>
					<div class="col-lg-4 col-md-4"> 
					  <div class="form-group">
                      <label>Date<font color="red">*</font></label>
                      <input type="text" class="form-control" name="datepaie" id="champ_date2">
					  <div id="calendarMain2"></div>
                    </div>
					</div>
					<div class="col-lg-4 col-md-4"> 
					  <div class="form-group">
                      <label>Mode de règlement <font color="red">*</font></label>
                      <select class="form-control select2" name="mode">
                        <option value="Espèce">Espèce</option>
                        <option value="Chèque">Chèque</option>
                        <option value="Virement">Virement</option>
                      </select>
                    </div>
					</div>
					<div class="col-lg-6 col-md-6" id="numcheque"> 
					  <div class="form-group">
                      <label>N° de virement/N° de chèque <font color="red">*</font></label>
                      <input type="text" class="form-control" name="numcheque">
                    </div>
					</div>
					<div class="col-lg-6 col-md-6"> 
					  <div class="form-group">
                      <label>Notes <font color="red">*</font></label>
                      <input type="text" class="form-control" name="note">
                    </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="addpaie"><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 paiements</h4>
                  </div>
                  <div class="card-body">
                    <div class="table-responsive">
                      <table class="table table-striped" id="table-1">
                        <thead>
                          <tr class="text-center">
                            <th class="text-center"> N° </th>
                            <th class="text-center">Date</th>
                            <th class="text-center">Montant payé</th>
                            <th class="text-center">Etat</th>
                            <th class="text-center">Action</th>
                          </tr>
                        </thead>
                        <tbody>
						@foreach($paie as $paies)
                          <tr>
							@php
							$dd = $paies->date;
							$dd= explode('-', $dd);
							$dd= array_reverse($dd);
							$dd= implode('/', $dd);
							@endphp
							<td style="text-align:center;">{{$loop->iteration}}</td>
							<td style="text-align:center;">{{$dd}}</td>
							@php
								$lh = $paies->montant;
								$lh1 =  number_format($lh, 0, ' ', ' ');
							@endphp
							<td style="text-align:center;">{{$lh1}} F</td>
							<td style="text-align:center;">
                                        @if( $paies->etat == "En cours")
                                        <div class="badge badge-warning badge-shadow">{{$paies->etat}}</div>
                                        @elseif( $paies->etat == "Validée")
                                        <div class="badge badge-success badge-shadow">{{$paies->etat}}</div>
                                        @else
                                        <div class="badge badge-danger badge-shadow">{{$paies->etat}}</div>
                                        @endif
                                    </td>
							<td style="text-align:center;">
							@if ($paies->etat=="En cours")
							<a type="button" href="#" data-id="{{$paies->id}}" data-toggle="tooltip" data-placement="bottom" title="Valider" class="btn btn-icon btn-success data-valider"><i class="fas fa-check"></i></a>
							<a type="button" href="#" data-id="{{$paies->id}}" data-toggle="tooltip" data-placement="bottom" title="Supprimer" class="btn btn-icon btn-danger data-supprimer"><i class="fas fa-trash"></i></a>
							@endif 
							<!--<a type="button" target="_blank" href="{{route('facturepaie',['id'=>$paies->id])}}" class="btn btn-icon btn-warning"><i class="fas fa-print"></i></a>-->
							</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 ce paiement ?
              </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 ce paiement ?
              </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/Paiefou/'+id, function (data) {
                  var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal">Non</button>
                      <form action="{{route('SupprimerpaiefouPost')}}" method="post" style="display: inline">
                        {{csrf_field()}}
                      <input type="hidden" value="`+data.pai.id+`" name="id" >
                      <button type="submit" class="btn btn-success">Oui</button>

            </form>`
            ;
            $('#delete').html('').append(html);
          })
          $('#Supprimermodal').modal();
        });
    </script> 
    <script>
        $(".data-valider").on('click', function () {
                var id=$(this).data('id');
                $.get(location.origin+'/es/Paiefou/'+id, function (data) {
                  var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal">Non</button>
                      <form action="{{route('ValiderpaiefouPost')}}" method="post" style="display: inline">
                        {{csrf_field()}}
                      <input type="hidden" value="`+data.pai.id+`" name="id" >
                      <button type="submit" class="btn btn-success">Oui</button>

            </form>`
            ;
            $('#valider').html('').append(html);
          })
          $('#Validermodal').modal();
        });
    </script> 
	   <script>
	 
	$('#numcheque').hide();
				
			$('select[name="mode"]').change(function(){
				var valeur = $(this).val();
				if (valeur != ''){
					if (valeur == 'Chèque' || valeur == 'Virement'){
						$('#numcheque').show();
					} else {
						$('#numcheque').hide();
					}
				}
			});
			
	 </script>
		<script>
		$( "#addpaie").on('click', function( e ) {
			e.preventDefault();
			var data = {};
			data.appro_id = $("input[name=appro_id]").val();
            data.montant = $("input[name=montant]").val();
			data.mode = $("select[name=mode]").val();
			data.note = $("input[name=note]").val();
			data.numcheque = $("input[name=numcheque]").val();
			data.datepaie = $("input[name=datepaie]").val();
			if(!data.montant){
				toastr.error('Veuillez saisir le montant ', 'Erreur !', {timeOut: 5000})
				return false;
			}
             if(!data.datepaie){
				toastr.error('Veuillez sélectionner la date ', 'Erreur !', {timeOut: 5000})
				return false;
			}
            if(!data.mode){
				toastr.error('Veuillez sélectionner le mode de règlement ', 'Erreur !', {timeOut: 5000})
				return false;
			}
			
			if( data.numcheque ){
				
				if(!data.numcheque){
				toastr.error('Veuillez saisir le numéro du chèque ou du virement ', 'Erreur !', {timeOut: 5000})
				return false;
			  }
			}
			
			data._token = "{{csrf_token()}}";
			
			if( data.numcheque ){

			if(data.montant && data.mode && data.numcheque){
				$.post("{{route('paiementfouPost')}}", data, function (result) {
					if(result.type == 'error'){
						toastr.error(result.response, result.title, {timeOut: 10000})
					}else{
					     toastr.success(result.response, result.title, {timeOut: 15000})
						 location.reload();
						 //window.location.href = '/liste_des_fournisseurs';
					}

				})
			}
			} else {
				if(data.montant && data.mode){
				$.post("{{route('paiementfouPost')}}", data, function (result) {
					if(result.type == 'error'){
						toastr.error(result.response, result.title, {timeOut: 10000})
					}else{
					     toastr.success(result.response, result.title, {timeOut: 15000})
						 location.reload();
						 //window.location.href = '/liste_des_fournisseurs';
					}

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

@endsection