@extends('layout')
@section('title','Dépense')
@section('description','Dépense')
@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 dépense (<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 col-sm-4 col-xs-4"> 
					  <div class="form-group">
                      <label>Type de l'opération <font color="red">*</font></label>
                      <select class="form-control select2" name="nature">
                        <option>Sélectionner</option>
                        <option value="Appro caisse">Appro caisse</option>
                        @foreach ($typedepense as $typedepenses)
						<option value="{{$typedepenses->id}}">{{$typedepenses->libelle}}</option>
						@endforeach
                      </select>
                    </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="dated" id="champ_date" value="{{date('d/m/Y')}}">
					  <div id="calendarMain"></div>
                    </div>
					</div>
					<div class="col-lg-6 col-md-6"> 
					  <div class="form-group">
                      <label>Note <font color="red">*</font></label>
                      <input type="text" class="form-control" name="note">
                    </div>
					</div>
					@php
				        $d = date('Y-m-d');
					@endphp
                      </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="addsolde"><i class="fas fa-check"></i> Enregistrer</button>
                  </div>
					<br><br>	  
				</form>
					  
					    </div>
						 
                    </div>
                  </div>
                </div>
				<div class="row">
              <div class="col-12">
                <div class="card">
                  <div class="card-header">
                    <h4>Liste des dépenses</h4>
                  </div>
                  <div class="card-body">
                    <div class="table-responsive">
                      <table class="table table-striped" id="table-1">
                        <thead>
                          <tr>
                            <th style="text-align:center;">N°</th>
							<th style="text-align:center;">Date</th>
							<th style="text-align:center;">Nature</th>
							<th style="text-align:center;">Montant</th>
							<th style="text-align:center;">Note</th>
							<th style="text-align:center;">Etat</th>
							<th style="text-align:center;">Actions</th>
                          </tr>
                        </thead>
                        <tbody>
						@foreach($depense as $depenses)
						<tr>
							@php
							$dd = $depenses->date;
							$dd= explode('-', $dd);
							$dd= array_reverse($dd);
							$dd= implode('/', $dd);
							$today= date('d/m/Y');
							@endphp
							<td>{{$loop->iteration}}</td>
							<td>{{$dd}}</td>
							<td>
							    @if ($depenses->depense_id ==0)
							        Appro caisse
							    @else
							    {{DB::table('typedepense')->where('id', $depenses->depense_id)->first()->libelle}}
							    @endif
							</td>
							<td style="text-align:center;font-size:12px;">
							@php
							$lhh = $depenses->montant;
							$lhh1 =  number_format($lhh, 0, ' ', ' ');
							echo $lhh1."&nbsp;F";
							@endphp
							</td>
							<td>{{$depenses->note}}</td>
							<td style="text-align:center;">
								@if( $depenses->etat == "En cours")
								<div class="badge badge-warning badge-shadow">{{$depenses->etat}}</div>
								@elseif( $depenses->etat == "Validée")
								<div class="badge badge-success badge-shadow">{{$depenses->etat}}</div>
								@else
								<div class="badge badge-danger badge-shadow">{{$depenses->etat}}</div>
								@endif
							</td>
							<td style="text-align:center;">
							@if( $depenses->etat == "Validée")
								@if ($today==$dd)
							<a type="button" href="#" data-toggle="tooltip" data-id="{{$depenses->id}}"
                                data-placement="bottom" title="Annuler"
                                class="btn btn-icon btn-danger data-annuler"><i
                                class="fas fa-times"></i>
								</a>
								@endif
							@endif
							@if( $depenses->etat == "En cours")
							<a type="button" href="#" data-toggle="tooltip" data-id="{{$depenses->id}}"
                                data-placement="bottom" title="Supprimer"
                                class="btn btn-icon btn-danger data-supprimer"><i
                                class="fas fa-trash"></i>
								</a>
							<a type="button" href="#" data-toggle="tooltip" data-id="{{$depenses->id}}"
							data-placement="bottom" title="Valider"
							class="btn btn-icon btn-success data-valider"><i
								class="fas fa-check"></i>
							</a>
							@endif
							</td>
						</tr>
						@endforeach
						</tbody>
						</table>
                    </div>
                  </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 ?
            </div>
            <div class="modal-footer bg-whitesmoke br" style="background-color:#ffffff" id="delete">

            </div>
        </div>
		</div>
		</div>
		<div class="modal fade" id="Annulermodal">
		<div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Annulation</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 annuler cet enregistrement ?
            </div>
            <div class="modal-footer bg-whitesmoke br" style="background-color:#ffffff" id="annuler">

            </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 enregistrement ?
            </div>
            <div class="modal-footer bg-whitesmoke br" style="background-color:#ffffff" id="valider">

            </div>
        </div>
		</div>
		</div>
	   @push('scripts') 
	   <script>
    $(".data-supprimer").on('click', function () {
        var id = $(this).data('id');
        $.get(location.origin + '/es/Depense/' + id, function (data) {
            var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal">Non</button>
                      <form action="{{route('supprimerdepensePost')}}" method="post" style="display: inline">
                        {{csrf_field()}}
                      <input type="hidden" value="`+ data.depense.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/Depense/' + id, function (data) {
            var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal">Non</button>
                      <form action="{{route('validerdepensePost')}}" method="post" style="display: inline">
                        {{csrf_field()}}
                      <input type="hidden" value="`+ data.depense.id + `" name="id" >
                      <button type="submit" class="btn btn-success">Oui</button>

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

            </form>`
                ;
            $('#annuler').html('').append(html);
        })
        $('#Annulermodal').modal();
    });
</script>
	    <script>
	 
	      $('#fourniss').hide();
				
			$('select[name="nature"]').change(function(){
				var valeur = $(this).val();
				if (valeur != ''){
					if (valeur == 'Dépense'){
						$('#fourniss').show();
					} else {
						$('#fourniss').hide();
					}
				}
			});
			
	 </script>
	   
		<script>
		$( "#addsolde").on('click', function( e ) {
			e.preventDefault();
			var data = {};
			data.nature = $("select[name=nature]").val();
			data.montant = $("input[name=montant]").val();
		    data.dated = $("input[name=dated]").val();
			data.note = $("input[name=note]").val();

            if(!data.nature){
				toastr.error('Veuillez saisir la nature', 'Erreur !', {timeOut: 5000})
				return false;
			}
			if(!data.montant){
				toastr.error('Veuillez saisir le montant', 'Erreur !', {timeOut: 5000})
				return false;
			}
			if(!data.dated){
				toastr.error('Veuillez sélectionner la date', 'Erreur !', {timeOut: 5000})
				return false;
			}

			data._token = "{{csrf_token()}}";
			if(data.montant){
			$.post("{{route('nouveausoldePost')}}", 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.reload();
				}

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

@endsection